@milkdown/preset-gfm 7.15.1 → 7.15.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 (65) hide show
  1. package/lib/index.js +265 -205
  2. package/lib/index.js.map +1 -1
  3. package/lib/mark/strike-through.d.ts.map +1 -1
  4. package/lib/node/table/command.d.ts.map +1 -1
  5. package/lib/node/table/utils/add-row-with-alignment.d.ts +5 -0
  6. package/lib/node/table/utils/add-row-with-alignment.d.ts.map +1 -0
  7. package/lib/node/table/utils/convert-rows-to-table.d.ts +3 -0
  8. package/lib/node/table/utils/convert-rows-to-table.d.ts.map +1 -0
  9. package/lib/node/table/utils/convert-table-to-rows.d.ts +3 -0
  10. package/lib/node/table/utils/convert-table-to-rows.d.ts.map +1 -0
  11. package/lib/node/table/utils/create-table.d.ts +4 -0
  12. package/lib/node/table/utils/create-table.d.ts.map +1 -0
  13. package/lib/node/table/utils/find-table.d.ts +3 -0
  14. package/lib/node/table/utils/find-table.d.ts.map +1 -0
  15. package/lib/node/table/utils/get-all-cells-in-table.d.ts +7 -0
  16. package/lib/node/table/utils/get-all-cells-in-table.d.ts.map +1 -0
  17. package/lib/node/table/utils/get-cells-in-col.d.ts +4 -0
  18. package/lib/node/table/utils/get-cells-in-col.d.ts.map +1 -0
  19. package/lib/node/table/utils/get-cells-in-row.d.ts +4 -0
  20. package/lib/node/table/utils/get-cells-in-row.d.ts.map +1 -0
  21. package/lib/node/table/utils/get-selection-range-in-col.d.ts +4 -0
  22. package/lib/node/table/utils/get-selection-range-in-col.d.ts.map +1 -0
  23. package/lib/node/table/utils/get-selection-range-in-row.d.ts +4 -0
  24. package/lib/node/table/utils/get-selection-range-in-row.d.ts.map +1 -0
  25. package/lib/node/table/utils/index.d.ts +12 -0
  26. package/lib/node/table/utils/index.d.ts.map +1 -0
  27. package/lib/node/table/utils/move-col.d.ts +10 -0
  28. package/lib/node/table/utils/move-col.d.ts.map +1 -0
  29. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts +3 -0
  30. package/lib/node/table/utils/move-row-in-array-of-rows.d.ts.map +1 -0
  31. package/lib/node/table/utils/move-row.d.ts +10 -0
  32. package/lib/node/table/utils/move-row.d.ts.map +1 -0
  33. package/lib/node/table/utils/select-line.d.ts +5 -0
  34. package/lib/node/table/utils/select-line.d.ts.map +1 -0
  35. package/lib/node/table/utils/select-table.d.ts +3 -0
  36. package/lib/node/table/utils/select-table.d.ts.map +1 -0
  37. package/lib/node/table/utils/transpose.d.ts +2 -0
  38. package/lib/node/table/utils/transpose.d.ts.map +1 -0
  39. package/lib/node/table/utils/types.d.ts +12 -0
  40. package/lib/node/table/utils/types.d.ts.map +1 -0
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +8 -8
  43. package/src/mark/strike-through.ts +4 -1
  44. package/src/node/table/command.ts +28 -10
  45. package/src/node/table/utils/add-row-with-alignment.ts +32 -0
  46. package/src/node/table/utils/convert-rows-to-table.ts +43 -0
  47. package/src/node/table/utils/convert-table-to-rows.ts +65 -0
  48. package/src/node/table/utils/create-table.ts +31 -0
  49. package/src/node/table/utils/find-table.ts +10 -0
  50. package/src/node/table/utils/get-all-cells-in-table.ts +24 -0
  51. package/src/node/table/utils/get-cells-in-col.ts +35 -0
  52. package/src/node/table/utils/get-cells-in-row.ts +37 -0
  53. package/src/node/table/utils/get-selection-range-in-col.ts +86 -0
  54. package/src/node/table/utils/get-selection-range-in-row.ts +86 -0
  55. package/src/node/table/utils/index.ts +11 -0
  56. package/src/node/table/utils/move-col.ts +73 -0
  57. package/src/node/table/utils/move-row-in-array-of-rows.ts +29 -0
  58. package/src/node/table/utils/move-row.ts +71 -0
  59. package/src/node/table/utils/select-line.ts +61 -0
  60. package/src/node/table/utils/select-table.ts +20 -0
  61. package/src/node/table/utils/transpose.ts +26 -0
  62. package/src/node/table/utils/types.ts +16 -0
  63. package/lib/node/table/utils.d.ts +0 -41
  64. package/lib/node/table/utils.d.ts.map +0 -1
  65. package/src/node/table/utils.ts +0 -564
package/lib/index.js CHANGED
@@ -66,7 +66,10 @@ withMeta(toggleStrikethroughCommand, {
66
66
  group: "Strikethrough"
67
67
  });
68
68
  const strikethroughInputRule = $inputRule((ctx) => {
69
- return markRule(/~([^~]+)~$/, strikethroughSchema.type(ctx));
69
+ return markRule(
70
+ new RegExp("(?<![\\w:/])(~{1,2})(.+?)\\1(?!\\w|\\/)"),
71
+ strikethroughSchema.type(ctx)
72
+ );
70
73
  });
71
74
  withMeta(strikethroughInputRule, {
72
75
  displayName: "InputRule<strikethrough>",
@@ -288,86 +291,45 @@ function findTable($pos) {
288
291
  (node) => node.type.spec.tableRole === "table"
289
292
  )($pos);
290
293
  }
291
- function getCellsInCol(columnIndex, selection) {
294
+ function getCellsInCol(columnIndexes, selection) {
292
295
  const table = findTable(selection.$from);
293
296
  if (!table) return void 0;
294
297
  const map = TableMap.get(table.node);
295
- if (columnIndex < 0 || columnIndex >= map.width) return void 0;
296
- return map.cellsInRect({
297
- left: columnIndex,
298
- right: columnIndex + 1,
299
- top: 0,
300
- bottom: map.height
301
- }).map((pos) => {
302
- const node = table.node.nodeAt(pos);
303
- if (!node) return void 0;
304
- const start = pos + table.start;
305
- return {
306
- pos: start,
307
- start: start + 1,
308
- node
309
- };
310
- }).filter((x) => x != null);
298
+ const indexes = Array.isArray(columnIndexes) ? columnIndexes : [columnIndexes];
299
+ return indexes.filter((index) => index >= 0 && index <= map.width - 1).flatMap((index) => {
300
+ const cells = map.cellsInRect({
301
+ left: index,
302
+ right: index + 1,
303
+ top: 0,
304
+ bottom: map.height
305
+ });
306
+ return cells.map((nodePos) => {
307
+ const node = table.node.nodeAt(nodePos);
308
+ const pos = nodePos + table.start;
309
+ return { pos, start: pos + 1, node, depth: table.depth + 2 };
310
+ });
311
+ });
311
312
  }
312
313
  function getCellsInRow(rowIndex, selection) {
313
314
  const table = findTable(selection.$from);
314
- if (!table) return void 0;
315
- const map = TableMap.get(table.node);
316
- if (rowIndex < 0 || rowIndex >= map.height) return void 0;
317
- return map.cellsInRect({
318
- left: 0,
319
- right: map.width,
320
- top: rowIndex,
321
- bottom: rowIndex + 1
322
- }).map((pos) => {
323
- const node = table.node.nodeAt(pos);
324
- if (!node) return void 0;
325
- const start = pos + table.start;
326
- return {
327
- pos: start,
328
- start: start + 1,
329
- node
330
- };
331
- }).filter((x) => x != null);
332
- }
333
- function getAllCellsInTable(selection) {
334
- const table = findTable(selection.$from);
335
- if (!table) return;
336
- const map = TableMap.get(table.node);
337
- const cells = map.cellsInRect({
338
- left: 0,
339
- right: map.width,
340
- top: 0,
341
- bottom: map.height
342
- });
343
- return cells.map((nodePos) => {
344
- const node = table.node.nodeAt(nodePos);
345
- const pos = nodePos + table.start;
346
- return { pos, start: pos + 1, node };
347
- });
348
- }
349
- function selectTable(tr) {
350
- const cells = getAllCellsInTable(tr.selection);
351
- if (cells && cells[0]) {
352
- const $firstCell = tr.doc.resolve(cells[0].pos);
353
- const last = cells[cells.length - 1];
354
- if (last) {
355
- const $lastCell = tr.doc.resolve(last.pos);
356
- return cloneTr(tr.setSelection(new CellSelection($lastCell, $firstCell)));
357
- }
315
+ if (!table) {
316
+ return;
358
317
  }
359
- return tr;
360
- }
361
- function addRowWithAlignment(ctx, tr, { map, tableStart, table }, row) {
362
- const rowPos = Array(row).fill(0).reduce((acc, _, i) => {
363
- return acc + table.child(i).nodeSize;
364
- }, tableStart);
365
- const cells = Array(map.width).fill(0).map((_, col) => {
366
- const headerCol = table.nodeAt(map.map[col]);
367
- return tableCellSchema.type(ctx).createAndFill({ alignment: headerCol?.attrs.alignment });
318
+ const map = TableMap.get(table.node);
319
+ const indexes = Array.isArray(rowIndex) ? rowIndex : [rowIndex];
320
+ return indexes.filter((index) => index >= 0 && index <= map.height - 1).flatMap((index) => {
321
+ const cells = map.cellsInRect({
322
+ left: 0,
323
+ right: map.width,
324
+ top: index,
325
+ bottom: index + 1
326
+ });
327
+ return cells.map((nodePos) => {
328
+ const node = table.node.nodeAt(nodePos);
329
+ const pos = nodePos + table.start;
330
+ return { pos, start: pos + 1, node, depth: table.depth + 2 };
331
+ });
368
332
  });
369
- tr.insert(rowPos, tableRowSchema.type(ctx).create(null, cells));
370
- return tr;
371
333
  }
372
334
  function selectLine(type) {
373
335
  return (index, pos) => (tr) => {
@@ -409,12 +371,18 @@ function selectLine(type) {
409
371
  }
410
372
  const selectRow = selectLine("row");
411
373
  const selectCol = selectLine("col");
412
- function transpose(array) {
413
- return array[0].map((_, i) => {
414
- return array.map((column) => column[i]);
374
+ function addRowWithAlignment(ctx, tr, { map, tableStart, table }, row) {
375
+ const rowPos = Array(row).fill(0).reduce((acc, _, i) => {
376
+ return acc + table.child(i).nodeSize;
377
+ }, tableStart);
378
+ const cells = Array(map.width).fill(0).map((_, col) => {
379
+ const headerCol = table.nodeAt(map.map[col]);
380
+ return tableCellSchema.type(ctx).createAndFill({ alignment: headerCol?.attrs.alignment });
415
381
  });
382
+ tr.insert(rowPos, tableRowSchema.type(ctx).create(null, cells));
383
+ return tr;
416
384
  }
417
- function convertArrayOfRowsToTableNode(tableNode, arrayOfNodes) {
385
+ function convertRowsToTable(tableNode, arrayOfNodes) {
418
386
  const rowsPM = [];
419
387
  const map = TableMap.get(tableNode);
420
388
  for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
@@ -441,27 +409,108 @@ function convertArrayOfRowsToTableNode(tableNode, arrayOfNodes) {
441
409
  );
442
410
  return newTable;
443
411
  }
444
- function convertTableNodeToArrayOfRows(tableNode) {
412
+ function convertTableToRows(tableNode) {
445
413
  const map = TableMap.get(tableNode);
446
414
  const rows = [];
447
- for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
448
- const rowCells = [];
449
- const seen = {};
450
- for (let colIndex = 0; colIndex < map.width; colIndex++) {
451
- const cellPos = map.map[rowIndex * map.width + colIndex];
452
- const cell = tableNode.nodeAt(cellPos);
453
- const rect = map.findCell(cellPos);
454
- if (seen[cellPos] || rect.top !== rowIndex) {
455
- rowCells.push(null);
456
- continue;
415
+ const rowCount = map.height;
416
+ const colCount = map.width;
417
+ for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
418
+ const row = [];
419
+ for (let colIndex = 0; colIndex < colCount; colIndex++) {
420
+ let cellIndex = rowIndex * colCount + colIndex;
421
+ let cellPos = map.map[cellIndex];
422
+ if (rowIndex > 0) {
423
+ const topCellIndex = cellIndex - colCount;
424
+ const topCellPos = map.map[topCellIndex];
425
+ if (cellPos === topCellPos) {
426
+ row.push(null);
427
+ continue;
428
+ }
429
+ }
430
+ if (colIndex > 0) {
431
+ const leftCellIndex = cellIndex - 1;
432
+ const leftCellPos = map.map[leftCellIndex];
433
+ if (cellPos === leftCellPos) {
434
+ row.push(null);
435
+ continue;
436
+ }
437
+ }
438
+ if (!cellPos) {
439
+ row.push(null);
440
+ } else {
441
+ row.push(tableNode.nodeAt(cellPos));
457
442
  }
458
- seen[cellPos] = true;
459
- rowCells.push(cell);
460
443
  }
461
- rows.push(rowCells);
444
+ rows.push(row);
462
445
  }
463
446
  return rows;
464
447
  }
448
+ function getSelectionRangeInRow(tr, startRowIndex, endRowIndex = startRowIndex) {
449
+ let startIndex = startRowIndex;
450
+ let endIndex = endRowIndex;
451
+ for (let i = startRowIndex; i >= 0; i--) {
452
+ const cells = getCellsInRow(i, tr.selection);
453
+ if (cells) {
454
+ cells.forEach((cell) => {
455
+ const maybeEndIndex = cell.node.attrs.rowspan + i - 1;
456
+ if (maybeEndIndex >= startIndex) {
457
+ startIndex = i;
458
+ }
459
+ if (maybeEndIndex > endIndex) {
460
+ endIndex = maybeEndIndex;
461
+ }
462
+ });
463
+ }
464
+ }
465
+ for (let i = startRowIndex; i <= endIndex; i++) {
466
+ const cells = getCellsInRow(i, tr.selection);
467
+ if (cells) {
468
+ cells.forEach((cell) => {
469
+ const maybeEndIndex = cell.node.attrs.rowspan + i - 1;
470
+ if (cell.node.attrs.rowspan > 1 && maybeEndIndex > endIndex) {
471
+ endIndex = maybeEndIndex;
472
+ }
473
+ });
474
+ }
475
+ }
476
+ const indexes = [];
477
+ for (let i = startIndex; i <= endIndex; i++) {
478
+ const maybeCells = getCellsInRow(i, tr.selection);
479
+ if (maybeCells && maybeCells.length > 0) {
480
+ indexes.push(i);
481
+ }
482
+ }
483
+ startIndex = indexes[0];
484
+ endIndex = indexes[indexes.length - 1];
485
+ const firstSelectedRowCells = getCellsInRow(startIndex, tr.selection);
486
+ const firstColumnCells = getCellsInCol(0, tr.selection);
487
+ if (!firstSelectedRowCells || !firstColumnCells) {
488
+ return;
489
+ }
490
+ const $anchor = tr.doc.resolve(
491
+ firstSelectedRowCells[firstSelectedRowCells.length - 1].pos
492
+ );
493
+ let headCell;
494
+ for (let i = endIndex; i >= startIndex; i--) {
495
+ const rowCells = getCellsInRow(i, tr.selection);
496
+ if (rowCells && rowCells.length > 0) {
497
+ for (let j = firstColumnCells.length - 1; j >= 0; j--) {
498
+ if (firstColumnCells[j].pos === rowCells[0].pos) {
499
+ headCell = rowCells[0];
500
+ break;
501
+ }
502
+ }
503
+ if (headCell) {
504
+ break;
505
+ }
506
+ }
507
+ }
508
+ if (!headCell) {
509
+ return;
510
+ }
511
+ const $head = tr.doc.resolve(headCell.pos);
512
+ return { $anchor, $head, indexes };
513
+ }
465
514
  function moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, directionOverride) {
466
515
  const direction = indexesOrigin[0] > indexesTarget[0] ? -1 : 1;
467
516
  const rowsExtracted = rows.splice(indexesOrigin[0], indexesOrigin.length);
@@ -473,168 +522,165 @@ function moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget, directionOverr
473
522
  rows.splice(target, 0, ...rowsExtracted);
474
523
  return rows;
475
524
  }
476
- function moveTableColumn(table, indexesOrigin, indexesTarget, direction) {
477
- let rows = transpose(convertTableNodeToArrayOfRows(table.node));
478
- rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget);
479
- rows = transpose(rows);
480
- return convertArrayOfRowsToTableNode(table.node, rows);
525
+ function moveRow(moveRowParams) {
526
+ const { tr, origin, target, select, pos } = moveRowParams;
527
+ const $pos = tr.doc.resolve(pos);
528
+ const table = findTable($pos);
529
+ if (!table) return false;
530
+ const indexesOriginRow = getSelectionRangeInRow(tr, origin)?.indexes;
531
+ const indexesTargetRow = getSelectionRangeInRow(tr, target)?.indexes;
532
+ if (!indexesOriginRow || !indexesTargetRow) return false;
533
+ if (indexesOriginRow.includes(target)) return false;
534
+ const newTable = moveTableRow(
535
+ table.node,
536
+ indexesOriginRow,
537
+ indexesTargetRow
538
+ );
539
+ tr.replaceWith(table.pos, table.pos + table.node.nodeSize, newTable);
540
+ if (!select) return true;
541
+ const map = TableMap.get(newTable);
542
+ const start = table.start;
543
+ const index = target;
544
+ const lastCell = map.positionAt(index, map.width - 1, newTable);
545
+ const $lastCell = tr.doc.resolve(start + lastCell);
546
+ const firstCell = map.positionAt(index, 0, newTable);
547
+ const $firstCell = tr.doc.resolve(start + firstCell);
548
+ tr.setSelection(CellSelection.rowSelection($lastCell, $firstCell));
549
+ return true;
481
550
  }
482
551
  function moveTableRow(table, indexesOrigin, indexesTarget, direction) {
483
- let rows = convertTableNodeToArrayOfRows(table.node);
552
+ let rows = convertTableToRows(table);
484
553
  rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget);
485
- return convertArrayOfRowsToTableNode(table.node, rows);
554
+ return convertRowsToTable(table, rows);
486
555
  }
487
- function getSelectionRangeInColumn(columnIndex, tr) {
488
- let startIndex = columnIndex;
489
- let endIndex = columnIndex;
490
- for (let i = columnIndex; i >= 0; i--) {
556
+ function getSelectionRangeInCol(tr, startColIndex, endColIndex = startColIndex) {
557
+ let startIndex = startColIndex;
558
+ let endIndex = endColIndex;
559
+ for (let i = startColIndex; i >= 0; i--) {
491
560
  const cells = getCellsInCol(i, tr.selection);
492
561
  if (cells) {
493
562
  cells.forEach((cell) => {
494
563
  const maybeEndIndex = cell.node.attrs.colspan + i - 1;
495
- if (maybeEndIndex >= startIndex) startIndex = i;
496
- if (maybeEndIndex > endIndex) endIndex = maybeEndIndex;
564
+ if (maybeEndIndex >= startIndex) {
565
+ startIndex = i;
566
+ }
567
+ if (maybeEndIndex > endIndex) {
568
+ endIndex = maybeEndIndex;
569
+ }
497
570
  });
498
571
  }
499
572
  }
500
- for (let i = columnIndex; i <= endIndex; i++) {
573
+ for (let i = startColIndex; i <= endIndex; i++) {
501
574
  const cells = getCellsInCol(i, tr.selection);
502
575
  if (cells) {
503
576
  cells.forEach((cell) => {
504
577
  const maybeEndIndex = cell.node.attrs.colspan + i - 1;
505
- if (cell.node.attrs.colspan > 1 && maybeEndIndex > endIndex)
578
+ if (cell.node.attrs.colspan > 1 && maybeEndIndex > endIndex) {
506
579
  endIndex = maybeEndIndex;
580
+ }
507
581
  });
508
582
  }
509
583
  }
510
584
  const indexes = [];
511
585
  for (let i = startIndex; i <= endIndex; i++) {
512
586
  const maybeCells = getCellsInCol(i, tr.selection);
513
- if (maybeCells && maybeCells.length) indexes.push(i);
587
+ if (maybeCells && maybeCells.length > 0) {
588
+ indexes.push(i);
589
+ }
514
590
  }
515
591
  startIndex = indexes[0];
516
592
  endIndex = indexes[indexes.length - 1];
517
593
  const firstSelectedColumnCells = getCellsInCol(startIndex, tr.selection);
518
594
  const firstRowCells = getCellsInRow(0, tr.selection);
595
+ if (!firstSelectedColumnCells || !firstRowCells) {
596
+ return;
597
+ }
519
598
  const $anchor = tr.doc.resolve(
520
599
  firstSelectedColumnCells[firstSelectedColumnCells.length - 1].pos
521
600
  );
522
601
  let headCell;
523
602
  for (let i = endIndex; i >= startIndex; i--) {
524
603
  const columnCells = getCellsInCol(i, tr.selection);
525
- if (columnCells && columnCells.length) {
604
+ if (columnCells && columnCells.length > 0) {
526
605
  for (let j = firstRowCells.length - 1; j >= 0; j--) {
527
606
  if (firstRowCells[j].pos === columnCells[0].pos) {
528
607
  headCell = columnCells[0];
529
608
  break;
530
609
  }
531
610
  }
532
- if (headCell) break;
533
- }
534
- }
535
- const $head = tr.doc.resolve(headCell.pos);
536
- return { $anchor, $head, indexes };
537
- }
538
- function getSelectionRangeInRow(rowIndex, tr) {
539
- let startIndex = rowIndex;
540
- let endIndex = rowIndex;
541
- for (let i = rowIndex; i >= 0; i--) {
542
- const cells = getCellsInRow(i, tr.selection);
543
- cells.forEach((cell) => {
544
- const maybeEndIndex = cell.node.attrs.rowspan + i - 1;
545
- if (maybeEndIndex >= startIndex) startIndex = i;
546
- if (maybeEndIndex > endIndex) endIndex = maybeEndIndex;
547
- });
548
- }
549
- for (let i = rowIndex; i <= endIndex; i++) {
550
- const cells = getCellsInRow(i, tr.selection);
551
- cells.forEach((cell) => {
552
- const maybeEndIndex = cell.node.attrs.rowspan + i - 1;
553
- if (cell.node.attrs.rowspan > 1 && maybeEndIndex > endIndex)
554
- endIndex = maybeEndIndex;
555
- });
556
- }
557
- const indexes = [];
558
- for (let i = startIndex; i <= endIndex; i++) {
559
- const maybeCells = getCellsInRow(i, tr.selection);
560
- if (maybeCells && maybeCells.length) indexes.push(i);
561
- }
562
- startIndex = indexes[0];
563
- endIndex = indexes[indexes.length - 1];
564
- const firstSelectedRowCells = getCellsInRow(startIndex, tr.selection);
565
- const firstColumnCells = getCellsInCol(0, tr.selection);
566
- const $anchor = tr.doc.resolve(
567
- firstSelectedRowCells[firstSelectedRowCells.length - 1].pos
568
- );
569
- let headCell;
570
- for (let i = endIndex; i >= startIndex; i--) {
571
- const rowCells = getCellsInRow(i, tr.selection);
572
- if (rowCells && rowCells.length) {
573
- for (let j = firstColumnCells.length - 1; j >= 0; j--) {
574
- if (firstColumnCells[j].pos === rowCells[0].pos) {
575
- headCell = rowCells[0];
576
- break;
577
- }
611
+ if (headCell) {
612
+ break;
578
613
  }
579
- if (headCell) break;
580
614
  }
581
615
  }
616
+ if (!headCell) {
617
+ return;
618
+ }
582
619
  const $head = tr.doc.resolve(headCell.pos);
583
620
  return { $anchor, $head, indexes };
584
621
  }
622
+ function transpose(array) {
623
+ return array[0].map((_, i) => array.map((column) => column[i]));
624
+ }
585
625
  function moveCol(moveColParams) {
586
- const { tr, origin, target, select = true, pos } = moveColParams;
587
- const $pos = pos != null ? tr.doc.resolve(pos) : tr.selection.$from;
626
+ const { tr, origin, target, select, pos } = moveColParams;
627
+ const $pos = tr.doc.resolve(pos);
588
628
  const table = findTable($pos);
589
- if (!table) return tr;
590
- const { indexes: indexesOriginColumn } = getSelectionRangeInColumn(origin, tr);
591
- const { indexes: indexesTargetColumn } = getSelectionRangeInColumn(target, tr);
592
- if (indexesOriginColumn.includes(target)) return tr;
593
- const newTable = moveTableColumn(
594
- table,
629
+ if (!table) return false;
630
+ const indexesOriginColumn = getSelectionRangeInCol(tr, origin)?.indexes;
631
+ const indexesTargetColumn = getSelectionRangeInCol(tr, target)?.indexes;
632
+ if (!indexesOriginColumn || !indexesTargetColumn) return false;
633
+ if (indexesOriginColumn.includes(target)) return false;
634
+ const newTable = moveTableCol(
635
+ table.node,
595
636
  indexesOriginColumn,
596
637
  indexesTargetColumn
597
638
  );
598
- const _tr = cloneTr(tr).replaceWith(
599
- table.pos,
600
- table.pos + table.node.nodeSize,
601
- newTable
602
- );
603
- if (!select) return _tr;
639
+ tr.replaceWith(table.pos, table.pos + table.node.nodeSize, newTable);
640
+ if (!select) return true;
604
641
  const map = TableMap.get(newTable);
605
642
  const start = table.start;
606
643
  const index = target;
607
644
  const lastCell = map.positionAt(map.height - 1, index, newTable);
608
- const $lastCell = _tr.doc.resolve(start + lastCell);
609
- const createCellSelection = CellSelection.colSelection;
645
+ const $lastCell = tr.doc.resolve(start + lastCell);
610
646
  const firstCell = map.positionAt(0, index, newTable);
611
- const $firstCell = _tr.doc.resolve(start + firstCell);
612
- return _tr.setSelection(createCellSelection($lastCell, $firstCell));
647
+ const $firstCell = tr.doc.resolve(start + firstCell);
648
+ tr.setSelection(CellSelection.colSelection($lastCell, $firstCell));
649
+ return true;
613
650
  }
614
- function moveRow(moveRowParams) {
615
- const { tr, origin, target, select = true, pos } = moveRowParams;
616
- const $pos = pos != null ? tr.doc.resolve(pos) : tr.selection.$from;
617
- const table = findTable($pos);
618
- if (!table) return tr;
619
- const { indexes: indexesOriginRow } = getSelectionRangeInRow(origin, tr);
620
- const { indexes: indexesTargetRow } = getSelectionRangeInRow(target, tr);
621
- if (indexesOriginRow.includes(target)) return tr;
622
- const newTable = moveTableRow(table, indexesOriginRow, indexesTargetRow);
623
- const _tr = cloneTr(tr).replaceWith(
624
- table.pos,
625
- table.pos + table.node.nodeSize,
626
- newTable
627
- );
628
- if (!select) return _tr;
629
- const map = TableMap.get(newTable);
630
- const start = table.start;
631
- const index = target;
632
- const lastCell = map.positionAt(index, map.width - 1, newTable);
633
- const $lastCell = _tr.doc.resolve(start + lastCell);
634
- const createCellSelection = CellSelection.rowSelection;
635
- const firstCell = map.positionAt(index, 0, newTable);
636
- const $firstCell = _tr.doc.resolve(start + firstCell);
637
- return _tr.setSelection(createCellSelection($lastCell, $firstCell));
651
+ function moveTableCol(table, indexesOrigin, indexesTarget, direction) {
652
+ let rows = transpose(convertTableToRows(table));
653
+ rows = moveRowInArrayOfRows(rows, indexesOrigin, indexesTarget);
654
+ rows = transpose(rows);
655
+ return convertRowsToTable(table, rows);
656
+ }
657
+ function getAllCellsInTable(selection) {
658
+ const table = findTable(selection.$from);
659
+ if (!table) return;
660
+ const map = TableMap.get(table.node);
661
+ const cells = map.cellsInRect({
662
+ left: 0,
663
+ right: map.width,
664
+ top: 0,
665
+ bottom: map.height
666
+ });
667
+ return cells.map((nodePos) => {
668
+ const node = table.node.nodeAt(nodePos);
669
+ const pos = nodePos + table.start;
670
+ return { pos, start: pos + 1, node };
671
+ });
672
+ }
673
+ function selectTable(tr) {
674
+ const cells = getAllCellsInTable(tr.selection);
675
+ if (cells && cells[0]) {
676
+ const $firstCell = tr.doc.resolve(cells[0].pos);
677
+ const last = cells[cells.length - 1];
678
+ if (last) {
679
+ const $lastCell = tr.doc.resolve(last.pos);
680
+ return cloneTr(tr.setSelection(new CellSelection($lastCell, $firstCell)));
681
+ }
682
+ }
683
+ return tr;
638
684
  }
639
685
  const goToPrevTableCellCommand = $command(
640
686
  "GoToPrevTableCell",
@@ -695,10 +741,17 @@ const moveRowCommand = $command(
695
741
  "MoveRow",
696
742
  () => ({ from, to, pos } = {}) => (state, dispatch) => {
697
743
  const { tr } = state;
698
- const result = dispatch?.(
699
- moveRow({ tr, origin: from ?? 0, target: to ?? 0, pos, select: true })
700
- );
701
- return Boolean(result);
744
+ if (moveRow({
745
+ tr,
746
+ origin: from ?? 0,
747
+ target: to ?? 0,
748
+ pos: pos ?? state.selection.from,
749
+ select: true
750
+ })) {
751
+ dispatch?.(tr);
752
+ return true;
753
+ }
754
+ return false;
702
755
  }
703
756
  );
704
757
  withMeta(moveRowCommand, {
@@ -709,10 +762,17 @@ const moveColCommand = $command(
709
762
  "MoveCol",
710
763
  () => ({ from, to, pos } = {}) => (state, dispatch) => {
711
764
  const { tr } = state;
712
- const result = dispatch?.(
713
- moveCol({ tr, origin: from ?? 0, target: to ?? 0, pos, select: true })
714
- );
715
- return Boolean(result);
765
+ if (moveCol({
766
+ tr,
767
+ origin: from ?? 0,
768
+ target: to ?? 0,
769
+ pos: pos ?? state.selection.from,
770
+ select: true
771
+ })) {
772
+ dispatch?.(tr);
773
+ return true;
774
+ }
775
+ return false;
716
776
  }
717
777
  );
718
778
  withMeta(moveColCommand, {