@mdxeditor/editor 2.3.5 → 2.3.7

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.
@@ -0,0 +1,137 @@
1
+ import { factorySpace } from "micromark-factory-space";
2
+ import { markdownLineEnding } from "./node_modules/micromark-util-character/index.js";
3
+ import { codes, types } from "micromark-util-symbol";
4
+ function commentFromMarkdown(_options) {
5
+ return {
6
+ canContainEols: ["comment"],
7
+ enter: {
8
+ comment(_) {
9
+ this.buffer();
10
+ }
11
+ },
12
+ exit: {
13
+ comment(token) {
14
+ const text = this.resume();
15
+ if (_options == null ? void 0 : _options.ast) {
16
+ this.enter(
17
+ {
18
+ // @ts-expect-error: hush.
19
+ type: "comment",
20
+ value: "",
21
+ commentValue: text.slice(0, -2)
22
+ },
23
+ token
24
+ );
25
+ this.exit(token);
26
+ }
27
+ }
28
+ }
29
+ };
30
+ }
31
+ const tokenize = (effects, ok, nok) => {
32
+ return start;
33
+ function start(code) {
34
+ effects.enter("comment");
35
+ effects.consume(code);
36
+ return open;
37
+ }
38
+ function open(code) {
39
+ if (code === codes.exclamationMark) {
40
+ effects.consume(code);
41
+ return declarationOpen;
42
+ }
43
+ return nok(code);
44
+ }
45
+ function declarationOpen(code) {
46
+ if (code === codes.dash) {
47
+ effects.consume(code);
48
+ return commentOpen;
49
+ }
50
+ return nok(code);
51
+ }
52
+ function commentOpen(code) {
53
+ if (code === codes.dash) {
54
+ effects.consume(code);
55
+ return commentStart;
56
+ }
57
+ return nok(code);
58
+ }
59
+ function commentStart(code) {
60
+ if (code === codes.greaterThan) {
61
+ return nok(code);
62
+ }
63
+ if (markdownLineEnding(code)) {
64
+ return atLineEnding(code);
65
+ }
66
+ effects.enter(types.data);
67
+ if (code === codes.dash) {
68
+ effects.consume(code);
69
+ return commentStartDash;
70
+ }
71
+ return comment2(code);
72
+ }
73
+ function commentStartDash(code) {
74
+ if (code === codes.greaterThan) {
75
+ return nok(code);
76
+ }
77
+ return comment2(code);
78
+ }
79
+ function comment2(code) {
80
+ if (code === codes.eof) {
81
+ return nok(code);
82
+ }
83
+ if (code === codes.dash) {
84
+ effects.consume(code);
85
+ return commentClose;
86
+ }
87
+ if (markdownLineEnding(code)) {
88
+ effects.exit(types.data);
89
+ return atLineEnding(code);
90
+ }
91
+ effects.consume(code);
92
+ return comment2;
93
+ }
94
+ function atLineEnding(code) {
95
+ effects.enter(types.lineEnding);
96
+ effects.consume(code);
97
+ effects.exit(types.lineEnding);
98
+ return factorySpace(effects, afterPrefix, types.linePrefix);
99
+ }
100
+ function afterPrefix(code) {
101
+ if (markdownLineEnding(code)) {
102
+ return atLineEnding(code);
103
+ }
104
+ effects.enter(types.data);
105
+ return comment2(code);
106
+ }
107
+ function commentClose(code) {
108
+ if (code === codes.dash) {
109
+ effects.consume(code);
110
+ return end;
111
+ }
112
+ return comment2(code);
113
+ }
114
+ function end(code) {
115
+ if (code === codes.greaterThan) {
116
+ effects.exit(types.data);
117
+ effects.enter("commentEnd");
118
+ effects.consume(code);
119
+ effects.exit("commentEnd");
120
+ effects.exit("comment");
121
+ return ok(code);
122
+ }
123
+ if (code === codes.dash) {
124
+ effects.consume(code);
125
+ return end;
126
+ }
127
+ return comment2(code);
128
+ }
129
+ };
130
+ const comment = {
131
+ flow: { [60]: { tokenize, concrete: true } },
132
+ text: { [60]: { tokenize } }
133
+ };
134
+ export {
135
+ comment,
136
+ commentFromMarkdown
137
+ };
@@ -0,0 +1,6 @@
1
+ function markdownLineEnding(code) {
2
+ return code !== null && code < -2;
3
+ }
4
+ export {
5
+ markdownLineEnding
6
+ };
@@ -27,6 +27,7 @@ import { MdastParagraphVisitor } from "./MdastParagraphVisitor.js";
27
27
  import { MdastRootVisitor } from "./MdastRootVisitor.js";
28
28
  import { MdastTextVisitor } from "./MdastTextVisitor.js";
29
29
  import { SharedHistoryPlugin } from "./SharedHistoryPlugin.js";
30
+ import { commentFromMarkdown, comment } from "../../mdastUtilHtmlComment.js";
30
31
  const NESTED_EDITOR_UPDATED_COMMAND = createCommand("NESTED_EDITOR_UPDATED_COMMAND");
31
32
  const rootEditor$ = Cell(null);
32
33
  const activeEditor$ = Cell(null);
@@ -488,8 +489,8 @@ const corePlugin = realmPlugin({
488
489
  });
489
490
  if (!(params == null ? void 0 : params.suppressHtmlProcessing)) {
490
491
  r.pubIn({
491
- [addMdastExtension$]: mdxJsxFromMarkdown(),
492
- [addSyntaxExtension$]: [mdxJsx(), mdxMd()],
492
+ [addMdastExtension$]: [mdxJsxFromMarkdown(), commentFromMarkdown({ ast: false })],
493
+ [addSyntaxExtension$]: [mdxJsx(), mdxMd(), comment],
493
494
  [addToMarkdownExtension$]: mdxJsxToMarkdown(),
494
495
  [addImportVisitor$]: MdastHTMLVisitor
495
496
  });
@@ -5,61 +5,73 @@ import { MergeView } from "@codemirror/merge";
5
5
  import { EditorState } from "@codemirror/state";
6
6
  import { EditorView } from "@codemirror/view";
7
7
  import { COMMON_STATE_CONFIG_EXTENSIONS } from "./SourceEditor.js";
8
- import { useCellValues, usePublisher, useCellValue } from "@mdxeditor/gurx";
8
+ import { useRealm, useCellValues, usePublisher, useCellValue } from "@mdxeditor/gurx";
9
+ function setContent(view, content) {
10
+ if (view !== void 0) {
11
+ view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: content } });
12
+ }
13
+ }
9
14
  const DiffViewer = () => {
10
- const [newText, oldText] = useCellValues(markdown$, diffMarkdown$);
11
- const updateMarkdown = usePublisher(markdownSourceEditorValue$);
12
- return /* @__PURE__ */ React__default.createElement(CmMergeView, { oldMarkdown: oldText, newMarkdown: newText, onUpdate: updateMarkdown });
13
- };
14
- const CmMergeView = ({ oldMarkdown, newMarkdown, onUpdate }) => {
15
+ const realm = useRealm();
16
+ const [newMarkdown, oldMarkdown] = useCellValues(markdown$, diffMarkdown$);
17
+ const onUpdate = usePublisher(markdownSourceEditorValue$);
18
+ const elRef = React__default.useRef(null);
15
19
  const cmMergeViewRef = React__default.useRef(null);
16
20
  const cmExtensions = useCellValue(cmExtensions$);
17
21
  const triggerOnBlur = usePublisher(onBlur$);
18
- const ref = React__default.useCallback(
19
- (el) => {
22
+ React__default.useEffect(() => {
23
+ return realm.sub(diffMarkdown$, (newDiffMarkdown) => {
24
+ var _a;
25
+ setContent((_a = cmMergeViewRef.current) == null ? void 0 : _a.a, newDiffMarkdown);
26
+ });
27
+ }, [realm]);
28
+ React__default.useEffect(() => {
29
+ return realm.sub(markdown$, (newMarkdown2) => {
20
30
  var _a;
21
- if (el !== null) {
22
- cmMergeViewRef.current = new MergeView({
23
- renderRevertControl: () => {
24
- const el2 = document.createElement("button");
25
- el2.classList.add("cm-merge-revert");
26
- el2.appendChild(document.createTextNode("⮕"));
27
- return el2;
28
- },
29
- parent: el,
30
- orientation: "a-b",
31
- revertControls: "a-to-b",
32
- gutter: true,
33
- a: {
34
- doc: oldMarkdown,
35
- extensions: [...cmExtensions, ...COMMON_STATE_CONFIG_EXTENSIONS, EditorState.readOnly.of(true)]
36
- },
37
- b: {
38
- doc: newMarkdown,
39
- extensions: [
40
- ...cmExtensions,
41
- ...COMMON_STATE_CONFIG_EXTENSIONS,
42
- EditorView.updateListener.of(({ state }) => {
43
- const md = state.doc.toString();
44
- onUpdate(md);
45
- }),
46
- EditorView.focusChangeEffect.of((_, focused) => {
47
- if (!focused) {
48
- triggerOnBlur(new FocusEvent("blur"));
49
- }
50
- return null;
51
- })
52
- ]
53
- }
54
- });
55
- } else {
56
- (_a = cmMergeViewRef.current) == null ? void 0 : _a.destroy();
57
- cmMergeViewRef.current = null;
31
+ setContent((_a = cmMergeViewRef.current) == null ? void 0 : _a.b, newMarkdown2);
32
+ });
33
+ }, [realm]);
34
+ React__default.useEffect(() => {
35
+ cmMergeViewRef.current = new MergeView({
36
+ renderRevertControl: () => {
37
+ const el = document.createElement("button");
38
+ el.classList.add("cm-merge-revert");
39
+ el.appendChild(document.createTextNode("⮕"));
40
+ return el;
41
+ },
42
+ parent: elRef.current,
43
+ orientation: "a-b",
44
+ revertControls: "a-to-b",
45
+ gutter: true,
46
+ a: {
47
+ doc: oldMarkdown,
48
+ extensions: [...cmExtensions, ...COMMON_STATE_CONFIG_EXTENSIONS, EditorState.readOnly.of(true)]
49
+ },
50
+ b: {
51
+ doc: newMarkdown,
52
+ extensions: [
53
+ ...cmExtensions,
54
+ ...COMMON_STATE_CONFIG_EXTENSIONS,
55
+ EditorView.updateListener.of(({ state }) => {
56
+ const md = state.doc.toString();
57
+ onUpdate(md);
58
+ }),
59
+ EditorView.focusChangeEffect.of((_, focused) => {
60
+ if (!focused) {
61
+ triggerOnBlur(new FocusEvent("blur"));
62
+ }
63
+ return null;
64
+ })
65
+ ]
58
66
  }
59
- },
60
- [newMarkdown, oldMarkdown, onUpdate, cmExtensions, triggerOnBlur]
61
- );
62
- return /* @__PURE__ */ React__default.createElement("div", { ref });
67
+ });
68
+ return () => {
69
+ var _a;
70
+ (_a = cmMergeViewRef.current) == null ? void 0 : _a.destroy();
71
+ cmMergeViewRef.current = null;
72
+ };
73
+ }, [onUpdate, cmExtensions]);
74
+ return /* @__PURE__ */ React__default.createElement("div", { ref: elRef });
63
75
  };
64
76
  export {
65
77
  DiffViewer
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "*.css"
12
12
  ],
13
13
  "type": "module",
14
- "version": "2.3.5",
14
+ "version": "2.3.7",
15
15
  "description": "React component for rich text markdown editing",
16
16
  "scripts": {
17
17
  "build": "vite build",
@@ -80,6 +80,8 @@
80
80
  "micromark-extension-mdx-jsx": "^3.0.0",
81
81
  "micromark-extension-mdx-md": "^2.0.0",
82
82
  "micromark-extension-mdxjs": "^3.0.0",
83
+ "micromark-factory-space": "^2.0.0",
84
+ "micromark-util-symbol": "^2.0.0",
83
85
  "react-hook-form": "^7.44.2",
84
86
  "unidiff": "^1.0.2"
85
87
  },