diff --git a/src/components/LexicalEditor/Index.jsx b/src/components/LexicalEditor/Index.jsx index cc0a69b..b0cfa13 100644 --- a/src/components/LexicalEditor/Index.jsx +++ b/src/components/LexicalEditor/Index.jsx @@ -87,38 +87,29 @@ function LexicalDefaultValuePlugin({ value = "" }= {}) { const nodes = $generateNodesFromDOM(editor, dom); if (clear) { root.clear(); - // $getRoot().clear(); - // $getRoot().select(); } - // const p = $createParagraphNode(); - // const _p = nodes.filter(n => n.__size !== 0).forEach((n) => { const paragraphNode = $createParagraphNode(); paragraphNode.append(n); - // p.append(paragraphNode); root.append(paragraphNode); }); - - // root.append(...nodes.filter(n => n)); }; - // 默认值设置只用初始化一次; - // 空值不更新 HTML; useEffect(() => { if (editor && !isEmpty(value)) { editor.update(() => { updateHTML(editor, value, true); }); } - }, [value]); + }, [editor, value]); return null; } function MyOnChangePlugin({ ignoreHistoryMergeTagChange = true, ignoreSelectionChange = true, onChange }) { const [editor] = useLexicalComposerContext(); useEffect(() => { - if (onChange) { + if (typeof onChange === 'function') { return editor.registerUpdateListener(({editorState, dirtyElements, dirtyLeaves, prevEditorState, tags}) => { if ( @@ -130,21 +121,11 @@ function MyOnChangePlugin({ ignoreHistoryMergeTagChange = true, ignoreSelectionC ) { return; } - // const editorStateJSON = editorState.toJSON(); - let html; - let textContent; editorState.read(() => { const root = $getRoot(); const textContent = root.getTextContent(); - // console.log('textContent', textContent); - const html = $generateHtmlFromNodes(editor); - // console.log('html', html); - - // setEditorContent(content); - if (typeof onChange === 'function') { - onChange({ editorStateJSON: editorState.toJSON(), editor, tags, htmlContent: html, textContent }); - } + onChange({ editorStateJSON: editorState.toJSON(), editor, tags, htmlContent: html, textContent }); }); }); }