Skip to content

Commit 74091fd

Browse files
committed
feat: additional tweaks for the lexical editor initialization
1 parent fa9d864 commit 74091fd

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/MDXEditor.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useCellValue, useCellValues, usePublisher, useRealm } from '@mdxeditor/gurx'
22
import React from 'react'
3-
import { RealmPlugin, RealmWithPlugins } from './RealmWithPlugins'
43
import {
5-
Translation,
4+
AdditionalLexicalNode,
5+
bottomAreaChildren$,
66
composerChildren$,
77
contentEditableClassName$,
8-
spellCheck$,
8+
contentEditableRef$,
99
corePlugin,
1010
editorRootElementRef$,
1111
editorWrappers$,
@@ -15,24 +15,25 @@ import {
1515
placeholder$,
1616
rootEditor$,
1717
setMarkdown$,
18+
spellCheck$,
1819
topAreaChildren$,
19-
bottomAreaChildren$,
20+
Translation,
2021
useTranslation,
21-
viewMode$,
22-
contentEditableRef$
22+
viewMode$
2323
} from './plugins/core'
24+
import { RealmPlugin, RealmWithPlugins } from './RealmWithPlugins'
2425

26+
import { createLexicalComposerContext, LexicalComposerContext, LexicalComposerContextType } from '@lexical/react/LexicalComposerContext'
2527
import { ContentEditable } from '@lexical/react/LexicalContentEditable'
28+
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary'
2629
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin'
2730
import classNames from 'classnames'
31+
import { EditorState, EditorThemeClasses, LexicalEditor } from 'lexical'
32+
import { defaultSvgIcons, IconKey } from './defaultSvgIcons'
2833
import { ToMarkdownOptions } from './exportMarkdownFromLexical'
2934
import { lexicalTheme } from './styles/lexicalTheme'
3035
import styles from './styles/ui.module.css'
3136
import { noop } from './utils/fp'
32-
import { createLexicalComposerContext, LexicalComposerContext, LexicalComposerContextType } from '@lexical/react/LexicalComposerContext'
33-
import { EditorState, EditorThemeClasses, LexicalEditor } from 'lexical'
34-
import { IconKey, defaultSvgIcons } from './defaultSvgIcons'
35-
import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary'
3637

3738
const LexicalProvider: React.FC<{
3839
children: JSX.Element | string | (JSX.Element | string)[]
@@ -327,6 +328,16 @@ export interface MDXEditorProps {
327328
* The initial state of the lexical editor. Pass null to disable any initiation.
328329
*/
329330
editorState?: EditorState | undefined | null
331+
332+
/**
333+
* Additional lexical nodes to include in the editor.
334+
*/
335+
additionalLexicalNodes?: AdditionalLexicalNode[]
336+
337+
/**
338+
* The lexical editor namespace.
339+
*/
340+
lexicalEditorNamespace?: string
330341
}
331342

332343
/**
@@ -354,7 +365,9 @@ export const MDXEditor = React.forwardRef<MDXEditorMethods, MDXEditorProps>((pro
354365
trim: props.trim ?? true,
355366
lexicalTheme: props.lexicalTheme,
356367
...('editorState' in props ? { editorState: props.editorState } : {}),
357-
suppressSharedHistory: props.suppressSharedHistory ?? false
368+
suppressSharedHistory: props.suppressSharedHistory ?? false,
369+
additionalLexicalNodes: props.additionalLexicalNodes ?? [],
370+
lexicalEditorNamespace: props.lexicalEditorNamespace ?? 'MDXEditor'
358371
}),
359372
...(props.plugins ?? [])
360373
]}

src/plugins/core/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import {
2121
FOCUS_COMMAND,
2222
FORMAT_TEXT_COMMAND,
2323
Klass,
24+
KlassConstructor,
2425
LexicalEditor,
2526
LexicalNode,
27+
LexicalNodeReplacement,
2628
ParagraphNode,
2729
RangeSelection,
2830
SELECTION_CHANGE_COMMAND,
@@ -75,6 +77,8 @@ import { IconKey } from '../../defaultSvgIcons'
7577
export * from './MdastHTMLNode'
7678
export * from './GenericHTMLNode'
7779

80+
export type AdditionalLexicalNode = KlassConstructor<typeof LexicalNode> | LexicalNodeReplacement
81+
7882
/**
7983
* A function that subscribes to Lexical editor updates or events, and retursns an unsubscribe function.
8084
* @group Core
@@ -895,6 +899,8 @@ export const corePlugin = realmPlugin<{
895899
lexicalTheme?: EditorThemeClasses
896900
editorState?: EditorState | null | undefined
897901
suppressSharedHistory?: boolean
902+
additionalLexicalNodes?: AdditionalLexicalNode[]
903+
lexicalEditorNamespace?: string
898904
}>({
899905
init(r, params) {
900906
const initialMarkdown = params?.initialMarkdown ?? ''
@@ -953,8 +959,8 @@ export const corePlugin = realmPlugin<{
953959
const newEditor = createEditor({
954960
// ...(params?.editorState ? { editorState: params.editorState } : {}),
955961
editable: params?.readOnly !== true,
956-
namespace: 'MDXEditor',
957-
nodes: r.getValue(usedLexicalNodes$),
962+
namespace: params?.lexicalEditorNamespace ?? 'MDXEditor',
963+
nodes: [...r.getValue(usedLexicalNodes$), ...(params?.additionalLexicalNodes ?? [])],
958964
onError: (error) => {
959965
throw error
960966
},

0 commit comments

Comments
 (0)