Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(web): open variable dropdown from prompt toolbar icon without prior focus (#19728)

euvrec1f36263c60b
diff --git a/web/src/pages/agent/form/components/prompt-editor/__tests__/insert-variable-trigger.test.ts b/web/src/pages/agent/form/components/prompt-editor/__tests__/insert-variable-trigger.test.ts
new file mode 100644
index 0000000000..8e623234df
--- /dev/null
+++ b/web/src/pages/agent/form/components/prompt-editor/__tests__/insert-variable-trigger.test.ts
@@ -0,0 +1,55 @@
+import {
+ $createParagraphNode,
+ $createTextNode,
+ $getRoot,
+ createEditor,
+ LexicalEditor,
+} from 'lexical';
+import { insertVariableTrigger } from '../insert-variable-trigger';
+
+describe('insertVariableTrigger', () => {
+ let editor: LexicalEditor;
+ let rootElement: HTMLDivElement;
+
+ beforeEach(() => {
+ editor = createEditor({
+ namespace: 'insert-variable-trigger-test',
+ onError: (error) => {
+ throw error;
+ },
+ });
+ rootElement = document.createElement('div');
+ document.body.appendChild(rootElement);
+ editor.setRootElement(rootElement);
+ });
+
+ afterEach(() => {
+ editor.setRootElement(null);
+ rootElement.remove();
+ });
+
+ it('inserts the variable menu trigger when the editor has never been focused', () => {
+ insertVariableTrigger(editor);
+
+ editor.getEditorState().read(() => {
+ expect($getRoot().getTextContent()).toBe(' /');
+ });
+ });
+
+ it('inserts the variable menu trigger at the existing caret', () => {
+ editor.update(
+ () => {
+ const text = $createTextNode('hello');
+ $getRoot().append($createParagraphNode().append(text));
+ text.select(2, 2);
+ },
+ { discrete: true },
+ );
+
+ insertVariableTrigger(editor);
+
+ editor.getEditorState().read(() => {
+ expect($getRoot().getTextContent()).toBe('he /llo');
+ });
+ });
+});
diff --git a/web/src/pages/agent/form/components/prompt-editor/index.tsx b/web/src/pages/agent/form/components/prompt-editor/index.tsx
index 4879e08dbe..8d4075122c 100644
--- a/web/src/pages/agent/form/components/prompt-editor/index.tsx
+++ b/web/src/pages/agent/form/components/prompt-editor/index.tsx
@@ -7,13 +7,7 @@ import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
-import {
- $getRoot,
- $getSelection,
- EditorState,
- Klass,
- LexicalNode,
-} from 'lexical';
+import { $getRoot, EditorState, Klass, LexicalNode } from 'lexical';
import { Switch } from '@/components/ui/switch';
import {
@@ -36,6 +30,7 @@ import {
} from 'react';
import { useTranslation } from 'react-i18next';
import { EnterKeyPlugin } from './enter-key-plugin';
+import { insertVariableTrigger } from './insert-variable-trigger';
import { PasteHandlerPlugin } from './paste-handler-plugin';
import theme from './theme';
import { VariableNode } from './variable-node';
@@ -92,19 +87,9 @@ function PromptContent({
const [isBlur, setIsBlur] = useState(false);
const { t } = useTranslation();
- const insertTextAtCursor = useCallback(() => {
- editor.update(() => {
- const selection = $getSelection();
-
- if (selection !== null) {
- selection.insertText(' /');
- }
- });
- }, [editor]);
-
const handleVariableIconClick = useCallback(() => {
- insertTextAtCursor();
- }, [insertTextAtCursor]);
+ insertVariableTrigger(editor);
+ }, [editor]);
const handleBlur = useCallback(() => {
setIsBlur(true);
diff --git a/web/src/pages/agent/form/components/prompt-editor/insert-variable-trigger.ts b/web/src/pages/agent/form/components/prompt-editor/insert-variable-trigger.ts
new file mode 100644
index 0000000000..7717eede32
--- /dev/null
+++ b/web/src/pages/agent/form/components/prompt-editor/insert-variable-trigger.ts
@@ -0,0 +1,21 @@
+import {
+ $getRoot,
+ $getSelection,
+ $isRangeSelection,
+ LexicalEditor,
+} from 'lexical';
+
+export function insertVariableTrigger(editor: LexicalEditor) {
+ editor.update(
+ () => {
+ const selection = $getSelection();
+ if ($isRangeSelection(selection)) {
+ selection.insertText(' /');
+ } else {
+ $getRoot().selectEnd().insertText(' /');
+ }
+ },
+ { discrete: true },
+ );
+ editor.focus();
+}