@mdxeditor/editor 2.20.6 → 3.0.0-alpha.2

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/index.d.ts CHANGED
@@ -66,7 +66,6 @@ import { ReactNode } from 'react';
66
66
  import { Realm } from '@mdxeditor/gurx';
67
67
  import { RootNode } from 'lexical';
68
68
  import { SandpackProvider } from '@codesandbox/sandpack-react';
69
- import { SandpackThemeProp } from '@codesandbox/sandpack-react/types';
70
69
  import { SerializedElementNode } from 'lexical';
71
70
  import { SerializedLexicalNode } from 'lexical';
72
71
  import { Spread } from 'lexical';
@@ -536,8 +535,21 @@ export declare const codeBlockPlugin: (params?: {
536
535
  defaultCodeBlockLanguage?: string | undefined;
537
536
  } | undefined) => RealmPlugin;
538
537
 
538
+ /**
539
+ * Whether or not to try to dynamically load the code block language support.
540
+ * Disable if you want to manually pass the supported languages.
541
+ * @group CodeMirror
542
+ */
543
+ export declare const codeMirrorAutoLoadLanguageSupport$: NodeRef<boolean>;
544
+
539
545
  export declare const CodeMirrorEditor: ({ language, nodeKey, code, focusEmitter }: CodeBlockEditorProps) => React_2.JSX.Element;
540
546
 
547
+ /**
548
+ * The code mirror extensions for the coemirror code block editor.
549
+ * @group CodeMirror
550
+ */
551
+ export declare const codeMirrorExtensions$: NodeRef<Extension_2[]>;
552
+
541
553
  /**
542
554
  * A plugin that adds lets users edit code blocks with CodeMirror.
543
555
  * @group CodeMirror
@@ -545,20 +557,17 @@ export declare const CodeMirrorEditor: ({ language, nodeKey, code, focusEmitter
545
557
  export declare const codeMirrorPlugin: (params?: {
546
558
  codeBlockLanguages: Record<string, string>;
547
559
  /**
548
- * The theme of CodeMirrorEditor
560
+ * Optional, additional CodeMirror extensions to load in the diff/source mode.
561
+ */
562
+ codeMirrorExtensions?: Extension_2[] | undefined;
563
+ /**
564
+ * Whether or not to try to dynamically load the code block language support.
565
+ * Disable if you want to manually pass the supported languages.
566
+ * @group CodeMirror
549
567
  */
550
- theme?: SandpackThemeProp | undefined;
568
+ autoLoadLanguageSupport?: boolean | undefined;
551
569
  } | undefined) => RealmPlugin;
552
570
 
553
- /**
554
- * The theme CodeMirrorEditor used.
555
- * It can be "light" | "dark" | "auto",
556
- * or the theme in "@codesandbox/sandpack-themes" package,
557
- * or you can also custom one,
558
- * learn more https://sandpack.codesandbox.io/docs/getting-started/themes#custom-theme
559
- */
560
- export declare const codeMirrorTheme$: NodeRef<SandpackThemeProp>;
561
-
562
571
  /**
563
572
  * A toolbar component that lets the user toggle code formatting.
564
573
  * Use for inline `code` elements (like variables, methods, etc).
@@ -566,6 +575,8 @@ export declare const codeMirrorTheme$: NodeRef<SandpackThemeProp>;
566
575
  */
567
576
  export declare const CodeToggle: React_2.FC;
568
577
 
578
+ export declare const COMMON_STATE_CONFIG_EXTENSIONS: Extension_2[];
579
+
569
580
  /**
570
581
  * Performs left to right composition of two functions.
571
582
  * @group Utils
package/dist/index.js CHANGED
@@ -16,8 +16,8 @@ import { maxLengthPlugin } from "./plugins/maxlength/index.js";
16
16
  import { insertJsx$, isMdastJsxNode, jsxPlugin } from "./plugins/jsx/index.js";
17
17
  import { GenericJsxEditor } from "./jsx-editors/GenericJsxEditor.js";
18
18
  import { insertSandpack$, sandpackConfig$, sandpackPlugin } from "./plugins/sandpack/index.js";
19
- import { codeBlockLanguages$, codeMirrorPlugin, codeMirrorTheme$, insertCodeMirror$ } from "./plugins/codemirror/index.js";
20
- import { CodeMirrorEditor } from "./plugins/codemirror/CodeMirrorEditor.js";
19
+ import { codeBlockLanguages$, codeMirrorAutoLoadLanguageSupport$, codeMirrorExtensions$, codeMirrorPlugin, insertCodeMirror$ } from "./plugins/codemirror/index.js";
20
+ import { COMMON_STATE_CONFIG_EXTENSIONS, CodeMirrorEditor } from "./plugins/codemirror/CodeMirrorEditor.js";
21
21
  import { appendCodeBlockEditorDescriptor$, codeBlockPlugin, defaultCodeBlockLanguage$, insertCodeBlock$ } from "./plugins/codeblock/index.js";
22
22
  import { directivesPlugin, insertDirective$ } from "./plugins/directives/index.js";
23
23
  import { ADMONITION_TYPES, AdmonitionDirectiveDescriptor } from "./directive-editors/AdmonitionDirectiveDescriptor.js";
@@ -85,6 +85,7 @@ export {
85
85
  ButtonOrDropdownButton,
86
86
  ButtonWithTooltip,
87
87
  CAN_USE_DOM,
88
+ COMMON_STATE_CONFIG_EXTENSIONS,
88
89
  ChangeAdmonitionType,
89
90
  ChangeCodeMirrorLanguage,
90
91
  CodeBlockNode,
@@ -165,8 +166,9 @@ export {
165
166
  codeBlockEditorDescriptors$,
166
167
  codeBlockLanguages$,
167
168
  codeBlockPlugin,
169
+ codeMirrorAutoLoadLanguageSupport$,
170
+ codeMirrorExtensions$,
168
171
  codeMirrorPlugin,
169
- codeMirrorTheme$,
170
172
  compose,
171
173
  composerChildren$,
172
174
  contentEditableClassName$,
@@ -97,33 +97,30 @@ class CodeBlockNode extends DecoratorNode {
97
97
  }
98
98
  const CodeBlockEditorContext = React__default.createContext(null);
99
99
  const CodeBlockEditorContextProvider = ({ parentEditor, lexicalNode, children }) => {
100
- return /* @__PURE__ */ React__default.createElement(
101
- CodeBlockEditorContext.Provider,
102
- {
103
- value: {
104
- lexicalNode,
105
- setCode: (code) => {
106
- parentEditor.update(() => {
107
- lexicalNode.setCode(code);
108
- setTimeout(() => {
109
- parentEditor.dispatchCommand(NESTED_EDITOR_UPDATED_COMMAND, void 0);
110
- }, 0);
111
- });
112
- },
113
- setLanguage: (language) => {
114
- parentEditor.update(() => {
115
- lexicalNode.setLanguage(language);
116
- });
117
- },
118
- setMeta: (meta) => {
119
- parentEditor.update(() => {
120
- lexicalNode.setMeta(meta);
121
- });
122
- }
100
+ const contextValue = React__default.useMemo(() => {
101
+ return {
102
+ lexicalNode,
103
+ setCode: (code) => {
104
+ parentEditor.update(() => {
105
+ lexicalNode.setCode(code);
106
+ setTimeout(() => {
107
+ parentEditor.dispatchCommand(NESTED_EDITOR_UPDATED_COMMAND, void 0);
108
+ }, 0);
109
+ });
110
+ },
111
+ setLanguage: (language) => {
112
+ parentEditor.update(() => {
113
+ lexicalNode.setLanguage(language);
114
+ });
115
+ },
116
+ setMeta: (meta) => {
117
+ parentEditor.update(() => {
118
+ lexicalNode.setMeta(meta);
119
+ });
123
120
  }
124
- },
125
- children
126
- );
121
+ };
122
+ }, [lexicalNode, parentEditor]);
123
+ return /* @__PURE__ */ React__default.createElement(CodeBlockEditorContext.Provider, { value: contextValue }, children);
127
124
  };
128
125
  function useCodeBlockEditorContext() {
129
126
  const context = React__default.useContext(CodeBlockEditorContext);
@@ -1,44 +1,83 @@
1
- import { SandpackProvider, CodeEditor } from "@codesandbox/sandpack-react";
2
1
  import React__default from "react";
3
2
  import styles from "../../styles/ui.module.css.js";
4
3
  import { useCodeBlockEditorContext } from "../codeblock/CodeBlockNode.js";
5
4
  import { readOnly$ } from "../core/index.js";
6
- import { useCodeMirrorRef } from "../sandpack/useCodeMirrorRef.js";
7
5
  import { useCellValues } from "@mdxeditor/gurx";
8
- import { codeMirrorTheme$ } from "./index.js";
6
+ import { EditorState } from "@codemirror/state";
7
+ import { lineNumbers, EditorView } from "@codemirror/view";
8
+ import { basicLight } from "cm6-theme-basic-light";
9
+ import { basicSetup } from "codemirror";
10
+ import { languages } from "@codemirror/language-data";
11
+ import { useCodeMirrorRef } from "../sandpack/useCodeMirrorRef.js";
12
+ import { codeMirrorExtensions$, codeMirrorAutoLoadLanguageSupport$ } from "./index.js";
13
+ const COMMON_STATE_CONFIG_EXTENSIONS = [];
9
14
  const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }) => {
10
- const codeMirrorRef = useCodeMirrorRef(nodeKey, "codeblock", "jsx", focusEmitter);
11
- const [readOnly, theme] = useCellValues(readOnly$, codeMirrorTheme$);
15
+ const [readOnly, codeMirrorExtensions, autoLoadLanguageSupport] = useCellValues(
16
+ readOnly$,
17
+ codeMirrorExtensions$,
18
+ codeMirrorAutoLoadLanguageSupport$
19
+ );
20
+ const codeMirrorRef = useCodeMirrorRef(nodeKey, "codeblock", language, focusEmitter);
12
21
  const { setCode } = useCodeBlockEditorContext();
22
+ const editorViewRef = React__default.useRef(null);
23
+ const elRef = React__default.useRef(null);
24
+ const setCodeRef = React__default.useRef(setCode);
25
+ setCodeRef.current = setCode;
26
+ codeMirrorRef.current = {
27
+ getCodemirror: () => editorViewRef.current
28
+ };
13
29
  React__default.useEffect(() => {
14
- var _a, _b;
15
- (_b = (_a = codeMirrorRef.current) == null ? void 0 : _a.getCodemirror()) == null ? void 0 : _b.dom.addEventListener("paste", (e) => {
16
- e.stopPropagation();
17
- });
18
- }, [codeMirrorRef, language]);
30
+ void (async () => {
31
+ const extensions = [
32
+ ...codeMirrorExtensions,
33
+ basicSetup,
34
+ basicLight,
35
+ lineNumbers(),
36
+ EditorView.lineWrapping,
37
+ EditorView.updateListener.of(({ state }) => {
38
+ setCodeRef.current(state.doc.toString());
39
+ })
40
+ ];
41
+ if (readOnly) {
42
+ extensions.push(EditorState.readOnly.of(true));
43
+ }
44
+ if (language !== "" && autoLoadLanguageSupport) {
45
+ const languageData = languages.find((l) => {
46
+ return l.name === language || l.alias.includes(language) || l.extensions.includes(language);
47
+ });
48
+ if (languageData) {
49
+ try {
50
+ const languageSupport = await languageData.load();
51
+ extensions.push(languageSupport.extension);
52
+ } catch (e) {
53
+ console.warn("failed to load language support for", language);
54
+ }
55
+ }
56
+ }
57
+ elRef.current.innerHTML = "";
58
+ editorViewRef.current = new EditorView({
59
+ parent: elRef.current,
60
+ state: EditorState.create({ doc: code, extensions })
61
+ });
62
+ })();
63
+ return () => {
64
+ var _a;
65
+ (_a = editorViewRef.current) == null ? void 0 : _a.destroy();
66
+ editorViewRef.current = null;
67
+ };
68
+ }, [readOnly, language]);
19
69
  return /* @__PURE__ */ React__default.createElement(
20
70
  "div",
21
71
  {
22
- className: styles.sandpackWrapper,
72
+ className: styles.codeMirrorWrapper,
23
73
  onKeyDown: (e) => {
24
74
  e.stopPropagation();
25
75
  }
26
76
  },
27
- /* @__PURE__ */ React__default.createElement(SandpackProvider, { theme }, /* @__PURE__ */ React__default.createElement(
28
- CodeEditor,
29
- {
30
- readOnly,
31
- showLineNumbers: true,
32
- initMode: "immediate",
33
- key: language,
34
- filePath: `file.${language || "txt"}`,
35
- code,
36
- onCodeUpdate: setCode,
37
- ref: codeMirrorRef
38
- }
39
- ))
77
+ /* @__PURE__ */ React__default.createElement("div", { ref: elRef })
40
78
  );
41
79
  };
42
80
  export {
81
+ COMMON_STATE_CONFIG_EXTENSIONS,
43
82
  CodeMirrorEditor
44
83
  };
@@ -24,19 +24,22 @@ const insertCodeMirror$ = Signal((r) => {
24
24
  insertCodeBlock$
25
25
  );
26
26
  });
27
- const codeMirrorTheme$ = Cell("auto");
27
+ const codeMirrorExtensions$ = Cell([]);
28
+ const codeMirrorAutoLoadLanguageSupport$ = Cell(true);
28
29
  const codeMirrorPlugin = realmPlugin({
29
30
  update(r, params) {
30
31
  r.pubIn({
31
32
  [codeBlockLanguages$]: params == null ? void 0 : params.codeBlockLanguages,
32
- [codeMirrorTheme$]: (params == null ? void 0 : params.theme) || "auto"
33
+ [codeMirrorExtensions$]: (params == null ? void 0 : params.codeMirrorExtensions) || [],
34
+ [codeMirrorAutoLoadLanguageSupport$]: (params == null ? void 0 : params.autoLoadLanguageSupport) ?? true
33
35
  });
34
36
  },
35
37
  init(r, params) {
36
38
  r.pubIn({
37
39
  [codeBlockLanguages$]: params == null ? void 0 : params.codeBlockLanguages,
38
- [codeMirrorTheme$]: (params == null ? void 0 : params.theme) || "auto",
39
- [appendCodeBlockEditorDescriptor$]: buildCodeBlockDescriptor((params == null ? void 0 : params.codeBlockLanguages) || {})
40
+ [codeMirrorExtensions$]: (params == null ? void 0 : params.codeMirrorExtensions) || [],
41
+ [appendCodeBlockEditorDescriptor$]: buildCodeBlockDescriptor((params == null ? void 0 : params.codeBlockLanguages) || {}),
42
+ [codeMirrorAutoLoadLanguageSupport$]: (params == null ? void 0 : params.autoLoadLanguageSupport) ?? true
40
43
  });
41
44
  }
42
45
  });
@@ -51,7 +54,8 @@ function buildCodeBlockDescriptor(codeBlockLanguages) {
51
54
  }
52
55
  export {
53
56
  codeBlockLanguages$,
57
+ codeMirrorAutoLoadLanguageSupport$,
58
+ codeMirrorExtensions$,
54
59
  codeMirrorPlugin,
55
- codeMirrorTheme$,
56
60
  insertCodeMirror$
57
61
  };
@@ -76,7 +76,7 @@ function useCodeMirrorRef(nodeKey, editorType, language, focusEmitter) {
76
76
  var _a, _b, _c, _d;
77
77
  (_b = (_a = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _a.contentDOM) == null ? void 0 : _b.addEventListener("focus", onFocusHandler);
78
78
  (_d = (_c = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _c.contentDOM) == null ? void 0 : _d.addEventListener("keydown", onKeyDownHandler);
79
- }, 100);
79
+ }, 300);
80
80
  return () => {
81
81
  var _a, _b;
82
82
  (_a = codeMirror == null ? void 0 : codeMirror.getCodemirror()) == null ? void 0 : _a.contentDOM.removeEventListener("focus", onFocusHandler);