|
|
@ -43,6 +43,7 @@ import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
|
|
|
|
import { $getRoot, $getSelection, $createParagraphNode } from 'lexical';
|
|
|
|
import { $getRoot, $getSelection, $createParagraphNode } from 'lexical';
|
|
|
|
import { $generateHtmlFromNodes, $generateNodesFromDOM, } from '@lexical/html';
|
|
|
|
import { $generateHtmlFromNodes, $generateNodesFromDOM, } from '@lexical/html';
|
|
|
|
// import { } from '@lexical/clipboard';
|
|
|
|
// import { } from '@lexical/clipboard';
|
|
|
|
|
|
|
|
import { isEmpty } from '@/utils/commons';
|
|
|
|
|
|
|
|
|
|
|
|
import './styles.css';
|
|
|
|
import './styles.css';
|
|
|
|
|
|
|
|
|
|
|
@ -87,7 +88,9 @@ function LexicalDefaultValuePlugin({ value = "" }= {}) {
|
|
|
|
if (clear) {
|
|
|
|
if (clear) {
|
|
|
|
root.clear();
|
|
|
|
root.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// console.log(nodes);
|
|
|
|
console.log('default value:');
|
|
|
|
|
|
|
|
console.log(value);
|
|
|
|
|
|
|
|
console.log(isEmpty(value))
|
|
|
|
|
|
|
|
|
|
|
|
const p = $createParagraphNode();
|
|
|
|
const p = $createParagraphNode();
|
|
|
|
const _p = nodes.filter(n => n).forEach((n) => {
|
|
|
|
const _p = nodes.filter(n => n).forEach((n) => {
|
|
|
@ -100,41 +103,56 @@ function LexicalDefaultValuePlugin({ value = "" }= {}) {
|
|
|
|
// root.append(...nodes.filter(n => n));
|
|
|
|
// root.append(...nodes.filter(n => n));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 默认值设置只用初始化一次;
|
|
|
|
|
|
|
|
// 空值不更新 HTML;
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
if (editor && value) {
|
|
|
|
if (editor && !isEmpty(value)) {
|
|
|
|
editor.update(() => {
|
|
|
|
editor.update(() => {
|
|
|
|
updateHTML(editor, value, true);
|
|
|
|
updateHTML(editor, value, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [value]);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function MyOnChangePlugin({ onChange }) {
|
|
|
|
function MyOnChangePlugin({ ignoreHistoryMergeTagChange = true, ignoreSelectionChange = true, onChange }) {
|
|
|
|
const [editor] = useLexicalComposerContext();
|
|
|
|
const [editor] = useLexicalComposerContext();
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
return editor.registerUpdateListener(({ editorState }) => {
|
|
|
|
if (onChange) {
|
|
|
|
// const editorStateJSON = editorState.toJSON();
|
|
|
|
return editor.registerUpdateListener(({editorState, dirtyElements, dirtyLeaves, prevEditorState, tags}) => {
|
|
|
|
let html;
|
|
|
|
|
|
|
|
let textContent;
|
|
|
|
if (
|
|
|
|
editorState.read(() => {
|
|
|
|
(ignoreSelectionChange &&
|
|
|
|
const root = $getRoot();
|
|
|
|
dirtyElements.size === 0 &&
|
|
|
|
const textContent = root.getTextContent();
|
|
|
|
dirtyLeaves.size === 0) ||
|
|
|
|
// console.log('textContent', textContent);
|
|
|
|
(ignoreHistoryMergeTagChange && tags.has('history-merge')) ||
|
|
|
|
|
|
|
|
prevEditorState.isEmpty()
|
|
|
|
const html = $generateHtmlFromNodes(editor);
|
|
|
|
) {
|
|
|
|
// console.log('html', html);
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// setEditorContent(content);
|
|
|
|
|
|
|
|
if (typeof onChange === 'function') {
|
|
|
|
|
|
|
|
onChange({ editorState, html, textContent });
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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({ editorState, editor, tags, html, textContent });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [editor, onChange]);
|
|
|
|
}, [editor, ignoreHistoryMergeTagChange, ignoreSelectionChange, onChange]);
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default function Editor({ isRichText, editorRef, onChange, initialValue, ...props }) {
|
|
|
|
export default function Editor({ isRichText, isDebug, editorRef, onChange, initialValue, ...props }) {
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<LexicalComposer initialConfig={editorConfig}>
|
|
|
|
<LexicalComposer initialConfig={editorConfig}>
|
|
|
|
<div className='editor-container'>
|
|
|
|
<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} />
|
|
|
|
<PlainTextPlugin contentEditable={<ContentEditable className='editor-pure-input' />} ErrorBoundary={LexicalErrorBoundary} />
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
<HistoryPlugin />
|
|
|
|
<HistoryPlugin />
|
|
|
|
{import.meta.env.DEV && <TreeViewPlugin />}
|
|
|
|
{(import.meta.env.DEV && isDebug) && <TreeViewPlugin />}
|
|
|
|
<LexicalDefaultValuePlugin value={initialValue} />
|
|
|
|
<LexicalDefaultValuePlugin value={initialValue} />
|
|
|
|
<AutoFocusPlugin />
|
|
|
|
<AutoFocusPlugin />
|
|
|
|
<CodeHighlightPlugin />
|
|
|
|
<CodeHighlightPlugin />
|
|
|
|