feat: DefaultValuePlugin 默认值只用初始化一次;OnChangePlugin 可以忽略选择文本更新

2.0/email-builder
Jimmy Liow 11 months ago
parent b3f50ecb90
commit 78266378ff

@ -43,6 +43,7 @@ import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
import { $getRoot, $getSelection, $createParagraphNode } from 'lexical';
import { $generateHtmlFromNodes, $generateNodesFromDOM, } from '@lexical/html';
// import { } from '@lexical/clipboard';
import { isEmpty } from '@/utils/commons';
import './styles.css';
@ -87,7 +88,9 @@ function LexicalDefaultValuePlugin({ value = "" }= {}) {
if (clear) {
root.clear();
}
// console.log(nodes);
console.log('default value:');
console.log(value);
console.log(isEmpty(value))
const p = $createParagraphNode();
const _p = nodes.filter(n => n).forEach((n) => {
@ -100,20 +103,33 @@ function LexicalDefaultValuePlugin({ value = "" }= {}) {
// root.append(...nodes.filter(n => n));
};
//
// HTML
useEffect(() => {
if (editor && value) {
if (editor && !isEmpty(value)) {
editor.update(() => {
updateHTML(editor, value, true);
});
}
}, [value]);
}, []);
return null;
}
function MyOnChangePlugin({ onChange }) {
function MyOnChangePlugin({ ignoreHistoryMergeTagChange = true, ignoreSelectionChange = true, onChange }) {
const [editor] = useLexicalComposerContext();
useEffect(() => {
return editor.registerUpdateListener(({ editorState }) => {
if (onChange) {
return editor.registerUpdateListener(({editorState, dirtyElements, dirtyLeaves, prevEditorState, tags}) => {
if (
(ignoreSelectionChange &&
dirtyElements.size === 0 &&
dirtyLeaves.size === 0) ||
(ignoreHistoryMergeTagChange && tags.has('history-merge')) ||
prevEditorState.isEmpty()
) {
return;
}
// const editorStateJSON = editorState.toJSON();
let html;
let textContent;
@ -127,14 +143,16 @@ function MyOnChangePlugin({ onChange }) {
// setEditorContent(content);
if (typeof onChange === 'function') {
onChange({ editorState, html, textContent });
onChange({ editorState, editor, tags, html, textContent });
}
});
});
}, [editor, onChange]);
}
}, [editor, ignoreHistoryMergeTagChange, ignoreSelectionChange, onChange]);
return null;
}
export default function Editor({ isRichText, editorRef, onChange, initialValue, ...props }) {
export default function Editor({ isRichText, isDebug, editorRef, onChange, initialValue, ...props }) {
return (
<LexicalComposer initialConfig={editorConfig}>
<div className='editor-container'>
@ -147,7 +165,7 @@ export default function Editor({ isRichText, editorRef, onChange, initialValue,
<PlainTextPlugin contentEditable={<ContentEditable className='editor-pure-input' />} ErrorBoundary={LexicalErrorBoundary} />
)}
<HistoryPlugin />
{import.meta.env.DEV && <TreeViewPlugin />}
{(import.meta.env.DEV && isDebug) && <TreeViewPlugin />}
<LexicalDefaultValuePlugin value={initialValue} />
<AutoFocusPlugin />
<CodeHighlightPlugin />

Loading…
Cancel
Save