@milkdown/preset-gfm 6.5.2 → 6.5.4

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 (94) hide show
  1. package/lib/footnote/definition.d.ts +1 -1
  2. package/lib/footnote/definition.d.ts.map +1 -1
  3. package/lib/footnote/index.d.ts.map +1 -1
  4. package/lib/footnote/reference.d.ts +1 -1
  5. package/lib/footnote/reference.d.ts.map +1 -1
  6. package/lib/footnote/utils.d.ts.map +1 -1
  7. package/lib/index.d.ts +1 -1
  8. package/lib/index.d.ts.map +1 -1
  9. package/lib/index.es.js +649 -607
  10. package/lib/index.es.js.map +1 -1
  11. package/lib/strike-through.d.ts +1 -1
  12. package/lib/strike-through.d.ts.map +1 -1
  13. package/lib/supported-keys.d.ts +1 -1
  14. package/lib/supported-keys.d.ts.map +1 -1
  15. package/lib/table/command.d.ts +2 -2
  16. package/lib/table/command.d.ts.map +1 -1
  17. package/lib/table/index.d.ts +1 -1
  18. package/lib/table/index.d.ts.map +1 -1
  19. package/lib/table/nodes/index.d.ts +1 -1
  20. package/lib/table/nodes/index.d.ts.map +1 -1
  21. package/lib/table/operator-plugin/actions.d.ts +5 -5
  22. package/lib/table/operator-plugin/actions.d.ts.map +1 -1
  23. package/lib/table/operator-plugin/calc-pos.d.ts +1 -1
  24. package/lib/table/operator-plugin/calc-pos.d.ts.map +1 -1
  25. package/lib/table/operator-plugin/constant.d.ts.map +1 -1
  26. package/lib/table/operator-plugin/helper.d.ts +3 -3
  27. package/lib/table/operator-plugin/helper.d.ts.map +1 -1
  28. package/lib/table/operator-plugin/index.d.ts +2 -2
  29. package/lib/table/operator-plugin/index.d.ts.map +1 -1
  30. package/lib/table/operator-plugin/style.d.ts +1 -1
  31. package/lib/table/operator-plugin/style.d.ts.map +1 -1
  32. package/lib/table/operator-plugin/widget.d.ts +2 -2
  33. package/lib/table/operator-plugin/widget.d.ts.map +1 -1
  34. package/lib/table/plugin/auto-insert-zero-space.d.ts.map +1 -1
  35. package/lib/table/plugin/cell-selection.d.ts +5 -3
  36. package/lib/table/plugin/cell-selection.d.ts.map +1 -1
  37. package/lib/table/plugin/column-resizing.d.ts +2 -1
  38. package/lib/table/plugin/column-resizing.d.ts.map +1 -1
  39. package/lib/table/plugin/commands.d.ts +2 -2
  40. package/lib/table/plugin/commands.d.ts.map +1 -1
  41. package/lib/table/plugin/copy-paste.d.ts +6 -5
  42. package/lib/table/plugin/copy-paste.d.ts.map +1 -1
  43. package/lib/table/plugin/fix-tables.d.ts +3 -2
  44. package/lib/table/plugin/fix-tables.d.ts.map +1 -1
  45. package/lib/table/plugin/index.d.ts.map +1 -1
  46. package/lib/table/plugin/schema.d.ts +1 -1
  47. package/lib/table/plugin/schema.d.ts.map +1 -1
  48. package/lib/table/plugin/table-editing.d.ts +1 -1
  49. package/lib/table/plugin/table-editing.d.ts.map +1 -1
  50. package/lib/table/plugin/table-map.d.ts +2 -2
  51. package/lib/table/plugin/table-map.d.ts.map +1 -1
  52. package/lib/table/plugin/table-view.d.ts +2 -2
  53. package/lib/table/plugin/table-view.d.ts.map +1 -1
  54. package/lib/table/plugin/types.d.ts +2 -2
  55. package/lib/table/plugin/types.d.ts.map +1 -1
  56. package/lib/table/plugin/util.d.ts +2 -2
  57. package/lib/table/plugin/util.d.ts.map +1 -1
  58. package/lib/table/utils.d.ts +5 -5
  59. package/lib/table/utils.d.ts.map +1 -1
  60. package/lib/task-list-item.d.ts +2 -2
  61. package/lib/task-list-item.d.ts.map +1 -1
  62. package/package.json +22 -17
  63. package/src/footnote/definition.ts +172 -166
  64. package/src/footnote/index.ts +2 -2
  65. package/src/footnote/reference.ts +166 -162
  66. package/src/footnote/utils.ts +2 -2
  67. package/src/index.ts +83 -83
  68. package/src/strike-through.ts +36 -36
  69. package/src/supported-keys.ts +9 -8
  70. package/src/table/command.ts +17 -16
  71. package/src/table/index.ts +9 -9
  72. package/src/table/nodes/index.ts +177 -174
  73. package/src/table/operator-plugin/actions.ts +103 -102
  74. package/src/table/operator-plugin/calc-pos.ts +31 -34
  75. package/src/table/operator-plugin/constant.ts +3 -3
  76. package/src/table/operator-plugin/helper.ts +31 -32
  77. package/src/table/operator-plugin/index.ts +104 -95
  78. package/src/table/operator-plugin/style.ts +10 -9
  79. package/src/table/operator-plugin/widget.ts +47 -45
  80. package/src/table/plugin/auto-insert-zero-space.ts +41 -41
  81. package/src/table/plugin/cell-selection.ts +325 -296
  82. package/src/table/plugin/column-resizing.ts +226 -198
  83. package/src/table/plugin/commands.ts +464 -421
  84. package/src/table/plugin/copy-paste.ts +256 -240
  85. package/src/table/plugin/fix-tables.ts +103 -88
  86. package/src/table/plugin/index.ts +3 -3
  87. package/src/table/plugin/schema.ts +100 -94
  88. package/src/table/plugin/table-editing.ts +324 -230
  89. package/src/table/plugin/table-map.ts +294 -229
  90. package/src/table/plugin/table-view.ts +66 -62
  91. package/src/table/plugin/types.ts +8 -8
  92. package/src/table/plugin/util.ts +78 -66
  93. package/src/table/utils.ts +141 -138
  94. package/src/task-list-item.ts +151 -146
@@ -1,275 +1,369 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { keydownHandler } from '@milkdown/prose/keymap';
3
- import { Fragment, ResolvedPos, Slice } from '@milkdown/prose/model';
4
- import { Command, EditorState, Plugin, PluginKey, Selection, TextSelection, Transaction } from '@milkdown/prose/state';
5
- import { EditorView } from '@milkdown/prose/view';
6
2
 
7
- import { CellSelection, drawCellSelection, normalizeSelection } from './cell-selection';
8
- import { clipCells, fitSlice, insertCells, pastedCells } from './copy-paste';
9
- import { fixTables } from './fix-tables';
10
- import { tableNodeTypes } from './schema';
11
- import { TableMap } from './table-map';
12
- import { cellAround, inSameTable, isInTable, nextCell, selectionCell } from './util';
3
+ import { keydownHandler } from '@milkdown/prose/keymap'
4
+ import type { ResolvedPos } from '@milkdown/prose/model'
5
+ import { Fragment, Slice } from '@milkdown/prose/model'
6
+ import type { Command, EditorState, Transaction } from '@milkdown/prose/state'
7
+ import {
8
+ Plugin,
9
+ PluginKey,
10
+ Selection,
11
+ TextSelection,
12
+ } from '@milkdown/prose/state'
13
+ import type { EditorView } from '@milkdown/prose/view'
13
14
 
14
- export const tableEditingKey = new PluginKey('selectingCells');
15
+ import {
16
+ CellSelection,
17
+ drawCellSelection,
18
+ normalizeSelection,
19
+ } from './cell-selection'
20
+ import { clipCells, fitSlice, insertCells, pastedCells } from './copy-paste'
21
+ import { fixTables } from './fix-tables'
22
+ import { tableNodeTypes } from './schema'
23
+ import { TableMap } from './table-map'
24
+ import {
25
+ cellAround,
26
+ inSameTable,
27
+ isInTable,
28
+ nextCell,
29
+ selectionCell,
30
+ } from './util'
31
+
32
+ export const tableEditingKey = new PluginKey('selectingCells')
15
33
 
16
34
  function domInCell(view: EditorView, dom: EventTarget | null) {
17
- for (; dom && dom != view.dom; dom = (dom as Element).parentNode as Element)
18
- if ((dom as Element).nodeName == 'TD' || (dom as Element).nodeName == 'TH') return dom;
19
- return;
35
+ for (; dom && dom != view.dom; dom = (dom as Element).parentNode as Element) {
36
+ if ((dom as Element).nodeName == 'TD' || (dom as Element).nodeName == 'TH')
37
+ return dom
38
+ }
39
+
40
+ return undefined
20
41
  }
21
42
 
22
43
  function cellUnderMouse(view: EditorView, event: MouseEvent) {
23
- const mousePos = view.posAtCoords({ left: event.clientX, top: event.clientY });
24
- if (!mousePos) return null;
25
- return mousePos ? cellAround(view.state.doc.resolve(mousePos.pos)) : null;
44
+ const mousePos = view.posAtCoords({
45
+ left: event.clientX,
46
+ top: event.clientY,
47
+ })
48
+ if (!mousePos)
49
+ return null
50
+ return mousePos ? cellAround(view.state.doc.resolve(mousePos.pos)) : null
26
51
  }
27
52
 
28
53
  function handleMouseDown(view: EditorView, event: Event) {
29
- const startEvent = event as MouseEvent;
30
- if (startEvent.ctrlKey || startEvent.metaKey) return;
31
-
32
- const startDOMCell = domInCell(view, startEvent.target as Element);
33
- let $anchor;
34
- if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
35
- // Adding to an existing cell selection
36
- setCellSelection(view.state.selection.$anchorCell, startEvent);
37
- startEvent.preventDefault();
38
- } else if (
39
- startEvent.shiftKey &&
40
- startDOMCell &&
41
- ($anchor = cellAround(view.state.selection.$anchor)) != null &&
42
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43
- cellUnderMouse(view, startEvent)!.pos != $anchor.pos
44
- ) {
45
- // Adding to a selection that starts in another cell (causing a
46
- // cell selection to be created).
47
- setCellSelection($anchor, startEvent);
48
- startEvent.preventDefault();
49
- } else if (!startDOMCell) {
50
- // Not in a cell, let the default behavior happen.
51
- return;
52
- }
54
+ const startEvent = event as MouseEvent
55
+ if (startEvent.ctrlKey || startEvent.metaKey)
56
+ return
53
57
 
54
- // Create and dispatch a cell selection between the given anchor and
55
- // the position under the mouse.
56
- function setCellSelection($anchor: ResolvedPos, event: MouseEvent) {
57
- let $head = cellUnderMouse(view, event);
58
- const starting = tableEditingKey.getState(view.state) == null;
59
- if (!$head || !inSameTable($anchor, $head)) {
60
- if (starting) $head = $anchor;
61
- else return;
62
- }
63
- const selection = new CellSelection($anchor, $head);
64
- if (starting || !view.state.selection.eq(selection)) {
65
- const tr = view.state.tr.setSelection(selection);
66
- if (starting) tr.setMeta(tableEditingKey, $anchor.pos);
67
- view.dispatch(tr);
68
- }
69
- }
58
+ const startDOMCell = domInCell(view, startEvent.target as Element)
59
+ let $anchor
60
+ if (startEvent.shiftKey && view.state.selection instanceof CellSelection) {
61
+ // Adding to an existing cell selection
62
+ setCellSelection(view.state.selection.$anchorCell, startEvent)
63
+ startEvent.preventDefault()
64
+ }
65
+ else if (
66
+ startEvent.shiftKey
67
+ && startDOMCell
68
+ // eslint-disable-next-line no-cond-assign
69
+ && ($anchor = cellAround(view.state.selection.$anchor)) != null
70
+ && cellUnderMouse(view, startEvent)!.pos != $anchor.pos
71
+ ) {
72
+ // Adding to a selection that starts in another cell (causing a
73
+ // cell selection to be created).
74
+ setCellSelection($anchor, startEvent)
75
+ startEvent.preventDefault()
76
+ }
77
+ else if (!startDOMCell) {
78
+ // Not in a cell, let the default behavior happen.
79
+ return
80
+ }
70
81
 
71
- // Stop listening to mouse motion events.
72
- function stop() {
73
- view.root.removeEventListener('mouseup', stop);
74
- view.root.removeEventListener('dragstart', stop);
75
- view.root.removeEventListener('mousemove', move);
76
- if (tableEditingKey.getState(view.state) != null) view.dispatch(view.state.tr.setMeta(tableEditingKey, -1));
82
+ // Create and dispatch a cell selection between the given anchor and
83
+ // the position under the mouse.
84
+ function setCellSelection($anchor: ResolvedPos, event: MouseEvent) {
85
+ let $head = cellUnderMouse(view, event)
86
+ const starting = tableEditingKey.getState(view.state) == null
87
+ if (!$head || !inSameTable($anchor, $head)) {
88
+ if (starting)
89
+ $head = $anchor
90
+ else return
91
+ }
92
+ const selection = new CellSelection($anchor, $head)
93
+ if (starting || !view.state.selection.eq(selection)) {
94
+ const tr = view.state.tr.setSelection(selection)
95
+ if (starting)
96
+ tr.setMeta(tableEditingKey, $anchor.pos)
97
+ view.dispatch(tr)
77
98
  }
99
+ }
78
100
 
79
- function move(event: Event) {
80
- const anchor = tableEditingKey.getState(view.state);
81
- let $anchor;
82
- if (anchor != null) {
83
- // Continuing an existing cross-cell selection
84
- $anchor = view.state.doc.resolve(anchor);
85
- } else if (domInCell(view, event.target) != startDOMCell) {
86
- // Moving out of the initial cell -- start a new cell selection
87
- $anchor = cellUnderMouse(view, startEvent);
88
- if (!$anchor) return stop();
89
- }
90
- if ($anchor) setCellSelection($anchor, event as MouseEvent);
101
+ // Stop listening to mouse motion events.
102
+ function stop() {
103
+ view.root.removeEventListener('mouseup', stop)
104
+ view.root.removeEventListener('dragstart', stop)
105
+ view.root.removeEventListener('mousemove', move)
106
+ if (tableEditingKey.getState(view.state) != null)
107
+ view.dispatch(view.state.tr.setMeta(tableEditingKey, -1))
108
+ }
109
+
110
+ function move(event: Event) {
111
+ const anchor = tableEditingKey.getState(view.state)
112
+ let $anchor
113
+ if (anchor != null) {
114
+ // Continuing an existing cross-cell selection
115
+ $anchor = view.state.doc.resolve(anchor)
116
+ }
117
+ else if (domInCell(view, event.target) != startDOMCell) {
118
+ // Moving out of the initial cell -- start a new cell selection
119
+ $anchor = cellUnderMouse(view, startEvent)
120
+ if (!$anchor)
121
+ return stop()
91
122
  }
92
- view.root.addEventListener('mouseup', stop);
93
- view.root.addEventListener('dragstart', stop);
94
- view.root.addEventListener('mousemove', move);
123
+ if ($anchor)
124
+ setCellSelection($anchor, event as MouseEvent)
125
+ }
126
+ view.root.addEventListener('mouseup', stop)
127
+ view.root.addEventListener('dragstart', stop)
128
+ view.root.addEventListener('mousemove', move)
95
129
  }
96
130
 
97
131
  function handleTripleClick(view: EditorView, pos: number) {
98
- const doc = view.state.doc,
99
- $cell = cellAround(doc.resolve(pos));
100
- if (!$cell) return false;
101
- view.dispatch(view.state.tr.setSelection(new CellSelection($cell)));
102
- return true;
132
+ const doc = view.state.doc
133
+ const $cell = cellAround(doc.resolve(pos))
134
+ if (!$cell)
135
+ return false
136
+ view.dispatch(view.state.tr.setSelection(new CellSelection($cell)))
137
+ return true
103
138
  }
104
139
 
105
140
  function maybeSetSelection(
106
- state: EditorState,
107
- dispatch: undefined | ((tr: Transaction) => void),
108
- selection: Selection,
141
+ state: EditorState,
142
+ dispatch: undefined | ((tr: Transaction) => void),
143
+ selection: Selection,
109
144
  ) {
110
- if (selection.eq(state.selection)) return false;
111
- if (dispatch) dispatch(state.tr.setSelection(selection).scrollIntoView());
112
- return true;
145
+ if (selection.eq(state.selection))
146
+ return false
147
+ if (dispatch)
148
+ dispatch(state.tr.setSelection(selection).scrollIntoView())
149
+ return true
113
150
  }
114
151
 
115
152
  function atEndOfCell(view: EditorView, axis: string, dir: number) {
116
- if (!(view.state.selection instanceof TextSelection)) return null;
117
- const { $head } = view.state.selection;
118
- for (let d = $head.depth - 1; d >= 0; d--) {
119
- const parent = $head.node(d),
120
- index = dir < 0 ? $head.index(d) : $head.indexAfter(d);
121
- if (index != (dir < 0 ? 0 : parent.childCount)) return null;
122
- if (parent.type.spec['tableRole'] == 'cell' || parent.type.spec['tableRole'] == 'header_cell') {
123
- const cellPos = $head.before(d);
124
- const dirStr = axis == 'vert' ? (dir > 0 ? 'down' : 'up') : dir > 0 ? 'right' : 'left';
125
- return view.endOfTextblock(dirStr) ? cellPos : null;
126
- }
153
+ if (!(view.state.selection instanceof TextSelection))
154
+ return null
155
+ const { $head } = view.state.selection
156
+ for (let d = $head.depth - 1; d >= 0; d--) {
157
+ const parent = $head.node(d)
158
+ const index = dir < 0 ? $head.index(d) : $head.indexAfter(d)
159
+ if (index != (dir < 0 ? 0 : parent.childCount))
160
+ return null
161
+ if (
162
+ parent.type.spec.tableRole == 'cell'
163
+ || parent.type.spec.tableRole == 'header_cell'
164
+ ) {
165
+ const cellPos = $head.before(d)
166
+ const dirStr
167
+ = axis == 'vert' ? (dir > 0 ? 'down' : 'up') : dir > 0 ? 'right' : 'left'
168
+ return view.endOfTextblock(dirStr) ? cellPos : null
127
169
  }
128
- return null;
170
+ }
171
+ return null
129
172
  }
130
173
 
131
174
  function arrow(axis: string, dir: number): Command {
132
- return (state, dispatch, view) => {
133
- const sel = state.selection;
134
- if (sel instanceof CellSelection) {
135
- return maybeSetSelection(state, dispatch, Selection.near(sel.$headCell, dir));
136
- }
137
- if (axis != 'horiz' && !sel.empty) return false;
138
- const end = atEndOfCell(view as EditorView, axis, dir);
139
- if (end == null) return false;
140
- if (axis == 'horiz') {
141
- return maybeSetSelection(state, dispatch, Selection.near(state.doc.resolve(sel.head + dir), dir));
142
- } else {
143
- const $cell = state.doc.resolve(end),
144
- $next = nextCell($cell, axis, dir);
145
- let newSel;
146
- if ($next) newSel = Selection.near($next, 1);
147
- else if (dir < 0) newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1);
148
- else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1);
149
- return maybeSetSelection(state, dispatch, newSel);
150
- }
151
- };
175
+ return (state, dispatch, view) => {
176
+ const sel = state.selection
177
+ if (sel instanceof CellSelection) {
178
+ return maybeSetSelection(
179
+ state,
180
+ dispatch,
181
+ Selection.near(sel.$headCell, dir),
182
+ )
183
+ }
184
+
185
+ if (axis != 'horiz' && !sel.empty)
186
+ return false
187
+ const end = atEndOfCell(view as EditorView, axis, dir)
188
+ if (end == null)
189
+ return false
190
+ if (axis == 'horiz') {
191
+ return maybeSetSelection(
192
+ state,
193
+ dispatch,
194
+ Selection.near(state.doc.resolve(sel.head + dir), dir),
195
+ )
196
+ }
197
+ else {
198
+ const $cell = state.doc.resolve(end)
199
+ const $next = nextCell($cell, axis, dir)
200
+ let newSel
201
+ if ($next)
202
+ newSel = Selection.near($next, 1)
203
+ else if (dir < 0)
204
+ newSel = Selection.near(state.doc.resolve($cell.before(-1)), -1)
205
+ else newSel = Selection.near(state.doc.resolve($cell.after(-1)), 1)
206
+ return maybeSetSelection(state, dispatch, newSel)
207
+ }
208
+ }
152
209
  }
153
210
 
154
211
  function shiftArrow(axis: string, dir: number): Command {
155
- return (state, dispatch, view) => {
156
- let sel = state.selection;
157
- if (!(sel instanceof CellSelection)) {
158
- const end = atEndOfCell(view as EditorView, axis, dir);
159
- if (end == null) return false;
160
- sel = new CellSelection(state.doc.resolve(end));
161
- }
162
- const $head = nextCell((sel as CellSelection).$headCell, axis, dir);
163
- if (!$head) return false;
164
- return maybeSetSelection(state, dispatch, new CellSelection((sel as CellSelection).$anchorCell, $head));
165
- };
212
+ return (state, dispatch, view) => {
213
+ let sel = state.selection
214
+ if (!(sel instanceof CellSelection)) {
215
+ const end = atEndOfCell(view as EditorView, axis, dir)
216
+ if (end == null)
217
+ return false
218
+ sel = new CellSelection(state.doc.resolve(end))
219
+ }
220
+ const $head = nextCell((sel as CellSelection).$headCell, axis, dir)
221
+ if (!$head)
222
+ return false
223
+ return maybeSetSelection(
224
+ state,
225
+ dispatch,
226
+ new CellSelection((sel as CellSelection).$anchorCell, $head),
227
+ )
228
+ }
166
229
  }
167
230
 
168
- function deleteCellSelection(state: EditorState, dispatch?: (tr: Transaction) => void) {
169
- const sel = state.selection;
170
- if (!(sel instanceof CellSelection)) return false;
171
- if (dispatch) {
172
- const tr = state.tr,
173
- baseContent = tableNodeTypes(state.schema).cell.createAndFill().content;
174
- sel.forEachCell((cell, pos) => {
175
- if (!cell.content.eq(baseContent))
176
- tr.replace(
177
- tr.mapping.map(pos + 1),
178
- tr.mapping.map(pos + cell.nodeSize - 1),
179
- new Slice(baseContent, 0, 0),
180
- );
181
- });
182
- if (tr.docChanged) dispatch(tr);
183
- }
184
- return true;
231
+ function deleteCellSelection(
232
+ state: EditorState,
233
+ dispatch?: (tr: Transaction) => void,
234
+ ) {
235
+ const sel = state.selection
236
+ if (!(sel instanceof CellSelection))
237
+ return false
238
+ if (dispatch) {
239
+ const tr = state.tr
240
+ const baseContent = tableNodeTypes(state.schema).cell.createAndFill()
241
+ .content
242
+ sel.forEachCell((cell, pos) => {
243
+ if (!cell.content.eq(baseContent)) {
244
+ tr.replace(
245
+ tr.mapping.map(pos + 1),
246
+ tr.mapping.map(pos + cell.nodeSize - 1),
247
+ new Slice(baseContent, 0, 0),
248
+ )
249
+ }
250
+ })
251
+ if (tr.docChanged)
252
+ dispatch(tr)
253
+ }
254
+ return true
185
255
  }
186
256
 
187
257
  const handleKeyDown = keydownHandler({
188
- ArrowLeft: arrow('horiz', -1),
189
- ArrowRight: arrow('horiz', 1),
190
- ArrowUp: arrow('vert', -1),
191
- ArrowDown: arrow('vert', 1),
192
-
193
- 'Shift-ArrowLeft': shiftArrow('horiz', -1),
194
- 'Shift-ArrowRight': shiftArrow('horiz', 1),
195
- 'Shift-ArrowUp': shiftArrow('vert', -1),
196
- 'Shift-ArrowDown': shiftArrow('vert', 1),
197
-
198
- Backspace: deleteCellSelection,
199
- 'Mod-Backspace': deleteCellSelection,
200
- Delete: deleteCellSelection,
201
- 'Mod-Delete': deleteCellSelection,
202
- });
258
+ 'ArrowLeft': arrow('horiz', -1),
259
+ 'ArrowRight': arrow('horiz', 1),
260
+ 'ArrowUp': arrow('vert', -1),
261
+ 'ArrowDown': arrow('vert', 1),
262
+
263
+ 'Shift-ArrowLeft': shiftArrow('horiz', -1),
264
+ 'Shift-ArrowRight': shiftArrow('horiz', 1),
265
+ 'Shift-ArrowUp': shiftArrow('vert', -1),
266
+ 'Shift-ArrowDown': shiftArrow('vert', 1),
267
+
268
+ 'Backspace': deleteCellSelection,
269
+ 'Mod-Backspace': deleteCellSelection,
270
+ 'Delete': deleteCellSelection,
271
+ 'Mod-Delete': deleteCellSelection,
272
+ })
203
273
 
204
274
  export function handlePaste(view: EditorView, _: Event, slice: Slice) {
205
- if (!isInTable(view.state)) return false;
206
- let cells = pastedCells(slice);
207
- const sel = view.state.selection;
208
- if (sel instanceof CellSelection) {
209
- if (!cells)
210
- cells = {
211
- width: 1,
212
- height: 1,
213
- rows: [Fragment.from(fitSlice(tableNodeTypes(view.state.schema).cell, slice))],
214
- };
215
- const table = sel.$anchorCell.node(-1),
216
- start = sel.$anchorCell.start(-1);
217
- const rect = TableMap.get(table).rectBetween(sel.$anchorCell.pos - start, sel.$headCell.pos - start);
218
- cells = clipCells(cells, rect.right - rect.left, rect.bottom - rect.top);
219
- insertCells(view.state, view.dispatch, start, rect, cells);
220
- return true;
221
- } else if (cells) {
222
- const $cell = selectionCell(view.state) as ResolvedPos,
223
- start = $cell.start(-1);
224
- insertCells(view.state, view.dispatch, start, TableMap.get($cell.node(-1)).findCell($cell.pos - start), cells);
225
- return true;
226
- } else {
227
- return false;
275
+ if (!isInTable(view.state))
276
+ return false
277
+ let cells = pastedCells(slice)
278
+ const sel = view.state.selection
279
+ if (sel instanceof CellSelection) {
280
+ if (!cells) {
281
+ cells = {
282
+ width: 1,
283
+ height: 1,
284
+ rows: [
285
+ Fragment.from(
286
+ fitSlice(tableNodeTypes(view.state.schema).cell, slice),
287
+ ),
288
+ ],
289
+ }
228
290
  }
291
+ const table = sel.$anchorCell.node(-1)
292
+ const start = sel.$anchorCell.start(-1)
293
+ const rect = TableMap.get(table).rectBetween(
294
+ sel.$anchorCell.pos - start,
295
+ sel.$headCell.pos - start,
296
+ )
297
+ cells = clipCells(cells, rect.right - rect.left, rect.bottom - rect.top)
298
+ insertCells(view.state, view.dispatch, start, rect, cells)
299
+ return true
300
+ }
301
+ else if (cells) {
302
+ const $cell = selectionCell(view.state) as ResolvedPos
303
+ const start = $cell.start(-1)
304
+ insertCells(
305
+ view.state,
306
+ view.dispatch,
307
+ start,
308
+ TableMap.get($cell.node(-1)).findCell($cell.pos - start),
309
+ cells,
310
+ )
311
+ return true
312
+ }
313
+ else {
314
+ return false
315
+ }
229
316
  }
230
317
 
231
318
  export function tableEditing({ allowTableNodeSelection = false } = {}) {
232
- return new Plugin({
233
- key: tableEditingKey,
234
-
235
- // This piece of state is used to remember when a mouse-drag
236
- // cell-selection is happening, so that it can continue even as
237
- // transactions (which might move its anchor cell) come in.
238
- state: {
239
- init() {
240
- return null;
241
- },
242
- apply(tr, cur) {
243
- const set = tr.getMeta(tableEditingKey);
244
- if (set != null) return set == -1 ? null : set;
245
- if (cur == null || !tr.docChanged) return cur;
246
- const { deleted, pos } = tr.mapping.mapResult(cur);
247
- return deleted ? null : pos;
248
- },
249
- },
250
-
251
- props: {
252
- decorations: drawCellSelection,
253
-
254
- handleDOMEvents: {
255
- mousedown: handleMouseDown,
256
- },
257
-
258
- createSelectionBetween(view) {
259
- if (tableEditingKey.getState(view.state) != null) return view.state.selection;
260
-
261
- return null;
262
- },
263
-
264
- handleTripleClick,
265
-
266
- handleKeyDown,
267
-
268
- handlePaste,
269
- },
270
-
271
- appendTransaction(_, oldState, state) {
272
- return normalizeSelection(state, fixTables(state, oldState), allowTableNodeSelection);
273
- },
274
- });
319
+ return new Plugin({
320
+ key: tableEditingKey,
321
+
322
+ // This piece of state is used to remember when a mouse-drag
323
+ // cell-selection is happening, so that it can continue even as
324
+ // transactions (which might move its anchor cell) come in.
325
+ state: {
326
+ init() {
327
+ return null
328
+ },
329
+ apply(tr, cur) {
330
+ const set = tr.getMeta(tableEditingKey)
331
+ if (set != null)
332
+ return set == -1 ? null : set
333
+ if (cur == null || !tr.docChanged)
334
+ return cur
335
+ const { deleted, pos } = tr.mapping.mapResult(cur)
336
+ return deleted ? null : pos
337
+ },
338
+ },
339
+
340
+ props: {
341
+ decorations: drawCellSelection,
342
+
343
+ handleDOMEvents: {
344
+ mousedown: handleMouseDown,
345
+ },
346
+
347
+ createSelectionBetween(view) {
348
+ if (tableEditingKey.getState(view.state) != null)
349
+ return view.state.selection
350
+
351
+ return null
352
+ },
353
+
354
+ handleTripleClick,
355
+
356
+ handleKeyDown,
357
+
358
+ handlePaste,
359
+ },
360
+
361
+ appendTransaction(_, oldState, state) {
362
+ return normalizeSelection(
363
+ state,
364
+ fixTables(state, oldState),
365
+ allowTableNodeSelection,
366
+ )
367
+ },
368
+ })
275
369
  }