@kerebron/extension-tables 0.0.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 (65) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +34 -0
  3. package/assets/tables.css +48 -0
  4. package/esm/ExtensionTables.d.ts +10 -0
  5. package/esm/ExtensionTables.d.ts.map +1 -0
  6. package/esm/ExtensionTables.js +27 -0
  7. package/esm/NodeTable.d.ts +24 -0
  8. package/esm/NodeTable.d.ts.map +1 -0
  9. package/esm/NodeTable.js +115 -0
  10. package/esm/NodeTableCell.d.ts +16 -0
  11. package/esm/NodeTableCell.d.ts.map +1 -0
  12. package/esm/NodeTableCell.js +77 -0
  13. package/esm/NodeTableHeader.d.ts +16 -0
  14. package/esm/NodeTableHeader.d.ts.map +1 -0
  15. package/esm/NodeTableHeader.js +75 -0
  16. package/esm/NodeTableRow.d.ts +16 -0
  17. package/esm/NodeTableRow.d.ts.map +1 -0
  18. package/esm/NodeTableRow.js +47 -0
  19. package/esm/_dnt.shims.d.ts +6 -0
  20. package/esm/_dnt.shims.d.ts.map +1 -0
  21. package/esm/_dnt.shims.js +61 -0
  22. package/esm/package.json +3 -0
  23. package/esm/utilities/CellSelection.d.ts +53 -0
  24. package/esm/utilities/CellSelection.d.ts.map +1 -0
  25. package/esm/utilities/CellSelection.js +382 -0
  26. package/esm/utilities/TableMap.d.ts +92 -0
  27. package/esm/utilities/TableMap.d.ts.map +1 -0
  28. package/esm/utilities/TableMap.js +335 -0
  29. package/esm/utilities/TableView.d.ts +21 -0
  30. package/esm/utilities/TableView.d.ts.map +1 -0
  31. package/esm/utilities/TableView.js +108 -0
  32. package/esm/utilities/columnResizing.d.ts +50 -0
  33. package/esm/utilities/columnResizing.d.ts.map +1 -0
  34. package/esm/utilities/columnResizing.js +307 -0
  35. package/esm/utilities/commands.d.ts +166 -0
  36. package/esm/utilities/commands.d.ts.map +1 -0
  37. package/esm/utilities/commands.js +702 -0
  38. package/esm/utilities/copypaste.d.ts +35 -0
  39. package/esm/utilities/copypaste.d.ts.map +1 -0
  40. package/esm/utilities/copypaste.js +283 -0
  41. package/esm/utilities/createCell.d.ts +3 -0
  42. package/esm/utilities/createCell.d.ts.map +1 -0
  43. package/esm/utilities/createCell.js +6 -0
  44. package/esm/utilities/createTable.d.ts +3 -0
  45. package/esm/utilities/createTable.d.ts.map +1 -0
  46. package/esm/utilities/createTable.js +30 -0
  47. package/esm/utilities/fixTables.d.ts +18 -0
  48. package/esm/utilities/fixTables.d.ts.map +1 -0
  49. package/esm/utilities/fixTables.js +146 -0
  50. package/esm/utilities/getTableNodeTypes.d.ts +5 -0
  51. package/esm/utilities/getTableNodeTypes.d.ts.map +1 -0
  52. package/esm/utilities/getTableNodeTypes.js +14 -0
  53. package/esm/utilities/input.d.ts +21 -0
  54. package/esm/utilities/input.d.ts.map +1 -0
  55. package/esm/utilities/input.js +241 -0
  56. package/esm/utilities/tableEditing.d.ts +23 -0
  57. package/esm/utilities/tableEditing.d.ts.map +1 -0
  58. package/esm/utilities/tableEditing.js +63 -0
  59. package/esm/utilities/tableNodeTypes.d.ts +14 -0
  60. package/esm/utilities/tableNodeTypes.d.ts.map +1 -0
  61. package/esm/utilities/tableNodeTypes.js +16 -0
  62. package/esm/utilities/util.d.ts +73 -0
  63. package/esm/utilities/util.d.ts.map +1 -0
  64. package/esm/utilities/util.js +155 -0
  65. package/package.json +30 -0
@@ -0,0 +1,702 @@
1
+ // This file defines a number of table-related commands.
2
+ import { Fragment, Slice, } from 'prosemirror-model';
3
+ import { TextSelection, } from 'prosemirror-state';
4
+ import { CellSelection } from './CellSelection.js';
5
+ import { tableNodeTypes } from './tableNodeTypes.js';
6
+ import { TableMap } from './TableMap.js';
7
+ import { addColSpan, cellAround, cellWrapping, columnIsHeader, isInTable, moveCellForward, removeColSpan, selectionCell, } from './util.js';
8
+ /**
9
+ * Helper to get the selected rectangle in a table, if any. Adds table
10
+ * map, table node, and table start offset to the object for
11
+ * convenience.
12
+ *
13
+ * @public
14
+ */
15
+ export function selectedRect(state) {
16
+ const sel = state.selection;
17
+ const $pos = selectionCell(state);
18
+ const table = $pos.node(-1);
19
+ const tableStart = $pos.start(-1);
20
+ const map = TableMap.get(table);
21
+ const rect = sel instanceof CellSelection
22
+ ? map.rectBetween(sel.$anchorCell.pos - tableStart, sel.$headCell.pos - tableStart)
23
+ : map.findCell($pos.pos - tableStart);
24
+ return { ...rect, tableStart, map, table };
25
+ }
26
+ /**
27
+ * Add a column at the given position in a table.
28
+ *
29
+ * @public
30
+ */
31
+ export function addColumn(tr, { map, tableStart, table }, col) {
32
+ let refColumn = col > 0 ? -1 : 0;
33
+ if (columnIsHeader(map, table, col + refColumn)) {
34
+ refColumn = col == 0 || col == map.width ? null : 0;
35
+ }
36
+ for (let row = 0; row < map.height; row++) {
37
+ const index = row * map.width + col;
38
+ // If this position falls inside a col-spanning cell
39
+ if (col > 0 && col < map.width && map.map[index - 1] == map.map[index]) {
40
+ const pos = map.map[index];
41
+ const cell = table.nodeAt(pos);
42
+ tr.setNodeMarkup(tr.mapping.map(tableStart + pos), null, addColSpan(cell.attrs, col - map.colCount(pos)));
43
+ // Skip ahead if rowspan > 1
44
+ row += cell.attrs.rowspan - 1;
45
+ }
46
+ else {
47
+ const type = refColumn == null
48
+ ? tableNodeTypes(table.type.schema).cell
49
+ : table.nodeAt(map.map[index + refColumn]).type;
50
+ const pos = map.positionAt(row, col, table);
51
+ tr.insert(tr.mapping.map(tableStart + pos), type.createAndFill());
52
+ }
53
+ }
54
+ return tr;
55
+ }
56
+ /**
57
+ * Command to add a column before the column with the selection.
58
+ *
59
+ * @public
60
+ */
61
+ export function addColumnBefore(state, dispatch) {
62
+ if (!isInTable(state))
63
+ return false;
64
+ if (dispatch) {
65
+ const rect = selectedRect(state);
66
+ dispatch(addColumn(state.tr, rect, rect.left));
67
+ }
68
+ return true;
69
+ }
70
+ /**
71
+ * Command to add a column after the column with the selection.
72
+ *
73
+ * @public
74
+ */
75
+ export function addColumnAfter(state, dispatch) {
76
+ if (!isInTable(state))
77
+ return false;
78
+ if (dispatch) {
79
+ const rect = selectedRect(state);
80
+ dispatch(addColumn(state.tr, rect, rect.right));
81
+ }
82
+ return true;
83
+ }
84
+ /**
85
+ * @public
86
+ */
87
+ export function removeColumn(tr, { map, table, tableStart }, col) {
88
+ const mapStart = tr.mapping.maps.length;
89
+ for (let row = 0; row < map.height;) {
90
+ const index = row * map.width + col;
91
+ const pos = map.map[index];
92
+ const cell = table.nodeAt(pos);
93
+ const attrs = cell.attrs;
94
+ // If this is part of a col-spanning cell
95
+ if ((col > 0 && map.map[index - 1] == pos) ||
96
+ (col < map.width - 1 && map.map[index + 1] == pos)) {
97
+ tr.setNodeMarkup(tr.mapping.slice(mapStart).map(tableStart + pos), null, removeColSpan(attrs, col - map.colCount(pos)));
98
+ }
99
+ else {
100
+ const start = tr.mapping.slice(mapStart).map(tableStart + pos);
101
+ tr.delete(start, start + cell.nodeSize);
102
+ }
103
+ row += attrs.rowspan;
104
+ }
105
+ }
106
+ /**
107
+ * Command function that removes the selected columns from a table.
108
+ *
109
+ * @public
110
+ */
111
+ export function deleteColumn(state, dispatch) {
112
+ if (!isInTable(state))
113
+ return false;
114
+ if (dispatch) {
115
+ const rect = selectedRect(state);
116
+ const tr = state.tr;
117
+ if (rect.left == 0 && rect.right == rect.map.width)
118
+ return false;
119
+ for (let i = rect.right - 1;; i--) {
120
+ removeColumn(tr, rect, i);
121
+ if (i == rect.left)
122
+ break;
123
+ const table = rect.tableStart
124
+ ? tr.doc.nodeAt(rect.tableStart - 1)
125
+ : tr.doc;
126
+ if (!table) {
127
+ throw RangeError('No table found');
128
+ }
129
+ rect.table = table;
130
+ rect.map = TableMap.get(table);
131
+ }
132
+ dispatch(tr);
133
+ }
134
+ return true;
135
+ }
136
+ /**
137
+ * @public
138
+ */
139
+ export function rowIsHeader(map, table, row) {
140
+ const headerCell = tableNodeTypes(table.type.schema).header_cell;
141
+ for (let col = 0; col < map.width; col++) {
142
+ if (table.nodeAt(map.map[col + row * map.width])?.type != headerCell) {
143
+ return false;
144
+ }
145
+ }
146
+ return true;
147
+ }
148
+ /**
149
+ * @public
150
+ */
151
+ export function addRow(tr, { map, tableStart, table }, row) {
152
+ let rowPos = tableStart;
153
+ for (let i = 0; i < row; i++)
154
+ rowPos += table.child(i).nodeSize;
155
+ const cells = [];
156
+ let refRow = row > 0 ? -1 : 0;
157
+ if (rowIsHeader(map, table, row + refRow)) {
158
+ refRow = row == 0 || row == map.height ? null : 0;
159
+ }
160
+ for (let col = 0, index = map.width * row; col < map.width; col++, index++) {
161
+ // Covered by a rowspan cell
162
+ if (row > 0 &&
163
+ row < map.height &&
164
+ map.map[index] == map.map[index - map.width]) {
165
+ const pos = map.map[index];
166
+ const attrs = table.nodeAt(pos).attrs;
167
+ tr.setNodeMarkup(tableStart + pos, null, {
168
+ ...attrs,
169
+ rowspan: attrs.rowspan + 1,
170
+ });
171
+ col += attrs.colspan - 1;
172
+ }
173
+ else {
174
+ const type = refRow == null
175
+ ? tableNodeTypes(table.type.schema).cell
176
+ : table.nodeAt(map.map[index + refRow * map.width])?.type;
177
+ const node = type?.createAndFill();
178
+ if (node)
179
+ cells.push(node);
180
+ }
181
+ }
182
+ tr.insert(rowPos, tableNodeTypes(table.type.schema).row.create(null, cells));
183
+ return tr;
184
+ }
185
+ /**
186
+ * Add a table row before the selection.
187
+ *
188
+ * @public
189
+ */
190
+ export function addRowBefore(state, dispatch) {
191
+ if (!isInTable(state))
192
+ return false;
193
+ if (dispatch) {
194
+ const rect = selectedRect(state);
195
+ dispatch(addRow(state.tr, rect, rect.top));
196
+ }
197
+ return true;
198
+ }
199
+ /**
200
+ * Add a table row after the selection.
201
+ *
202
+ * @public
203
+ */
204
+ export function addRowAfter(state, dispatch) {
205
+ if (!isInTable(state))
206
+ return false;
207
+ if (dispatch) {
208
+ const rect = selectedRect(state);
209
+ dispatch(addRow(state.tr, rect, rect.bottom));
210
+ }
211
+ return true;
212
+ }
213
+ /**
214
+ * @public
215
+ */
216
+ export function removeRow(tr, { map, table, tableStart }, row) {
217
+ let rowPos = 0;
218
+ for (let i = 0; i < row; i++)
219
+ rowPos += table.child(i).nodeSize;
220
+ const nextRow = rowPos + table.child(row).nodeSize;
221
+ const mapFrom = tr.mapping.maps.length;
222
+ tr.delete(rowPos + tableStart, nextRow + tableStart);
223
+ const seen = new Set();
224
+ for (let col = 0, index = row * map.width; col < map.width; col++, index++) {
225
+ const pos = map.map[index];
226
+ // Skip cells that are checked already
227
+ if (seen.has(pos))
228
+ continue;
229
+ seen.add(pos);
230
+ if (row > 0 && pos == map.map[index - map.width]) {
231
+ // If this cell starts in the row above, simply reduce its rowspan
232
+ const attrs = table.nodeAt(pos).attrs;
233
+ tr.setNodeMarkup(tr.mapping.slice(mapFrom).map(pos + tableStart), null, {
234
+ ...attrs,
235
+ rowspan: attrs.rowspan - 1,
236
+ });
237
+ col += attrs.colspan - 1;
238
+ }
239
+ else if (row < map.height && pos == map.map[index + map.width]) {
240
+ // Else, if it continues in the row below, it has to be moved down
241
+ const cell = table.nodeAt(pos);
242
+ const attrs = cell.attrs;
243
+ const copy = cell.type.create({ ...attrs, rowspan: cell.attrs.rowspan - 1 }, cell.content);
244
+ const newPos = map.positionAt(row + 1, col, table);
245
+ tr.insert(tr.mapping.slice(mapFrom).map(tableStart + newPos), copy);
246
+ col += attrs.colspan - 1;
247
+ }
248
+ }
249
+ }
250
+ /**
251
+ * Remove the selected rows from a table.
252
+ *
253
+ * @public
254
+ */
255
+ export function deleteRow(state, dispatch) {
256
+ if (!isInTable(state))
257
+ return false;
258
+ if (dispatch) {
259
+ const rect = selectedRect(state), tr = state.tr;
260
+ if (rect.top == 0 && rect.bottom == rect.map.height)
261
+ return false;
262
+ for (let i = rect.bottom - 1;; i--) {
263
+ removeRow(tr, rect, i);
264
+ if (i == rect.top)
265
+ break;
266
+ const table = rect.tableStart
267
+ ? tr.doc.nodeAt(rect.tableStart - 1)
268
+ : tr.doc;
269
+ if (!table) {
270
+ throw RangeError('No table found');
271
+ }
272
+ rect.table = table;
273
+ rect.map = TableMap.get(rect.table);
274
+ }
275
+ dispatch(tr);
276
+ }
277
+ return true;
278
+ }
279
+ function isEmpty(cell) {
280
+ const c = cell.content;
281
+ return (c.childCount == 1 && c.child(0).isTextblock && c.child(0).childCount == 0);
282
+ }
283
+ function cellsOverlapRectangle({ width, height, map }, rect) {
284
+ let indexTop = rect.top * width + rect.left, indexLeft = indexTop;
285
+ let indexBottom = (rect.bottom - 1) * width + rect.left, indexRight = indexTop + (rect.right - rect.left - 1);
286
+ for (let i = rect.top; i < rect.bottom; i++) {
287
+ if ((rect.left > 0 && map[indexLeft] == map[indexLeft - 1]) ||
288
+ (rect.right < width && map[indexRight] == map[indexRight + 1])) {
289
+ return true;
290
+ }
291
+ indexLeft += width;
292
+ indexRight += width;
293
+ }
294
+ for (let i = rect.left; i < rect.right; i++) {
295
+ if ((rect.top > 0 && map[indexTop] == map[indexTop - width]) ||
296
+ (rect.bottom < height && map[indexBottom] == map[indexBottom + width])) {
297
+ return true;
298
+ }
299
+ indexTop++;
300
+ indexBottom++;
301
+ }
302
+ return false;
303
+ }
304
+ /**
305
+ * Merge the selected cells into a single cell. Only available when
306
+ * the selected cells' outline forms a rectangle.
307
+ *
308
+ * @public
309
+ */
310
+ export function mergeCells(state, dispatch) {
311
+ const sel = state.selection;
312
+ if (!(sel instanceof CellSelection) ||
313
+ sel.$anchorCell.pos == sel.$headCell.pos) {
314
+ return false;
315
+ }
316
+ const rect = selectedRect(state), { map } = rect;
317
+ if (cellsOverlapRectangle(map, rect))
318
+ return false;
319
+ if (dispatch) {
320
+ const tr = state.tr;
321
+ const seen = {};
322
+ let content = Fragment.empty;
323
+ let mergedPos;
324
+ let mergedCell;
325
+ for (let row = rect.top; row < rect.bottom; row++) {
326
+ for (let col = rect.left; col < rect.right; col++) {
327
+ const cellPos = map.map[row * map.width + col];
328
+ const cell = rect.table.nodeAt(cellPos);
329
+ if (seen[cellPos] || !cell)
330
+ continue;
331
+ seen[cellPos] = true;
332
+ if (mergedPos == null) {
333
+ mergedPos = cellPos;
334
+ mergedCell = cell;
335
+ }
336
+ else {
337
+ if (!isEmpty(cell))
338
+ content = content.append(cell.content);
339
+ const mapped = tr.mapping.map(cellPos + rect.tableStart);
340
+ tr.delete(mapped, mapped + cell.nodeSize);
341
+ }
342
+ }
343
+ }
344
+ if (mergedPos == null || mergedCell == null) {
345
+ return true;
346
+ }
347
+ tr.setNodeMarkup(mergedPos + rect.tableStart, null, {
348
+ ...addColSpan(mergedCell.attrs, mergedCell.attrs.colspan, rect.right - rect.left - mergedCell.attrs.colspan),
349
+ rowspan: rect.bottom - rect.top,
350
+ });
351
+ if (content.size) {
352
+ const end = mergedPos + 1 + mergedCell.content.size;
353
+ const start = isEmpty(mergedCell) ? mergedPos + 1 : end;
354
+ tr.replaceWith(start + rect.tableStart, end + rect.tableStart, content);
355
+ }
356
+ tr.setSelection(new CellSelection(tr.doc.resolve(mergedPos + rect.tableStart)));
357
+ dispatch(tr);
358
+ }
359
+ return true;
360
+ }
361
+ /**
362
+ * Split a selected cell, whose rowpan or colspan is greater than one,
363
+ * into smaller cells. Use the first cell type for the new cells.
364
+ *
365
+ * @public
366
+ */
367
+ export function splitCell(state, dispatch) {
368
+ const nodeTypes = tableNodeTypes(state.schema);
369
+ return splitCellWithType(({ node }) => {
370
+ return nodeTypes[node.type.spec.tableRole];
371
+ })(state, dispatch);
372
+ }
373
+ /**
374
+ * Split a selected cell, whose rowpan or colspan is greater than one,
375
+ * into smaller cells with the cell type (th, td) returned by getType function.
376
+ *
377
+ * @public
378
+ */
379
+ export function splitCellWithType(getCellType) {
380
+ return (state, dispatch) => {
381
+ const sel = state.selection;
382
+ let cellNode;
383
+ let cellPos;
384
+ if (!(sel instanceof CellSelection)) {
385
+ cellNode = cellWrapping(sel.$from);
386
+ if (!cellNode)
387
+ return false;
388
+ cellPos = cellAround(sel.$from)?.pos;
389
+ }
390
+ else {
391
+ if (sel.$anchorCell.pos != sel.$headCell.pos)
392
+ return false;
393
+ cellNode = sel.$anchorCell.nodeAfter;
394
+ cellPos = sel.$anchorCell.pos;
395
+ }
396
+ if (cellNode == null || cellPos == null) {
397
+ return false;
398
+ }
399
+ if (cellNode.attrs.colspan == 1 && cellNode.attrs.rowspan == 1) {
400
+ return false;
401
+ }
402
+ if (dispatch) {
403
+ let baseAttrs = cellNode.attrs;
404
+ const attrs = [];
405
+ const colwidth = baseAttrs.colwidth;
406
+ if (baseAttrs.rowspan > 1)
407
+ baseAttrs = { ...baseAttrs, rowspan: 1 };
408
+ if (baseAttrs.colspan > 1)
409
+ baseAttrs = { ...baseAttrs, colspan: 1 };
410
+ const rect = selectedRect(state), tr = state.tr;
411
+ for (let i = 0; i < rect.right - rect.left; i++) {
412
+ attrs.push(colwidth
413
+ ? {
414
+ ...baseAttrs,
415
+ colwidth: colwidth && colwidth[i] ? [colwidth[i]] : null,
416
+ }
417
+ : baseAttrs);
418
+ }
419
+ let lastCell;
420
+ for (let row = rect.top; row < rect.bottom; row++) {
421
+ let pos = rect.map.positionAt(row, rect.left, rect.table);
422
+ if (row == rect.top)
423
+ pos += cellNode.nodeSize;
424
+ for (let col = rect.left, i = 0; col < rect.right; col++, i++) {
425
+ if (col == rect.left && row == rect.top)
426
+ continue;
427
+ tr.insert(lastCell = tr.mapping.map(pos + rect.tableStart, 1), getCellType({ node: cellNode, row, col }).createAndFill(attrs[i]));
428
+ }
429
+ }
430
+ tr.setNodeMarkup(cellPos, getCellType({ node: cellNode, row: rect.top, col: rect.left }), attrs[0]);
431
+ if (sel instanceof CellSelection) {
432
+ tr.setSelection(new CellSelection(tr.doc.resolve(sel.$anchorCell.pos), lastCell ? tr.doc.resolve(lastCell) : undefined));
433
+ }
434
+ dispatch(tr);
435
+ }
436
+ return true;
437
+ };
438
+ }
439
+ /**
440
+ * Returns a command that sets the given attribute to the given value,
441
+ * and is only available when the currently selected cell doesn't
442
+ * already have that attribute set to that value.
443
+ *
444
+ * @public
445
+ */
446
+ export function setCellAttr(name, value) {
447
+ return function (state, dispatch) {
448
+ if (!isInTable(state))
449
+ return false;
450
+ const $cell = selectionCell(state);
451
+ if ($cell.nodeAfter.attrs[name] === value)
452
+ return false;
453
+ if (dispatch) {
454
+ const tr = state.tr;
455
+ if (state.selection instanceof CellSelection) {
456
+ state.selection.forEachCell((node, pos) => {
457
+ if (node.attrs[name] !== value) {
458
+ tr.setNodeMarkup(pos, null, {
459
+ ...node.attrs,
460
+ [name]: value,
461
+ });
462
+ }
463
+ });
464
+ }
465
+ else {
466
+ tr.setNodeMarkup($cell.pos, null, {
467
+ ...$cell.nodeAfter.attrs,
468
+ [name]: value,
469
+ });
470
+ }
471
+ dispatch(tr);
472
+ }
473
+ return true;
474
+ };
475
+ }
476
+ function deprecated_toggleHeader(type) {
477
+ return function (state, dispatch) {
478
+ if (!isInTable(state))
479
+ return false;
480
+ if (dispatch) {
481
+ const types = tableNodeTypes(state.schema);
482
+ const rect = selectedRect(state), tr = state.tr;
483
+ const cells = rect.map.cellsInRect(type == 'column'
484
+ ? {
485
+ left: rect.left,
486
+ top: 0,
487
+ right: rect.right,
488
+ bottom: rect.map.height,
489
+ }
490
+ : type == 'row'
491
+ ? {
492
+ left: 0,
493
+ top: rect.top,
494
+ right: rect.map.width,
495
+ bottom: rect.bottom,
496
+ }
497
+ : rect);
498
+ const nodes = cells.map((pos) => rect.table.nodeAt(pos));
499
+ for (let i = 0; i < cells.length; i++ // Remove headers, if any
500
+ ) {
501
+ if (nodes[i].type == types.header_cell) {
502
+ tr.setNodeMarkup(rect.tableStart + cells[i], types.cell, nodes[i].attrs);
503
+ }
504
+ }
505
+ if (tr.steps.length == 0) {
506
+ for (let i = 0; i < cells.length; i++ // No headers removed, add instead
507
+ ) {
508
+ tr.setNodeMarkup(rect.tableStart + cells[i], types.header_cell, nodes[i].attrs);
509
+ }
510
+ }
511
+ dispatch(tr);
512
+ }
513
+ return true;
514
+ };
515
+ }
516
+ function isHeaderEnabledByType(type, rect, types) {
517
+ // Get cell positions for first row or first column
518
+ const cellPositions = rect.map.cellsInRect({
519
+ left: 0,
520
+ top: 0,
521
+ right: type == 'row' ? rect.map.width : 1,
522
+ bottom: type == 'column' ? rect.map.height : 1,
523
+ });
524
+ for (let i = 0; i < cellPositions.length; i++) {
525
+ const cell = rect.table.nodeAt(cellPositions[i]);
526
+ if (cell && cell.type !== types.header_cell) {
527
+ return false;
528
+ }
529
+ }
530
+ return true;
531
+ }
532
+ /**
533
+ * Toggles between row/column header and normal cells (Only applies to first row/column).
534
+ * For deprecated behavior pass `useDeprecatedLogic` in options with true.
535
+ *
536
+ * @public
537
+ */
538
+ export function toggleHeader(type, options) {
539
+ options = options || { useDeprecatedLogic: false };
540
+ if (options.useDeprecatedLogic)
541
+ return deprecated_toggleHeader(type);
542
+ return function (state, dispatch) {
543
+ if (!isInTable(state))
544
+ return false;
545
+ if (dispatch) {
546
+ const types = tableNodeTypes(state.schema);
547
+ const rect = selectedRect(state), tr = state.tr;
548
+ const isHeaderRowEnabled = isHeaderEnabledByType('row', rect, types);
549
+ const isHeaderColumnEnabled = isHeaderEnabledByType('column', rect, types);
550
+ const isHeaderEnabled = type === 'column'
551
+ ? isHeaderRowEnabled
552
+ : type === 'row'
553
+ ? isHeaderColumnEnabled
554
+ : false;
555
+ const selectionStartsAt = isHeaderEnabled ? 1 : 0;
556
+ const cellsRect = type == 'column'
557
+ ? {
558
+ left: 0,
559
+ top: selectionStartsAt,
560
+ right: 1,
561
+ bottom: rect.map.height,
562
+ }
563
+ : type == 'row'
564
+ ? {
565
+ left: selectionStartsAt,
566
+ top: 0,
567
+ right: rect.map.width,
568
+ bottom: 1,
569
+ }
570
+ : rect;
571
+ const newType = type == 'column'
572
+ ? isHeaderColumnEnabled ? types.cell : types.header_cell
573
+ : type == 'row'
574
+ ? isHeaderRowEnabled ? types.cell : types.header_cell
575
+ : types.cell;
576
+ rect.map.cellsInRect(cellsRect).forEach((relativeCellPos) => {
577
+ const cellPos = relativeCellPos + rect.tableStart;
578
+ const cell = tr.doc.nodeAt(cellPos);
579
+ if (cell) {
580
+ tr.setNodeMarkup(cellPos, newType, cell.attrs);
581
+ }
582
+ });
583
+ dispatch(tr);
584
+ }
585
+ return true;
586
+ };
587
+ }
588
+ /**
589
+ * Toggles whether the selected row contains header cells.
590
+ *
591
+ * @public
592
+ */
593
+ export const toggleHeaderRow = toggleHeader('row', {
594
+ useDeprecatedLogic: true,
595
+ });
596
+ /**
597
+ * Toggles whether the selected column contains header cells.
598
+ *
599
+ * @public
600
+ */
601
+ export const toggleHeaderColumn = toggleHeader('column', {
602
+ useDeprecatedLogic: true,
603
+ });
604
+ /**
605
+ * Toggles whether the selected cells are header cells.
606
+ *
607
+ * @public
608
+ */
609
+ export const toggleHeaderCell = toggleHeader('cell', {
610
+ useDeprecatedLogic: true,
611
+ });
612
+ function findNextCell($cell, dir) {
613
+ if (dir < 0) {
614
+ const before = $cell.nodeBefore;
615
+ if (before)
616
+ return $cell.pos - before.nodeSize;
617
+ for (let row = $cell.index(-1) - 1, rowEnd = $cell.before(); row >= 0; row--) {
618
+ const rowNode = $cell.node(-1).child(row);
619
+ const lastChild = rowNode.lastChild;
620
+ if (lastChild) {
621
+ return rowEnd - 1 - lastChild.nodeSize;
622
+ }
623
+ rowEnd -= rowNode.nodeSize;
624
+ }
625
+ }
626
+ else {
627
+ if ($cell.index() < $cell.parent.childCount - 1) {
628
+ return $cell.pos + $cell.nodeAfter.nodeSize;
629
+ }
630
+ const table = $cell.node(-1);
631
+ for (let row = $cell.indexAfter(-1), rowStart = $cell.after(); row < table.childCount; row++) {
632
+ const rowNode = table.child(row);
633
+ if (rowNode.childCount)
634
+ return rowStart + 1;
635
+ rowStart += rowNode.nodeSize;
636
+ }
637
+ }
638
+ return null;
639
+ }
640
+ /**
641
+ * Returns a command for selecting the next (direction=1) or previous
642
+ * (direction=-1) cell in a table.
643
+ *
644
+ * @public
645
+ */
646
+ export function goToNextCell(direction) {
647
+ return function (state, dispatch) {
648
+ if (!isInTable(state))
649
+ return false;
650
+ const cell = findNextCell(selectionCell(state), direction);
651
+ if (cell == null)
652
+ return false;
653
+ if (dispatch) {
654
+ const $cell = state.doc.resolve(cell);
655
+ dispatch(state.tr
656
+ .setSelection(TextSelection.between($cell, moveCellForward($cell)))
657
+ .scrollIntoView());
658
+ }
659
+ return true;
660
+ };
661
+ }
662
+ /**
663
+ * Deletes the table around the selection, if any.
664
+ *
665
+ * @public
666
+ */
667
+ export function deleteTable(state, dispatch) {
668
+ const $pos = state.selection.$anchor;
669
+ for (let d = $pos.depth; d > 0; d--) {
670
+ const node = $pos.node(d);
671
+ if (node.type.spec.tableRole == 'table') {
672
+ if (dispatch) {
673
+ dispatch(state.tr.delete($pos.before(d), $pos.after(d)).scrollIntoView());
674
+ }
675
+ return true;
676
+ }
677
+ }
678
+ return false;
679
+ }
680
+ /**
681
+ * Deletes the content of the selected cells, if they are not empty.
682
+ *
683
+ * @public
684
+ */
685
+ export function deleteCellSelection(state, dispatch) {
686
+ const sel = state.selection;
687
+ if (!(sel instanceof CellSelection))
688
+ return false;
689
+ if (dispatch) {
690
+ const tr = state.tr;
691
+ const baseContent = tableNodeTypes(state.schema).cell.createAndFill()
692
+ .content;
693
+ sel.forEachCell((cell, pos) => {
694
+ if (!cell.content.eq(baseContent)) {
695
+ tr.replace(tr.mapping.map(pos + 1), tr.mapping.map(pos + cell.nodeSize - 1), new Slice(baseContent, 0, 0));
696
+ }
697
+ });
698
+ if (tr.docChanged)
699
+ dispatch(tr);
700
+ }
701
+ return true;
702
+ }