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
This commit is contained in:
parent
1a37fd0ca4
commit
053b801262
2 changed files with 23 additions and 5 deletions
|
@ -17,7 +17,7 @@ const textToCustomHtml = (node: FormattedText): string => {
|
||||||
const elementToCustomHtml = (node: CustomElement, children: string): string => {
|
const elementToCustomHtml = (node: CustomElement, children: string): string => {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case BlockType.Paragraph:
|
case BlockType.Paragraph:
|
||||||
return `<p>${children}</p>`;
|
return `${children}<br/>`;
|
||||||
case BlockType.Heading:
|
case BlockType.Heading:
|
||||||
return `<h${node.level}>${children}</h${node.level}>`;
|
return `<h${node.level}>${children}</h${node.level}>`;
|
||||||
case BlockType.CodeLine:
|
case BlockType.CodeLine:
|
||||||
|
@ -25,7 +25,7 @@ const elementToCustomHtml = (node: CustomElement, children: string): string => {
|
||||||
case BlockType.CodeBlock:
|
case BlockType.CodeBlock:
|
||||||
return `<pre><code>${children}</code></pre>`;
|
return `<pre><code>${children}</code></pre>`;
|
||||||
case BlockType.QuoteLine:
|
case BlockType.QuoteLine:
|
||||||
return `<p>${children}</p>`;
|
return `${children}<br/>`;
|
||||||
case BlockType.BlockQuote:
|
case BlockType.BlockQuote:
|
||||||
return `<blockquote>${children}</blockquote>`;
|
return `<blockquote>${children}</blockquote>`;
|
||||||
case BlockType.ListItem:
|
case BlockType.ListItem:
|
||||||
|
@ -93,3 +93,17 @@ export const toPlainText = (node: Descendant | Descendant[]): string => {
|
||||||
const children = node.children.map((n) => toPlainText(n)).join('');
|
const children = node.children.map((n) => toPlainText(n)).join('');
|
||||||
return elementToPlainText(node, children);
|
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, '');
|
||||||
|
|
|
@ -51,6 +51,8 @@ import {
|
||||||
createEmoticonElement,
|
createEmoticonElement,
|
||||||
moveCursor,
|
moveCursor,
|
||||||
resetEditorHistory,
|
resetEditorHistory,
|
||||||
|
customHtmlEqualsPlainText,
|
||||||
|
trimCustomHtml,
|
||||||
} from '../../components/editor';
|
} from '../../components/editor';
|
||||||
import { EmojiBoard, EmojiBoardTab } from '../../components/emoji-board';
|
import { EmojiBoard, EmojiBoardTab } from '../../components/emoji-board';
|
||||||
import { UseStateProvider } from '../../components/UseStateProvider';
|
import { UseStateProvider } from '../../components/UseStateProvider';
|
||||||
|
@ -251,7 +253,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||||
uploadBoardHandlers.current?.handleSend();
|
uploadBoardHandlers.current?.handleSend();
|
||||||
|
|
||||||
const plainText = toPlainText(editor.children).trim();
|
const plainText = toPlainText(editor.children).trim();
|
||||||
const customHtml = toMatrixCustomHTML(editor.children);
|
const customHtml = trimCustomHtml(toMatrixCustomHTML(editor.children));
|
||||||
|
|
||||||
if (plainText === '') return;
|
if (plainText === '') return;
|
||||||
|
|
||||||
|
@ -271,9 +273,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||||
const content: IContent = {
|
const content: IContent = {
|
||||||
msgtype: MsgType.Text,
|
msgtype: MsgType.Text,
|
||||||
body,
|
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) {
|
if (replyDraft) {
|
||||||
content['m.relates_to'] = {
|
content['m.relates_to'] = {
|
||||||
'm.in_reply_to': {
|
'm.in_reply_to': {
|
||||||
|
|
Loading…
Reference in a new issue