|
| 1 | +import './LexicalEditor.css'; |
| 2 | + |
| 3 | +import {AutoLinkNode, LinkNode} from '@lexical/link'; |
| 4 | +import {ListItemNode, ListNode} from '@lexical/list'; |
| 5 | +import {InitialConfigType, LexicalComposer} from '@lexical/react/LexicalComposer'; |
| 6 | +import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; |
| 7 | +import {ContentEditable} from '@lexical/react/LexicalContentEditable'; |
| 8 | +import {LexicalErrorBoundary} from '@lexical/react/LexicalErrorBoundary'; |
| 9 | +import {ListPlugin} from '@lexical/react/LexicalListPlugin'; |
| 10 | +import {HeadingNode} from '@lexical/rich-text'; |
| 11 | +import {joinClassNames} from '../../utils/classes.js'; |
| 12 | +import {SharedHistoryProvider, useSharedHistory} from './hooks/useSharedHistory.js'; |
| 13 | +import {ToolbarProvider} from './hooks/useToolbar.js'; |
| 14 | +import {ToolbarPlugin} from './plugins/ToolbarPlugin.js'; |
| 15 | +import {useMemo, useState} from 'preact/hooks'; |
| 16 | +import {ShortcutsPlugin} from './plugins/ShortcutsPlugin.js'; |
| 17 | +import {RichTextPlugin} from '@lexical/react/LexicalRichTextPlugin'; |
| 18 | +import {HistoryPlugin} from '@lexical/react/LexicalHistoryPlugin'; |
| 19 | +import {TabIndentationPlugin} from '@lexical/react/LexicalTabIndentationPlugin'; |
| 20 | +import {MarkdownTransformPlugin} from './plugins/MarkdownTransformPlugin.js'; |
| 21 | +import {LexicalTheme} from './LexicalTheme.js'; |
| 22 | +import {FloatingToolbarPlugin} from './plugins/FloatingToolbarPlugin.js'; |
| 23 | +import {OnChangePlugin} from './plugins/OnChangePlugin.js'; |
| 24 | +import {RichTextData} from '../../../shared/richtext.js'; |
| 25 | + |
| 26 | +const INITIAL_CONFIG: InitialConfigType = { |
| 27 | + namespace: 'RootCMS', |
| 28 | + theme: LexicalTheme, |
| 29 | + nodes: [ |
| 30 | + AutoLinkNode, |
| 31 | + HeadingNode, |
| 32 | + // ImageNode, |
| 33 | + LinkNode, |
| 34 | + ListNode, |
| 35 | + ListItemNode, |
| 36 | + // YouTubeNode, |
| 37 | + ], |
| 38 | + onError: (err: Error) => { |
| 39 | + console.error('[LexicalEditor] error:', err); |
| 40 | + throw err; |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +export interface LexicalEditorProps { |
| 45 | + className?: string; |
| 46 | + placeholder?: string; |
| 47 | + value?: RichTextData | null; |
| 48 | + onChange?: (value: RichTextData | null) => void; |
| 49 | +} |
| 50 | + |
| 51 | +export function LexicalEditor(props: LexicalEditorProps) { |
| 52 | + // This component sets up the context providers and shell for lexical, and |
| 53 | + // then renders the <Editor> component which can use the shared context states |
| 54 | + // to render the rich text editor. |
| 55 | + return ( |
| 56 | + <LexicalComposer initialConfig={INITIAL_CONFIG}> |
| 57 | + <SharedHistoryProvider> |
| 58 | + <ToolbarProvider> |
| 59 | + <div className={joinClassNames(props.className, 'LexicalEditor')}> |
| 60 | + <Editor |
| 61 | + placeholder={props.placeholder} |
| 62 | + value={props.value} |
| 63 | + onChange={props.onChange} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + </ToolbarProvider> |
| 67 | + </SharedHistoryProvider> |
| 68 | + </LexicalComposer> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +interface EditorProps { |
| 73 | + placeholder?: string; |
| 74 | + value?: RichTextData; |
| 75 | + onChange?: (value: RichTextData | null) => void; |
| 76 | +} |
| 77 | + |
| 78 | +function Editor(props: EditorProps) { |
| 79 | + const {historyState} = useSharedHistory(); |
| 80 | + const [editor] = useLexicalComposerContext(); |
| 81 | + const [activeEditor, setActiveEditor] = useState(editor); |
| 82 | + const [isLinkEditMode, setIsLinkEditMode] = useState(false); |
| 83 | + const [floatingAnchorElem, setFloatingAnchorElem] = |
| 84 | + useState<HTMLElement | null>(null); |
| 85 | + |
| 86 | + const onRef = (el: HTMLDivElement) => { |
| 87 | + if (el) { |
| 88 | + setFloatingAnchorElem(el); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + return ( |
| 93 | + <> |
| 94 | + <ToolbarPlugin |
| 95 | + editor={editor} |
| 96 | + activeEditor={activeEditor} |
| 97 | + setActiveEditor={setActiveEditor} |
| 98 | + setIsLinkEditMode={setIsLinkEditMode} |
| 99 | + /> |
| 100 | + <ShortcutsPlugin |
| 101 | + editor={activeEditor} |
| 102 | + setIsLinkEditMode={setIsLinkEditMode} |
| 103 | + /> |
| 104 | + <HistoryPlugin externalHistoryState={historyState} /> |
| 105 | + <RichTextPlugin |
| 106 | + contentEditable={ |
| 107 | + <div className="LexicalEditor__scroller"> |
| 108 | + <div className="LexicalEditor__root" ref={onRef}> |
| 109 | + <ContentEditable |
| 110 | + className="LexicalEditor__editor" |
| 111 | + placeholder={<Placeholder placeholder={props.placeholder} />} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + } |
| 116 | + ErrorBoundary={LexicalErrorBoundary} |
| 117 | + /> |
| 118 | + <ListPlugin /> |
| 119 | + <TabIndentationPlugin maxIndent={7} /> |
| 120 | + <MarkdownTransformPlugin /> |
| 121 | + <FloatingToolbarPlugin |
| 122 | + anchorElem={floatingAnchorElem!} |
| 123 | + setIsLinkEditMode={setIsLinkEditMode} |
| 124 | + /> |
| 125 | + <OnChangePlugin value={props.value} onChange={props.onChange} /> |
| 126 | + </> |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +const PLACEHOLDERS = [ |
| 131 | + 'Once upon a placeholder...', |
| 132 | + 'Start writing something legendary...', |
| 133 | + 'Words go here. Preferably brilliant ones...', |
| 134 | + 'Compose like nobody’s watching...', |
| 135 | + 'Your masterpiece begins here...', |
| 136 | + 'Add text that makes Hemingway jealous...', |
| 137 | + 'Here lies your unwritten brilliance...', |
| 138 | +]; |
| 139 | + |
| 140 | +function getRandPlaceholder() { |
| 141 | + const rand = Math.floor(Math.random() * PLACEHOLDERS.length); |
| 142 | + const placeholder = PLACEHOLDERS[rand]; |
| 143 | + return placeholder; |
| 144 | +} |
| 145 | + |
| 146 | +interface PlaceholderProps { |
| 147 | + placeholder?: string; |
| 148 | +} |
| 149 | + |
| 150 | +function Placeholder(props: PlaceholderProps) { |
| 151 | + const placeholder = useMemo(() => props.placeholder || getRandPlaceholder(), []); |
| 152 | + return ( |
| 153 | + <div className="LexicalEditor__placeholder">{placeholder}</div> |
| 154 | + ); |
| 155 | +} |
0 commit comments