= content;
-
- const mMentions: IMentions | undefined = content['m.mentions'];
-
- return [
- typeof body === 'string' ? body : undefined,
- typeof customHtml === 'string' ? customHtml : undefined,
- mMentions,
- ];
- }, [roomId, event]);
-
- const [saveState, save] = useAsyncCallback(
- useCallback(async () => {
- const plainText = toPlainText(editor.children, isMarkdown).trim();
- const customHtml = trimCustomHtml(
- toMatrixCustomHTML(editor.children, {
- allowTextFormatting: true,
- allowBlockMarkdown: isMarkdown,
- allowInlineMarkdown: isMarkdown,
- })
- );
-
- const [prevBody, prevCustomHtml, prevMentions] = getPrevBodyAndFormattedBody();
-
- if (plainText === '') return undefined;
- if (prevBody) {
- if (prevCustomHtml && trimReplyFromFormattedBody(prevCustomHtml) === customHtml) {
- return undefined;
- }
- if (
- !prevCustomHtml &&
- prevBody === plainText &&
- customHtmlEqualsPlainText(customHtml, plainText)
- ) {
- return undefined;
- }
- }
-
- const prevContent = event.content as MessageContent;
- const newContent: MessageContent = {
- msgtype: prevContent.msgtype,
- body: plainText,
- };
-
- const mentionData = getMentions(ct, editor);
-
- prevMentions?.user_ids?.forEach((prevMentionId) => {
- mentionData.users.add(prevMentionId);
- });
-
- const mMentions = getMentionContent(Array.from(mentionData.users), mentionData.room);
- newContent['m.mentions'] = mMentions;
-
- if (!customHtmlEqualsPlainText(customHtml, plainText)) {
- newContent.format = 'org.matrix.custom.html';
- newContent.formatted_body = customHtml;
- }
-
- const content: MessageContent = {
- ...newContent,
- body: `* ${plainText}`,
- 'm.new_content': newContent,
- 'm.relates_to': {
- event_id: event.event_id,
- rel_type: RelationType.Replace,
- },
- };
-
- // TODO: send message
- // return mx.sendMessage(roomId, content);
- return Promise.resolve();
- }, [ct, editor, event, isMarkdown, getPrevBodyAndFormattedBody])
- );
+ const senderState = useObservableValueOptionally(localEventSender?.state);
+ const saving =
+ saveState.status === AsyncStatus.Loading || senderState instanceof EventSenderPending;
const handleSave = useCallback(() => {
if (saveState.status !== AsyncStatus.Loading) {
- save();
+ save(onCancel);
}
- }, [saveState, save]);
+ }, [saveState, save, onCancel]);
const handleKeyDown: KeyboardEventHandler = useCallback(
(evt) => {
@@ -209,27 +130,27 @@ export const MessageEditor = as<'div', MessageEditorProps>(
};
useEffect(() => {
- const [body, customHtml] = getPrevBodyAndFormattedBody();
+ if (!mobileOrTablet()) ReactEditor.focus(editor);
+ }, [editor]);
+
+ const initialEditorValue = useMemo(() => {
+ const prevContent = rContent.newContent(event.content) ?? event.content;
+ const customHtml = rContent.formattedBody(prevContent);
+ const body = rContent.body(prevContent);
const initialValue =
typeof customHtml === 'string'
? htmlToEditorInput(customHtml, isMarkdown)
: plainToEditorInput(typeof body === 'string' ? body : '', isMarkdown);
- Transforms.select(editor, {
- anchor: Editor.start(editor, []),
- focus: Editor.end(editor, []),
- });
-
- editor.insertFragment(initialValue);
- if (!mobileOrTablet()) ReactEditor.focus(editor);
- }, [editor, getPrevBodyAndFormattedBody, isMarkdown]);
+ return initialValue;
+ }, [event, isMarkdown]);
useEffect(() => {
- if (saveState.status === AsyncStatus.Success) {
+ if (senderState instanceof EventSenderSuccess) {
onCancel();
}
- }, [saveState, onCancel]);
+ }, [senderState, onCancel]);
return (
@@ -259,6 +180,7 @@ export const MessageEditor = as<'div', MessageEditorProps>(
)}
(
justifyContent="SpaceBetween"
gap="100"
>
-
-
- ) : undefined
- }
- >
- Save
-
-
- Cancel
-
+
+ {senderState instanceof EventSenderError && (
+
+ {senderState.error?.data.error ??
+ (senderState.cause instanceof Error
+ ? senderState.cause.message
+ : 'Unknown Error')}
+
+ )}
+ {saveState.status === AsyncStatus.Error && (
+
+ {saveState.error instanceof Error ? saveState.error.message : 'Unknown Error'}
+
+ )}
+
+ : undefined
+ }
+ >
+ Save
+
+
+ Cancel
+
+
- {(contentEvent) => (
-
- )}
+ {(contentEvent) =>
+ editId === event.event_id ? (
+
+ ) : (
+
+ )
+ }
)}
{reactionJSX}