|
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) { |
|
|
|
const [editor] = useLexicalComposerContext(); |
|
|
|
useEffect(() => { |
|
|
|
return editor.registerUpdateListener( |
|
({ editorState, tags, dirtyElements }) => { |
|
|
|
const isProgrammaticUpdate = tags.has(ProgrammaticTag); |
|
|
|
|
|
|
|
if (dirtyElements.size > 0 && !isProgrammaticUpdate) { |
|
onChange(editorState); |
|
} |
|
}, |
|
); |
|
}, [editor, onChange]); |
|
|
|
return null; |
|
} |
|
|