From 28d93aed2ac6984b798042bb9c32bf19369320f4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-RTLN3BA\\$punk" Date: Mon, 16 Feb 2026 01:34:36 -0800 Subject: [PATCH] feat: enhance inline mention editor to preserve mention tokens - Updated the text extraction logic to include inline mention tokens for chips, maintaining the original query structure sent to the backend. - Refactored the text extraction process to improve readability and efficiency. --- .../assistant-ui/inline-mention-editor.tsx | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/surfsense_web/components/assistant-ui/inline-mention-editor.tsx b/surfsense_web/components/assistant-ui/inline-mention-editor.tsx index b8194c91f..1e48abb9d 100644 --- a/surfsense_web/components/assistant-ui/inline-mention-editor.tsx +++ b/surfsense_web/components/assistant-ui/inline-mention-editor.tsx @@ -126,38 +126,41 @@ export const InlineMentionEditor = forwardRef { if (!editorRef.current) return ""; - let text = ""; - const walker = document.createTreeWalker( - editorRef.current, - NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, - { - acceptNode: (node) => { - // Skip chip elements entirely - if (node.nodeType === Node.ELEMENT_NODE) { - const el = node as Element; - if (el.hasAttribute(CHIP_DATA_ATTR)) { - return NodeFilter.FILTER_REJECT; // Skip this subtree - } - return NodeFilter.FILTER_SKIP; // Continue into children - } - return NodeFilter.FILTER_ACCEPT; - }, - } - ); - - let node: Node | null = walker.nextNode(); - while (node) { + const extractText = (node: Node): string => { if (node.nodeType === Node.TEXT_NODE) { - text += node.textContent; + return node.textContent ?? ""; } - node = walker.nextNode(); - } - return text.trim(); + if (node.nodeType === Node.ELEMENT_NODE) { + const element = node as Element; + + // Preserve mention chips as inline @title tokens. + if (element.hasAttribute(CHIP_DATA_ATTR)) { + const title = element + .querySelector("[data-mention-title='true']") + ?.textContent?.trim(); + if (title) { + return `@${title}`; + } + return ""; + } + + let result = ""; + for (const child of Array.from(element.childNodes)) { + result += extractText(child); + } + return result; + } + + return ""; + }; + + return extractText(editorRef.current).trim(); }, []); // Get all mentioned documents