|
| 1 | +import FormikBridgePlugin from '@/components/editor/plugins/formik' |
| 2 | +import LocalDraftPlugin from '@/components/editor/plugins/local-draft' |
| 3 | +import { MaxLengthPlugin } from '@/components/editor/plugins/max-length' |
| 4 | +import MentionsPlugin from '@/components/editor/plugins/mention' |
| 5 | +import FileUploadPlugin from '@/components/editor/plugins/upload' |
| 6 | +import PreviewPlugin from '@/components/editor/plugins/preview' |
| 7 | +import { AutoFocusExtension } from '@lexical/extension' |
| 8 | +import { ContentEditable } from '@lexical/react/LexicalContentEditable' |
| 9 | +import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' |
| 10 | +import { LexicalExtensionComposer } from '@lexical/react/LexicalExtensionComposer' |
| 11 | +import classNames from 'classnames' |
| 12 | +import { useFormikContext } from 'formik' |
| 13 | +import { configExtension, defineExtension } from 'lexical' |
| 14 | +import { useMemo, useState } from 'react' |
| 15 | +import theme from './theme' |
| 16 | +import styles from './theme/theme.module.css' |
| 17 | +import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin' |
| 18 | +import { ToolbarPlugin } from './plugins/tinytoolbar' |
| 19 | +import { ToolbarContextProvider } from './contexts/toolbar' |
| 20 | + |
| 21 | +/** |
| 22 | + * main lexical editor component with formik integration |
| 23 | + * @param {string} props.name - form field name |
| 24 | + * @param {string} [props.appendValue] - value to append to initial content |
| 25 | + * @param {boolean} [props.autoFocus] - whether to auto-focus the editor |
| 26 | + * @returns {JSX.Element} lexical editor component |
| 27 | + */ |
| 28 | +export default function SNEditor ({ name, appendValue, autoFocus, topLevel, ...props }) { |
| 29 | + const { values } = useFormikContext() |
| 30 | + |
| 31 | + const editor = useMemo(() => |
| 32 | + defineExtension({ |
| 33 | + $initialEditorState: (editor) => { |
| 34 | + // existing lexical state |
| 35 | + if (values.lexicalState) { |
| 36 | + try { |
| 37 | + const state = editor.parseEditorState(values.lexicalState) |
| 38 | + if (!state.isEmpty()) { |
| 39 | + editor.setEditorState(state) |
| 40 | + } |
| 41 | + } catch (error) { |
| 42 | + console.error('failed to load initial state:', error) |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + name: 'editor', |
| 47 | + namespace: 'sn', |
| 48 | + dependencies: [ |
| 49 | + configExtension(AutoFocusExtension, { disabled: !autoFocus }) |
| 50 | + ], |
| 51 | + theme: { ...theme, topLevel: topLevel ? 'topLevel' : '' }, |
| 52 | + onError: (error) => console.error('editor has encountered an error:', error) |
| 53 | + }), [autoFocus, topLevel]) |
| 54 | + |
| 55 | + return ( |
| 56 | + <LexicalExtensionComposer extension={editor} contentEditable={null}> |
| 57 | + <ToolbarContextProvider> |
| 58 | + <EditorContent topLevel={topLevel} name={name} {...props} /> |
| 59 | + </ToolbarContextProvider> |
| 60 | + </LexicalExtensionComposer> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * editor content component containing all plugins and UI elements |
| 66 | + * @param {string} props.name - form field name for draft saving |
| 67 | + * @param {string} props.placeholder - placeholder text for empty editor |
| 68 | + * @param {Object} props.lengthOptions - max length configuration |
| 69 | + * @param {boolean} props.topLevel - whether this is a top-level editor |
| 70 | + * @returns {JSX.Element} editor content with all plugins |
| 71 | + */ |
| 72 | +function EditorContent ({ name, placeholder, lengthOptions, topLevel }) { |
| 73 | + const [floatingAnchorElem, setFloatingAnchorElem] = useState(null) |
| 74 | + |
| 75 | + const onRef = (_floatingAnchorElem) => { |
| 76 | + if (_floatingAnchorElem !== null) { |
| 77 | + setFloatingAnchorElem(_floatingAnchorElem) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <div className={styles.editorContainer}> |
| 84 | + <ToolbarPlugin topLevel={topLevel} /> |
| 85 | + {/* we only need a plain text editor for markdown */} |
| 86 | + <PlainTextPlugin |
| 87 | + contentEditable={ |
| 88 | + <div className={styles.editor} ref={onRef}> |
| 89 | + <ContentEditable |
| 90 | + className={classNames(styles.editorInput, styles.text, topLevel && 'topLevel')} |
| 91 | + placeholder={<div className={styles.editorPlaceholder}>{placeholder}</div>} |
| 92 | + /> |
| 93 | + </div> |
| 94 | + } |
| 95 | + ErrorBoundary={LexicalErrorBoundary} |
| 96 | + /> |
| 97 | + {floatingAnchorElem && <PreviewPlugin editorRef={floatingAnchorElem} topLevel={topLevel} />} |
| 98 | + <FileUploadPlugin anchorElem={floatingAnchorElem} /> |
| 99 | + <MentionsPlugin /> |
| 100 | + <MaxLengthPlugin lengthOptions={lengthOptions} /> |
| 101 | + <LocalDraftPlugin name={name} /> |
| 102 | + <FormikBridgePlugin /> |
| 103 | + </div> |
| 104 | + </> |
| 105 | + ) |
| 106 | +} |
0 commit comments