@mdxeditor/editor 2.20.5 → 3.0.0-alpha.1
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 +23 -12
- package/dist/index.js +5 -3
- package/dist/plugins/codeblock/CodeBlockNode.js +23 -26
- package/dist/plugins/codemirror/CodeMirrorEditor.js +59 -24
- package/dist/plugins/codemirror/index.js +9 -5
- package/dist/plugins/core/index.js +10 -1
- package/dist/plugins/sandpack/useCodeMirrorRef.js +1 -1
- package/dist/style.css +202 -198
- package/dist/styles/ui.module.css.js +106 -106
- package/package.json +2 -1
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
|
-
*
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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,79 @@
|
|
|
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 {
|
|
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$ } from "./index.js";
|
|
13
|
+
const COMMON_STATE_CONFIG_EXTENSIONS = [];
|
|
9
14
|
const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }) => {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
15
|
+
const [readOnly, codeMirrorExtensions] = useCellValues(readOnly$, codeMirrorExtensions$);
|
|
16
|
+
const codeMirrorRef = useCodeMirrorRef(nodeKey, "codeblock", language, focusEmitter);
|
|
12
17
|
const { setCode } = useCodeBlockEditorContext();
|
|
18
|
+
const editorViewRef = React__default.useRef(null);
|
|
19
|
+
const elRef = React__default.useRef(null);
|
|
20
|
+
const setCodeRef = React__default.useRef(setCode);
|
|
21
|
+
setCodeRef.current = setCode;
|
|
22
|
+
codeMirrorRef.current = {
|
|
23
|
+
getCodemirror: () => editorViewRef.current
|
|
24
|
+
};
|
|
13
25
|
React__default.useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
void (async () => {
|
|
27
|
+
const extensions = [
|
|
28
|
+
...codeMirrorExtensions,
|
|
29
|
+
basicSetup,
|
|
30
|
+
basicLight,
|
|
31
|
+
lineNumbers(),
|
|
32
|
+
EditorView.lineWrapping,
|
|
33
|
+
EditorView.updateListener.of(({ state }) => {
|
|
34
|
+
setCodeRef.current(state.doc.toString());
|
|
35
|
+
})
|
|
36
|
+
];
|
|
37
|
+
if (readOnly) {
|
|
38
|
+
extensions.push(EditorState.readOnly.of(true));
|
|
39
|
+
}
|
|
40
|
+
if (language !== "") {
|
|
41
|
+
const languageData = languages.find((l) => {
|
|
42
|
+
return l.name === language || l.alias.includes(language) || l.extensions.includes(language);
|
|
43
|
+
});
|
|
44
|
+
if (languageData) {
|
|
45
|
+
try {
|
|
46
|
+
const languageSupport = await languageData.load();
|
|
47
|
+
extensions.push(languageSupport.extension);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
console.warn("failed to load language support for", language);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
elRef.current.innerHTML = "";
|
|
54
|
+
editorViewRef.current = new EditorView({
|
|
55
|
+
parent: elRef.current,
|
|
56
|
+
state: EditorState.create({ doc: code, extensions })
|
|
57
|
+
});
|
|
58
|
+
})();
|
|
59
|
+
return () => {
|
|
60
|
+
var _a;
|
|
61
|
+
(_a = editorViewRef.current) == null ? void 0 : _a.destroy();
|
|
62
|
+
editorViewRef.current = null;
|
|
63
|
+
};
|
|
64
|
+
}, [readOnly, language]);
|
|
19
65
|
return /* @__PURE__ */ React__default.createElement(
|
|
20
66
|
"div",
|
|
21
67
|
{
|
|
22
|
-
className: styles.
|
|
68
|
+
className: styles.codeMirrorWrapper,
|
|
23
69
|
onKeyDown: (e) => {
|
|
24
70
|
e.stopPropagation();
|
|
25
71
|
}
|
|
26
72
|
},
|
|
27
|
-
/* @__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
|
-
))
|
|
73
|
+
/* @__PURE__ */ React__default.createElement("div", { ref: elRef })
|
|
40
74
|
);
|
|
41
75
|
};
|
|
42
76
|
export {
|
|
77
|
+
COMMON_STATE_CONFIG_EXTENSIONS,
|
|
43
78
|
CodeMirrorEditor
|
|
44
79
|
};
|
|
@@ -24,19 +24,22 @@ const insertCodeMirror$ = Signal((r) => {
|
|
|
24
24
|
insertCodeBlock$
|
|
25
25
|
);
|
|
26
26
|
});
|
|
27
|
-
const
|
|
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
|
-
[
|
|
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
|
-
[
|
|
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
|
};
|
|
@@ -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,
|
|
7
|
+
import { createCommand, FORMAT_TEXT_COMMAND, $isRootOrShadowRoot, $getRoot, $setSelection, $getSelection, $insertNodes, SELECTION_CHANGE_COMMAND, COMMAND_PRIORITY_CRITICAL, DecoratorNode, $createParagraphNode, FOCUS_COMMAND, KEY_DOWN_COMMAND, $isDecoratorNode, BLUR_COMMAND, $isRangeSelection, 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";
|
|
@@ -251,6 +251,15 @@ const createRootEditorSubscription$ = Appender(rootEditorSubscriptions$, (r, sig
|
|
|
251
251
|
}
|
|
252
252
|
let theNewMarkdownValue;
|
|
253
253
|
editorState.read(() => {
|
|
254
|
+
const lastChild = $getRoot().getLastChild();
|
|
255
|
+
if (lastChild instanceof DecoratorNode) {
|
|
256
|
+
rootEditor.update(
|
|
257
|
+
() => {
|
|
258
|
+
$getRoot().append($createParagraphNode());
|
|
259
|
+
},
|
|
260
|
+
{ discrete: true }
|
|
261
|
+
);
|
|
262
|
+
}
|
|
254
263
|
theNewMarkdownValue = exportMarkdownFromLexical({
|
|
255
264
|
root: $getRoot(),
|
|
256
265
|
visitors: r.getValue(exportVisitors$),
|
|
@@ -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
|
-
},
|
|
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);
|