@milkdown/preset-gfm 6.5.4 → 7.0.0-next.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.
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 +634 -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 +5 -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 +26 -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 +13 -10
  46. package/src/composed/commands.ts +24 -0
  47. package/src/composed/index.ts +6 -0
  48. package/src/composed/inputrules.ts +8 -0
  49. package/src/composed/keymap.ts +10 -0
  50. package/src/composed/plugins.ts +11 -0
  51. package/src/composed/schema.ts +20 -0
  52. package/src/index.ts +6 -89
  53. package/src/mark/index.ts +2 -0
  54. package/src/mark/strike-through.ts +46 -0
  55. package/src/node/footnote/definition.ts +70 -0
  56. package/src/{footnote → node/footnote}/index.ts +0 -0
  57. package/src/node/footnote/reference.ts +60 -0
  58. package/src/node/index.ts +4 -0
  59. package/src/node/table/index.ts +318 -0
  60. package/src/node/table/utils.ts +509 -0
  61. package/src/node/task-list-item.ts +88 -0
  62. package/src/{table/plugin/auto-insert-zero-space.ts → plugin/auto-insert-zero-space-plugin.ts} +14 -15
  63. package/src/plugin/column-resizing-plugin.ts +6 -0
  64. package/src/plugin/index.ts +5 -0
  65. package/src/plugin/remark-gfm-plugin.ts +6 -0
  66. package/src/plugin/table-editing-plugin.ts +6 -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,318 @@
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
+ /// Schema for table node.
27
+ export const tableSchema = $nodeSchema('table', () => ({
28
+ ...originalSchema.table,
29
+ parseMarkdown: {
30
+ match: node => node.type === 'table',
31
+ runner: (state, node, type) => {
32
+ const align = node.align as (string | null)[]
33
+ const children = (node.children as MarkdownNode[]).map((x, i) => ({
34
+ ...x,
35
+ align,
36
+ isHeader: i === 0,
37
+ }))
38
+ state.openNode(type)
39
+ state.next(children)
40
+ state.closeNode()
41
+ },
42
+ },
43
+ toMarkdown: {
44
+ match: node => node.type.name === 'table',
45
+ runner: (state, node) => {
46
+ const firstLine = node.content.firstChild?.content
47
+ if (!firstLine)
48
+ return
49
+
50
+ const align: (string | null)[] = []
51
+ firstLine.forEach((cell) => {
52
+ align.push(cell.attrs.alignment)
53
+ })
54
+ state.openNode('table', undefined, { align })
55
+ state.next(node.content)
56
+ state.closeNode()
57
+ },
58
+ },
59
+
60
+ }))
61
+
62
+ /// Schema for table row node.
63
+ export const tableRowSchema = $nodeSchema('table_row', () => ({
64
+ ...originalSchema.table_row,
65
+ parseMarkdown: {
66
+ match: node => node.type === 'tableRow',
67
+ runner: (state, node, type) => {
68
+ const align = node.align as (string | null)[]
69
+ const children = (node.children as MarkdownNode[]).map((x, i) => ({
70
+ ...x,
71
+ align: align[i],
72
+ isHeader: node.isHeader,
73
+ }))
74
+ state.openNode(type)
75
+ state.next(children)
76
+ state.closeNode()
77
+ },
78
+ },
79
+ toMarkdown: {
80
+ match: node => node.type.name === 'table_row',
81
+ runner: (state, node) => {
82
+ state.openNode('tableRow')
83
+ state.next(node.content)
84
+ state.closeNode()
85
+ },
86
+ },
87
+ }))
88
+
89
+ /// Schema for table cell node.
90
+ export const tableCellSchema = $nodeSchema('table_cell', () => ({
91
+ ...originalSchema.table_cell,
92
+ parseMarkdown: {
93
+ match: node => node.type === 'tableCell' && !node.isHeader,
94
+ runner: (state, node, type) => {
95
+ const align = node.align as string
96
+ state
97
+ .openNode(type, { alignment: align })
98
+ .openNode(state.schema.nodes.paragraph as NodeType)
99
+ .next(node.children)
100
+ .closeNode()
101
+ .closeNode()
102
+ },
103
+ },
104
+ toMarkdown: {
105
+ match: node => node.type.name === 'table_cell',
106
+ runner: (state, node) => {
107
+ state.openNode('tableCell').next(node.content).closeNode()
108
+ },
109
+ },
110
+ }))
111
+
112
+ /// Schema for table header node.
113
+ export const tableHeaderSchema = $nodeSchema('table_header', () => ({
114
+ ...originalSchema.table_header,
115
+ parseMarkdown: {
116
+ match: node => node.type === 'tableCell' && !!node.isHeader,
117
+ runner: (state, node, type) => {
118
+ const align = node.align as string
119
+ state.openNode(type, { alignment: align })
120
+ state.openNode(state.schema.nodes.paragraph as NodeType)
121
+ state.next(node.children)
122
+ state.closeNode()
123
+ state.closeNode()
124
+ },
125
+ },
126
+ toMarkdown: {
127
+ match: node => node.type.name === 'table_header',
128
+ runner: (state, node) => {
129
+ state.openNode('tableCell')
130
+ state.next(node.content)
131
+ state.closeNode()
132
+ },
133
+ },
134
+ }))
135
+
136
+ /// A input rule for creating table.
137
+ /// For example, `|2x2|` will create a 2x2 table.
138
+ export const insertTableInputRule = $inputRule(() => new InputRule(
139
+ /^\|(?<col>\d+)[xX](?<row>\d+)\|\s$/, (state, match, start, end) => {
140
+ const $start = state.doc.resolve(start)
141
+ if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), tableSchema.type()))
142
+ return null
143
+
144
+ const tableNode = createTable(
145
+ Number(match.groups?.row),
146
+ Number(match.groups?.col),
147
+ )
148
+ const tr = state.tr.replaceRangeWith(start, end, tableNode).scrollIntoView()
149
+ return tr.setSelection(TextSelection.create(tr.doc, start + 3))
150
+ },
151
+ ))
152
+
153
+ /// A command for moving cursor to previous cell.
154
+ export const goToPrevTableCellCommand = $command('GoToPrevTableCell', () => () => goToNextCell(-1))
155
+
156
+ /// A command for moving cursor to next cell.
157
+ export const goToNextTableCellCommand = $command('GoToNextTableCell', () => () => goToNextCell(1))
158
+
159
+ /// A command for splitting current table into two tables.
160
+ /// If the selection is at the end of the table,
161
+ /// it will just quit the table and insert a new paragraph node.
162
+ export const breakTableCommand = $command('BreakTable', () => () => (state, dispatch) => {
163
+ if (!isInTable(state))
164
+ return false
165
+
166
+ const { $head } = state.selection
167
+ const pos = $head.after()
168
+ const tr = state.tr
169
+ .replaceWith(pos, pos, paragraphSchema.type().createAndFill()!)
170
+
171
+ tr.setSelection(Selection.near(tr.doc.resolve(pos), 1)).scrollIntoView()
172
+ dispatch?.(tr)
173
+ return true
174
+ })
175
+
176
+ /// A command for inserting a table.
177
+ /// You can specify the number of rows and columns.
178
+ /// By default, it will insert a 3x3 table.
179
+ export const insertTableCommand = $command('InsertTable', () => ({ row, col }: { row?: number; col?: number } = {}) => (state, dispatch) => {
180
+ const { selection, tr } = state
181
+ const { from } = selection
182
+ const table = createTable(row, col)
183
+ const _tr = tr.replaceSelectionWith(table)
184
+ const sel = Selection.findFrom(_tr.doc.resolve(from), 1, true)
185
+ if (sel)
186
+ dispatch?.(_tr.setSelection(sel))
187
+
188
+ return true
189
+ })
190
+
191
+ /// A command for moving a row in a table.
192
+ /// You should specify the `from` and `to` index.
193
+ export const moveRowCommand = $command('MoveRow', () => ({ from, to }: { from?: number; to?: number } = {}) => (state, dispatch) => {
194
+ const { tr } = state
195
+ const result = dispatch?.(moveRow(tr, from ?? 0, to ?? 0, true))
196
+
197
+ return Boolean(result)
198
+ })
199
+
200
+ /// A command for moving a column in a table.
201
+ /// You should specify the `from` and `to` index.
202
+ export const moveColCommand = $command('MoveCol', () => ({ from, to }: { from?: number; to?: number } = {}) => (state, dispatch) => {
203
+ const { tr } = state
204
+ const result = dispatch?.(moveCol(tr, from ?? 0, to ?? 0, true))
205
+
206
+ return Boolean(result)
207
+ })
208
+
209
+ /// A command for selecting a row.
210
+ export const selectRowCommand = $command<number, 'SelectRow'>('SelectRow', () => (index = 0) => (state, dispatch) => {
211
+ const { tr } = state
212
+ const result = dispatch?.(selectRow(index)(tr))
213
+
214
+ return Boolean(result)
215
+ })
216
+
217
+ /// A command for selecting a column.
218
+ export const selectColCommand = $command<number, 'SelectCol'>('SelectCol', () => (index = 0) => (state, dispatch) => {
219
+ const { tr } = state
220
+ const result = dispatch?.(selectCol(index)(tr))
221
+
222
+ return Boolean(result)
223
+ })
224
+
225
+ /// A command for selecting a table.
226
+ export const selectTableCommand = $command('SelectTable', () => () => (state, dispatch) => {
227
+ const { tr } = state
228
+ const result = dispatch?.(selectTable(tr))
229
+
230
+ return Boolean(result)
231
+ })
232
+
233
+ /// A command for deleting selected cells.
234
+ /// If the selection is a row or column, the row or column will be deleted.
235
+ /// If all cells are selected, the table will be deleted.
236
+ export const deleteSelectedCellsCommand = $command('DeleteSelectedCells', () => () => (state, dispatch) => {
237
+ const { selection } = state
238
+ if (!(selection instanceof CellSelection))
239
+ return false
240
+
241
+ const isRow = selection.isRowSelection()
242
+ const isCol = selection.isColSelection()
243
+
244
+ if (isRow && isCol)
245
+ return deleteTable(state, dispatch)
246
+
247
+ if (isCol)
248
+ return deleteColumn(state, dispatch)
249
+
250
+ else
251
+ return deleteRow(state, dispatch)
252
+ })
253
+
254
+ /// A command for adding a column before the current column.
255
+ export const addColBeforeCommand = $command('AddColBefore', () => () => addColumnBefore)
256
+
257
+ /// A command for adding a column after the current column.
258
+ export const addColAfterCommand = $command('AddColAfter', () => () => addColumnAfter)
259
+
260
+ /// A command for adding a row before the current row.
261
+ export const addRowBeforeCommand = $command('AddRowBefore', () => () => (state, dispatch) => {
262
+ if (!isInTable(state))
263
+ return false
264
+ if (dispatch) {
265
+ const rect = selectedRect(state)
266
+ dispatch(addRowWithAlignment(state.tr, rect, rect.top))
267
+ }
268
+ return true
269
+ })
270
+
271
+ /// A command for adding a row after the current row.
272
+ export const addRowAfterCommand = $command('AddRowAfter', () => () => (state, dispatch) => {
273
+ if (!isInTable(state))
274
+ return false
275
+ if (dispatch) {
276
+ const rect = selectedRect(state)
277
+ dispatch(addRowWithAlignment(state.tr, rect, rect.bottom))
278
+ }
279
+ return true
280
+ })
281
+
282
+ /// A command for setting alignment property for selected cells.
283
+ /// You can specify the alignment as `left`, `center`, or `right`.
284
+ /// It's `left` by default.
285
+ export const setAlignCommand = $command<'left' | 'center' | 'right', 'SetAlign'>('SetAlign', () => (alignment = 'left') => setCellAttr('alignment', alignment))
286
+
287
+ /// Keymap for table commands.
288
+ /// - `<Mod-]>`/`<Tab>`: Move to the next cell.
289
+ /// - `<Mod-[>`/`<Shift-Tab>`: Move to the previous cell.
290
+ /// - `<Mod-Enter>`: Exit the table, and break it if possible.
291
+ export const tableKeymap = $useKeymap('tableKeymap', {
292
+ NextCell: {
293
+ shortcuts: ['Mod-]', 'Tab'],
294
+ command: (ctx) => {
295
+ const commands = ctx.get(commandsCtx)
296
+
297
+ return () => commands.call(goToNextTableCellCommand.key)
298
+ },
299
+ },
300
+ PrevCell: {
301
+ shortcuts: ['Mod-[', 'Shift-Tab'],
302
+ command: (ctx) => {
303
+ const commands = ctx.get(commandsCtx)
304
+
305
+ return () => commands.call(goToPrevTableCellCommand.key)
306
+ },
307
+ },
308
+ ExitTable: {
309
+ shortcuts: ['Mod-Enter'],
310
+ command: (ctx) => {
311
+ const commands = ctx.get(commandsCtx)
312
+
313
+ return () => commands.call(breakTableCommand.key)
314
+ },
315
+ },
316
+ })
317
+
318
+ export * from './utils'