@mdxeditor/editor 2.20.1 → 2.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/MDXEditor.js CHANGED
@@ -1,8 +1,7 @@
1
- import { useCellValues, usePublisher, useRealm } from "@mdxeditor/gurx";
1
+ import { useCellValue, useCellValues, usePublisher, useRealm } from "@mdxeditor/gurx";
2
2
  import React__default from "react";
3
3
  import { RealmWithPlugins } from "./RealmWithPlugins.js";
4
- import { corePlugin, initialRootEditorState$, usedLexicalNodes$, readOnly$, contentEditableClassName$, composerChildren$, topAreaChildren$, editorWrappers$, placeholder$, editorRootElementRef$, viewMode$, markdownSourceEditorValue$, markdown$, setMarkdown$, insertMarkdown$, rootEditor$ } from "./plugins/core/index.js";
5
- import { LexicalComposer } from "@lexical/react/LexicalComposer.js";
4
+ import { corePlugin, rootEditor$, contentEditableClassName$, composerChildren$, topAreaChildren$, editorWrappers$, placeholder$, editorRootElementRef$, viewMode$, markdownSourceEditorValue$, markdown$, setMarkdown$, insertMarkdown$ } from "./plugins/core/index.js";
6
5
  import { ContentEditable } from "@lexical/react/LexicalContentEditable.js";
7
6
  import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary.js";
8
7
  import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin.js";
@@ -10,24 +9,13 @@ import classNames from "classnames";
10
9
  import { lexicalTheme } from "./styles/lexicalTheme.js";
11
10
  import styles from "./styles/ui.module.css.js";
12
11
  import { noop } from "./utils/fp.js";
12
+ import { createLexicalComposerContext, LexicalComposerContext } from "@lexical/react/LexicalComposerContext";
13
13
  const LexicalProvider = ({ children }) => {
14
- const [initialRootEditorState, nodes, readOnly] = useCellValues(initialRootEditorState$, usedLexicalNodes$, readOnly$);
15
- return /* @__PURE__ */ React__default.createElement(
16
- LexicalComposer,
17
- {
18
- initialConfig: {
19
- editable: !readOnly,
20
- editorState: initialRootEditorState,
21
- namespace: "MDXEditor",
22
- theme: lexicalTheme,
23
- nodes,
24
- onError: (error) => {
25
- throw error;
26
- }
27
- }
28
- },
29
- children
30
- );
14
+ const rootEditor = useCellValue(rootEditor$);
15
+ const composerContextValue = React__default.useMemo(() => {
16
+ return [rootEditor, createLexicalComposerContext(null, lexicalTheme)];
17
+ }, [rootEditor]);
18
+ return /* @__PURE__ */ React__default.createElement(LexicalComposerContext.Provider, { value: composerContextValue }, children);
31
19
  };
32
20
  const RichTextEditor = () => {
33
21
  const [contentEditableClassName, composerChildren, topAreaChildren, editorWrappers, placeholder] = useCellValues(
@@ -8,6 +8,10 @@ function realmPlugin(plugin) {
8
8
  var _a;
9
9
  return (_a = plugin.init) == null ? void 0 : _a.call(plugin, realm, params);
10
10
  },
11
+ postInit: (realm) => {
12
+ var _a;
13
+ return (_a = plugin.postInit) == null ? void 0 : _a.call(plugin, realm, params);
14
+ },
11
15
  update: (realm) => {
12
16
  var _a;
13
17
  return (_a = plugin.update) == null ? void 0 : _a.call(plugin, realm, params);
@@ -18,10 +22,13 @@ function realmPlugin(plugin) {
18
22
  function RealmWithPlugins({ children, plugins }) {
19
23
  const theRealm = React__default.useMemo(() => {
20
24
  return tap(new Realm(), (r) => {
21
- var _a;
25
+ var _a, _b;
22
26
  for (const plugin of plugins) {
23
27
  (_a = plugin.init) == null ? void 0 : _a.call(plugin, r);
24
28
  }
29
+ for (const plugin of plugins) {
30
+ (_b = plugin.postInit) == null ? void 0 : _b.call(plugin, r);
31
+ }
25
32
  });
26
33
  }, []);
27
34
  React__default.useEffect(() => {
package/dist/index.d.ts CHANGED
@@ -43,7 +43,6 @@ import { Extension as Extension_2 } from '@codemirror/state';
43
43
  import { FC } from 'react';
44
44
  import { HeadingTagType } from '@lexical/rich-text';
45
45
  import { HistoryState } from '@lexical/history';
46
- import { InitialEditorStateType } from '@lexical/react/LexicalComposer.js';
47
46
  import { KlassConstructor } from 'lexical';
48
47
  import { LexicalCommand } from 'lexical';
49
48
  import { LexicalEditor } from 'lexical';
@@ -1295,8 +1294,6 @@ export declare const inFocus$: NodeRef<boolean>;
1295
1294
 
1296
1295
  /* Excluded from this release type: initialMarkdown$ */
1297
1296
 
1298
- /* Excluded from this release type: initialRootEditorState$ */
1299
-
1300
1297
  /* Excluded from this release type: INSERT_IMAGE_COMMAND */
1301
1298
 
1302
1299
  /**
@@ -2243,6 +2240,7 @@ export declare const readOnly$: NodeRef<boolean>;
2243
2240
  export declare interface RealmPlugin {
2244
2241
  init?: (realm: Realm) => void;
2245
2242
  update?: (realm: Realm) => void;
2243
+ postInit?: (realm: Realm) => void;
2246
2244
  }
2247
2245
 
2248
2246
  /**
@@ -2255,6 +2253,10 @@ export declare function realmPlugin<Params>(plugin: {
2255
2253
  * Called when the MDXEditor component is mounted and the plugin is initialized.
2256
2254
  */
2257
2255
  init?: (realm: Realm, params?: Params) => void;
2256
+ /**
2257
+ * Called after the MDXEditor component is mounted and all plugins are initialized.
2258
+ */
2259
+ postInit?: (realm: Realm, params?: Params) => void;
2258
2260
  /**
2259
2261
  * Called on each re-render. Use this to update the realm with updated property values.
2260
2262
  */
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ export * from "@mdxeditor/gurx";
3
3
  import { MDXEditor } from "./MDXEditor.js";
4
4
  import { MarkdownParseError, UnrecognizedMarkdownConstructError, importMarkdownToLexical, importMdastTreeToLexical } from "./importMarkdownToLexical.js";
5
5
  import { exportLexicalTreeToMdast, exportMarkdownFromLexical } from "./exportMarkdownFromLexical.js";
6
- import { Appender, NESTED_EDITOR_UPDATED_COMMAND, activeEditor$, activeEditorSubscriptions$, activePlugins$, addActivePlugin$, addComposerChild$, addEditorWrapper$, addExportVisitor$, addImportVisitor$, addLexicalNode$, addMdastExtension$, addNestedEditorChild$, addSyntaxExtension$, addToMarkdownExtension$, addTopAreaChild$, applyBlockType$, applyFormat$, autoFocus$, codeBlockEditorDescriptors$, composerChildren$, contentEditableClassName$, convertSelectionToNode$, corePlugin, createActiveEditorSubscription$, createRootEditorSubscription$, currentBlockType$, currentFormat$, currentSelection$, directiveDescriptors$, editorInFocus$, editorRootElementRef$, editorWrappers$, exportVisitors$, historyState$, iconComponentFor$, importVisitors$, inFocus$, initialMarkdown$, initialRootEditorState$, insertDecoratorNode$, insertMarkdown$, jsxComponentDescriptors$, jsxIsAvailable$, markdown$, markdownErrorSignal$, markdownProcessingError$, markdownSourceEditorValue$, mdastExtensions$, muteChange$, nestedEditorChildren$, onBlur$, placeholder$, readOnly$, rootEditor$, rootEditorSubscriptions$, setMarkdown$, syntaxExtensions$, toMarkdownExtensions$, toMarkdownOptions$, topAreaChildren$, translation$, useTranslation, usedLexicalNodes$, viewMode$ } from "./plugins/core/index.js";
6
+ import { Appender, NESTED_EDITOR_UPDATED_COMMAND, activeEditor$, activeEditorSubscriptions$, activePlugins$, addActivePlugin$, addComposerChild$, addEditorWrapper$, addExportVisitor$, addImportVisitor$, addLexicalNode$, addMdastExtension$, addNestedEditorChild$, addSyntaxExtension$, addToMarkdownExtension$, addTopAreaChild$, applyBlockType$, applyFormat$, autoFocus$, codeBlockEditorDescriptors$, composerChildren$, contentEditableClassName$, convertSelectionToNode$, corePlugin, createActiveEditorSubscription$, createRootEditorSubscription$, currentBlockType$, currentFormat$, currentSelection$, directiveDescriptors$, editorInFocus$, editorRootElementRef$, editorWrappers$, exportVisitors$, historyState$, iconComponentFor$, importVisitors$, inFocus$, initialMarkdown$, insertDecoratorNode$, insertMarkdown$, jsxComponentDescriptors$, jsxIsAvailable$, markdown$, markdownErrorSignal$, markdownProcessingError$, markdownSourceEditorValue$, mdastExtensions$, muteChange$, nestedEditorChildren$, onBlur$, placeholder$, readOnly$, rootEditor$, rootEditorSubscriptions$, setMarkdown$, syntaxExtensions$, toMarkdownExtensions$, toMarkdownOptions$, topAreaChildren$, translation$, useTranslation, usedLexicalNodes$, viewMode$ } from "./plugins/core/index.js";
7
7
  import { ALL_HEADING_LEVELS, allowedHeadingLevels$, headingsPlugin } from "./plugins/headings/index.js";
8
8
  import { insertThematicBreak$, thematicBreakPlugin } from "./plugins/thematic-break/index.js";
9
9
  import { applyListType$, currentListType$, listsPlugin } from "./plugins/lists/index.js";
@@ -216,7 +216,6 @@ export {
216
216
  importVisitors$,
217
217
  inFocus$,
218
218
  initialMarkdown$,
219
- initialRootEditorState$,
220
219
  insertCodeBlock$,
221
220
  insertCodeMirror$,
222
221
  insertDecoratorNode$,
@@ -4,7 +4,7 @@ import { $isHeadingNode } from "@lexical/rich-text";
4
4
  import { $setBlocksType } from "@lexical/selection";
5
5
  import { $findMatchingParent, $wrapNodeInElement, $insertNodeToNearestRoot } from "@lexical/utils";
6
6
  import { Cell, withLatestFrom, Signal, filter, map, useCellValue, scan } from "@mdxeditor/gurx";
7
- import { createCommand, FORMAT_TEXT_COMMAND, $isRootOrShadowRoot, $getRoot, $setSelection, $getSelection, $insertNodes, SELECTION_CHANGE_COMMAND, COMMAND_PRIORITY_CRITICAL, FOCUS_COMMAND, KEY_DOWN_COMMAND, $isDecoratorNode, BLUR_COMMAND, $isRangeSelection, $createParagraphNode, ParagraphNode, TextNode } from "lexical";
7
+ import { createCommand, FORMAT_TEXT_COMMAND, $isRootOrShadowRoot, $getRoot, $setSelection, $getSelection, $insertNodes, SELECTION_CHANGE_COMMAND, COMMAND_PRIORITY_CRITICAL, FOCUS_COMMAND, KEY_DOWN_COMMAND, $isDecoratorNode, BLUR_COMMAND, $isRangeSelection, $createParagraphNode, ParagraphNode, TextNode, createEditor } from "lexical";
8
8
  import { mdxJsxFromMarkdown, mdxJsxToMarkdown } from "mdast-util-mdx-jsx";
9
9
  import { mdxJsx } from "micromark-extension-mdx-jsx";
10
10
  import { mdxMd } from "micromark-extension-mdx-md";
@@ -28,6 +28,7 @@ import { MdastRootVisitor } from "./MdastRootVisitor.js";
28
28
  import { MdastTextVisitor } from "./MdastTextVisitor.js";
29
29
  import { SharedHistoryPlugin } from "./SharedHistoryPlugin.js";
30
30
  import { commentFromMarkdown, comment } from "../../mdastUtilHtmlComment.js";
31
+ import { lexicalTheme } from "../../styles/lexicalTheme.js";
31
32
  const NESTED_EDITOR_UPDATED_COMMAND = createCommand("NESTED_EDITOR_UPDATED_COMMAND");
32
33
  const rootEditor$ = Cell(null);
33
34
  const activeEditor$ = Cell(null);
@@ -362,25 +363,6 @@ function tryImportingMarkdown(r, node, markdownValue) {
362
363
  }
363
364
  }
364
365
  }
365
- const initialRootEditorState$ = Cell(null, (r) => {
366
- r.pub(initialRootEditorState$, (theRootEditor) => {
367
- r.pub(rootEditor$, theRootEditor);
368
- r.pub(activeEditor$, theRootEditor);
369
- tryImportingMarkdown(r, $getRoot(), r.getValue(initialMarkdown$));
370
- const autoFocusValue = r.getValue(autoFocus$);
371
- if (autoFocusValue) {
372
- if (autoFocusValue === true) {
373
- setTimeout(() => theRootEditor.focus(noop, { defaultSelection: "rootStart" }));
374
- return;
375
- }
376
- setTimeout(
377
- () => theRootEditor.focus(noop, {
378
- defaultSelection: autoFocusValue.defaultSelection ?? "rootStart"
379
- })
380
- );
381
- }
382
- });
383
- });
384
366
  const composerChildren$ = Cell([]);
385
367
  const addComposerChild$ = Appender(composerChildren$);
386
368
  const topAreaChildren$ = Cell([]);
@@ -520,6 +502,37 @@ const corePlugin = realmPlugin({
520
502
  });
521
503
  }
522
504
  },
505
+ postInit(r, params) {
506
+ const newEditor = createEditor({
507
+ editable: (params == null ? void 0 : params.readOnly) !== true,
508
+ namespace: "MDXEditor",
509
+ nodes: r.getValue(usedLexicalNodes$),
510
+ onError: (error) => {
511
+ throw error;
512
+ },
513
+ theme: lexicalTheme
514
+ });
515
+ newEditor.update(() => {
516
+ const markdown = params == null ? void 0 : params.initialMarkdown.trim();
517
+ if (markdown) {
518
+ tryImportingMarkdown(r, $getRoot(), markdown);
519
+ }
520
+ const autoFocusValue = params == null ? void 0 : params.autoFocus;
521
+ if (autoFocusValue) {
522
+ if (autoFocusValue === true) {
523
+ setTimeout(() => newEditor.focus(noop, { defaultSelection: "rootStart" }));
524
+ return;
525
+ }
526
+ setTimeout(
527
+ () => newEditor.focus(noop, {
528
+ defaultSelection: autoFocusValue.defaultSelection ?? "rootStart"
529
+ })
530
+ );
531
+ }
532
+ });
533
+ r.pub(rootEditor$, newEditor);
534
+ r.pub(activeEditor$, newEditor);
535
+ },
523
536
  update(realm, params) {
524
537
  realm.pubIn({
525
538
  [contentEditableClassName$]: params == null ? void 0 : params.contentEditableClassName,
@@ -580,7 +593,6 @@ export {
580
593
  importVisitors$,
581
594
  inFocus$,
582
595
  initialMarkdown$,
583
- initialRootEditorState$,
584
596
  insertDecoratorNode$,
585
597
  insertMarkdown$,
586
598
  jsxComponentDescriptors$,
@@ -2,7 +2,7 @@ import { TOGGLE_LINK_COMMAND, $isLinkNode } from "@lexical/link";
2
2
  import { KEY_ESCAPE_COMMAND, COMMAND_PRIORITY_LOW, KEY_MODIFIER_COMMAND, $getSelection, $isRangeSelection, COMMAND_PRIORITY_HIGH, $createTextNode, $insertNodes } from "lexical";
3
3
  import { IS_APPLE } from "../../utils/detectMac.js";
4
4
  import { getSelectionRectangle, getSelectedNode } from "../../utils/lexicalHelpers.js";
5
- import { createActiveEditorSubscription$, activeEditor$, currentSelection$, addComposerChild$ } from "../core/index.js";
5
+ import { createActiveEditorSubscription$, readOnly$, activeEditor$, currentSelection$, addComposerChild$ } from "../core/index.js";
6
6
  import { LinkDialog } from "./LinkDialog.js";
7
7
  import { Signal, Cell, withLatestFrom, map, Action, filter } from "@mdxeditor/gurx";
8
8
  import { realmPlugin } from "../../RealmWithPlugins.js";
@@ -42,7 +42,7 @@ const linkDialogState$ = Cell({ type: "inactive" }, (r) => {
42
42
  return editor.registerCommand(
43
43
  KEY_MODIFIER_COMMAND,
44
44
  (event) => {
45
- if (event.key === "k" && (IS_APPLE ? event.metaKey : event.ctrlKey)) {
45
+ if (event.key === "k" && (IS_APPLE ? event.metaKey : event.ctrlKey) && !r.getValue(readOnly$)) {
46
46
  const selection = $getSelection();
47
47
  if ($isRangeSelection(selection)) {
48
48
  r.pub(openLinkEditDialog$);
@@ -143,9 +143,9 @@ const linkDialogState$ = Cell({ type: "inactive" }, (r) => {
143
143
  r.link(
144
144
  r.pipe(
145
145
  r.combine(currentSelection$, onWindowChange$),
146
- withLatestFrom(activeEditor$, linkDialogState$),
147
- map(([[selection], activeEditor]) => {
148
- if ($isRangeSelection(selection) && activeEditor) {
146
+ withLatestFrom(activeEditor$, linkDialogState$, readOnly$),
147
+ map(([[selection], activeEditor, readOnly]) => {
148
+ if ($isRangeSelection(selection) && activeEditor && !readOnly) {
149
149
  const node = getLinkNodeInSelection(selection);
150
150
  if (node) {
151
151
  return {
@@ -17,6 +17,12 @@ import { isPartOftheEditorUI } from "../../utils/isPartOftheEditorUI.js";
17
17
  import { uuidv4 } from "../../utils/uuid4.js";
18
18
  import { iconComponentFor$, readOnly$, useTranslation, importVisitors$, exportVisitors$, usedLexicalNodes$, jsxComponentDescriptors$, directiveDescriptors$, codeBlockEditorDescriptors$, jsxIsAvailable$, rootEditor$, NESTED_EDITOR_UPDATED_COMMAND, editorRootElementRef$ } from "../core/index.js";
19
19
  import { useCellValues } from "@mdxeditor/gurx";
20
+ const getCellType = (rowIndex) => {
21
+ if (rowIndex === 0) {
22
+ return "th";
23
+ }
24
+ return "td";
25
+ };
20
26
  const AlignToTailwindClassMap = {
21
27
  center: styles.centeredCell,
22
28
  left: styles.leftAlignedCell,
@@ -151,7 +157,8 @@ const TableEditor = ({ mdastNode, parentEditor, lexicalTable }) => {
151
157
  }
152
158
  ));
153
159
  }), /* @__PURE__ */ React__default.createElement("th", { className: styles.tableToolsColumn }))), /* @__PURE__ */ React__default.createElement("tbody", null, mdastNode.children.map((row, rowIndex) => {
154
- return /* @__PURE__ */ React__default.createElement("tr", { key: rowIndex }, readOnly || /* @__PURE__ */ React__default.createElement("td", { className: styles.toolCell, "data-tool-cell": true }, /* @__PURE__ */ React__default.createElement(RowEditor, { ...{ setActiveCellWithBoundaries, parentEditor, rowIndex, highlightedCoordinates, lexicalTable } })), row.children.map((mdastCell, colIndex) => {
160
+ const CellElement = getCellType(rowIndex);
161
+ return /* @__PURE__ */ React__default.createElement("tr", { key: rowIndex }, readOnly || /* @__PURE__ */ React__default.createElement(CellElement, { className: styles.toolCell, "data-tool-cell": true }, /* @__PURE__ */ React__default.createElement(RowEditor, { ...{ setActiveCellWithBoundaries, parentEditor, rowIndex, highlightedCoordinates, lexicalTable } })), row.children.map((mdastCell, colIndex) => {
155
162
  var _a;
156
163
  return /* @__PURE__ */ React__default.createElement(
157
164
  Cell,
@@ -176,8 +183,9 @@ const Cell = ({ align, ...props }) => {
176
183
  const { activeCell, setActiveCell } = props;
177
184
  const isActive = Boolean(activeCell && activeCell[0] === props.colIndex && activeCell[1] === props.rowIndex);
178
185
  const className = AlignToTailwindClassMap[align || "left"];
186
+ const CellElement = getCellType(props.rowIndex);
179
187
  return /* @__PURE__ */ React__default.createElement(
180
- "td",
188
+ CellElement,
181
189
  {
182
190
  className,
183
191
  "data-active": isActive,