import {forwardRef, useImperativeHandle } from 'react'; import clsx from 'clsx'; import { LexicalComposer } from '@lexical/react/LexicalComposer'; import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin'; import { ContentEditable } from '@lexical/react/LexicalContentEditable'; import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary'; import { $getSelection } from 'lexical'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import InitialValuePlugin from './plugin/InitialValuePlugin' import LineBreakPlugin from './plugin/LineBreakPlugin'; import InsertTextPlugin from './plugin/InsertTextPlugin'; export interface EditorRef { insertText: (text: string) => void; } interface LexicalEditorProps { className?: string; placeholder?: string; value?: string; onChange?: (value: string) => void; height?: number; } const theme = { paragraph: 'editor-paragraph', text: { bold: 'editor-text-bold', italic: 'editor-text-italic', }, }; const EditorContent = forwardRef(({ className = '', value, placeholder = "请输入内容...", onChange, }, ref) => { const [editor] = useLexicalComposerContext(); useImperativeHandle(ref, () => ({ insertText: (text: string) => { editor.update(() => { const selection = $getSelection(); if (selection) { selection.insertText(text); } }); } }), [editor]); return (
} placeholder={
{placeholder}
} ErrorBoundary={LexicalErrorBoundary} />
); }); const Editor = forwardRef((props, ref) => { const initialConfig = { namespace: 'Editor', theme, nodes: [], onError: (error: Error) => { console.error(error); }, }; return ( ); }); export default Editor;