const elementToCustomHtml = (node: CustomElement, children: string): string => {
switch (node.type) {
case BlockType.Paragraph:
- return `<p>${children}</p>`;
+ return `${children}<br/>`;
case BlockType.Heading:
return `<h${node.level}>${children}</h${node.level}>`;
case BlockType.CodeLine:
case BlockType.CodeBlock:
return `<pre><code>${children}</code></pre>`;
case BlockType.QuoteLine:
- return `<p>${children}</p>`;
+ return `${children}<br/>`;
case BlockType.BlockQuote:
return `<blockquote>${children}</blockquote>`;
case BlockType.ListItem:
const children = node.children.map((n) => toPlainText(n)).join('');
return elementToPlainText(node, children);
};
+
+/**
+ * Check if customHtml is equals to plainText
+ * by replacing `<br/>` 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(/<br\/>/g, '\n') === sanitizeText(plain);
+
+export const trimCustomHtml = (customHtml: string) => customHtml.replace(/<br\/>$/g, '');
createEmoticonElement,
moveCursor,
resetEditorHistory,
+ customHtmlEqualsPlainText,
+ trimCustomHtml,
} from '../../components/editor';
import { EmojiBoard, EmojiBoardTab } from '../../components/emoji-board';
import { UseStateProvider } from '../../components/UseStateProvider';
uploadBoardHandlers.current?.handleSend();
const plainText = toPlainText(editor.children).trim();
- const customHtml = toMatrixCustomHTML(editor.children);
+ const customHtml = trimCustomHtml(toMatrixCustomHTML(editor.children));
if (plainText === '') return;
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': {