From 42879ed09d34228f8cf90c783c338c3312f7cc6e Mon Sep 17 00:00:00 2001 From: Jimmy Liow Date: Thu, 7 Nov 2024 10:24:30 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=88=A0=E9=99=A4=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=90=88=E5=B9=B6=20onChange=20?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/LexicalEditor/Index.jsx | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) 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 }); }); }); }