@milkdown/preset-commonmark 6.3.1 → 6.3.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.
@@ -69,7 +69,11 @@ export const codeFence = createNode<Keys, { languageList?: string[] }>((utils, o
69
69
  if (!(dom instanceof HTMLElement)) {
70
70
  throw expectDomTypeError(dom);
71
71
  }
72
- const textNode = schema.text(dom.querySelector('pre')?.textContent ?? '');
72
+ const text = dom.querySelector('pre')?.textContent ?? '';
73
+ if (!text) {
74
+ return Fragment.empty;
75
+ }
76
+ const textNode = schema.text(text);
73
77
  return Fragment.from(textNode);
74
78
  },
75
79
  },
@@ -33,10 +33,10 @@ const createKeepListOrderPlugin = (type: NodeType) => {
33
33
  changed = true;
34
34
  }
35
35
 
36
- const prev = parent?.maybeChild(index - 1);
37
- if (prev && prev.type === type && prev.attrs['listType'] === 'ordered') {
38
- const label = prev.attrs['label'];
39
- attrs['label'] = `${Number(label.slice(0, -1)) + 1}.`;
36
+ const base = parent?.maybeChild(0);
37
+ if (base && base.type === type && base.attrs['listType'] === 'ordered') {
38
+ const label = base.attrs['label'];
39
+ attrs['label'] = `${Number(label.slice(0, -1)) + index}.`;
40
40
  changed = true;
41
41
  }
42
42
 
@@ -4,11 +4,11 @@ import links from 'remark-inline-links';
4
4
 
5
5
  import { addOrderInList } from './add-order-in-list';
6
6
  import { filterHTMLPlugin } from './filter-html';
7
- import { inlineNodesCursorPlugin } from './inline-nodes-cursor';
7
+ import { getInlineNodesCursorPlugin } from './inline-nodes-cursor';
8
8
 
9
9
  export const commonmarkPlugins = [
10
10
  createPlugin(() => ({
11
- prosePlugins: () => [inlineNodesCursorPlugin],
11
+ prosePlugins: () => [getInlineNodesCursorPlugin()],
12
12
  remarkPlugins: () => [links, filterHTMLPlugin, addOrderInList],
13
13
  }))(),
14
14
  ];
@@ -8,59 +8,87 @@ const inlineNodesCursorPluginKey = new PluginKey('MILKDOWN_INLINE_NODES_CURSOR')
8
8
  * This plugin is to solve the chrome 98 bug:
9
9
  * https://discuss.prosemirror.net/t/cursor-jumps-at-the-end-of-line-when-it-betweens-two-inline-nodes/4641
10
10
  */
11
- export const inlineNodesCursorPlugin: Plugin = new Plugin({
12
- key: inlineNodesCursorPluginKey,
13
- state: {
14
- init() {
15
- return false;
16
- },
17
- apply(tr) {
18
- if (!tr.selection.empty) {
11
+ export const getInlineNodesCursorPlugin = (): Plugin => {
12
+ let lock = false;
13
+ const inlineNodesCursorPlugin: Plugin = new Plugin({
14
+ key: inlineNodesCursorPluginKey,
15
+ state: {
16
+ init() {
19
17
  return false;
20
- }
21
- const pos = tr.selection.$from;
22
- const left = pos.nodeBefore;
23
- const right = pos.nodeAfter;
24
- if (left && right && left.isInline && !left.isText && right.isInline && !right.isText) {
25
- return true;
26
- }
27
-
28
- return false;
29
- },
30
- },
31
- props: {
32
- handleDOMEvents: {
33
- beforeinput: (view, e) => {
34
- const active = inlineNodesCursorPlugin.getState(view.state);
35
- if (active && e instanceof InputEvent) {
36
- const from = view.state.selection.from;
37
- e.preventDefault();
38
- view.dispatch(view.state.tr.insertText(e.data || '', from));
39
-
18
+ },
19
+ apply(tr) {
20
+ if (!tr.selection.empty) {
21
+ return false;
22
+ }
23
+ const pos = tr.selection.$from;
24
+ const left = pos.nodeBefore;
25
+ const right = pos.nodeAfter;
26
+ if (left && right && left.isInline && !left.isText && right.isInline && !right.isText) {
40
27
  return true;
41
28
  }
42
29
 
43
30
  return false;
44
31
  },
45
32
  },
46
- decorations(state) {
47
- const active = inlineNodesCursorPlugin.getState(state);
48
- if (active) {
49
- const pos = state.selection.$from;
50
- const position = pos.pos;
51
- const left = document.createElement('span');
52
- const leftDec = Decoration.widget(position, left, {
53
- side: -1,
54
- });
55
- const right = document.createElement('span');
56
- const rightDec = Decoration.widget(position, right);
57
- setTimeout(() => {
58
- left.contentEditable = 'true';
59
- right.contentEditable = 'true';
60
- });
61
- return DecorationSet.create(state.doc, [leftDec, rightDec]);
62
- }
63
- return DecorationSet.empty;
33
+ props: {
34
+ handleDOMEvents: {
35
+ compositionend: (view, e) => {
36
+ if (lock) {
37
+ lock = false;
38
+ requestAnimationFrame(() => {
39
+ const active = inlineNodesCursorPlugin.getState(view.state);
40
+ if (active) {
41
+ const from = view.state.selection.from;
42
+ e.preventDefault();
43
+ view.dispatch(view.state.tr.insertText(e.data || '', from));
44
+ }
45
+ });
46
+
47
+ return true;
48
+ }
49
+ return false;
50
+ },
51
+ compositionstart: (view) => {
52
+ const active = inlineNodesCursorPlugin.getState(view.state);
53
+ if (active) {
54
+ lock = true;
55
+ }
56
+ return false;
57
+ },
58
+ beforeinput: (view, e) => {
59
+ const active = inlineNodesCursorPlugin.getState(view.state);
60
+ if (active && e instanceof InputEvent && e.data && !lock) {
61
+ const from = view.state.selection.from;
62
+ e.preventDefault();
63
+ view.dispatch(view.state.tr.insertText(e.data || '', from));
64
+
65
+ return true;
66
+ }
67
+
68
+ return false;
69
+ },
70
+ },
71
+ decorations(state) {
72
+ const active = inlineNodesCursorPlugin.getState(state);
73
+ if (active) {
74
+ const pos = state.selection.$from;
75
+ const position = pos.pos;
76
+ const left = document.createElement('span');
77
+ const leftDec = Decoration.widget(position, left, {
78
+ side: -1,
79
+ });
80
+ const right = document.createElement('span');
81
+ const rightDec = Decoration.widget(position, right);
82
+ setTimeout(() => {
83
+ left.contentEditable = 'true';
84
+ right.contentEditable = 'true';
85
+ });
86
+ return DecorationSet.create(state.doc, [leftDec, rightDec]);
87
+ }
88
+ return DecorationSet.empty;
89
+ },
64
90
  },
65
- },
66
- });
91
+ });
92
+
93
+ return inlineNodesCursorPlugin;
94
+ };