@atlaskit/editor-tables 2.2.4 → 2.2.6

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 (158) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/cell-bookmark.js +0 -10
  3. package/dist/cjs/cell-selection.js +37 -102
  4. package/dist/cjs/index.js +0 -4
  5. package/dist/cjs/pm-plugins/input.js +17 -83
  6. package/dist/cjs/pm-plugins/plugin-key.js +0 -2
  7. package/dist/cjs/pm-plugins/table-editing.js +17 -18
  8. package/dist/cjs/pm-plugins.js +0 -2
  9. package/dist/cjs/table-map.js +67 -138
  10. package/dist/cjs/utils/add-column-at.js +0 -9
  11. package/dist/cjs/utils/add-column.js +8 -24
  12. package/dist/cjs/utils/add-row-at.js +8 -37
  13. package/dist/cjs/utils/add-row.js +2 -23
  14. package/dist/cjs/utils/analytics-helpers.js +4 -15
  15. package/dist/cjs/utils/cells.js +0 -12
  16. package/dist/cjs/utils/clone-tr.js +0 -2
  17. package/dist/cjs/utils/colspan.js +2 -18
  18. package/dist/cjs/utils/copy-paste.js +51 -139
  19. package/dist/cjs/utils/create-table.js +14 -28
  20. package/dist/cjs/utils/draw-cell-selection.js +0 -4
  21. package/dist/cjs/utils/empty-cells.js +0 -6
  22. package/dist/cjs/utils/find.js +6 -18
  23. package/dist/cjs/utils/fix-tables.js +13 -56
  24. package/dist/cjs/utils/for-each-cell.js +3 -17
  25. package/dist/cjs/utils/get-cell-selection-ranges.js +2 -9
  26. package/dist/cjs/utils/get-cells-in-column.js +0 -7
  27. package/dist/cjs/utils/get-cells-in-row.js +0 -7
  28. package/dist/cjs/utils/get-cells-in-table.js +0 -8
  29. package/dist/cjs/utils/get-selection-range-in-column.js +6 -27
  30. package/dist/cjs/utils/get-selection-range-in-row.js +6 -27
  31. package/dist/cjs/utils/get-selection-rect.js +0 -7
  32. package/dist/cjs/utils/go-to-next-cell.js +3 -22
  33. package/dist/cjs/utils/handle-paste.js +5 -34
  34. package/dist/cjs/utils/is-selected.js +6 -23
  35. package/dist/cjs/utils/is-selection-type.js +0 -2
  36. package/dist/cjs/utils/move-column.js +0 -12
  37. package/dist/cjs/utils/move-row.js +0 -11
  38. package/dist/cjs/utils/normalize-selection.js +3 -27
  39. package/dist/cjs/utils/remove-column.js +10 -46
  40. package/dist/cjs/utils/remove-row.js +8 -49
  41. package/dist/cjs/utils/remove-table.js +0 -6
  42. package/dist/cjs/utils/reorder-utils.js +13 -37
  43. package/dist/cjs/utils/replace-table.js +0 -8
  44. package/dist/cjs/utils/select-nodes.js +11 -31
  45. package/dist/cjs/utils/selection-cell.js +0 -5
  46. package/dist/cjs/utils/selection-rect.js +0 -10
  47. package/dist/cjs/utils/set-cell-attrs.js +0 -4
  48. package/dist/cjs/utils/split-cell-with-type.js +2 -35
  49. package/dist/cjs/utils/split-cell.js +0 -4
  50. package/dist/cjs/utils/table-node-types.js +4 -8
  51. package/dist/cjs/utils/tables.js +0 -4
  52. package/dist/cjs/utils/test-utils.js +9 -26
  53. package/dist/cjs/utils/toggle-header.js +3 -16
  54. package/dist/cjs/utils/uuid.js +0 -3
  55. package/dist/cjs/utils.js +0 -42
  56. package/dist/cjs/version.json +1 -1
  57. package/dist/es2019/cell-bookmark.js +0 -5
  58. package/dist/es2019/cell-selection.js +37 -81
  59. package/dist/es2019/index.js +1 -0
  60. package/dist/es2019/pm-plugins/input.js +15 -67
  61. package/dist/es2019/pm-plugins/table-editing.js +3 -12
  62. package/dist/es2019/table-map.js +57 -127
  63. package/dist/es2019/utils/add-column-at.js +2 -4
  64. package/dist/es2019/utils/add-column.js +6 -16
  65. package/dist/es2019/utils/add-row-at.js +8 -22
  66. package/dist/es2019/utils/add-row.js +2 -17
  67. package/dist/es2019/utils/analytics-helpers.js +0 -5
  68. package/dist/es2019/utils/cells.js +4 -9
  69. package/dist/es2019/utils/colspan.js +6 -11
  70. package/dist/es2019/utils/copy-paste.js +41 -115
  71. package/dist/es2019/utils/create-table.js +3 -12
  72. package/dist/es2019/utils/draw-cell-selection.js +0 -1
  73. package/dist/es2019/utils/empty-cells.js +2 -3
  74. package/dist/es2019/utils/find.js +8 -8
  75. package/dist/es2019/utils/fix-tables.js +17 -47
  76. package/dist/es2019/utils/for-each-cell.js +6 -11
  77. package/dist/es2019/utils/get-cell-selection-ranges.js +2 -4
  78. package/dist/es2019/utils/get-cells-in-column.js +2 -3
  79. package/dist/es2019/utils/get-cells-in-row.js +2 -3
  80. package/dist/es2019/utils/get-cells-in-table.js +2 -4
  81. package/dist/es2019/utils/get-selection-range-in-column.js +8 -22
  82. package/dist/es2019/utils/get-selection-range-in-row.js +8 -22
  83. package/dist/es2019/utils/get-selection-rect.js +2 -2
  84. package/dist/es2019/utils/go-to-next-cell.js +3 -19
  85. package/dist/es2019/utils/handle-paste.js +3 -14
  86. package/dist/es2019/utils/is-selected.js +8 -10
  87. package/dist/es2019/utils/is-selection-type.js +0 -1
  88. package/dist/es2019/utils/move-column.js +3 -8
  89. package/dist/es2019/utils/move-row.js +3 -7
  90. package/dist/es2019/utils/normalize-selection.js +0 -16
  91. package/dist/es2019/utils/remove-column.js +10 -27
  92. package/dist/es2019/utils/remove-row.js +12 -32
  93. package/dist/es2019/utils/remove-table.js +2 -4
  94. package/dist/es2019/utils/reorder-utils.js +11 -26
  95. package/dist/es2019/utils/replace-table.js +0 -2
  96. package/dist/es2019/utils/select-nodes.js +9 -20
  97. package/dist/es2019/utils/selection-cell.js +0 -2
  98. package/dist/es2019/utils/selection-rect.js +0 -4
  99. package/dist/es2019/utils/set-cell-attrs.js +2 -2
  100. package/dist/es2019/utils/split-cell-with-type.js +8 -28
  101. package/dist/es2019/utils/split-cell.js +3 -2
  102. package/dist/es2019/utils/table-node-types.js +4 -7
  103. package/dist/es2019/utils/tables.js +0 -2
  104. package/dist/es2019/utils/test-utils.js +12 -16
  105. package/dist/es2019/utils/toggle-header.js +3 -10
  106. package/dist/es2019/utils/uuid.js +0 -2
  107. package/dist/es2019/version.json +1 -1
  108. package/dist/esm/cell-bookmark.js +0 -5
  109. package/dist/esm/cell-selection.js +37 -79
  110. package/dist/esm/index.js +1 -0
  111. package/dist/esm/pm-plugins/input.js +17 -71
  112. package/dist/esm/pm-plugins/table-editing.js +8 -13
  113. package/dist/esm/table-map.js +68 -133
  114. package/dist/esm/utils/add-column-at.js +2 -4
  115. package/dist/esm/utils/add-column.js +8 -20
  116. package/dist/esm/utils/add-row-at.js +8 -23
  117. package/dist/esm/utils/add-row.js +2 -21
  118. package/dist/esm/utils/analytics-helpers.js +4 -10
  119. package/dist/esm/utils/cells.js +0 -5
  120. package/dist/esm/utils/colspan.js +2 -14
  121. package/dist/esm/utils/copy-paste.js +44 -125
  122. package/dist/esm/utils/create-table.js +14 -25
  123. package/dist/esm/utils/draw-cell-selection.js +0 -1
  124. package/dist/esm/utils/empty-cells.js +2 -3
  125. package/dist/esm/utils/find.js +8 -8
  126. package/dist/esm/utils/fix-tables.js +13 -49
  127. package/dist/esm/utils/for-each-cell.js +6 -11
  128. package/dist/esm/utils/get-cell-selection-ranges.js +2 -4
  129. package/dist/esm/utils/get-cells-in-column.js +2 -3
  130. package/dist/esm/utils/get-cells-in-row.js +2 -3
  131. package/dist/esm/utils/get-cells-in-table.js +2 -4
  132. package/dist/esm/utils/get-selection-range-in-column.js +8 -24
  133. package/dist/esm/utils/get-selection-range-in-row.js +8 -24
  134. package/dist/esm/utils/get-selection-rect.js +2 -2
  135. package/dist/esm/utils/go-to-next-cell.js +3 -19
  136. package/dist/esm/utils/handle-paste.js +5 -23
  137. package/dist/esm/utils/is-selected.js +8 -10
  138. package/dist/esm/utils/is-selection-type.js +0 -1
  139. package/dist/esm/utils/move-column.js +3 -8
  140. package/dist/esm/utils/move-row.js +3 -7
  141. package/dist/esm/utils/normalize-selection.js +3 -22
  142. package/dist/esm/utils/remove-column.js +10 -31
  143. package/dist/esm/utils/remove-row.js +8 -35
  144. package/dist/esm/utils/remove-table.js +2 -4
  145. package/dist/esm/utils/reorder-utils.js +11 -26
  146. package/dist/esm/utils/replace-table.js +0 -2
  147. package/dist/esm/utils/select-nodes.js +11 -22
  148. package/dist/esm/utils/selection-cell.js +0 -2
  149. package/dist/esm/utils/selection-rect.js +0 -4
  150. package/dist/esm/utils/set-cell-attrs.js +2 -2
  151. package/dist/esm/utils/split-cell-with-type.js +2 -28
  152. package/dist/esm/utils/split-cell.js +3 -2
  153. package/dist/esm/utils/table-node-types.js +4 -7
  154. package/dist/esm/utils/tables.js +0 -2
  155. package/dist/esm/utils/test-utils.js +12 -16
  156. package/dist/esm/utils/toggle-header.js +3 -10
  157. package/dist/esm/version.json +1 -1
  158. package/package.json +6 -4
@@ -1,25 +1,24 @@
1
1
  import { TableMap } from '../table-map';
2
- import { isSelectionType } from './is-selection-type'; // Checks if a given CellSelection rect is selected
2
+ import { isSelectionType } from './is-selection-type';
3
3
 
4
+ // Checks if a given CellSelection rect is selected
4
5
  export const isRectSelected = rect => selection => {
5
6
  if (!isSelectionType(selection, 'cell')) {
6
7
  return false;
7
8
  }
8
-
9
9
  const map = TableMap.get(selection.$anchorCell.node(-1));
10
10
  const start = selection.$anchorCell.start(-1);
11
11
  const cells = map.cellsInRect(rect);
12
12
  const selectedCells = map.cellsInRect(map.rectBetween(selection.$anchorCell.pos - start, selection.$headCell.pos - start));
13
-
14
13
  for (let i = 0, count = cells.length; i < count; i++) {
15
14
  if (selectedCells.indexOf(cells[i]) === -1) {
16
15
  return false;
17
16
  }
18
17
  }
19
-
20
18
  return true;
21
- }; // Checks if entire column at index `columnIndex` is selected.
19
+ };
22
20
 
21
+ // Checks if entire column at index `columnIndex` is selected.
23
22
  export const isColumnSelected = columnIndex => selection => {
24
23
  if (isSelectionType(selection, 'cell')) {
25
24
  const map = TableMap.get(selection.$anchorCell.node(-1));
@@ -30,10 +29,10 @@ export const isColumnSelected = columnIndex => selection => {
30
29
  bottom: map.height
31
30
  })(selection);
32
31
  }
33
-
34
32
  return false;
35
- }; // Checks if entire row at index `rowIndex` is selected.
33
+ };
36
34
 
35
+ // Checks if entire row at index `rowIndex` is selected.
37
36
  export const isRowSelected = rowIndex => selection => {
38
37
  if (isSelectionType(selection, 'cell')) {
39
38
  const map = TableMap.get(selection.$anchorCell.node(-1));
@@ -44,10 +43,10 @@ export const isRowSelected = rowIndex => selection => {
44
43
  bottom: rowIndex + 1
45
44
  })(selection);
46
45
  }
47
-
48
46
  return false;
49
- }; // Checks if entire table is selected
47
+ };
50
48
 
49
+ // Checks if entire table is selected
51
50
  export const isTableSelected = selection => {
52
51
  if (isSelectionType(selection, 'cell')) {
53
52
  const map = TableMap.get(selection.$anchorCell.node(-1));
@@ -58,6 +57,5 @@ export const isTableSelected = selection => {
58
57
  bottom: map.height
59
58
  })(selection);
60
59
  }
61
-
62
60
  return false;
63
61
  };
@@ -2,7 +2,6 @@ export function isSelectionType(selection, type) {
2
2
  if (!selection) {
3
3
  return false;
4
4
  }
5
-
6
5
  const serialized = selection.toJSON();
7
6
  return serialized.type === type;
8
7
  }
@@ -1,7 +1,9 @@
1
1
  import { cloneTr } from './clone-tr';
2
2
  import { findTable } from './find';
3
3
  import { getSelectionRangeInColumn } from './get-selection-range-in-column';
4
- import { isValidReorder, moveTableColumn } from './reorder-utils'; // :: (originColumnIndex: number, targetColumnIndex: targetColumnIndex, options?: MovementOptions) → (tr: Transaction) → Transaction
4
+ import { isValidReorder, moveTableColumn } from './reorder-utils';
5
+
6
+ // :: (originColumnIndex: number, targetColumnIndex: targetColumnIndex, options?: MovementOptions) → (tr: Transaction) → Transaction
5
7
  // Returns a new transaction that moves the origin column to the target index;
6
8
  //
7
9
  // by default "tryToFit" is false, that means if you try to move a column to a place
@@ -129,32 +131,25 @@ import { isValidReorder, moveTableColumn } from './reorder-utils'; // :: (origin
129
131
  // moveColumn(x, y, options)(state.tr)
130
132
  // );
131
133
  // ```
132
-
133
134
  export const moveColumn = (originColumnIndex, targetColumnIndex, options = {
134
135
  tryToFit: false,
135
136
  direction: 0
136
137
  }) => tr => {
137
138
  var _originalColumnRanges, _targetColumnRanges$i;
138
-
139
139
  const table = findTable(tr.selection);
140
-
141
140
  if (!table) {
142
141
  return tr;
143
142
  }
144
-
145
143
  const originalColumnRanges = getSelectionRangeInColumn(originColumnIndex)(tr);
146
144
  const targetColumnRanges = getSelectionRangeInColumn(targetColumnIndex)(tr);
147
145
  const indexesOriginColumn = (_originalColumnRanges = originalColumnRanges === null || originalColumnRanges === void 0 ? void 0 : originalColumnRanges.indexes) !== null && _originalColumnRanges !== void 0 ? _originalColumnRanges : [];
148
146
  const indexesTargetColumn = (_targetColumnRanges$i = targetColumnRanges === null || targetColumnRanges === void 0 ? void 0 : targetColumnRanges.indexes) !== null && _targetColumnRanges$i !== void 0 ? _targetColumnRanges$i : [];
149
-
150
147
  if (indexesOriginColumn.includes(targetColumnIndex)) {
151
148
  return tr;
152
149
  }
153
-
154
150
  if (!options.tryToFit && indexesTargetColumn.length > 1) {
155
151
  isValidReorder(originColumnIndex, targetColumnIndex, indexesTargetColumn, 'column');
156
152
  }
157
-
158
153
  const newTable = moveTableColumn(table, indexesOriginColumn, indexesTargetColumn, options.direction);
159
154
  return cloneTr(tr).replaceWith(table.pos, table.pos + table.node.nodeSize, newTable);
160
155
  };
@@ -1,7 +1,9 @@
1
1
  import { cloneTr } from './clone-tr';
2
2
  import { findTable } from './find';
3
3
  import { getSelectionRangeInRow } from './get-selection-range-in-row';
4
- import { isValidReorder, moveTableRow } from './reorder-utils'; // :: (originRowIndex: number, targetRowIndex: targetColumnIndex, options?: MovementOptions) → (tr: Transaction) → Transaction
4
+ import { isValidReorder, moveTableRow } from './reorder-utils';
5
+
6
+ // :: (originRowIndex: number, targetRowIndex: targetColumnIndex, options?: MovementOptions) → (tr: Transaction) → Transaction
5
7
  // Returns a new transaction that moves the origin row to the target index;
6
8
  //
7
9
  // by default "tryToFit" is false, that means if you try to move a row to a place
@@ -144,30 +146,24 @@ import { isValidReorder, moveTableRow } from './reorder-utils'; // :: (originRow
144
146
  // moveRow(x, y, options)(state.tr)
145
147
  // );
146
148
  // ```
147
-
148
149
  export const moveRow = (originRowIndex, targetRowIndex, options = {
149
150
  tryToFit: false,
150
151
  direction: 0
151
152
  }) => tr => {
152
153
  const table = findTable(tr.selection);
153
-
154
154
  if (!table) {
155
155
  return tr;
156
156
  }
157
-
158
157
  const originalRowRanges = getSelectionRangeInRow(originRowIndex)(tr);
159
158
  const targetRowRanges = getSelectionRangeInRow(targetRowIndex)(tr);
160
159
  const indexesOriginRow = (originalRowRanges === null || originalRowRanges === void 0 ? void 0 : originalRowRanges.indexes) || [];
161
160
  const indexesTargetRow = (targetRowRanges === null || targetRowRanges === void 0 ? void 0 : targetRowRanges.indexes) || [];
162
-
163
161
  if (indexesOriginRow.includes(targetRowIndex)) {
164
162
  return tr;
165
163
  }
166
-
167
164
  if (!options.tryToFit && indexesTargetRow.length > 1) {
168
165
  isValidReorder(originRowIndex, targetRowIndex, indexesTargetRow, 'row');
169
166
  }
170
-
171
167
  const newTable = moveTableRow(table, indexesOriginRow, indexesTargetRow, options.direction);
172
168
  return cloneTr(tr).replaceWith(table.pos, table.pos + table.node.nodeSize, newTable);
173
169
  };
@@ -9,11 +9,9 @@ export function normalizeSelection(state, transaction, allowTableNodeSelection)
9
9
  } = tr || state;
10
10
  let normalize;
11
11
  let role;
12
-
13
12
  if (sel instanceof NodeSelection) {
14
13
  role = sel.node.type.spec.tableRole;
15
14
  }
16
-
17
15
  if (sel instanceof NodeSelection && role) {
18
16
  if (role === 'cell' || role === 'header_cell') {
19
17
  normalize = CellSelection.create(doc, sel.from);
@@ -31,14 +29,11 @@ export function normalizeSelection(state, transaction, allowTableNodeSelection)
31
29
  } else if (sel instanceof TextSelection && isTextSelectionAcrossCells(sel)) {
32
30
  normalize = TextSelection.create(doc, sel.$from.start(), sel.$from.end());
33
31
  }
34
-
35
32
  if (normalize) {
36
33
  (tr || (tr = state.tr)).setSelection(normalize);
37
34
  }
38
-
39
35
  return tr;
40
36
  }
41
-
42
37
  function isCellBoundarySelection({
43
38
  $from,
44
39
  $to
@@ -46,53 +41,42 @@ function isCellBoundarySelection({
46
41
  if ($from.pos === $to.pos || $from.pos < $from.pos - 6) {
47
42
  return false;
48
43
  } // Cheap elimination
49
-
50
-
51
44
  let afterFrom = $from.pos;
52
45
  let beforeTo = $to.pos;
53
46
  let {
54
47
  depth
55
48
  } = $from;
56
-
57
49
  for (; depth >= 0; depth--, afterFrom++) {
58
50
  if ($from.after(depth + 1) < $from.end(depth)) {
59
51
  break;
60
52
  }
61
53
  }
62
-
63
54
  for (let d = $to.depth; d >= 0; d--, beforeTo--) {
64
55
  if ($to.before(d + 1) > $to.start(d)) {
65
56
  break;
66
57
  }
67
58
  }
68
-
69
59
  return afterFrom === beforeTo && /row|table/.test($from.node(depth).type.spec.tableRole);
70
60
  }
71
-
72
61
  function isTextSelectionAcrossCells({
73
62
  $from,
74
63
  $to
75
64
  }) {
76
65
  let fromCellBoundaryNode;
77
66
  let toCellBoundaryNode;
78
-
79
67
  for (let i = $from.depth; i > 0; i--) {
80
68
  const node = $from.node(i);
81
-
82
69
  if (node.type.spec.tableRole === 'cell' || node.type.spec.tableRole === 'header_cell') {
83
70
  fromCellBoundaryNode = node;
84
71
  break;
85
72
  }
86
73
  }
87
-
88
74
  for (let i = $to.depth; i > 0; i--) {
89
75
  const node = $to.node(i);
90
-
91
76
  if (node.type.spec.tableRole === 'cell' || node.type.spec.tableRole === 'header_cell') {
92
77
  toCellBoundaryNode = node;
93
78
  break;
94
79
  }
95
80
  }
96
-
97
81
  return fromCellBoundaryNode !== toCellBoundaryNode && $to.parentOffset === 0;
98
82
  }
@@ -6,44 +6,37 @@ import { findCellRectClosestToPos, findTable } from './find';
6
6
  import { isTableSelected } from './is-selected';
7
7
  import { isSelectionType } from './is-selection-type';
8
8
  import { removeTable } from './remove-table';
9
-
10
9
  function removeColumn(tr, {
11
10
  map,
12
11
  table,
13
12
  tableStart
14
13
  }, columnIndex) {
15
14
  const mapStart = tr.mapping.maps.length;
16
-
17
15
  for (let row = 0; row < map.height;) {
18
16
  const index = row * map.width + columnIndex;
19
17
  const pos = map.map[index];
20
18
  const cell = table.nodeAt(pos);
21
-
22
19
  if (!cell) {
23
20
  continue;
24
- } // If this is part of a col-spanning cell
25
-
21
+ }
26
22
 
23
+ // If this is part of a col-spanning cell
27
24
  if (columnIndex > 0 && map.map[index - 1] === pos || columnIndex < map.width - 1 && map.map[index + 1] === pos) {
28
25
  tr.setNodeMarkup(tr.mapping.slice(mapStart).map(tableStart + pos), undefined, removeColSpan(cell.attrs, columnIndex - map.colCount(pos)));
29
26
  } else {
30
27
  const start = tr.mapping.slice(mapStart).map(tableStart + pos);
31
28
  tr.delete(start, start + cell.nodeSize);
32
29
  }
33
-
34
30
  row += cell.attrs.rowspan;
35
31
  }
36
-
37
32
  return tr;
38
- } // Returns a new transaction that removes a column at index `columnIndex`. If there is only one column left, it will remove the entire table.
39
-
33
+ }
40
34
 
35
+ // Returns a new transaction that removes a column at index `columnIndex`. If there is only one column left, it will remove the entire table.
41
36
  export const removeColumnAt = columnIndex => tr => {
42
37
  const table = findTable(tr.selection);
43
-
44
38
  if (table) {
45
39
  const map = TableMap.get(table.node);
46
-
47
40
  if (columnIndex === 0 && map.width === 1) {
48
41
  return removeTable(tr);
49
42
  } else if (columnIndex >= 0 && columnIndex <= map.width) {
@@ -55,60 +48,50 @@ export const removeColumnAt = columnIndex => tr => {
55
48
  return cloneTr(tr);
56
49
  }
57
50
  }
58
-
59
51
  return tr;
60
- }; // Returns a new transaction that removes selected columns.
52
+ };
61
53
 
54
+ // Returns a new transaction that removes selected columns.
62
55
  export const removeSelectedColumns = tr => {
63
56
  const {
64
57
  selection
65
58
  } = tr;
66
-
67
59
  if (isTableSelected(selection)) {
68
60
  return removeTable(tr);
69
61
  }
70
-
71
62
  if (isSelectionType(selection, 'cell')) {
72
63
  const table = findTable(selection);
73
-
74
64
  if (table) {
75
65
  const map = TableMap.get(table.node);
76
66
  const rect = map.rectBetween(selection.$anchorCell.pos - table.start, selection.$headCell.pos - table.start);
77
-
78
67
  if (rect.left === 0 && rect.right === map.width) {
79
68
  return tr;
80
69
  }
81
-
82
- const pmTableRect = { ...rect,
70
+ const pmTableRect = {
71
+ ...rect,
83
72
  map,
84
73
  table: table.node,
85
74
  tableStart: table.start
86
75
  };
87
-
88
76
  for (let i = pmTableRect.right - 1;; i--) {
89
77
  removeColumn(tr, pmTableRect, i);
90
-
91
78
  if (i === pmTableRect.left) {
92
79
  break;
93
80
  }
94
-
95
81
  pmTableRect.table = pmTableRect.tableStart ? tr.doc.nodeAt(pmTableRect.tableStart - 1) : tr.doc;
96
82
  pmTableRect.map = TableMap.get(pmTableRect.table);
97
83
  }
98
-
99
84
  return cloneTr(tr);
100
85
  }
101
86
  }
102
-
103
87
  return tr;
104
- }; // Returns a new transaction that removes a column closest to a given `$pos`.
88
+ };
105
89
 
90
+ // Returns a new transaction that removes a column closest to a given `$pos`.
106
91
  export const removeColumnClosestToPos = $pos => tr => {
107
92
  const rect = findCellRectClosestToPos($pos);
108
-
109
93
  if (rect) {
110
94
  return removeColumnAt(rect.left)(setTextSelection($pos.pos)(tr));
111
95
  }
112
-
113
96
  return tr;
114
97
  };
@@ -5,47 +5,40 @@ import { findCellRectClosestToPos, findTable } from './find';
5
5
  import { isTableSelected } from './is-selected';
6
6
  import { isSelectionType } from './is-selection-type';
7
7
  import { removeTable } from './remove-table';
8
-
9
8
  function removeRow(tr, {
10
9
  map,
11
10
  table,
12
11
  tableStart
13
12
  }, rowIndex) {
14
13
  let rowPos = 0;
15
-
16
14
  for (let i = 0; i < rowIndex; i++) {
17
15
  rowPos += table.child(i).nodeSize;
18
16
  }
19
-
20
17
  let nextRow = rowPos + table.child(rowIndex).nodeSize;
21
18
  let mapFrom = tr.mapping.maps.length;
22
19
  tr.delete(rowPos + tableStart, nextRow + tableStart);
23
-
24
20
  for (let col = 0, index = rowIndex * map.width; col < map.width; col++, index++) {
25
21
  let pos = map.map[index];
26
-
27
22
  if (rowIndex > 0 && pos === map.map[index - map.width]) {
28
23
  // If this cell starts in the row above, simply reduce its rowspan
29
24
  const cell = table.nodeAt(pos);
30
-
31
25
  if (!cell) {
32
26
  continue;
33
27
  }
34
-
35
28
  const attrs = cell.attrs;
36
- tr.setNodeMarkup(tr.mapping.slice(mapFrom).map(pos + tableStart), undefined, { ...attrs,
29
+ tr.setNodeMarkup(tr.mapping.slice(mapFrom).map(pos + tableStart), undefined, {
30
+ ...attrs,
37
31
  rowspan: attrs.rowspan - 1
38
32
  });
39
33
  col += attrs.colspan - 1;
40
34
  } else if (rowIndex < map.width && pos === map.map[index + map.width]) {
41
35
  // Else, if it continues in the row below, it has to be moved down
42
36
  const cell = table.nodeAt(pos);
43
-
44
37
  if (!cell) {
45
38
  continue;
46
39
  }
47
-
48
- const copy = cell.type.create({ ...cell.attrs,
40
+ const copy = cell.type.create({
41
+ ...cell.attrs,
49
42
  rowspan: cell.attrs.rowspan - 1
50
43
  }, cell.content);
51
44
  const newPos = map.positionAt(rowIndex + 1, col, table);
@@ -53,17 +46,14 @@ function removeRow(tr, {
53
46
  col += cell.attrs.colspan - 1;
54
47
  }
55
48
  }
56
-
57
49
  return tr;
58
- } // Returns a new transaction that removes a row at index `rowIndex`. If there is only one row left, it will remove the entire table.
59
-
50
+ }
60
51
 
52
+ // Returns a new transaction that removes a row at index `rowIndex`. If there is only one row left, it will remove the entire table.
61
53
  export const removeRowAt = rowIndex => tr => {
62
54
  const table = findTable(tr.selection);
63
-
64
55
  if (table) {
65
56
  const map = TableMap.get(table.node);
66
-
67
57
  if (rowIndex === 0 && map.height === 1) {
68
58
  return removeTable(tr);
69
59
  } else if (rowIndex >= 0 && rowIndex <= map.height) {
@@ -75,60 +65,50 @@ export const removeRowAt = rowIndex => tr => {
75
65
  return cloneTr(tr);
76
66
  }
77
67
  }
78
-
79
68
  return tr;
80
- }; // Returns a new transaction that removes selected rows.
69
+ };
81
70
 
71
+ // Returns a new transaction that removes selected rows.
82
72
  export const removeSelectedRows = tr => {
83
73
  const {
84
74
  selection
85
75
  } = tr;
86
-
87
76
  if (isTableSelected(selection)) {
88
77
  return removeTable(tr);
89
78
  }
90
-
91
79
  if (isSelectionType(selection, 'cell')) {
92
80
  const table = findTable(selection);
93
-
94
81
  if (table) {
95
82
  const map = TableMap.get(table.node);
96
83
  const rect = map.rectBetween(selection.$anchorCell.pos - table.start, selection.$headCell.pos - table.start);
97
-
98
84
  if (rect.top === 0 && rect.bottom === map.height) {
99
85
  return tr;
100
86
  }
101
-
102
- const pmTableRect = { ...rect,
87
+ const pmTableRect = {
88
+ ...rect,
103
89
  map,
104
90
  table: table.node,
105
91
  tableStart: table.start
106
92
  };
107
-
108
93
  for (let i = pmTableRect.bottom - 1;; i--) {
109
94
  removeRow(tr, pmTableRect, i);
110
-
111
95
  if (i === pmTableRect.top) {
112
96
  break;
113
97
  }
114
-
115
98
  pmTableRect.table = pmTableRect.tableStart ? tr.doc.nodeAt(pmTableRect.tableStart - 1) : tr.doc;
116
99
  pmTableRect.map = TableMap.get(pmTableRect.table);
117
100
  }
118
-
119
101
  return cloneTr(tr);
120
102
  }
121
103
  }
122
-
123
104
  return tr;
124
- }; // Returns a new transaction that removes a row closest to a given `$pos`.
105
+ };
125
106
 
107
+ // Returns a new transaction that removes a row closest to a given `$pos`.
126
108
  export const removeRowClosestToPos = $pos => tr => {
127
109
  const rect = findCellRectClosestToPos($pos);
128
-
129
110
  if (rect) {
130
111
  return removeRowAt(rect.top)(setTextSelection($pos.pos)(tr));
131
112
  }
132
-
133
113
  return tr;
134
114
  };
@@ -1,17 +1,15 @@
1
- import { cloneTr } from './clone-tr'; // Returns a new transaction that removes a table node if the cursor is inside of it.
1
+ import { cloneTr } from './clone-tr';
2
2
 
3
+ // Returns a new transaction that removes a table node if the cursor is inside of it.
3
4
  export const removeTable = tr => {
4
5
  const {
5
6
  $from
6
7
  } = tr.selection;
7
-
8
8
  for (let depth = $from.depth; depth > 0; depth--) {
9
9
  let node = $from.node(depth);
10
-
11
10
  if (node.type.spec.tableRole === 'table') {
12
11
  return cloneTr(tr.delete($from.before(depth), $from.after(depth)));
13
12
  }
14
13
  }
15
-
16
14
  return tr;
17
15
  };
@@ -1,9 +1,10 @@
1
- import { TableMap } from '../table-map'; // array = [
1
+ import { TableMap } from '../table-map';
2
+
3
+ // array = [
2
4
  // [A1, B1, C1, null],
3
5
  // [A2, B2, null, D1],
4
6
  // [A3. B3, C2, null],
5
7
  // ]
6
-
7
8
  // This function transposes an array of array flipping the columns for rows,
8
9
  // transposition is a familiar algebra concept;
9
10
  // you can get more details here:
@@ -30,7 +31,9 @@ export const transpose = array => {
30
31
  return array[0].map((_, i) => {
31
32
  return array.map(column => column[i]);
32
33
  });
33
- }; // :: (tableNode: Node, tableArray: Array<Node>) -> Node
34
+ };
35
+
36
+ // :: (tableNode: Node, tableArray: Array<Node>) -> Node
34
37
  // This function will transform a matrix of nodes
35
38
  // into table node respecting merged cells and rows configurations,
36
39
  // for example this array will be convert to the table below:
@@ -56,57 +59,49 @@ export const transpose = array => {
56
59
  // |______|______|______|______|
57
60
  // ```
58
61
  //
59
-
60
62
  export const convertArrayOfRowsToTableNode = (tableNode, arrayOfNodes) => {
61
63
  const rowsPM = [];
62
64
  const map = TableMap.get(tableNode);
63
-
64
65
  for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
65
66
  const row = tableNode.child(rowIndex);
66
67
  const rowCells = [];
67
-
68
68
  for (let colIndex = 0; colIndex < map.width; colIndex++) {
69
69
  if (!arrayOfNodes[rowIndex][colIndex]) {
70
70
  continue;
71
71
  }
72
-
73
72
  const cellPos = map.map[rowIndex * map.width + colIndex];
74
73
  const cell = arrayOfNodes[rowIndex][colIndex];
75
74
  const oldCell = tableNode.nodeAt(cellPos);
76
-
77
75
  if (!cell || !oldCell) {
78
76
  continue;
79
77
  }
80
-
81
78
  const newCell = oldCell.type.createChecked(Object.assign({}, cell.attrs), cell.content, cell.marks);
82
79
  rowCells.push(newCell);
83
80
  }
84
-
85
81
  rowsPM.push(row.type.createChecked(row.attrs, rowCells, row.marks));
86
82
  }
87
-
88
83
  const newTable = tableNode.type.createChecked(tableNode.attrs, rowsPM, tableNode.marks);
89
84
  return newTable;
90
85
  };
91
-
92
86
  const moveRowInArrayOfRows = (arrayOfNodes, indexesOrigin, indexesTarget, directionOverride) => {
93
87
  let direction = indexesOrigin[0] > indexesTarget[0] ? -1 : 1;
94
88
  const rowsExtracted = arrayOfNodes.splice(indexesOrigin[0], indexesOrigin.length);
95
89
  const positionOffset = rowsExtracted.length % 2 === 0 ? 1 : 0;
96
90
  let target;
97
-
98
91
  if (directionOverride === -1 && direction === 1) {
99
92
  target = indexesTarget[0] - 1;
100
93
  } else if (directionOverride === 1 && direction === -1) {
101
94
  target = indexesTarget[indexesTarget.length - 1] - positionOffset + 1;
102
95
  } else {
103
96
  target = direction === -1 ? indexesTarget[0] : indexesTarget[indexesTarget.length - 1] - positionOffset;
104
- } // @ts-ignore no idea what this line does
105
-
97
+ }
106
98
 
99
+ // @ts-ignore no idea what this line does
107
100
  arrayOfNodes.splice.apply(arrayOfNodes, [target, 0].concat(rowsExtracted));
108
101
  return arrayOfNodes;
109
- }; // :: (tableNode: Node) -> Array<Node>
102
+ };
103
+
104
+ // :: (tableNode: Node) -> Array<Node>
110
105
  // This function will transform the table node
111
106
  // into a matrix of rows and columns respecting merged cells,
112
107
  // for example this table will be convert to the below:
@@ -132,33 +127,25 @@ const moveRowInArrayOfRows = (arrayOfNodes, indexesOrigin, indexesTarget, direct
132
127
  // [A3. B3, C2, null],
133
128
  // ]
134
129
  // ```
135
-
136
-
137
130
  export const convertTableNodeToArrayOfRows = tableNode => {
138
131
  const map = TableMap.get(tableNode);
139
132
  const rows = [];
140
-
141
133
  for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
142
134
  const rowCells = [];
143
135
  const seen = {};
144
-
145
136
  for (let colIndex = 0; colIndex < map.width; colIndex++) {
146
137
  const cellPos = map.map[rowIndex * map.width + colIndex];
147
138
  const cell = tableNode.nodeAt(cellPos);
148
139
  const rect = map.findCell(cellPos);
149
-
150
140
  if (!cell || seen[cellPos] || rect.top !== rowIndex) {
151
141
  rowCells.push(null);
152
142
  continue;
153
143
  }
154
-
155
144
  seen[cellPos] = true;
156
145
  rowCells.push(cell);
157
146
  }
158
-
159
147
  rows.push(rowCells);
160
148
  }
161
-
162
149
  return rows;
163
150
  };
164
151
  export const moveTableRow = (table, indexesOrigin, indexesTarget, direction) => {
@@ -175,7 +162,6 @@ export const moveTableColumn = (table, indexesOrigin, indexesTarget, direction)
175
162
  export const isValidReorder = (originIndex, targetIndex, targets, type) => {
176
163
  const direction = originIndex > targetIndex ? -1 : 1;
177
164
  const errorMessage = `Target position is invalid, you can't move the ${type} ${originIndex} to ${targetIndex}, the target can't be split. You could use tryToFit option.`;
178
-
179
165
  if (direction === 1) {
180
166
  if (targets.slice(0, targets.length - 1).indexOf(targetIndex) !== -1) {
181
167
  throw new Error(errorMessage);
@@ -185,6 +171,5 @@ export const isValidReorder = (originIndex, targetIndex, targets, type) => {
185
171
  throw new Error(errorMessage);
186
172
  }
187
173
  }
188
-
189
174
  return true;
190
175
  };
@@ -5,7 +5,6 @@ import { isTableSelected } from './is-selected';
5
5
  export const replaceSelectedTable = (state, content) => {
6
6
  if (isTableSelected(state.selection)) {
7
7
  const table = findTable(state.selection);
8
-
9
8
  if (table) {
10
9
  const slice = typeof content === 'string' ? new Slice(Fragment.from(state.schema.text(content)), 0, 0) : content;
11
10
  let tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice);
@@ -13,6 +12,5 @@ export const replaceSelectedTable = (state, content) => {
13
12
  return tr;
14
13
  }
15
14
  }
16
-
17
15
  return state.tr;
18
16
  };