@milkdown/preset-gfm 6.5.2 → 6.5.3

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 +17 -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,13 +1,15 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
 
3
- import { Fragment, Node, NodeType, Schema, Slice } from '@milkdown/prose/model';
4
- import { EditorState, Transaction } from '@milkdown/prose/state';
5
- import { Transform } from '@milkdown/prose/transform';
3
+ import type { Node, NodeType, Schema } from '@milkdown/prose/model'
4
+ import { Fragment, Slice } from '@milkdown/prose/model'
5
+ import type { EditorState, Transaction } from '@milkdown/prose/state'
6
+ import { Transform } from '@milkdown/prose/transform'
6
7
 
7
- import { CellSelection } from './cell-selection';
8
- import { tableNodeTypes } from './schema';
9
- import { Rect, TableMap } from './table-map';
10
- import { removeColSpan, setAttr } from './util';
8
+ import { CellSelection } from './cell-selection'
9
+ import { tableNodeTypes } from './schema'
10
+ import type { Rect } from './table-map'
11
+ import { TableMap } from './table-map'
12
+ import { removeColSpan, setAttr } from './util'
11
13
 
12
14
  // Utilities to help with copying and pasting table cells
13
15
 
@@ -15,292 +17,306 @@ import { removeColSpan, setAttr } from './util';
15
17
  // Get a rectangular area of cells from a slice, or null if the outer
16
18
  // nodes of the slice aren't table cells or rows.
17
19
  export function pastedCells(slice: Slice) {
18
- if (!slice.size) return null;
19
- let { content, openStart, openEnd } = slice;
20
- while (
21
- content.childCount == 1 &&
22
- ((openStart > 0 && openEnd > 0) || (content.firstChild as Node).type.spec['tableRole'] == 'table')
23
- ) {
24
- openStart--;
25
- openEnd--;
26
- content = (content.firstChild as Node).content;
20
+ if (!slice.size)
21
+ return null
22
+ let { content, openStart, openEnd } = slice
23
+ while (
24
+ content.childCount == 1
25
+ && ((openStart > 0 && openEnd > 0) || (content.firstChild as Node).type.spec.tableRole == 'table')
26
+ ) {
27
+ openStart--
28
+ openEnd--
29
+ content = (content.firstChild as Node).content
30
+ }
31
+ const first = content.firstChild as Node
32
+ const role = first.type.spec.tableRole
33
+ const schema = first.type.schema
34
+ const rows = []
35
+ if (role == 'row') {
36
+ for (let i = 0; i < content.childCount; i++) {
37
+ let cells = content.child(i).content
38
+ const left = i ? 0 : Math.max(0, openStart - 1)
39
+ const right = i < content.childCount - 1 ? 0 : Math.max(0, openEnd - 1)
40
+ if (left || right)
41
+ cells = fitSlice(tableNodeTypes(schema).row, new Slice(cells, left, right)).content
42
+ rows.push(cells)
27
43
  }
28
- const first = content.firstChild as Node,
29
- role = first.type.spec['tableRole'];
30
- const schema = first.type.schema,
31
- rows = [];
32
- if (role == 'row') {
33
- for (let i = 0; i < content.childCount; i++) {
34
- let cells = content.child(i).content;
35
- const left = i ? 0 : Math.max(0, openStart - 1);
36
- const right = i < content.childCount - 1 ? 0 : Math.max(0, openEnd - 1);
37
- if (left || right) cells = fitSlice(tableNodeTypes(schema).row, new Slice(cells, left, right)).content;
38
- rows.push(cells);
39
- }
40
- } else if (role == 'cell' || role == 'header_cell') {
41
- rows.push(
42
- openStart || openEnd
43
- ? fitSlice(tableNodeTypes(schema).row, new Slice(content, openStart, openEnd)).content
44
- : content,
45
- );
46
- } else {
47
- return null;
48
- }
49
- return ensureRectangular(schema, rows);
44
+ }
45
+ else if (role == 'cell' || role == 'header_cell') {
46
+ rows.push(
47
+ openStart || openEnd
48
+ ? fitSlice(tableNodeTypes(schema).row, new Slice(content, openStart, openEnd)).content
49
+ : content,
50
+ )
51
+ }
52
+ else {
53
+ return null
54
+ }
55
+ return ensureRectangular(schema, rows)
50
56
  }
51
57
 
52
58
  // : (Schema, [Fragment]) → {width: number, height: number, rows: [Fragment]}
53
59
  // Compute the width and height of a set of cells, and make sure each
54
60
  // row has the same number of cells.
55
- export type R = { width: number; height: number; rows: Fragment[] };
61
+ export interface R { width: number; height: number; rows: Fragment[] }
56
62
  function ensureRectangular(schema: Schema, rows: Fragment[]): R {
57
- const widths: number[] = [];
58
- for (let i = 0; i < rows.length; i++) {
59
- const row = rows[i] as Fragment;
60
- for (let j = row.childCount - 1; j >= 0; j--) {
61
- const { rowspan, colspan } = row.child(j).attrs;
62
- for (let r = i; r < i + rowspan; r++) widths[r] = (widths[r] || 0) + colspan;
63
- }
63
+ const widths: number[] = []
64
+ for (let i = 0; i < rows.length; i++) {
65
+ const row = rows[i] as Fragment
66
+ for (let j = row.childCount - 1; j >= 0; j--) {
67
+ const { rowspan, colspan } = row.child(j).attrs
68
+ for (let r = i; r < i + rowspan; r++) widths[r] = (widths[r] || 0) + colspan
64
69
  }
65
- let width = 0;
66
- for (let r = 0; r < widths.length; r++) width = Math.max(width, widths[r] as number);
67
- for (let r = 0; r < widths.length; r++) {
68
- if (r >= rows.length) rows.push(Fragment.empty);
69
- if ((widths[r] as number) < width) {
70
- const empty = tableNodeTypes(schema).cell.createAndFill(),
71
- cells = [];
72
- for (let i = widths[r] as number; i < width; i++) cells.push(empty);
73
- rows[r] = (rows[r] as Fragment).append(Fragment.from(cells));
74
- }
70
+ }
71
+ let width = 0
72
+ for (let r = 0; r < widths.length; r++) width = Math.max(width, widths[r] as number)
73
+ for (let r = 0; r < widths.length; r++) {
74
+ if (r >= rows.length)
75
+ rows.push(Fragment.empty)
76
+ if ((widths[r] as number) < width) {
77
+ const empty = tableNodeTypes(schema).cell.createAndFill()
78
+ const cells = []
79
+ for (let i = widths[r] as number; i < width; i++) cells.push(empty)
80
+ rows[r] = (rows[r] as Fragment).append(Fragment.from(cells))
75
81
  }
76
- return { height: rows.length, width, rows };
82
+ }
83
+ return { height: rows.length, width, rows }
77
84
  }
78
85
 
79
86
  export function fitSlice(nodeType: NodeType, slice: Slice) {
80
- const node = nodeType.createAndFill() as Node;
81
- const tr = new Transform(node).replace(0, node.content.size, slice);
82
- return tr.doc;
87
+ const node = nodeType.createAndFill() as Node
88
+ const tr = new Transform(node).replace(0, node.content.size, slice)
89
+ return tr.doc
83
90
  }
84
91
 
85
92
  // Clip or extend (repeat) the given set of cells to cover the given
86
93
  // width and height. Will clip rowspan/colspan cells at the edges when
87
94
  // they stick out.
88
95
  export function clipCells({ width, height, rows }: R, newWidth: number, newHeight: number): R {
89
- if (width != newWidth) {
90
- const added: number[] = [],
91
- newRows: Fragment[] = [];
92
- for (let row = 0; row < rows.length; row++) {
93
- const frag = rows[row] as Fragment,
94
- cells = [];
95
- for (let col = added[row] || 0, i = 0; col < newWidth; i++) {
96
- let cell = frag.child(i % frag.childCount);
97
- if (col + cell.attrs['colspan'] > newWidth)
98
- cell = cell.type.create(
99
- removeColSpan(cell.attrs, cell.attrs['colspan'], col + cell.attrs['colspan'] - newWidth),
100
- cell.content,
101
- );
102
- cells.push(cell);
103
- col += cell.attrs['colspan'];
104
- for (let j = 1; j < cell.attrs['rowspan']; j++)
105
- added[row + j] = (added[row + j] || 0) + cell.attrs['colspan'];
106
- }
107
- newRows.push(Fragment.from(cells));
96
+ if (width != newWidth) {
97
+ const added: number[] = []
98
+ const newRows: Fragment[] = []
99
+ for (let row = 0; row < rows.length; row++) {
100
+ const frag = rows[row] as Fragment
101
+ const cells = []
102
+ for (let col = added[row] || 0, i = 0; col < newWidth; i++) {
103
+ let cell = frag.child(i % frag.childCount)
104
+ if (col + cell.attrs.colspan > newWidth) {
105
+ cell = cell.type.create(
106
+ removeColSpan(cell.attrs, cell.attrs.colspan, col + cell.attrs.colspan - newWidth),
107
+ cell.content,
108
+ )
108
109
  }
109
- rows = newRows;
110
- width = newWidth;
110
+ cells.push(cell)
111
+ col += cell.attrs.colspan
112
+ for (let j = 1; j < cell.attrs.rowspan; j++)
113
+ added[row + j] = (added[row + j] || 0) + cell.attrs.colspan
114
+ }
115
+ newRows.push(Fragment.from(cells))
111
116
  }
117
+ rows = newRows
118
+ width = newWidth
119
+ }
112
120
 
113
- if (height != newHeight) {
114
- const newRows = [];
115
- for (let row = 0, i = 0; row < newHeight; row++, i++) {
116
- const cells = [],
117
- source = rows[i % height] as Fragment;
118
- for (let j = 0; j < source.childCount; j++) {
119
- let cell = source.child(j);
120
- if (row + cell.attrs['rowspan'] > newHeight)
121
- cell = cell.type.create(
122
- setAttr(cell.attrs, 'rowspan', Math.max(1, newHeight - cell.attrs['rowspan'])),
123
- cell.content,
124
- );
125
- cells.push(cell);
126
- }
127
- newRows.push(Fragment.from(cells));
121
+ if (height != newHeight) {
122
+ const newRows = []
123
+ for (let row = 0, i = 0; row < newHeight; row++, i++) {
124
+ const cells = []
125
+ const source = rows[i % height] as Fragment
126
+ for (let j = 0; j < source.childCount; j++) {
127
+ let cell = source.child(j)
128
+ if (row + cell.attrs.rowspan > newHeight) {
129
+ cell = cell.type.create(
130
+ setAttr(cell.attrs, 'rowspan', Math.max(1, newHeight - cell.attrs.rowspan)),
131
+ cell.content,
132
+ )
128
133
  }
129
- rows = newRows;
130
- height = newHeight;
134
+ cells.push(cell)
135
+ }
136
+ newRows.push(Fragment.from(cells))
131
137
  }
138
+ rows = newRows
139
+ height = newHeight
140
+ }
132
141
 
133
- return { width, height, rows };
142
+ return { width, height, rows }
134
143
  }
135
144
 
136
145
  // Make sure a table has at least the given width and height. Return
137
146
  // true if something was changed.
138
147
  function growTable(
139
- tr: Transaction,
140
- map: TableMap,
141
- table: Node,
142
- start: number,
143
- width: number,
144
- height: number,
145
- mapFrom: number,
148
+ tr: Transaction,
149
+ map: TableMap,
150
+ table: Node,
151
+ start: number,
152
+ width: number,
153
+ height: number,
154
+ mapFrom: number,
146
155
  ) {
147
- const schema = tr.doc.type.schema,
148
- types = tableNodeTypes(schema);
149
- let empty, emptyHead;
150
- if (width > map.width) {
151
- for (let row = 0, rowEnd = 0; row < map.height; row++) {
152
- const rowNode = table.child(row);
153
- rowEnd += rowNode.nodeSize;
154
- const cells = [];
155
- let add;
156
- if (rowNode.lastChild == null || rowNode.lastChild.type == types.cell)
157
- add = empty || (empty = types.cell.createAndFill());
158
- else add = emptyHead || (emptyHead = types.header_cell.createAndFill());
159
- for (let i = map.width; i < width; i++) cells.push(add);
160
- tr.insert(tr.mapping.slice(mapFrom).map(rowEnd - 1 + start), cells);
161
- }
156
+ const schema = tr.doc.type.schema
157
+ const types = tableNodeTypes(schema)
158
+ let empty, emptyHead
159
+ if (width > map.width) {
160
+ for (let row = 0, rowEnd = 0; row < map.height; row++) {
161
+ const rowNode = table.child(row)
162
+ rowEnd += rowNode.nodeSize
163
+ const cells = []
164
+ let add
165
+ if (rowNode.lastChild == null || rowNode.lastChild.type == types.cell)
166
+ add = empty || (empty = types.cell.createAndFill())
167
+ else add = emptyHead || (emptyHead = types.header_cell.createAndFill())
168
+ for (let i = map.width; i < width; i++) cells.push(add)
169
+ tr.insert(tr.mapping.slice(mapFrom).map(rowEnd - 1 + start), cells)
162
170
  }
163
- if (height > map.height) {
164
- const cells = [];
165
- for (let i = 0, start = (map.height - 1) * map.width; i < Math.max(map.width, width); i++) {
166
- const header =
167
- i >= map.width ? false : (table.nodeAt(map.map[start + i] as number) as Node).type == types.header_cell;
168
- cells.push(
169
- header
170
- ? emptyHead || (emptyHead = types.header_cell.createAndFill())
171
- : empty || (empty = types.cell.createAndFill()),
172
- );
173
- }
174
-
175
- const emptyRow = types.row.create(null, Fragment.from(cells)),
176
- rows = [];
177
- for (let i = map.height; i < height; i++) rows.push(emptyRow);
178
- tr.insert(tr.mapping.slice(mapFrom).map(start + table.nodeSize - 2), rows);
171
+ }
172
+ if (height > map.height) {
173
+ const cells = []
174
+ for (let i = 0, start = (map.height - 1) * map.width; i < Math.max(map.width, width); i++) {
175
+ const header
176
+ = i >= map.width ? false : (table.nodeAt(map.map[start + i] as number) as Node).type == types.header_cell
177
+ cells.push(
178
+ header
179
+ ? emptyHead || (emptyHead = types.header_cell.createAndFill())
180
+ : empty || (empty = types.cell.createAndFill()),
181
+ )
179
182
  }
180
- return !!(empty || emptyHead);
183
+
184
+ const emptyRow = types.row.create(null, Fragment.from(cells))
185
+ const rows = []
186
+ for (let i = map.height; i < height; i++) rows.push(emptyRow)
187
+ tr.insert(tr.mapping.slice(mapFrom).map(start + table.nodeSize - 2), rows)
188
+ }
189
+ return !!(empty || emptyHead)
181
190
  }
182
191
 
183
192
  // Make sure the given line (left, top) to (right, top) doesn't cross
184
193
  // any rowspan cells by splitting cells that cross it. Return true if
185
194
  // something changed.
186
195
  function isolateHorizontal(
187
- tr: Transaction,
188
- map: TableMap,
189
- table: Node,
190
- start: number,
191
- left: number,
192
- right: number,
193
- top: number,
194
- mapFrom: number,
196
+ tr: Transaction,
197
+ map: TableMap,
198
+ table: Node,
199
+ start: number,
200
+ left: number,
201
+ right: number,
202
+ top: number,
203
+ mapFrom: number,
195
204
  ) {
196
- if (top == 0 || top == map.height) return false;
197
- let found = false;
198
- for (let col = left; col < right; col++) {
199
- const index = top * map.width + col,
200
- pos = map.map[index] as number;
201
- if (map.map[index - map.width] == pos) {
202
- found = true;
203
- const cell = table.nodeAt(pos) as Node;
204
- const { top: cellTop, left: cellLeft } = map.findCell(pos);
205
- tr.setNodeMarkup(
206
- tr.mapping.slice(mapFrom).map(pos + start),
207
- null,
208
- setAttr(cell.attrs, 'rowspan', top - cellTop),
209
- );
210
- tr.insert(
211
- tr.mapping.slice(mapFrom).map(map.positionAt(top, cellLeft, table)),
212
- cell.type.createAndFill(setAttr(cell.attrs, 'rowspan', cellTop + cell.attrs['rowspan'] - top)) as Node,
213
- );
214
- col += cell.attrs['colspan'] - 1;
215
- }
205
+ if (top == 0 || top == map.height)
206
+ return false
207
+ let found = false
208
+ for (let col = left; col < right; col++) {
209
+ const index = top * map.width + col
210
+ const pos = map.map[index] as number
211
+ if (map.map[index - map.width] == pos) {
212
+ found = true
213
+ const cell = table.nodeAt(pos) as Node
214
+ const { top: cellTop, left: cellLeft } = map.findCell(pos)
215
+ tr.setNodeMarkup(
216
+ tr.mapping.slice(mapFrom).map(pos + start),
217
+ null,
218
+ setAttr(cell.attrs, 'rowspan', top - cellTop),
219
+ )
220
+ tr.insert(
221
+ tr.mapping.slice(mapFrom).map(map.positionAt(top, cellLeft, table)),
222
+ cell.type.createAndFill(setAttr(cell.attrs, 'rowspan', cellTop + cell.attrs.rowspan - top)) as Node,
223
+ )
224
+ col += cell.attrs.colspan - 1
216
225
  }
217
- return found;
226
+ }
227
+ return found
218
228
  }
219
229
 
220
230
  // Make sure the given line (left, top) to (left, bottom) doesn't
221
231
  // cross any colspan cells by splitting cells that cross it. Return
222
232
  // true if something changed.
223
233
  function isolateVertical(
224
- tr: Transaction,
225
- map: TableMap,
226
- table: Node,
227
- start: number,
228
- top: number,
229
- bottom: number,
230
- left: number,
231
- mapFrom: number,
234
+ tr: Transaction,
235
+ map: TableMap,
236
+ table: Node,
237
+ start: number,
238
+ top: number,
239
+ bottom: number,
240
+ left: number,
241
+ mapFrom: number,
232
242
  ) {
233
- if (left == 0 || left == map.width) return false;
234
- let found = false;
235
- for (let row = top; row < bottom; row++) {
236
- const index = row * map.width + left,
237
- pos = map.map[index] as number;
238
- if (map.map[index - 1] == pos) {
239
- found = true;
240
- const cell = table.nodeAt(pos) as Node,
241
- cellLeft = map.colCount(pos);
242
- const updatePos = tr.mapping.slice(mapFrom).map(pos + start);
243
- tr.setNodeMarkup(
244
- updatePos,
245
- null,
246
- removeColSpan(cell.attrs, left - cellLeft, cell.attrs['colspan'] - (left - cellLeft)),
247
- );
248
- tr.insert(
249
- updatePos + cell.nodeSize,
250
- cell.type.createAndFill(removeColSpan(cell.attrs, 0, left - cellLeft)) as Node,
251
- );
252
- row += cell.attrs['rowspan'] - 1;
253
- }
243
+ if (left == 0 || left == map.width)
244
+ return false
245
+ let found = false
246
+ for (let row = top; row < bottom; row++) {
247
+ const index = row * map.width + left
248
+ const pos = map.map[index] as number
249
+ if (map.map[index - 1] == pos) {
250
+ found = true
251
+ const cell = table.nodeAt(pos) as Node
252
+ const cellLeft = map.colCount(pos)
253
+ const updatePos = tr.mapping.slice(mapFrom).map(pos + start)
254
+ tr.setNodeMarkup(
255
+ updatePos,
256
+ null,
257
+ removeColSpan(cell.attrs, left - cellLeft, cell.attrs.colspan - (left - cellLeft)),
258
+ )
259
+ tr.insert(
260
+ updatePos + cell.nodeSize,
261
+ cell.type.createAndFill(removeColSpan(cell.attrs, 0, left - cellLeft)) as Node,
262
+ )
263
+ row += cell.attrs.rowspan - 1
254
264
  }
255
- return found;
265
+ }
266
+ return found
256
267
  }
257
268
 
258
269
  // Insert the given set of cells (as returned by `pastedCells`) into a
259
270
  // table, at the position pointed at by rect.
260
271
  export function insertCells(
261
- state: EditorState,
262
- dispatch: (tr: Transaction) => void,
263
- tableStart: number,
264
- rect: Rect,
265
- cells: R,
272
+ state: EditorState,
273
+ dispatch: (tr: Transaction) => void,
274
+ tableStart: number,
275
+ rect: Rect,
276
+ cells: R,
266
277
  ) {
267
- let table = (tableStart ? state.doc.nodeAt(tableStart - 1) : state.doc) as Node,
268
- map = TableMap.get(table);
269
- const { top, left } = rect;
270
- const right = left + cells.width,
271
- bottom = top + cells.height;
272
- const tr = state.tr;
273
- let mapFrom = 0;
274
- function recomp() {
275
- table = (tableStart ? tr.doc.nodeAt(tableStart - 1) : tr.doc) as Node;
276
- map = TableMap.get(table);
277
- mapFrom = tr.mapping.maps.length;
278
- }
279
- // Prepare the table to be large enough and not have any cells
280
- // crossing the boundaries of the rectangle that we want to
281
- // insert into. If anything about it changes, recompute the table
282
- // map so that subsequent operations can see the current shape.
283
- if (growTable(tr, map, table, tableStart, right, bottom, mapFrom)) recomp();
284
- if (isolateHorizontal(tr, map, table, tableStart, left, right, top, mapFrom)) recomp();
285
- if (isolateHorizontal(tr, map, table, tableStart, left, right, bottom, mapFrom)) recomp();
286
- if (isolateVertical(tr, map, table, tableStart, top, bottom, left, mapFrom)) recomp();
287
- if (isolateVertical(tr, map, table, tableStart, top, bottom, right, mapFrom)) recomp();
278
+ let table = (tableStart ? state.doc.nodeAt(tableStart - 1) : state.doc) as Node
279
+ let map = TableMap.get(table)
280
+ const { top, left } = rect
281
+ const right = left + cells.width
282
+ const bottom = top + cells.height
283
+ const tr = state.tr
284
+ let mapFrom = 0
285
+ function recomp() {
286
+ table = (tableStart ? tr.doc.nodeAt(tableStart - 1) : tr.doc) as Node
287
+ map = TableMap.get(table)
288
+ mapFrom = tr.mapping.maps.length
289
+ }
290
+ // Prepare the table to be large enough and not have any cells
291
+ // crossing the boundaries of the rectangle that we want to
292
+ // insert into. If anything about it changes, recompute the table
293
+ // map so that subsequent operations can see the current shape.
294
+ if (growTable(tr, map, table, tableStart, right, bottom, mapFrom))
295
+ recomp()
296
+ if (isolateHorizontal(tr, map, table, tableStart, left, right, top, mapFrom))
297
+ recomp()
298
+ if (isolateHorizontal(tr, map, table, tableStart, left, right, bottom, mapFrom))
299
+ recomp()
300
+ if (isolateVertical(tr, map, table, tableStart, top, bottom, left, mapFrom))
301
+ recomp()
302
+ if (isolateVertical(tr, map, table, tableStart, top, bottom, right, mapFrom))
303
+ recomp()
288
304
 
289
- for (let row = top; row < bottom; row++) {
290
- const from = map.positionAt(row, left, table),
291
- to = map.positionAt(row, right, table);
292
- tr.replace(
293
- tr.mapping.slice(mapFrom).map(from + tableStart),
294
- tr.mapping.slice(mapFrom).map(to + tableStart),
295
- new Slice(cells.rows[row - top] as Fragment, 0, 0),
296
- );
297
- }
298
- recomp();
299
- tr.setSelection(
300
- new CellSelection(
301
- tr.doc.resolve(tableStart + map.positionAt(top, left, table)),
302
- tr.doc.resolve(tableStart + map.positionAt(bottom - 1, right - 1, table)),
303
- ),
304
- );
305
- dispatch(tr);
305
+ for (let row = top; row < bottom; row++) {
306
+ const from = map.positionAt(row, left, table)
307
+ const to = map.positionAt(row, right, table)
308
+ tr.replace(
309
+ tr.mapping.slice(mapFrom).map(from + tableStart),
310
+ tr.mapping.slice(mapFrom).map(to + tableStart),
311
+ new Slice(cells.rows[row - top] as Fragment, 0, 0),
312
+ )
313
+ }
314
+ recomp()
315
+ tr.setSelection(
316
+ new CellSelection(
317
+ tr.doc.resolve(tableStart + map.positionAt(top, left, table)),
318
+ tr.doc.resolve(tableStart + map.positionAt(bottom - 1, right - 1, table)),
319
+ ),
320
+ )
321
+ dispatch(tr)
306
322
  }