From 1503b242eabdc4ff6485419e10189c50cb663a58 Mon Sep 17 00:00:00 2001 From: zhaoying Date: Thu, 9 Apr 2026 12:32:24 +0800 Subject: [PATCH] fix(web): editor init --- .../Workflow/components/Editor/index.tsx | 4 +-- .../Editor/plugin/CharacterCountPlugin.tsx | 27 +++---------------- .../Editor/plugin/InitialValuePlugin.tsx | 20 ++++++++++---- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/web/src/views/Workflow/components/Editor/index.tsx b/web/src/views/Workflow/components/Editor/index.tsx index a631c9a3..52a4f23e 100644 --- a/web/src/views/Workflow/components/Editor/index.tsx +++ b/web/src/views/Workflow/components/Editor/index.tsx @@ -149,8 +149,8 @@ const Editor: FC =({ - - + + diff --git a/web/src/views/Workflow/components/Editor/plugin/CharacterCountPlugin.tsx b/web/src/views/Workflow/components/Editor/plugin/CharacterCountPlugin.tsx index 2a826fd0..ee9f4b89 100644 --- a/web/src/views/Workflow/components/Editor/plugin/CharacterCountPlugin.tsx +++ b/web/src/views/Workflow/components/Editor/plugin/CharacterCountPlugin.tsx @@ -1,41 +1,22 @@ -import { useEffect, useRef } from 'react'; +import { useEffect } from 'react'; import { $getRoot, $isParagraphNode } from 'lexical'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; -import { $isVariableNode } from '../nodes/VariableNode'; - -const CharacterCountPlugin = ({ setCount, onChange }: { setCount: (count: number) => void; onChange?: (value: string) => void }) => { +const CharacterCountPlugin = ({ setCount }: { setCount: (count: number) => void }) => { const [editor] = useLexicalComposerContext(); - const onChangeRef = useRef(onChange); - onChangeRef.current = onChange; useEffect(() => { return editor.registerUpdateListener(({ editorState, tags }) => { if (tags.has('programmatic')) return; editorState.read(() => { const root = $getRoot(); - let serializedContent = ''; - - // Traverse all nodes and serialize properly const paragraphs: string[] = []; root.getChildren().forEach(child => { if ($isParagraphNode(child)) { - let paragraphContent = ''; - child.getChildren().forEach(node => { - if ($isVariableNode(node)) { - paragraphContent += node.getTextContent(); - } else { - paragraphContent += node.getTextContent(); - } - }); - paragraphs.push(paragraphContent); + paragraphs.push(child.getChildren().map(n => n.getTextContent()).join('')); } }); - - serializedContent = paragraphs.join('\n'); - - setCount(serializedContent.length); - onChangeRef.current?.(serializedContent); + setCount(paragraphs.join('\n').length); }); }); }, [editor, setCount]); diff --git a/web/src/views/Workflow/components/Editor/plugin/InitialValuePlugin.tsx b/web/src/views/Workflow/components/Editor/plugin/InitialValuePlugin.tsx index 8b1685ae..cbdff244 100644 --- a/web/src/views/Workflow/components/Editor/plugin/InitialValuePlugin.tsx +++ b/web/src/views/Workflow/components/Editor/plugin/InitialValuePlugin.tsx @@ -6,7 +6,7 @@ */ import { useEffect, useRef } from 'react'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; -import { $getRoot, $createParagraphNode, $createTextNode } from 'lexical'; +import { $getRoot, $createParagraphNode, $createTextNode, $isParagraphNode } from 'lexical'; import { $createVariableNode } from '../nodes/VariableNode'; import { type Suggestion } from '../plugin/AutocompletePlugin' @@ -14,24 +14,34 @@ import { type Suggestion } from '../plugin/AutocompletePlugin' interface InitialValuePluginProps { value: string; options?: Suggestion[]; + onChange?: (value: string) => void; } -const InitialValuePlugin: React.FC = ({ value, options = [] }) => { +const InitialValuePlugin: React.FC = ({ value, options = [], onChange }) => { const [editor] = useLexicalComposerContext(); const prevValueRef = useRef(''); const isUserInputRef = useRef(false); const optionsRef = useRef(options); optionsRef.current = options; + const onChangeRef = useRef(onChange); + onChangeRef.current = onChange; useEffect(() => { return editor.registerUpdateListener(({ editorState, tags }) => { if (tags.has('programmatic')) return; editorState.read(() => { const root = $getRoot(); - const textContent = root.getTextContent(); - if (textContent !== prevValueRef.current) { + const paragraphs: string[] = []; + root.getChildren().forEach(child => { + if ($isParagraphNode(child)) { + paragraphs.push(child.getChildren().map(n => n.getTextContent()).join('')); + } + }); + const text = paragraphs.join('\n'); + if (text !== prevValueRef.current) { isUserInputRef.current = true; - prevValueRef.current = textContent; + prevValueRef.current = text; + onChangeRef.current?.(text); } }); });