diff --git a/src/view/com/composer/state/composer.ts b/src/view/com/composer/state/composer.ts index ea4399568d..75ea8153cf 100644 --- a/src/view/com/composer/state/composer.ts +++ b/src/view/com/composer/state/composer.ts @@ -670,6 +670,9 @@ export function createComposerState({ } } } + } else if (initMention) { + // highlight the mention + initRichText.detectFacetsWithoutResolution() } return { diff --git a/src/view/com/composer/text-input/TextInput.web.tsx b/src/view/com/composer/text-input/TextInput.web.tsx index 3f12b2f597..a087a3d69b 100644 --- a/src/view/com/composer/text-input/TextInput.web.tsx +++ b/src/view/com/composer/text-input/TextInput.web.tsx @@ -241,7 +241,7 @@ export function TextInput({ } }, }, - content: generateJSON(richtext.text.toString(), extensions, { + content: generateJSON(richTextToHTML(richtext), extensions, { preserveWhitespace: 'full', }), autofocus: 'end', @@ -382,6 +382,36 @@ export function TextInput({ ) } +/** + * Helper function to initialise the editor with RichText, which expects HTML + * + * All the extensions are able to initialise themselves from plain text, *except* + * for the Mention extension - we need to manually convert it into a `` element + * + * It also escapes HTML characters + */ +function richTextToHTML(richtext: RichText): string { + let html = '' + + for (const segment of richtext.segments()) { + if (segment.mention) { + html += `` + } else { + html += escapeHTML(segment.text) + } + } + + return html +} + +function escapeHTML(str: string): string { + return str + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') +} + function editorJsonToText( json: JSONContent, isLastDocumentChild: boolean = false,