From 053b80126239cc51679d7ece5b912af5fb68ddb8 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Sun, 23 Jul 2023 18:12:09 +1000 Subject: [PATCH] Fix editor custom html output (#1348) * replace paragraph with line breaks * stop sending plain msg as custom html * removes console log * fix false negative for sanitized customHtml * fix customHtmlEqualsPlainText doc --- src/app/components/editor/output.ts | 18 ++++++++++++++++-- src/app/organisms/room/RoomInput.tsx | 10 +++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/app/components/editor/output.ts b/src/app/components/editor/output.ts index 38c5449..5d0443f 100644 --- a/src/app/components/editor/output.ts +++ b/src/app/components/editor/output.ts @@ -17,7 +17,7 @@ const textToCustomHtml = (node: FormattedText): string => { const elementToCustomHtml = (node: CustomElement, children: string): string => { switch (node.type) { case BlockType.Paragraph: - return `

${children}

`; + return `${children}
`; case BlockType.Heading: return `${children}`; case BlockType.CodeLine: @@ -25,7 +25,7 @@ const elementToCustomHtml = (node: CustomElement, children: string): string => { case BlockType.CodeBlock: return `
${children}
`; case BlockType.QuoteLine: - return `

${children}

`; + return `${children}
`; case BlockType.BlockQuote: return `
${children}
`; case BlockType.ListItem: @@ -93,3 +93,17 @@ export const toPlainText = (node: Descendant | Descendant[]): string => { const children = node.children.map((n) => toPlainText(n)).join(''); return elementToPlainText(node, children); }; + +/** + * Check if customHtml is equals to plainText + * by replacing `
` with `/n` in customHtml + * and sanitizing plainText before comparison + * because text are sanitized in customHtml + * @param customHtml string + * @param plain string + * @returns boolean + */ +export const customHtmlEqualsPlainText = (customHtml: string, plain: string): boolean => + customHtml.replace(//g, '\n') === sanitizeText(plain); + +export const trimCustomHtml = (customHtml: string) => customHtml.replace(/$/g, ''); diff --git a/src/app/organisms/room/RoomInput.tsx b/src/app/organisms/room/RoomInput.tsx index e869e16..a50c800 100644 --- a/src/app/organisms/room/RoomInput.tsx +++ b/src/app/organisms/room/RoomInput.tsx @@ -51,6 +51,8 @@ import { createEmoticonElement, moveCursor, resetEditorHistory, + customHtmlEqualsPlainText, + trimCustomHtml, } from '../../components/editor'; import { EmojiBoard, EmojiBoardTab } from '../../components/emoji-board'; import { UseStateProvider } from '../../components/UseStateProvider'; @@ -251,7 +253,7 @@ export const RoomInput = forwardRef( uploadBoardHandlers.current?.handleSend(); const plainText = toPlainText(editor.children).trim(); - const customHtml = toMatrixCustomHTML(editor.children); + const customHtml = trimCustomHtml(toMatrixCustomHTML(editor.children)); if (plainText === '') return; @@ -271,9 +273,11 @@ export const RoomInput = forwardRef( const content: IContent = { msgtype: MsgType.Text, body, - format: 'org.matrix.custom.html', - formatted_body: formattedBody, }; + if (replyDraft || !customHtmlEqualsPlainText(formattedBody, body)) { + content.format = 'org.matrix.custom.html'; + content.formatted_body = formattedBody; + } if (replyDraft) { content['m.relates_to'] = { 'm.in_reply_to': {