@milkdown/preset-commonmark 7.0.0 → 7.0.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkdown/preset-commonmark",
3
3
  "type": "module",
4
- "version": "7.0.0",
4
+ "version": "7.0.1",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -30,14 +30,14 @@
30
30
  "remark-inline-links": "^6.0.0",
31
31
  "tslib": "^2.4.0",
32
32
  "unist-util-visit": "^4.0.0",
33
- "@milkdown/exception": "7.0.0",
34
- "@milkdown/utils": "7.0.0"
33
+ "@milkdown/exception": "7.0.1",
34
+ "@milkdown/utils": "7.0.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/unist": "^2.0.6",
38
- "@milkdown/core": "7.0.0",
39
- "@milkdown/ctx": "7.0.0",
40
- "@milkdown/prose": "7.0.0"
38
+ "@milkdown/core": "7.0.1",
39
+ "@milkdown/ctx": "7.0.1",
40
+ "@milkdown/prose": "7.0.1"
41
41
  },
42
42
  "nx": {
43
43
  "targets": {
@@ -1,57 +1,84 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import type { EditorState, Transaction } from '@milkdown/prose/state'
3
2
  import { Plugin, PluginKey } from '@milkdown/prose/state'
4
3
  import { $prose } from '@milkdown/utils'
4
+ import type { EditorView } from '@milkdown/prose/view'
5
5
  import { listItemSchema } from '../node/list-item'
6
6
 
7
7
  import { orderedListSchema } from '../node/ordered-list'
8
+ import { bulletListSchema } from '../node'
8
9
 
9
10
  /// This plugin is used to keep the label of list item up to date in ordered list.
10
11
  export const syncListOrderPlugin = $prose(() => {
11
- const walkThrough = (state: EditorState, callback: (tr: Transaction) => void) => {
12
+ const syncOrderLabel = (view: EditorView) => {
13
+ if (view.composing || !view.editable)
14
+ return
15
+
12
16
  const orderedListType = orderedListSchema.type()
17
+ const bulletListType = bulletListSchema.type()
13
18
  const listItemType = listItemSchema.type()
19
+ const state = view.state
20
+ const handleNodeItem = (attrs: Record<string, any>, index: number): boolean => {
21
+ let changed = false
22
+ const expectedLabel = `${index + 1}.`
23
+ if (attrs.label !== expectedLabel) {
24
+ attrs.label = expectedLabel
25
+ changed = true
26
+ }
27
+
28
+ return changed
29
+ }
30
+
14
31
  let tr = state.tr
32
+ let needDispatch = false
15
33
  state.doc.descendants((node, pos, parent, index) => {
16
- if (node.type === listItemType && parent?.type === orderedListType) {
17
- let changed = false
34
+ if (node.type === bulletListType) {
35
+ const base = node.maybeChild(0)
36
+ if (base?.type === listItemType && base.attrs.listType === 'ordered') {
37
+ needDispatch = true
38
+ tr.setNodeMarkup(pos, orderedListType, { spread: 'true' })
39
+
40
+ node.descendants((child, pos, _parent, index) => {
41
+ if (child.type === listItemType) {
42
+ const attrs = { ...child.attrs }
43
+ const changed = handleNodeItem(attrs, index)
44
+ if (changed)
45
+ tr = tr.setNodeMarkup(pos, undefined, attrs)
46
+ }
47
+ return false
48
+ })
49
+ }
50
+ }
51
+ else if (node.type === listItemType && parent?.type === orderedListType) {
18
52
  const attrs = { ...node.attrs }
19
- if (node.attrs.listType !== 'ordered') {
53
+ let changed = false
54
+ if (attrs.listType !== 'ordered') {
20
55
  attrs.listType = 'ordered'
21
56
  changed = true
22
57
  }
23
58
 
24
59
  const base = parent?.maybeChild(0)
25
- if (base === node) {
26
- attrs.label = '1.'
27
- changed = true
28
- }
29
- else if (base?.type === listItemType && base.attrs.listType === 'ordered') {
30
- attrs.label = `${index + 1}.`
31
- changed = true
32
- }
33
- else if (node.attrs.label === '•') {
34
- attrs.label = `${index + 1}.`
35
- changed = true
36
- }
60
+ if (base)
61
+ changed = handleNodeItem(attrs, index)
37
62
 
38
- if (changed)
63
+ if (changed) {
39
64
  tr = tr.setNodeMarkup(pos, undefined, attrs)
65
+ needDispatch = true
66
+ }
40
67
  }
41
68
  })
42
- callback(tr)
69
+
70
+ if (needDispatch)
71
+ view.dispatch(tr.setMeta('addToHistory', false))
43
72
  }
44
73
  return new Plugin({
45
74
  key: new PluginKey('MILKDOWN_KEEP_LIST_ORDER'),
46
- appendTransaction: (transactions, _oldState, nextState) => {
47
- let tr: Transaction | null = null
48
- if (transactions.some(transaction => transaction.docChanged)) {
49
- walkThrough(nextState, (t) => {
50
- tr = t
51
- })
75
+ view: (view) => {
76
+ syncOrderLabel(view)
77
+ return {
78
+ update: (view) => {
79
+ syncOrderLabel(view)
80
+ },
52
81
  }
53
-
54
- return tr
55
82
  },
56
83
  })
57
84
  })