diff --git a/web/app/components/base/prompt-editor/index.tsx b/web/app/components/base/prompt-editor/index.tsx index 50fdc1f920..0b73a7b8c9 100644 --- a/web/app/components/base/prompt-editor/index.tsx +++ b/web/app/components/base/prompt-editor/index.tsx @@ -2,6 +2,7 @@ import type { FC } from 'react' import React, { useEffect } from 'react' +import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' import type { EditorState, } from 'lexical' @@ -80,6 +81,29 @@ import { import { useEventEmitterContextContext } from '@/context/event-emitter' import cn from '@/utils/classnames' +const ValueSyncPlugin: FC<{ value?: string }> = ({ value }) => { + const [editor] = useLexicalComposerContext() + + useEffect(() => { + if (value === undefined) + return + + const incomingValue = value ?? '' + const shouldUpdate = editor.getEditorState().read(() => { + const currentText = $getRoot().getChildren().map(node => node.getTextContent()).join('\n') + return currentText !== incomingValue + }) + + if (!shouldUpdate) + return + + const editorState = editor.parseEditorState(textToEditorState(incomingValue)) + editor.setEditorState(editorState) + }, [editor, value]) + + return null +} + export type PromptEditorProps = { instanceId?: string compact?: boolean @@ -293,6 +317,7 @@ const PromptEditor: FC = ({ ) } +