1- import { LexicalComposer } from '@lexical/react/LexicalComposer'
1+ import { defineExtension , configExtension } from 'lexical'
2+
3+ import { RichTextExtension } from '@lexical/rich-text'
4+ import { AutoFocusExtension } from '@lexical/extension'
5+ import { ListExtension } from '@lexical/list'
6+ import { LexicalExtensionComposer } from '@lexical/react/LexicalExtensionComposer'
7+ import { ReactExtension } from '@lexical/react/ReactExtension'
8+
29import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'
3- import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin'
410import { ContentEditable } from '@lexical/react/LexicalContentEditable'
5- import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin'
6- import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary'
711import { HeadingNode , QuoteNode } from '@lexical/rich-text'
812import { ListItemNode , ListNode } from '@lexical/list'
913import { CodeHighlightNode , CodeNode } from '@lexical/code'
10- import { ListPlugin } from '@lexical/react/LexicalListPlugin'
1114import { TableCellNode , TableNode , TableRowNode } from '@lexical/table'
1215import { AutoLinkNode , LinkNode } from '@lexical/link'
1316import { MarkdownShortcutPlugin } from '@lexical/react/LexicalMarkdownShortcutPlugin'
@@ -26,7 +29,6 @@ import DefaultTheme, {
2629} from './themes/DefaultTheme'
2730
2831import type { EditorState } from 'lexical'
29- import type { InitialConfigType } from '@lexical/react/LexicalComposer'
3032
3133export interface SliteProps {
3234 initialValue ?: string
@@ -35,10 +37,6 @@ export interface SliteProps {
3537 children : React . ReactNode
3638}
3739
38- function Placeholder ( ) {
39- return < div className = "editor-placeholder" > { 'Enter some rich text ...' } </ div >
40- }
41-
4240// ref: https://stackoverflow.com/questions/71976652/with-lexical-how-do-i-set-default-initial-text
4341const onChangeHandler = (
4442 editorState : EditorState ,
@@ -50,12 +48,17 @@ const onChangeHandler = (
5048 } )
5149}
5250
53- const getInitialConfig = (
54- initialValue : string ,
55- editable : boolean
56- ) : InitialConfigType => {
57- return {
58- editorState : ( ) => {
51+ const getExtensionConfig = ( initialValue : string , editable : boolean ) => {
52+ const appExtension = defineExtension ( {
53+ name : 'ReactSlite' ,
54+ namespace : 'ReactSlite' ,
55+ dependencies : [
56+ AutoFocusExtension ,
57+ RichTextExtension ,
58+ ListExtension ,
59+ configExtension ( ReactExtension , { contentEditable : null } ) ,
60+ ] ,
61+ $initialEditorState : ( ) => {
5962 // ref: https://stackoverflow.com/a/72172529/1410291
6063 // ref: https://github.com/facebook/lexical/issues/2308#issuecomment-1382721253
6164 if ( initialValue === '' ) {
@@ -86,22 +89,26 @@ const getInitialConfig = (
8689 AutoLinkNode ,
8790 LinkNode ,
8891 ] ,
89- namespace : '' ,
9092 editable,
91- }
93+ } )
94+
95+ // ref: https://lexical.dev/docs/extensions/react
96+ return appExtension
9297}
9398
9499export function Editor ( { readOnly } : { readOnly : SliteProps [ 'readOnly' ] } ) {
100+ const placeholderText = 'Enter some rich text...'
95101 return (
96102 < div className = "editor-inner" >
97- < RichTextPlugin
98- contentEditable = { < ContentEditable className = "editor-input" /> }
99- placeholder = { readOnly ? null : < Placeholder /> }
100- ErrorBoundary = { LexicalErrorBoundary }
103+ < ContentEditable
104+ readOnly = { readOnly }
105+ className = "editor-input"
106+ aria-placeholder = { placeholderText }
107+ placeholder = {
108+ < div className = "editor-placeholder" > { placeholderText } </ div >
109+ }
101110 />
102- < AutoFocusPlugin />
103111 < CodeHighlightPlugin />
104- < ListPlugin />
105112 < ListMaxIndentLevelPlugin maxDepth = { 1 } />
106113 </ div >
107114 )
@@ -120,8 +127,9 @@ export default function LexicalWrapper({
120127 const editable = ! readOnly
121128
122129 return (
123- < LexicalComposer
124- initialConfig = { getInitialConfig ( initialValue || '' , editable ) }
130+ < LexicalExtensionComposer
131+ extension = { getExtensionConfig ( initialValue || '' , editable ) }
132+ contentEditable = { null }
125133 >
126134 < div className = { SLITE_EDITOR_CONTAINER_CLASS } >
127135 { editable && (
@@ -132,7 +140,7 @@ export default function LexicalWrapper({
132140 { editable && < MarkdownShortcutPlugin transformers = { TRANSFORMERS } /> }
133141 { children }
134142 </ div >
135- </ LexicalComposer >
143+ </ LexicalExtensionComposer >
136144 )
137145}
138146
0 commit comments