mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: Fixed the issue where the prompt always displayed the initial value when switching between different generate operators #4764 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
import { EditorState, LexicalEditor } from 'lexical';
|
|
import { useEffect } from 'react';
|
|
import { ProgrammaticTag } from './constant';
|
|
|
|
interface IProps {
|
|
onChange: (
|
|
editorState: EditorState,
|
|
editor?: LexicalEditor,
|
|
tags?: Set<string>,
|
|
) => void;
|
|
}
|
|
|
|
export function VariableOnChangePlugin({ onChange }: IProps) {
|
|
// Access the editor through the LexicalComposerContext
|
|
const [editor] = useLexicalComposerContext();
|
|
// Wrap our listener in useEffect to handle the teardown and avoid stale references.
|
|
useEffect(() => {
|
|
// most listeners return a teardown function that can be called to clean them up.
|
|
return editor.registerUpdateListener(
|
|
({ editorState, tags, dirtyElements }) => {
|
|
// Check if there is a "programmatic" tag
|
|
const isProgrammaticUpdate = tags.has(ProgrammaticTag);
|
|
|
|
// The onchange event is only triggered when the data is manually updated
|
|
// Otherwise, the content will be displayed incorrectly.
|
|
if (dirtyElements.size > 0 && !isProgrammaticUpdate) {
|
|
onChange(editorState);
|
|
}
|
|
},
|
|
);
|
|
}, [editor, onChange]);
|
|
|
|
return null;
|
|
}
|