@milkdown/preset-gfm 7.3.6 → 7.5.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 (69) hide show
  1. package/lib/__internal__/index.d.ts.map +1 -1
  2. package/lib/__internal__/with-meta.d.ts.map +1 -1
  3. package/lib/composed/commands.d.ts +5 -1
  4. package/lib/composed/commands.d.ts.map +1 -1
  5. package/lib/composed/index.d.ts.map +1 -1
  6. package/lib/composed/inputrules.d.ts.map +1 -1
  7. package/lib/composed/keymap.d.ts.map +1 -1
  8. package/lib/composed/plugins.d.ts.map +1 -1
  9. package/lib/composed/schema.d.ts.map +1 -1
  10. package/lib/index.d.ts.map +1 -1
  11. package/lib/index.es.js +545 -482
  12. package/lib/index.es.js.map +1 -1
  13. package/lib/mark/index.d.ts.map +1 -1
  14. package/lib/mark/strike-through.d.ts.map +1 -1
  15. package/lib/node/footnote/definition.d.ts.map +1 -1
  16. package/lib/node/footnote/index.d.ts.map +1 -1
  17. package/lib/node/footnote/reference.d.ts.map +1 -1
  18. package/lib/node/index.d.ts.map +1 -1
  19. package/lib/node/table/command.d.ts +33 -0
  20. package/lib/node/table/command.d.ts.map +1 -0
  21. package/lib/node/table/index.d.ts +3 -30
  22. package/lib/node/table/index.d.ts.map +1 -1
  23. package/lib/node/table/input.d.ts +3 -0
  24. package/lib/node/table/input.d.ts.map +1 -0
  25. package/lib/node/table/schema.d.ts +6 -0
  26. package/lib/node/table/schema.d.ts.map +1 -0
  27. package/lib/node/table/utils.d.ts +21 -7
  28. package/lib/node/table/utils.d.ts.map +1 -1
  29. package/lib/node/task-list-item.d.ts.map +1 -1
  30. package/lib/plugin/auto-insert-span-plugin.d.ts +2 -0
  31. package/lib/plugin/auto-insert-span-plugin.d.ts.map +1 -0
  32. package/lib/plugin/column-resizing-plugin.d.ts.map +1 -1
  33. package/lib/plugin/index.d.ts +2 -1
  34. package/lib/plugin/index.d.ts.map +1 -1
  35. package/lib/plugin/keep-table-align-plugin.d.ts +2 -0
  36. package/lib/plugin/keep-table-align-plugin.d.ts.map +1 -0
  37. package/lib/plugin/remark-gfm-plugin.d.ts.map +1 -1
  38. package/lib/plugin/table-editing-plugin.d.ts.map +1 -1
  39. package/package.json +12 -17
  40. package/src/__internal__/index.ts +0 -2
  41. package/src/__internal__/with-meta.ts +0 -1
  42. package/src/composed/commands.ts +2 -3
  43. package/src/composed/index.ts +0 -1
  44. package/src/composed/inputrules.ts +0 -1
  45. package/src/composed/keymap.ts +0 -1
  46. package/src/composed/plugins.ts +5 -6
  47. package/src/composed/schema.ts +11 -2
  48. package/src/index.ts +1 -3
  49. package/src/mark/index.ts +0 -1
  50. package/src/mark/strike-through.ts +0 -1
  51. package/src/node/footnote/definition.ts +0 -2
  52. package/src/node/footnote/index.ts +0 -1
  53. package/src/node/footnote/reference.ts +0 -2
  54. package/src/node/index.ts +0 -1
  55. package/src/node/table/command.ts +228 -0
  56. package/src/node/table/index.ts +3 -451
  57. package/src/node/table/input.ts +73 -0
  58. package/src/node/table/schema.ts +216 -0
  59. package/src/node/table/utils.ts +49 -20
  60. package/src/node/task-list-item.ts +1 -2
  61. package/src/plugin/auto-insert-span-plugin.ts +12 -0
  62. package/src/plugin/column-resizing-plugin.ts +0 -1
  63. package/src/plugin/index.ts +2 -2
  64. package/src/plugin/keep-table-align-plugin.ts +60 -0
  65. package/src/plugin/remark-gfm-plugin.ts +0 -1
  66. package/src/plugin/table-editing-plugin.ts +1 -2
  67. package/lib/plugin/auto-insert-zero-space-plugin.d.ts +0 -2
  68. package/lib/plugin/auto-insert-zero-space-plugin.d.ts.map +0 -1
  69. package/src/plugin/auto-insert-zero-space-plugin.ts +0 -56
@@ -1,452 +1,4 @@
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 { withMeta } from '../../__internal__'
11
- import { addRowWithAlignment, createTable, moveCol, moveRow, selectCol, selectRow, selectTable } from './utils'
12
-
13
- const originalSchema = tableNodes({
14
- tableGroup: 'block',
15
- cellContent: 'paragraph',
16
- cellAttributes: {
17
- alignment: {
18
- default: 'left',
19
- getFromDOM: dom => (dom).style.textAlign || 'left',
20
- setDOMAttr: (value, attrs) => {
21
- attrs.style = `text-align: ${value || 'left'}`
22
- },
23
- },
24
- },
25
- })
26
-
27
- /// Schema for table node.
28
- export const tableSchema = $nodeSchema('table', () => ({
29
- ...originalSchema.table,
30
- parseMarkdown: {
31
- match: node => node.type === 'table',
32
- runner: (state, node, type) => {
33
- const align = node.align as (string | null)[]
34
- const children = (node.children as MarkdownNode[]).map((x, i) => ({
35
- ...x,
36
- align,
37
- isHeader: i === 0,
38
- }))
39
- state.openNode(type)
40
- state.next(children)
41
- state.closeNode()
42
- },
43
- },
44
- toMarkdown: {
45
- match: node => node.type.name === 'table',
46
- runner: (state, node) => {
47
- const firstLine = node.content.firstChild?.content
48
- if (!firstLine)
49
- return
50
-
51
- const align: (string | null)[] = []
52
- firstLine.forEach((cell) => {
53
- align.push(cell.attrs.alignment)
54
- })
55
- state.openNode('table', undefined, { align })
56
- state.next(node.content)
57
- state.closeNode()
58
- },
59
- },
60
- }))
61
-
62
- withMeta(tableSchema.node, {
63
- displayName: 'NodeSchema<table>',
64
- group: 'Table',
65
- })
66
-
67
- withMeta(tableSchema.ctx, {
68
- displayName: 'NodeSchemaCtx<table>',
69
- group: 'Table',
70
- })
71
-
72
- /// Schema for table row node.
73
- export const tableRowSchema = $nodeSchema('table_row', () => ({
74
- ...originalSchema.table_row,
75
- parseMarkdown: {
76
- match: node => node.type === 'tableRow',
77
- runner: (state, node, type) => {
78
- const align = node.align as (string | null)[]
79
- const children = (node.children as MarkdownNode[]).map((x, i) => ({
80
- ...x,
81
- align: align[i],
82
- isHeader: node.isHeader,
83
- }))
84
- state.openNode(type)
85
- state.next(children)
86
- state.closeNode()
87
- },
88
- },
89
- toMarkdown: {
90
- match: node => node.type.name === 'table_row',
91
- runner: (state, node) => {
92
- state.openNode('tableRow')
93
- state.next(node.content)
94
- state.closeNode()
95
- },
96
- },
97
- }))
98
-
99
- withMeta(tableRowSchema.node, {
100
- displayName: 'NodeSchema<tableRow>',
101
- group: 'Table',
102
- })
103
-
104
- withMeta(tableRowSchema.ctx, {
105
- displayName: 'NodeSchemaCtx<tableRow>',
106
- group: 'Table',
107
- })
108
-
109
- /// Schema for table cell node.
110
- export const tableCellSchema = $nodeSchema('table_cell', () => ({
111
- ...originalSchema.table_cell,
112
- parseMarkdown: {
113
- match: node => node.type === 'tableCell' && !node.isHeader,
114
- runner: (state, node, type) => {
115
- const align = node.align as string
116
- state
117
- .openNode(type, { alignment: align })
118
- .openNode(state.schema.nodes.paragraph as NodeType)
119
- .next(node.children)
120
- .closeNode()
121
- .closeNode()
122
- },
123
- },
124
- toMarkdown: {
125
- match: node => node.type.name === 'table_cell',
126
- runner: (state, node) => {
127
- state.openNode('tableCell').next(node.content).closeNode()
128
- },
129
- },
130
- }))
131
-
132
- withMeta(tableCellSchema.node, {
133
- displayName: 'NodeSchema<tableCell>',
134
- group: 'Table',
135
- })
136
-
137
- withMeta(tableCellSchema.ctx, {
138
- displayName: 'NodeSchemaCtx<tableCell>',
139
- group: 'Table',
140
- })
141
-
142
- /// Schema for table header node.
143
- export const tableHeaderSchema = $nodeSchema('table_header', () => ({
144
- ...originalSchema.table_header,
145
- parseMarkdown: {
146
- match: node => node.type === 'tableCell' && !!node.isHeader,
147
- runner: (state, node, type) => {
148
- const align = node.align as string
149
- state.openNode(type, { alignment: align })
150
- state.openNode(state.schema.nodes.paragraph as NodeType)
151
- state.next(node.children)
152
- state.closeNode()
153
- state.closeNode()
154
- },
155
- },
156
- toMarkdown: {
157
- match: node => node.type.name === 'table_header',
158
- runner: (state, node) => {
159
- state.openNode('tableCell')
160
- state.next(node.content)
161
- state.closeNode()
162
- },
163
- },
164
- }))
165
-
166
- withMeta(tableHeaderSchema.node, {
167
- displayName: 'NodeSchema<tableHeader>',
168
- group: 'Table',
169
- })
170
-
171
- withMeta(tableHeaderSchema.ctx, {
172
- displayName: 'NodeSchemaCtx<tableHeader>',
173
- group: 'Table',
174
- })
175
-
176
- /// A input rule for creating table.
177
- /// For example, `|2x2|` will create a 2x2 table.
178
- export const insertTableInputRule = $inputRule(ctx => new InputRule(
179
- /^\|(?<col>\d+)[xX](?<row>\d+)\|\s$/,
180
- (state, match, start, end) => {
181
- const $start = state.doc.resolve(start)
182
- if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), tableSchema.type(ctx)))
183
- return null
184
-
185
- const tableNode = createTable(
186
- ctx,
187
- Number(match.groups?.row),
188
- Number(match.groups?.col),
189
- )
190
- const tr = state.tr.replaceRangeWith(start, end, tableNode)
191
- return tr.setSelection(TextSelection.create(tr.doc, start + 3)).scrollIntoView()
192
- },
193
- ))
194
-
195
- withMeta(insertTableInputRule, {
196
- displayName: 'InputRule<insertTableInputRule>',
197
- group: 'Table',
198
- })
199
-
200
- /// A command for moving cursor to previous cell.
201
- export const goToPrevTableCellCommand = $command('GoToPrevTableCell', () => () => goToNextCell(-1))
202
-
203
- withMeta(goToPrevTableCellCommand, {
204
- displayName: 'Command<goToPrevTableCellCommand>',
205
- group: 'Table',
206
- })
207
-
208
- /// A command for moving cursor to next cell.
209
- export const goToNextTableCellCommand = $command('GoToNextTableCell', () => () => goToNextCell(1))
210
-
211
- withMeta(goToNextTableCellCommand, {
212
- displayName: 'Command<goToNextTableCellCommand>',
213
- group: 'Table',
214
- })
215
-
216
- /// A command for splitting current table into two tables.
217
- /// If the selection is at the end of the table,
218
- /// it will just quit the table and insert a new paragraph node.
219
- export const breakTableCommand = $command('BreakTable', ctx => () => (state, dispatch) => {
220
- if (!isInTable(state))
221
- return false
222
-
223
- const { $head } = state.selection
224
- const pos = $head.after()
225
- const tr = state.tr
226
- .replaceWith(pos, pos, paragraphSchema.type(ctx).createAndFill()!)
227
-
228
- tr.setSelection(Selection.near(tr.doc.resolve(pos), 1)).scrollIntoView()
229
- dispatch?.(tr)
230
- return true
231
- })
232
-
233
- withMeta(breakTableCommand, {
234
- displayName: 'Command<breakTableCommand>',
235
- group: 'Table',
236
- })
237
-
238
- /// A command for inserting a table.
239
- /// You can specify the number of rows and columns.
240
- /// By default, it will insert a 3x3 table.
241
- export const insertTableCommand = $command('InsertTable', ctx => ({ row, col }: { row?: number, col?: number } = {}) => (state, dispatch) => {
242
- const { selection, tr } = state
243
- const { from } = selection
244
- const table = createTable(ctx, row, col)
245
- const _tr = tr.replaceSelectionWith(table)
246
- const sel = Selection.findFrom(_tr.doc.resolve(from), 1, true)
247
- if (sel)
248
- _tr.setSelection(sel)
249
-
250
- dispatch?.(_tr)
251
-
252
- return true
253
- })
254
-
255
- withMeta(insertTableCommand, {
256
- displayName: 'Command<insertTableCommand>',
257
- group: 'Table',
258
- })
259
-
260
- /// A command for moving a row in a table.
261
- /// You should specify the `from` and `to` index.
262
- export const moveRowCommand = $command('MoveRow', () => ({ from, to }: { from?: number, to?: number } = {}) => (state, dispatch) => {
263
- const { tr } = state
264
- const result = dispatch?.(moveRow(tr, from ?? 0, to ?? 0, true))
265
-
266
- return Boolean(result)
267
- })
268
-
269
- withMeta(moveRowCommand, {
270
- displayName: 'Command<moveRowCommand>',
271
- group: 'Table',
272
- })
273
-
274
- /// A command for moving a column in a table.
275
- /// You should specify the `from` and `to` index.
276
- export const moveColCommand = $command('MoveCol', () => ({ from, to }: { from?: number, to?: number } = {}) => (state, dispatch) => {
277
- const { tr } = state
278
- const result = dispatch?.(moveCol(tr, from ?? 0, to ?? 0, true))
279
-
280
- return Boolean(result)
281
- })
282
-
283
- withMeta(moveColCommand, {
284
- displayName: 'Command<moveColCommand>',
285
- group: 'Table',
286
- })
287
-
288
- /// A command for selecting a row.
289
- export const selectRowCommand = $command<number, 'SelectRow'>('SelectRow', () => (index = 0) => (state, dispatch) => {
290
- const { tr } = state
291
- const result = dispatch?.(selectRow(index)(tr))
292
-
293
- return Boolean(result)
294
- })
295
-
296
- withMeta(selectRowCommand, {
297
- displayName: 'Command<selectRowCommand>',
298
- group: 'Table',
299
- })
300
-
301
- /// A command for selecting a column.
302
- export const selectColCommand = $command<number, 'SelectCol'>('SelectCol', () => (index = 0) => (state, dispatch) => {
303
- const { tr } = state
304
- const result = dispatch?.(selectCol(index)(tr))
305
-
306
- return Boolean(result)
307
- })
308
-
309
- withMeta(selectColCommand, {
310
- displayName: 'Command<selectColCommand>',
311
- group: 'Table',
312
- })
313
-
314
- /// A command for selecting a table.
315
- export const selectTableCommand = $command('SelectTable', () => () => (state, dispatch) => {
316
- const { tr } = state
317
- const result = dispatch?.(selectTable(tr))
318
-
319
- return Boolean(result)
320
- })
321
-
322
- withMeta(selectTableCommand, {
323
- displayName: 'Command<selectTableCommand>',
324
- group: 'Table',
325
- })
326
-
327
- /// A command for deleting selected cells.
328
- /// If the selection is a row or column, the row or column will be deleted.
329
- /// If all cells are selected, the table will be deleted.
330
- export const deleteSelectedCellsCommand = $command('DeleteSelectedCells', () => () => (state, dispatch) => {
331
- const { selection } = state
332
- if (!(selection instanceof CellSelection))
333
- return false
334
-
335
- const isRow = selection.isRowSelection()
336
- const isCol = selection.isColSelection()
337
-
338
- if (isRow && isCol)
339
- return deleteTable(state, dispatch)
340
-
341
- if (isCol)
342
- return deleteColumn(state, dispatch)
343
-
344
- else
345
- return deleteRow(state, dispatch)
346
- })
347
-
348
- withMeta(deleteSelectedCellsCommand, {
349
- displayName: 'Command<deleteSelectedCellsCommand>',
350
- group: 'Table',
351
- })
352
-
353
- /// A command for adding a column before the current column.
354
- export const addColBeforeCommand = $command('AddColBefore', () => () => addColumnBefore)
355
-
356
- withMeta(addColBeforeCommand, {
357
- displayName: 'Command<addColBeforeCommand>',
358
- group: 'Table',
359
- })
360
-
361
- /// A command for adding a column after the current column.
362
- export const addColAfterCommand = $command('AddColAfter', () => () => addColumnAfter)
363
-
364
- withMeta(addColAfterCommand, {
365
- displayName: 'Command<addColAfterCommand>',
366
- group: 'Table',
367
- })
368
-
369
- /// A command for adding a row before the current row.
370
- export const addRowBeforeCommand = $command('AddRowBefore', ctx => () => (state, dispatch) => {
371
- if (!isInTable(state))
372
- return false
373
- if (dispatch) {
374
- const rect = selectedRect(state)
375
- dispatch(addRowWithAlignment(ctx, state.tr, rect, rect.top))
376
- }
377
- return true
378
- })
379
-
380
- withMeta(addRowBeforeCommand, {
381
- displayName: 'Command<addRowBeforeCommand>',
382
- group: 'Table',
383
- })
384
-
385
- /// A command for adding a row after the current row.
386
- export const addRowAfterCommand = $command('AddRowAfter', ctx => () => (state, dispatch) => {
387
- if (!isInTable(state))
388
- return false
389
- if (dispatch) {
390
- const rect = selectedRect(state)
391
- dispatch(addRowWithAlignment(ctx, state.tr, rect, rect.bottom))
392
- }
393
- return true
394
- })
395
-
396
- withMeta(addRowAfterCommand, {
397
- displayName: 'Command<addRowAfterCommand>',
398
- group: 'Table',
399
- })
400
-
401
- /// A command for setting alignment property for selected cells.
402
- /// You can specify the alignment as `left`, `center`, or `right`.
403
- /// It's `left` by default.
404
- export const setAlignCommand = $command<'left' | 'center' | 'right', 'SetAlign'>('SetAlign', () => (alignment = 'left') => setCellAttr('alignment', alignment))
405
-
406
- withMeta(setAlignCommand, {
407
- displayName: 'Command<setAlignCommand>',
408
- group: 'Table',
409
- })
410
-
411
- /// Keymap for table commands.
412
- /// - `<Mod-]>`/`<Tab>`: Move to the next cell.
413
- /// - `<Mod-[>`/`<Shift-Tab>`: Move to the previous cell.
414
- /// - `<Mod-Enter>`: Exit the table, and break it if possible.
415
- export const tableKeymap = $useKeymap('tableKeymap', {
416
- NextCell: {
417
- shortcuts: ['Mod-]', 'Tab'],
418
- command: (ctx) => {
419
- const commands = ctx.get(commandsCtx)
420
-
421
- return () => commands.call(goToNextTableCellCommand.key)
422
- },
423
- },
424
- PrevCell: {
425
- shortcuts: ['Mod-[', 'Shift-Tab'],
426
- command: (ctx) => {
427
- const commands = ctx.get(commandsCtx)
428
-
429
- return () => commands.call(goToPrevTableCellCommand.key)
430
- },
431
- },
432
- ExitTable: {
433
- shortcuts: ['Mod-Enter'],
434
- command: (ctx) => {
435
- const commands = ctx.get(commandsCtx)
436
-
437
- return () => commands.call(breakTableCommand.key)
438
- },
439
- },
440
- })
441
-
442
- withMeta(tableKeymap.ctx, {
443
- displayName: 'KeymapCtx<table>',
444
- group: 'Table',
445
- })
446
-
447
- withMeta(tableKeymap.shortcuts, {
448
- displayName: 'Keymap<table>',
449
- group: 'Table',
450
- })
451
-
1
+ export * from './schema'
2
+ export * from './command'
452
3
  export * from './utils'
4
+ export * from './input'
@@ -0,0 +1,73 @@
1
+ import { commandsCtx } from '@milkdown/core'
2
+ import { InputRule } from '@milkdown/prose/inputrules'
3
+ import { TextSelection } from '@milkdown/prose/state'
4
+ import { $inputRule, $useKeymap } from '@milkdown/utils'
5
+ import { withMeta } from '../../__internal__'
6
+ import { createTable } from './utils'
7
+ import { tableSchema } from './schema'
8
+ import { exitTable, goToNextTableCellCommand, goToPrevTableCellCommand } from './command'
9
+
10
+ /// A input rule for creating table.
11
+ /// For example, `|2x2|` will create a 2x2 table.
12
+ export const insertTableInputRule = $inputRule(ctx => new InputRule(
13
+ /^\|(?<col>\d+)[xX](?<row>\d+)\|\s$/,
14
+ (state, match, start, end) => {
15
+ const $start = state.doc.resolve(start)
16
+ if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), tableSchema.type(ctx)))
17
+ return null
18
+
19
+ const tableNode = createTable(
20
+ ctx,
21
+ Number(match.groups?.row),
22
+ Number(match.groups?.col),
23
+ )
24
+ const tr = state.tr.replaceRangeWith(start, end, tableNode)
25
+ return tr.setSelection(TextSelection.create(tr.doc, start + 3)).scrollIntoView()
26
+ },
27
+ ))
28
+
29
+ withMeta(insertTableInputRule, {
30
+ displayName: 'InputRule<insertTableInputRule>',
31
+ group: 'Table',
32
+ })
33
+
34
+ /// Keymap for table commands.
35
+ /// - `<Mod-]>`/`<Tab>`: Move to the next cell.
36
+ /// - `<Mod-[>`/`<Shift-Tab>`: Move to the previous cell.
37
+ /// - `<Mod-Enter>`: Exit the table, and break it if possible.
38
+ export const tableKeymap = $useKeymap('tableKeymap', {
39
+ NextCell: {
40
+ shortcuts: ['Mod-]', 'Tab'],
41
+ command: (ctx) => {
42
+ const commands = ctx.get(commandsCtx)
43
+
44
+ return () => commands.call(goToNextTableCellCommand.key)
45
+ },
46
+ },
47
+ PrevCell: {
48
+ shortcuts: ['Mod-[', 'Shift-Tab'],
49
+ command: (ctx) => {
50
+ const commands = ctx.get(commandsCtx)
51
+
52
+ return () => commands.call(goToPrevTableCellCommand.key)
53
+ },
54
+ },
55
+ ExitTable: {
56
+ shortcuts: ['Mod-Enter'],
57
+ command: (ctx) => {
58
+ const commands = ctx.get(commandsCtx)
59
+
60
+ return () => commands.call(exitTable.key)
61
+ },
62
+ },
63
+ })
64
+
65
+ withMeta(tableKeymap.ctx, {
66
+ displayName: 'KeymapCtx<table>',
67
+ group: 'Table',
68
+ })
69
+
70
+ withMeta(tableKeymap.shortcuts, {
71
+ displayName: 'Keymap<table>',
72
+ group: 'Table',
73
+ })