From 1eb09b72105ac07ec2d6774f6a886ad1110b99c4 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Fri, 30 Jan 2026 18:14:37 +0200 Subject: [PATCH] Detect facets when composer is opened with mention (#9625) * highlight the initial mention * fix mention initalisation on web --- src/view/com/composer/state/composer.ts | 3 ++ .../com/composer/text-input/TextInput.web.tsx | 32 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) 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,