@milkdown/preset-gfm 6.5.3 → 7.0.0-next.0

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.
Files changed (156) hide show
  1. package/lib/composed/commands.d.ts +8 -0
  2. package/lib/composed/commands.d.ts.map +1 -0
  3. package/lib/composed/index.d.ts +6 -0
  4. package/lib/composed/index.d.ts.map +1 -0
  5. package/lib/composed/inputrules.d.ts +3 -0
  6. package/lib/composed/inputrules.d.ts.map +1 -0
  7. package/lib/composed/keymap.d.ts +3 -0
  8. package/lib/composed/keymap.d.ts.map +1 -0
  9. package/lib/composed/plugins.d.ts +3 -0
  10. package/lib/composed/plugins.d.ts.map +1 -0
  11. package/lib/composed/schema.d.ts +3 -0
  12. package/lib/composed/schema.d.ts.map +1 -0
  13. package/lib/index.d.ts +4 -34
  14. package/lib/index.d.ts.map +1 -1
  15. package/lib/index.es.js +633 -2328
  16. package/lib/index.es.js.map +1 -1
  17. package/lib/mark/index.d.ts +2 -0
  18. package/lib/mark/index.d.ts.map +1 -0
  19. package/lib/mark/strike-through.d.ts +4 -0
  20. package/lib/mark/strike-through.d.ts.map +1 -0
  21. package/lib/node/footnote/definition.d.ts +2 -0
  22. package/lib/node/footnote/definition.d.ts.map +1 -0
  23. package/lib/{footnote → node/footnote}/index.d.ts +0 -0
  24. package/lib/node/footnote/index.d.ts.map +1 -0
  25. package/lib/node/footnote/reference.d.ts +2 -0
  26. package/lib/node/footnote/reference.d.ts.map +1 -0
  27. package/lib/node/index.d.ts +4 -0
  28. package/lib/node/index.d.ts.map +1 -0
  29. package/lib/node/table/index.d.ts +32 -0
  30. package/lib/node/table/index.d.ts.map +1 -0
  31. package/lib/node/table/utils.d.ts +27 -0
  32. package/lib/node/table/utils.d.ts.map +1 -0
  33. package/lib/node/task-list-item.d.ts +2 -0
  34. package/lib/node/task-list-item.d.ts.map +1 -0
  35. package/lib/plugin/auto-insert-zero-space-plugin.d.ts +2 -0
  36. package/lib/plugin/auto-insert-zero-space-plugin.d.ts.map +1 -0
  37. package/lib/plugin/column-resizing-plugin.d.ts +2 -0
  38. package/lib/plugin/column-resizing-plugin.d.ts.map +1 -0
  39. package/lib/plugin/index.d.ts +5 -0
  40. package/lib/plugin/index.d.ts.map +1 -0
  41. package/lib/plugin/remark-gfm-plugin.d.ts +2 -0
  42. package/lib/plugin/remark-gfm-plugin.d.ts.map +1 -0
  43. package/lib/plugin/table-editing-plugin.d.ts +2 -0
  44. package/lib/plugin/table-editing-plugin.d.ts.map +1 -0
  45. package/package.json +18 -10
  46. package/src/composed/commands.ts +23 -0
  47. package/src/composed/index.ts +6 -0
  48. package/src/composed/inputrules.ts +7 -0
  49. package/src/composed/keymap.ts +9 -0
  50. package/src/composed/plugins.ts +10 -0
  51. package/src/composed/schema.ts +18 -0
  52. package/src/index.ts +5 -89
  53. package/src/mark/index.ts +2 -0
  54. package/src/mark/strike-through.ts +39 -0
  55. package/src/node/footnote/definition.ts +68 -0
  56. package/src/{footnote → node/footnote}/index.ts +0 -0
  57. package/src/node/footnote/reference.ts +58 -0
  58. package/src/node/index.ts +4 -0
  59. package/src/node/table/index.ts +283 -0
  60. package/src/node/table/utils.ts +489 -0
  61. package/src/node/task-list-item.ts +87 -0
  62. package/src/{table/plugin/auto-insert-zero-space.ts → plugin/auto-insert-zero-space-plugin.ts} +13 -15
  63. package/src/plugin/column-resizing-plugin.ts +4 -0
  64. package/src/plugin/index.ts +5 -0
  65. package/src/plugin/remark-gfm-plugin.ts +5 -0
  66. package/src/plugin/table-editing-plugin.ts +4 -0
  67. package/lib/footnote/definition.d.ts +0 -3
  68. package/lib/footnote/definition.d.ts.map +0 -1
  69. package/lib/footnote/index.d.ts.map +0 -1
  70. package/lib/footnote/reference.d.ts +0 -3
  71. package/lib/footnote/reference.d.ts.map +0 -1
  72. package/lib/footnote/utils.d.ts +0 -3
  73. package/lib/footnote/utils.d.ts.map +0 -1
  74. package/lib/strike-through.d.ts +0 -3
  75. package/lib/strike-through.d.ts.map +0 -1
  76. package/lib/supported-keys.d.ts +0 -28
  77. package/lib/supported-keys.d.ts.map +0 -1
  78. package/lib/table/command.d.ts +0 -4
  79. package/lib/table/command.d.ts.map +0 -1
  80. package/lib/table/index.d.ts +0 -10
  81. package/lib/table/index.d.ts.map +0 -1
  82. package/lib/table/nodes/index.d.ts +0 -12
  83. package/lib/table/nodes/index.d.ts.map +0 -1
  84. package/lib/table/operator-plugin/actions.d.ts +0 -20
  85. package/lib/table/operator-plugin/actions.d.ts.map +0 -1
  86. package/lib/table/operator-plugin/calc-pos.d.ts +0 -3
  87. package/lib/table/operator-plugin/calc-pos.d.ts.map +0 -1
  88. package/lib/table/operator-plugin/constant.d.ts +0 -6
  89. package/lib/table/operator-plugin/constant.d.ts.map +0 -1
  90. package/lib/table/operator-plugin/helper.d.ts +0 -7
  91. package/lib/table/operator-plugin/helper.d.ts.map +0 -1
  92. package/lib/table/operator-plugin/index.d.ts +0 -6
  93. package/lib/table/operator-plugin/index.d.ts.map +0 -1
  94. package/lib/table/operator-plugin/style.d.ts +0 -3
  95. package/lib/table/operator-plugin/style.d.ts.map +0 -1
  96. package/lib/table/operator-plugin/widget.d.ts +0 -8
  97. package/lib/table/operator-plugin/widget.d.ts.map +0 -1
  98. package/lib/table/plugin/auto-insert-zero-space.d.ts +0 -3
  99. package/lib/table/plugin/auto-insert-zero-space.d.ts.map +0 -1
  100. package/lib/table/plugin/cell-selection.d.ts +0 -40
  101. package/lib/table/plugin/cell-selection.d.ts.map +0 -1
  102. package/lib/table/plugin/column-resizing.d.ts +0 -18
  103. package/lib/table/plugin/column-resizing.d.ts.map +0 -1
  104. package/lib/table/plugin/commands.d.ts +0 -30
  105. package/lib/table/plugin/commands.d.ts.map +0 -1
  106. package/lib/table/plugin/copy-paste.d.ts +0 -14
  107. package/lib/table/plugin/copy-paste.d.ts.map +0 -1
  108. package/lib/table/plugin/fix-tables.d.ts +0 -7
  109. package/lib/table/plugin/fix-tables.d.ts.map +0 -1
  110. package/lib/table/plugin/index.d.ts +0 -4
  111. package/lib/table/plugin/index.d.ts.map +0 -1
  112. package/lib/table/plugin/schema.d.ts +0 -4
  113. package/lib/table/plugin/schema.d.ts.map +0 -1
  114. package/lib/table/plugin/table-editing.d.ts +0 -9
  115. package/lib/table/plugin/table-editing.d.ts.map +0 -1
  116. package/lib/table/plugin/table-map.d.ts +0 -44
  117. package/lib/table/plugin/table-map.d.ts.map +0 -1
  118. package/lib/table/plugin/table-view.d.ts +0 -15
  119. package/lib/table/plugin/table-view.d.ts.map +0 -1
  120. package/lib/table/plugin/types.d.ts +0 -15
  121. package/lib/table/plugin/types.d.ts.map +0 -1
  122. package/lib/table/plugin/util.d.ts +0 -16
  123. package/lib/table/plugin/util.d.ts.map +0 -1
  124. package/lib/table/utils.d.ts +0 -21
  125. package/lib/table/utils.d.ts.map +0 -1
  126. package/lib/task-list-item.d.ts +0 -9
  127. package/lib/task-list-item.d.ts.map +0 -1
  128. package/src/footnote/definition.ts +0 -187
  129. package/src/footnote/reference.ts +0 -178
  130. package/src/footnote/utils.ts +0 -4
  131. package/src/strike-through.ts +0 -43
  132. package/src/supported-keys.ts +0 -13
  133. package/src/table/command.ts +0 -20
  134. package/src/table/index.ts +0 -13
  135. package/src/table/nodes/index.ts +0 -189
  136. package/src/table/operator-plugin/actions.ts +0 -116
  137. package/src/table/operator-plugin/calc-pos.ts +0 -36
  138. package/src/table/operator-plugin/constant.ts +0 -7
  139. package/src/table/operator-plugin/helper.ts +0 -39
  140. package/src/table/operator-plugin/index.ts +0 -110
  141. package/src/table/operator-plugin/style.ts +0 -123
  142. package/src/table/operator-plugin/widget.ts +0 -57
  143. package/src/table/plugin/cell-selection.ts +0 -381
  144. package/src/table/plugin/column-resizing.ts +0 -288
  145. package/src/table/plugin/commands.ts +0 -594
  146. package/src/table/plugin/copy-paste.ts +0 -322
  147. package/src/table/plugin/fix-tables.ts +0 -132
  148. package/src/table/plugin/index.ts +0 -4
  149. package/src/table/plugin/schema.ts +0 -120
  150. package/src/table/plugin/table-editing.ts +0 -369
  151. package/src/table/plugin/table-map.ts +0 -345
  152. package/src/table/plugin/table-view.ts +0 -80
  153. package/src/table/plugin/types.ts +0 -16
  154. package/src/table/plugin/util.ts +0 -119
  155. package/src/table/utils.ts +0 -165
  156. package/src/task-list-item.ts +0 -159
@@ -0,0 +1,283 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import type { MarkdownNode } from '@milkdown/transformer'
3
+ import { commandsCtx } from '@milkdown/core'
4
+ import { paragraphSchema } from '@milkdown/preset-commonmark'
5
+ import { InputRule } from '@milkdown/prose/inputrules'
6
+ import type { NodeType } from '@milkdown/prose/model'
7
+ import { Selection, TextSelection } from '@milkdown/prose/state'
8
+ import { CellSelection, addColumnAfter, addColumnBefore, deleteColumn, deleteRow, deleteTable, goToNextCell, isInTable, selectedRect, setCellAttr, tableNodes } from '@milkdown/prose/tables'
9
+ import { $command, $inputRule, $nodeSchema, $useKeymap } from '@milkdown/utils'
10
+ import { addRowWithAlignment, createTable, moveCol, moveRow, selectCol, selectRow, selectTable } from './utils'
11
+
12
+ const originalSchema = tableNodes({
13
+ tableGroup: 'block',
14
+ cellContent: 'paragraph',
15
+ cellAttributes: {
16
+ alignment: {
17
+ default: 'left',
18
+ getFromDOM: dom => (dom as HTMLElement).style.textAlign || 'left',
19
+ setDOMAttr: (value, attrs) => {
20
+ attrs.style = `text-align: ${value || 'left'}`
21
+ },
22
+ },
23
+ },
24
+ })
25
+
26
+ export const tableSchema = $nodeSchema('table', () => ({
27
+ ...originalSchema.table,
28
+ parseMarkdown: {
29
+ match: node => node.type === 'table',
30
+ runner: (state, node, type) => {
31
+ const align = node.align as (string | null)[]
32
+ const children = (node.children as MarkdownNode[]).map((x, i) => ({
33
+ ...x,
34
+ align,
35
+ isHeader: i === 0,
36
+ }))
37
+ state.openNode(type)
38
+ state.next(children)
39
+ state.closeNode()
40
+ },
41
+ },
42
+ toMarkdown: {
43
+ match: node => node.type.name === 'table',
44
+ runner: (state, node) => {
45
+ const firstLine = node.content.firstChild?.content
46
+ if (!firstLine)
47
+ return
48
+
49
+ const align: (string | null)[] = []
50
+ firstLine.forEach((cell) => {
51
+ align.push(cell.attrs.alignment)
52
+ })
53
+ state.openNode('table', undefined, { align })
54
+ state.next(node.content)
55
+ state.closeNode()
56
+ },
57
+ },
58
+
59
+ }))
60
+
61
+ export const tableRowSchema = $nodeSchema('table_row', () => ({
62
+ ...originalSchema.table_row,
63
+ parseMarkdown: {
64
+ match: node => node.type === 'tableRow',
65
+ runner: (state, node, type) => {
66
+ const align = node.align as (string | null)[]
67
+ const children = (node.children as MarkdownNode[]).map((x, i) => ({
68
+ ...x,
69
+ align: align[i],
70
+ isHeader: node.isHeader,
71
+ }))
72
+ state.openNode(type)
73
+ state.next(children)
74
+ state.closeNode()
75
+ },
76
+ },
77
+ toMarkdown: {
78
+ match: node => node.type.name === 'table_row',
79
+ runner: (state, node) => {
80
+ state.openNode('tableRow')
81
+ state.next(node.content)
82
+ state.closeNode()
83
+ },
84
+ },
85
+ }))
86
+
87
+ export const tableCellSchema = $nodeSchema('table_cell', () => ({
88
+ ...originalSchema.table_cell,
89
+ parseMarkdown: {
90
+ match: node => node.type === 'tableCell' && !node.isHeader,
91
+ runner: (state, node, type) => {
92
+ const align = node.align as string
93
+ state
94
+ .openNode(type, { alignment: align })
95
+ .openNode(state.schema.nodes.paragraph as NodeType)
96
+ .next(node.children)
97
+ .closeNode()
98
+ .closeNode()
99
+ },
100
+ },
101
+ toMarkdown: {
102
+ match: node => node.type.name === 'table_cell',
103
+ runner: (state, node) => {
104
+ state.openNode('tableCell').next(node.content).closeNode()
105
+ },
106
+ },
107
+ }))
108
+
109
+ export const tableHeaderSchema = $nodeSchema('table_header', () => ({
110
+ ...originalSchema.table_header,
111
+ parseMarkdown: {
112
+ match: node => node.type === 'tableCell' && !!node.isHeader,
113
+ runner: (state, node, type) => {
114
+ const align = node.align as string
115
+ state.openNode(type, { alignment: align })
116
+ state.openNode(state.schema.nodes.paragraph as NodeType)
117
+ state.next(node.children)
118
+ state.closeNode()
119
+ state.closeNode()
120
+ },
121
+ },
122
+ toMarkdown: {
123
+ match: node => node.type.name === 'table_header',
124
+ runner: (state, node) => {
125
+ state.openNode('tableCell')
126
+ state.next(node.content)
127
+ state.closeNode()
128
+ },
129
+ },
130
+ }))
131
+
132
+ export const insertTableInputRule = $inputRule(() => new InputRule(
133
+ /^\|(?<col>\d+)[xX](?<row>\d+)\|\s$/, (state, match, start, end) => {
134
+ const $start = state.doc.resolve(start)
135
+ if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), tableSchema.type()))
136
+ return null
137
+
138
+ const tableNode = createTable(
139
+ Number(match.groups?.row),
140
+ Number(match.groups?.col),
141
+ )
142
+ const tr = state.tr.replaceRangeWith(start, end, tableNode).scrollIntoView()
143
+ return tr.setSelection(TextSelection.create(tr.doc, start + 3))
144
+ },
145
+ ))
146
+
147
+ export const goToPrevTableCellCommand = $command('GoToPrevTableCell', () => () => goToNextCell(-1))
148
+
149
+ export const goToNextTableCellCommand = $command('GoToNextTableCell', () => () => goToNextCell(1))
150
+
151
+ export const breakTableCommand = $command('BreakTable', () => () => (state, dispatch) => {
152
+ if (!isInTable(state))
153
+ return false
154
+
155
+ const { $head } = state.selection
156
+ const pos = $head.after()
157
+ const tr = state.tr
158
+ .replaceWith(pos, pos, paragraphSchema.type().createAndFill()!)
159
+
160
+ tr.setSelection(Selection.near(tr.doc.resolve(pos), 1)).scrollIntoView()
161
+ dispatch?.(tr)
162
+ return true
163
+ })
164
+
165
+ export const insertTableCommand = $command('InsertTable', () => ({ row, col }: { row?: number; col?: number } = {}) => (state, dispatch) => {
166
+ const { selection, tr } = state
167
+ const { from } = selection
168
+ const table = createTable(row, col)
169
+ const _tr = tr.replaceSelectionWith(table)
170
+ const sel = Selection.findFrom(_tr.doc.resolve(from), 1, true)
171
+ if (sel)
172
+ dispatch?.(_tr.setSelection(sel))
173
+
174
+ return true
175
+ })
176
+
177
+ export const moveRowCommand = $command('MoveRow', () => ({ from, to }: { from?: number; to?: number } = {}) => (state, dispatch) => {
178
+ const { tr } = state
179
+ const result = dispatch?.(moveRow(tr, from ?? 0, to ?? 0, true))
180
+
181
+ return Boolean(result)
182
+ })
183
+
184
+ export const moveColCommand = $command('MoveCol', () => ({ from, to }: { from?: number; to?: number } = {}) => (state, dispatch) => {
185
+ const { tr } = state
186
+ const result = dispatch?.(moveCol(tr, from ?? 0, to ?? 0, true))
187
+
188
+ return Boolean(result)
189
+ })
190
+
191
+ export const selectRowCommand = $command<number, 'SelectRow'>('SelectRow', () => (index = 0) => (state, dispatch) => {
192
+ const { tr } = state
193
+ const result = dispatch?.(selectRow(index)(tr))
194
+
195
+ return Boolean(result)
196
+ })
197
+
198
+ export const selectColCommand = $command<number, 'SelectCol'>('SelectCol', () => (index = 0) => (state, dispatch) => {
199
+ const { tr } = state
200
+ const result = dispatch?.(selectCol(index)(tr))
201
+
202
+ return Boolean(result)
203
+ })
204
+
205
+ export const selectTableCommand = $command('SelectTable', () => () => (state, dispatch) => {
206
+ const { tr } = state
207
+ const result = dispatch?.(selectTable(tr))
208
+
209
+ return Boolean(result)
210
+ })
211
+
212
+ export const deleteSelectedCellsCommand = $command('DeleteSelectedCells', () => () => (state, dispatch) => {
213
+ const { selection } = state
214
+ if (!(selection instanceof CellSelection))
215
+ return false
216
+
217
+ const isRow = selection.isRowSelection()
218
+ const isCol = selection.isColSelection()
219
+
220
+ if (isRow && isCol)
221
+ return deleteTable(state, dispatch)
222
+
223
+ if (isCol)
224
+ return deleteColumn(state, dispatch)
225
+
226
+ else
227
+ return deleteRow(state, dispatch)
228
+ })
229
+
230
+ export const addColBeforeCommand = $command('AddColBefore', () => () => addColumnBefore)
231
+
232
+ export const addColAfterCommand = $command('AddColAfter', () => () => addColumnAfter)
233
+
234
+ export const addRowBeforeCommand = $command('AddRowBefore', () => () => (state, dispatch) => {
235
+ if (!isInTable(state))
236
+ return false
237
+ if (dispatch) {
238
+ const rect = selectedRect(state)
239
+ dispatch(addRowWithAlignment(state.tr, rect, rect.top))
240
+ }
241
+ return true
242
+ })
243
+
244
+ export const addRowAfterCommand = $command('AddRowAfter', () => () => (state, dispatch) => {
245
+ if (!isInTable(state))
246
+ return false
247
+ if (dispatch) {
248
+ const rect = selectedRect(state)
249
+ dispatch(addRowWithAlignment(state.tr, rect, rect.bottom))
250
+ }
251
+ return true
252
+ })
253
+
254
+ export const setAlignCommand = $command<'left' | 'center' | 'right', 'SetAlign'>('SetAlign', () => (alignment = 'left') => setCellAttr('alignment', alignment))
255
+
256
+ export const tableKeymap = $useKeymap('tableKeymap', {
257
+ NextCell: {
258
+ shortcuts: ['Mod-]', 'Tab'],
259
+ command: (ctx) => {
260
+ const commands = ctx.get(commandsCtx)
261
+
262
+ return () => commands.call(goToNextTableCellCommand.key)
263
+ },
264
+ },
265
+ PrevCell: {
266
+ shortcuts: ['Mod-[', 'Shift-Tab'],
267
+ command: (ctx) => {
268
+ const commands = ctx.get(commandsCtx)
269
+
270
+ return () => commands.call(goToPrevTableCellCommand.key)
271
+ },
272
+ },
273
+ ExitTable: {
274
+ shortcuts: ['Mod-Enter'],
275
+ command: (ctx) => {
276
+ const commands = ctx.get(commandsCtx)
277
+
278
+ return () => commands.call(breakTableCommand.key)
279
+ },
280
+ },
281
+ })
282
+
283
+ export * from './utils'