Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28c4a5f

Browse files
committed
feat: support explicit nullable state
1 parent 5851d4a commit 28c4a5f

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/MDXEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ export interface MDXEditorProps {
324324
*/
325325
suppressSharedHistory?: boolean
326326
/**
327-
* The initial state of the lexical editor.
327+
* The initial state of the lexical editor. Pass null to disable any initiation.
328328
*/
329-
editorState?: EditorState | undefined
329+
editorState?: EditorState | undefined | null
330330
}
331331

332332
/**

src/plugins/core/index.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ export const corePlugin = realmPlugin<{
893893
translation: Translation
894894
trim?: boolean
895895
lexicalTheme?: EditorThemeClasses
896-
editorState?: EditorState | undefined
896+
editorState?: EditorState | null | undefined
897897
suppressSharedHistory?: boolean
898898
}>({
899899
init(r, params) {
@@ -951,7 +951,7 @@ export const corePlugin = realmPlugin<{
951951

952952
postInit(r, params) {
953953
const newEditor = createEditor({
954-
...(params?.editorState ? { editorState: params.editorState } : {}),
954+
// ...(params?.editorState ? { editorState: params.editorState } : {}),
955955
editable: params?.readOnly !== true,
956956
namespace: 'MDXEditor',
957957
nodes: r.getValue(usedLexicalNodes$),
@@ -961,26 +961,28 @@ export const corePlugin = realmPlugin<{
961961
theme: r.getValue(lexicalTheme$)
962962
})
963963

964-
newEditor.update(() => {
965-
const markdown = params?.initialMarkdown.trim() ?? ''
966-
tryImportingMarkdown(r, $getRoot(), markdown)
964+
if (params?.editorState !== null) {
965+
newEditor.update(() => {
966+
const markdown = params?.initialMarkdown.trim() ?? ''
967+
tryImportingMarkdown(r, $getRoot(), markdown)
967968

968-
const autoFocusValue = params?.autoFocus
969-
if (autoFocusValue) {
970-
if (autoFocusValue === true) {
971-
// Default 'on' state
969+
const autoFocusValue = params?.autoFocus
970+
if (autoFocusValue) {
971+
if (autoFocusValue === true) {
972+
// Default 'on' state
973+
setTimeout(() => {
974+
newEditor.focus(noop, { defaultSelection: 'rootStart' })
975+
})
976+
return
977+
}
972978
setTimeout(() => {
973-
newEditor.focus(noop, { defaultSelection: 'rootStart' })
979+
newEditor.focus(noop, {
980+
defaultSelection: autoFocusValue.defaultSelection ?? 'rootStart'
981+
})
974982
})
975-
return
976983
}
977-
setTimeout(() => {
978-
newEditor.focus(noop, {
979-
defaultSelection: autoFocusValue.defaultSelection ?? 'rootStart'
980-
})
981-
})
982-
}
983-
})
984+
})
985+
}
984986

985987
r.pub(rootEditor$, newEditor)
986988
r.pub(activeEditor$, newEditor)

0 commit comments

Comments
 (0)