@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,14 +1,10 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
-
3
2
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
-
5
3
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
-
7
4
  import { fixTablesKey } from '../pm-plugins/plugin-key';
8
5
  import { TableMap, TableProblemTypes } from '../table-map';
9
6
  import { removeColSpan } from './colspan';
10
7
  import { tableNodeTypes } from './table-node-types';
11
-
12
8
  // Helper for iterating through the nodes in a document that changed
13
9
  // compared to the given previous document. Useful for avoiding
14
10
  // duplicate work on each transaction.
@@ -16,80 +12,68 @@ function changedDescendants(old, cur, offsetStart, f) {
16
12
  var offset = offsetStart;
17
13
  var oldSize = old.childCount;
18
14
  var curSize = cur.childCount;
19
-
20
15
  outer: for (var i = 0, j = 0; i < curSize; i++) {
21
16
  var _child = cur.child(i);
22
-
23
17
  for (var scan = j, e = Math.min(oldSize, i + 3); scan < e; scan++) {
24
18
  if (old.child(scan) === _child) {
25
19
  j = scan + 1;
26
- offset += _child.nodeSize; // eslint-disable-next-line no-continue
27
-
20
+ offset += _child.nodeSize;
21
+ // eslint-disable-next-line no-continue
28
22
  continue outer;
29
23
  }
30
24
  }
31
-
32
25
  f(_child, offset);
33
-
34
26
  if (j < oldSize && old.child(j).sameMarkup(_child)) {
35
27
  changedDescendants(old.child(j), _child, offset + 1, f);
36
28
  } else {
37
29
  _child.nodesBetween(0, _child.content.size, f, offset + 1);
38
30
  }
39
-
40
31
  offset += _child.nodeSize;
41
32
  }
42
- } // :: (EditorState, ?EditorState) → ?Transaction
33
+ }
34
+
35
+ // :: (EditorState, ?EditorState) → ?Transaction
43
36
  // Inspect all tables in the given state's document and return a
44
37
  // transaction that fixes them, if necessary. If `oldState` was
45
38
  // provided, that is assumed to hold a previous, known-good state,
46
39
  // which will be used to avoid re-scanning unchanged parts of the
47
40
  // document.
48
-
49
-
50
41
  export function fixTables(state, oldState, reportFixedTable) {
51
42
  var tr;
52
-
53
43
  var check = function check(node, pos) {
54
44
  if (node.type.spec.tableRole === 'table') {
55
45
  tr = fixTable(state, node, pos, tr, reportFixedTable);
56
46
  }
57
47
  };
58
-
59
48
  if (!oldState) {
60
49
  state.doc.descendants(check);
61
50
  } else if (oldState.doc !== state.doc) {
62
51
  changedDescendants(oldState.doc, state.doc, 0, check);
63
52
  }
64
-
65
53
  return tr;
66
- } // : (EditorState, Node, number, ?Transaction) → ?Transaction
54
+ }
55
+
56
+ // : (EditorState, Node, number, ?Transaction) → ?Transaction
67
57
  // Fix the given table, if necessary. Will append to the transaction
68
58
  // it was given, if non-null, or create a new one if necessary.
69
-
70
59
  export function fixTable(state, table, tablePos, transaction, reportFixedTable) {
71
60
  var tr = transaction;
72
61
  var map = TableMap.get(table);
73
-
74
62
  if (!map.problems) {
75
63
  return tr;
76
64
  }
77
-
78
65
  if (!tr) {
79
66
  tr = state.tr;
80
- } // Track which rows we must add cells to, so that we can adjust that
81
- // when fixing collisions.
82
-
67
+ }
83
68
 
69
+ // Track which rows we must add cells to, so that we can adjust that
70
+ // when fixing collisions.
84
71
  var mustAdd = [];
85
-
86
72
  for (var i = 0; i < map.height; i++) {
87
73
  mustAdd.push(0);
88
74
  }
89
-
90
75
  for (var _i = 0; _i < map.problems.length; _i++) {
91
76
  var prob = map.problems[_i];
92
-
93
77
  if (reportFixedTable) {
94
78
  reportFixedTable({
95
79
  state: state,
@@ -97,90 +81,70 @@ export function fixTable(state, table, tablePos, transaction, reportFixedTable)
97
81
  reason: prob.type || 'unknown'
98
82
  });
99
83
  }
100
-
101
84
  if (prob.type === TableProblemTypes.COLLISION) {
102
85
  var collision = prob;
103
86
  var cell = table.nodeAt(prob.pos);
104
-
105
87
  if (!cell) {
106
88
  throw new Error("fixTable: unable to find cell at pos ".concat(prob.pos));
107
89
  }
108
-
109
90
  for (var j = 0; j < cell.attrs.rowspan; j++) {
110
91
  mustAdd[collision.row + j] += collision.n;
111
92
  }
112
-
113
93
  tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + prob.pos), undefined, removeColSpan(cell.attrs, cell.attrs.colspan - collision.n, collision.n));
114
94
  } else if (prob.type === TableProblemTypes.MISSING) {
115
95
  var missing = prob;
116
96
  mustAdd[missing.row] += missing.n;
117
97
  } else if (prob.type === TableProblemTypes.OVERLONG_ROWSPAN) {
118
98
  var overlong = prob;
119
-
120
99
  var _cell = table.nodeAt(overlong.pos);
121
-
122
100
  if (!_cell) {
123
101
  throw new Error("fixTable: unable to find cell at pos ".concat(prob.pos));
124
102
  }
125
-
126
103
  tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + overlong.pos), undefined, _objectSpread(_objectSpread({}, _cell.attrs), {}, {
127
104
  rowspan: _cell.attrs.rowspan - overlong.n
128
105
  }));
129
106
  } else if (prob.type === TableProblemTypes.COLWIDTH_MISMATCH) {
130
107
  var _cell2 = table.nodeAt(prob.pos);
131
-
132
108
  if (!_cell2) {
133
109
  throw new Error("fixTable: unable to find cell at pos ".concat(prob.pos));
134
110
  }
135
-
136
111
  tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + prob.pos), undefined, _objectSpread(_objectSpread({}, _cell2.attrs), {}, {
137
112
  colwidth: prob.colwidth
138
113
  }));
139
114
  }
140
115
  }
141
-
142
116
  var first;
143
117
  var last;
144
-
145
118
  for (var _i2 = 0; _i2 < mustAdd.length; _i2++) {
146
119
  if (mustAdd[_i2]) {
147
120
  if (first == null) {
148
121
  first = _i2;
149
122
  }
150
-
151
123
  last = _i2;
152
124
  }
153
- } // Add the necessary cells, using a heuristic for whether to add the
125
+ }
126
+ // Add the necessary cells, using a heuristic for whether to add the
154
127
  // cells at the start or end of the rows (if it looks like a 'bite'
155
128
  // was taken out of the table, add cells at the start of the row
156
129
  // after the bite. Otherwise add them at the end).
157
-
158
-
159
130
  for (var _i3 = 0, pos = tablePos + 1; _i3 < map.height; _i3++) {
160
131
  var row = table.child(_i3);
161
132
  var end = pos + row.nodeSize;
162
133
  var add = mustAdd[_i3];
163
-
164
134
  if (add > 0) {
165
135
  var tableNodeType = 'cell';
166
-
167
136
  if (row.firstChild) {
168
137
  tableNodeType = row.firstChild.type.spec.tableRole;
169
138
  }
170
-
171
139
  var nodes = [];
172
-
173
140
  for (var _j = 0; _j < add; _j++) {
174
141
  nodes.push(tableNodeTypes(state.schema)[tableNodeType].createAndFill());
175
142
  }
176
-
177
143
  var side = (_i3 === 0 || first === _i3 - 1) && last === _i3 ? pos + 1 : end - 1;
178
144
  tr.insert(tr.mapping.map(side), nodes);
179
145
  }
180
-
181
146
  pos = end;
182
147
  }
183
-
184
148
  return tr.setMeta(fixTablesKey, {
185
149
  fixTables: true
186
150
  });
@@ -1,46 +1,41 @@
1
1
  import { Selection } from 'prosemirror-state';
2
2
  import { cloneTr } from './clone-tr';
3
3
  import { getCellsInColumn } from './get-cells-in-column';
4
- import { getCellsInRow } from './get-cells-in-row'; // Returns a new transaction that maps a given `cellTransform` function to each cell in a column at a given `columnIndex`.
5
- // It will set the selection into the last cell of the column if `setCursorToLastCell` param is set to `true`.
4
+ import { getCellsInRow } from './get-cells-in-row';
6
5
 
6
+ // Returns a new transaction that maps a given `cellTransform` function to each cell in a column at a given `columnIndex`.
7
+ // It will set the selection into the last cell of the column if `setCursorToLastCell` param is set to `true`.
7
8
  export var forEachCellInColumn = function forEachCellInColumn(columnIndex, cellTransform, setCursorToLastCell) {
8
9
  return function (tr) {
9
10
  var cells = getCellsInColumn(columnIndex)(tr.selection);
10
-
11
11
  if (cells) {
12
12
  for (var i = cells.length - 1; i >= 0; i--) {
13
13
  tr = cellTransform(cells[i], tr);
14
14
  }
15
-
16
15
  if (setCursorToLastCell) {
17
16
  var $pos = tr.doc.resolve(tr.mapping.map(cells[cells.length - 1].pos));
18
17
  tr.setSelection(Selection.near($pos));
19
18
  }
20
-
21
19
  return cloneTr(tr);
22
20
  }
23
-
24
21
  return tr;
25
22
  };
26
- }; // Returns a new transaction that maps a given `cellTransform` function to each cell in a row at a given `rowIndex`.
27
- // It will set the selection into the last cell of the row if `setCursorToLastCell` param is set to `true`.
23
+ };
28
24
 
25
+ // Returns a new transaction that maps a given `cellTransform` function to each cell in a row at a given `rowIndex`.
26
+ // It will set the selection into the last cell of the row if `setCursorToLastCell` param is set to `true`.
29
27
  export var forEachCellInRow = function forEachCellInRow(rowIndex, cellTransform, setCursorToLastCell) {
30
28
  return function (tr) {
31
29
  var cells = getCellsInRow(rowIndex)(tr.selection);
32
-
33
30
  if (cells) {
34
31
  for (var i = cells.length - 1; i >= 0; i--) {
35
32
  tr = cellTransform(cells[i], tr);
36
33
  }
37
-
38
34
  if (setCursorToLastCell) {
39
35
  var $pos = tr.doc.resolve(tr.mapping.map(cells[cells.length - 1].pos));
40
36
  tr.setSelection(Selection.near($pos));
41
37
  }
42
38
  }
43
-
44
39
  return tr;
45
40
  };
46
41
  };
@@ -8,17 +8,15 @@ export function getCellSelectionRanges($anchorCell, $headCell) {
8
8
  var doc = $anchorCell.node(0);
9
9
  var cells = map.cellsInRect(rect).filter(function (p) {
10
10
  return p !== $headCell.pos - start;
11
- }); // Make the head cell the first range, so that it counts as the
11
+ });
12
+ // Make the head cell the first range, so that it counts as the
12
13
  // primary part of the selection
13
-
14
14
  cells.unshift($headCell.pos - start);
15
15
  return cells.map(function (pos) {
16
16
  var cell = table.nodeAt(pos);
17
-
18
17
  if (cell === null) {
19
18
  throw new Error("No cell at position ".concat(pos));
20
19
  }
21
-
22
20
  var from = pos + start + 1;
23
21
  return new SelectionRange(doc.resolve(from), doc.resolve(from + cell.content.size));
24
22
  });
@@ -1,14 +1,13 @@
1
1
  import { TableMap } from '../table-map';
2
- import { findTable } from './find'; // Returns an array of cells in a column(s), where `columnIndex` could be a column index or an array of column indexes.
2
+ import { findTable } from './find';
3
3
 
4
+ // Returns an array of cells in a column(s), where `columnIndex` could be a column index or an array of column indexes.
4
5
  export var getCellsInColumn = function getCellsInColumn(columnIndexes) {
5
6
  return function (selection) {
6
7
  var table = findTable(selection);
7
-
8
8
  if (!table) {
9
9
  return;
10
10
  }
11
-
12
11
  var map = TableMap.get(table.node);
13
12
  var indexes = Array.isArray(columnIndexes) ? columnIndexes : [columnIndexes];
14
13
  return indexes.filter(function (index) {
@@ -1,14 +1,13 @@
1
1
  import { TableMap } from '../table-map';
2
- import { findTable } from './find'; // Returns an array of cells in a row(s), where `rowIndex` could be a row index or an array of row indexes.
2
+ import { findTable } from './find';
3
3
 
4
+ // Returns an array of cells in a row(s), where `rowIndex` could be a row index or an array of row indexes.
4
5
  export var getCellsInRow = function getCellsInRow(rowIndex) {
5
6
  return function (selection) {
6
7
  var table = findTable(selection);
7
-
8
8
  if (!table) {
9
9
  return;
10
10
  }
11
-
12
11
  var map = TableMap.get(table.node);
13
12
  var indexes = Array.isArray(rowIndex) ? rowIndex : [rowIndex];
14
13
  return indexes.filter(function (index) {
@@ -1,13 +1,12 @@
1
1
  import { TableMap } from '../table-map';
2
- import { findTable } from './find'; // Returns an array of all cells in a table.
2
+ import { findTable } from './find';
3
3
 
4
+ // Returns an array of all cells in a table.
4
5
  export var getCellsInTable = function getCellsInTable(selection) {
5
6
  var table = findTable(selection);
6
-
7
7
  if (!table) {
8
8
  return;
9
9
  }
10
-
11
10
  var map = TableMap.get(table.node);
12
11
  var positions = map.cellsInRect({
13
12
  left: 0,
@@ -17,7 +16,6 @@ export var getCellsInTable = function getCellsInTable(selection) {
17
16
  });
18
17
  return positions.map(function (cellPos) {
19
18
  var node = table.node.nodeAt(cellPos);
20
-
21
19
  if (node) {
22
20
  var pos = cellPos + table.start;
23
21
  return {
@@ -1,78 +1,65 @@
1
1
  import { getCellsInColumn } from './get-cells-in-column';
2
- import { getCellsInRow } from './get-cells-in-row'; // Returns a range of rectangular selection spanning all merged cells around a column at index `columnIndex`.
2
+ import { getCellsInRow } from './get-cells-in-row';
3
3
 
4
+ // Returns a range of rectangular selection spanning all merged cells around a column at index `columnIndex`.
4
5
  export var getSelectionRangeInColumn = function getSelectionRangeInColumn(columnIndex) {
5
6
  return function (tr) {
6
7
  var startIndex = columnIndex;
7
- var endIndex = columnIndex; // looking for selection start column (startIndex)
8
+ var endIndex = columnIndex;
8
9
 
10
+ // looking for selection start column (startIndex)
9
11
  var _loop = function _loop(i) {
10
12
  var cells = getCellsInColumn(i)(tr.selection);
11
-
12
13
  if (cells) {
13
14
  cells.forEach(function (cell) {
14
15
  var maybeEndIndex = cell.node.attrs.colspan + i - 1;
15
-
16
16
  if (maybeEndIndex >= startIndex) {
17
17
  startIndex = i;
18
18
  }
19
-
20
19
  if (maybeEndIndex > endIndex) {
21
20
  endIndex = maybeEndIndex;
22
21
  }
23
22
  });
24
23
  }
25
24
  };
26
-
27
25
  for (var i = columnIndex; i >= 0; i--) {
28
26
  _loop(i);
29
- } // looking for selection end column (endIndex)
30
-
31
-
27
+ }
28
+ // looking for selection end column (endIndex)
32
29
  var _loop2 = function _loop2(_i) {
33
30
  var cells = getCellsInColumn(_i)(tr.selection);
34
-
35
31
  if (cells) {
36
32
  cells.forEach(function (cell) {
37
33
  var maybeEndIndex = cell.node.attrs.colspan + _i - 1;
38
-
39
34
  if (cell.node.attrs.colspan > 1 && maybeEndIndex > endIndex) {
40
35
  endIndex = maybeEndIndex;
41
36
  }
42
37
  });
43
38
  }
44
39
  };
45
-
46
40
  for (var _i = columnIndex; _i <= endIndex; _i++) {
47
41
  _loop2(_i);
48
- } // filter out columns without cells (where all rows have colspan > 1 in the same column)
49
-
42
+ }
50
43
 
44
+ // filter out columns without cells (where all rows have colspan > 1 in the same column)
51
45
  var indexes = [];
52
-
53
46
  for (var _i2 = startIndex; _i2 <= endIndex; _i2++) {
54
47
  var maybeCells = getCellsInColumn(_i2)(tr.selection);
55
-
56
48
  if (maybeCells && maybeCells.length) {
57
49
  indexes.push(_i2);
58
50
  }
59
51
  }
60
-
61
52
  startIndex = indexes[0];
62
53
  endIndex = indexes[indexes.length - 1];
63
54
  var firstSelectedColumnCells = getCellsInColumn(startIndex)(tr.selection);
64
55
  var firstRowCells = getCellsInRow(0)(tr.selection);
65
-
66
56
  if (!firstSelectedColumnCells || !firstRowCells) {
67
57
  return;
68
58
  }
69
-
70
59
  var $anchor = tr.doc.resolve(firstSelectedColumnCells[firstSelectedColumnCells.length - 1].pos);
71
60
  var headCell;
72
-
73
61
  for (var _i3 = endIndex; _i3 >= startIndex; _i3--) {
74
62
  var columnCells = getCellsInColumn(_i3)(tr.selection);
75
-
76
63
  if (columnCells && columnCells.length) {
77
64
  for (var j = firstRowCells.length - 1; j >= 0; j--) {
78
65
  if (firstRowCells[j].pos === columnCells[0].pos) {
@@ -80,17 +67,14 @@ export var getSelectionRangeInColumn = function getSelectionRangeInColumn(column
80
67
  break;
81
68
  }
82
69
  }
83
-
84
70
  if (headCell) {
85
71
  break;
86
72
  }
87
73
  }
88
74
  }
89
-
90
75
  if (!headCell) {
91
76
  return;
92
77
  }
93
-
94
78
  var $head = tr.doc.resolve(headCell.pos);
95
79
  return {
96
80
  $anchor: $anchor,
@@ -1,78 +1,65 @@
1
1
  import { getCellsInColumn } from './get-cells-in-column';
2
- import { getCellsInRow } from './get-cells-in-row'; // Returns a range of rectangular selection spanning all merged cells around a row at index `rowIndex`.
2
+ import { getCellsInRow } from './get-cells-in-row';
3
3
 
4
+ // Returns a range of rectangular selection spanning all merged cells around a row at index `rowIndex`.
4
5
  export var getSelectionRangeInRow = function getSelectionRangeInRow(rowIndex) {
5
6
  return function (tr) {
6
7
  var startIndex = rowIndex;
7
- var endIndex = rowIndex; // looking for selection start row (startIndex)
8
+ var endIndex = rowIndex;
8
9
 
10
+ // looking for selection start row (startIndex)
9
11
  var _loop = function _loop(i) {
10
12
  var cells = getCellsInRow(i)(tr.selection);
11
-
12
13
  if (cells) {
13
14
  cells.forEach(function (cell) {
14
15
  var maybeEndIndex = cell.node.attrs.rowspan + i - 1;
15
-
16
16
  if (maybeEndIndex >= startIndex) {
17
17
  startIndex = i;
18
18
  }
19
-
20
19
  if (maybeEndIndex > endIndex) {
21
20
  endIndex = maybeEndIndex;
22
21
  }
23
22
  });
24
23
  }
25
24
  };
26
-
27
25
  for (var i = rowIndex; i >= 0; i--) {
28
26
  _loop(i);
29
- } // looking for selection end row (endIndex)
30
-
31
-
27
+ }
28
+ // looking for selection end row (endIndex)
32
29
  var _loop2 = function _loop2(_i) {
33
30
  var cells = getCellsInRow(_i)(tr.selection);
34
-
35
31
  if (cells) {
36
32
  cells.forEach(function (cell) {
37
33
  var maybeEndIndex = cell.node.attrs.rowspan + _i - 1;
38
-
39
34
  if (cell.node.attrs.rowspan > 1 && maybeEndIndex > endIndex) {
40
35
  endIndex = maybeEndIndex;
41
36
  }
42
37
  });
43
38
  }
44
39
  };
45
-
46
40
  for (var _i = rowIndex; _i <= endIndex; _i++) {
47
41
  _loop2(_i);
48
- } // filter out rows without cells (where all columns have rowspan > 1 in the same row)
49
-
42
+ }
50
43
 
44
+ // filter out rows without cells (where all columns have rowspan > 1 in the same row)
51
45
  var indexes = [];
52
-
53
46
  for (var _i2 = startIndex; _i2 <= endIndex; _i2++) {
54
47
  var maybeCells = getCellsInRow(_i2)(tr.selection);
55
-
56
48
  if (maybeCells && maybeCells.length) {
57
49
  indexes.push(_i2);
58
50
  }
59
51
  }
60
-
61
52
  startIndex = indexes[0];
62
53
  endIndex = indexes[indexes.length - 1];
63
54
  var firstSelectedRowCells = getCellsInRow(startIndex)(tr.selection);
64
55
  var firstColumnCells = getCellsInColumn(0)(tr.selection);
65
-
66
56
  if (!firstSelectedRowCells || !firstColumnCells) {
67
57
  return;
68
58
  }
69
-
70
59
  var $anchor = tr.doc.resolve(firstSelectedRowCells[firstSelectedRowCells.length - 1].pos);
71
60
  var headCell;
72
-
73
61
  for (var _i3 = endIndex; _i3 >= startIndex; _i3--) {
74
62
  var rowCells = getCellsInRow(_i3)(tr.selection);
75
-
76
63
  if (rowCells && rowCells.length) {
77
64
  for (var j = firstColumnCells.length - 1; j >= 0; j--) {
78
65
  if (firstColumnCells[j].pos === rowCells[0].pos) {
@@ -80,17 +67,14 @@ export var getSelectionRangeInRow = function getSelectionRangeInRow(rowIndex) {
80
67
  break;
81
68
  }
82
69
  }
83
-
84
70
  if (headCell) {
85
71
  break;
86
72
  }
87
73
  }
88
74
  }
89
-
90
75
  if (!headCell) {
91
76
  return;
92
77
  }
93
-
94
78
  var $head = tr.doc.resolve(headCell.pos);
95
79
  return {
96
80
  $anchor: $anchor,
@@ -1,11 +1,11 @@
1
1
  import { TableMap } from '../table-map';
2
- import { isSelectionType } from './is-selection-type'; // Get the selection rectangle. Returns `undefined` if selection is not a CellSelection.
2
+ import { isSelectionType } from './is-selection-type';
3
3
 
4
+ // Get the selection rectangle. Returns `undefined` if selection is not a CellSelection.
4
5
  export var getSelectionRect = function getSelectionRect(selection) {
5
6
  if (!isSelectionType(selection, 'cell')) {
6
7
  return;
7
8
  }
8
-
9
9
  var start = selection.$anchorCell.start(-1);
10
10
  var map = TableMap.get(selection.$anchorCell.node(-1));
11
11
  return map.rectBetween(selection.$anchorCell.pos - start, selection.$headCell.pos - start);
@@ -1,72 +1,56 @@
1
1
  import { TextSelection } from 'prosemirror-state';
2
2
  import { selectionCell } from './selection-cell';
3
3
  import { isInTable } from './tables';
4
-
5
4
  function moveCellForward($pos) {
6
5
  var nextNodeSize = $pos.nodeAfter ? $pos.nodeAfter.nodeSize : 0;
7
6
  return $pos.node(0).resolve($pos.pos + nextNodeSize);
8
- } // Returns a command for selecting the next (direction=1) or previous
9
- // (direction=-1) cell in a table.
10
-
7
+ }
11
8
 
9
+ // Returns a command for selecting the next (direction=1) or previous
10
+ // (direction=-1) cell in a table.
12
11
  export function goToNextCell(direction) {
13
12
  return function (state, dispatch) {
14
13
  if (!isInTable(state)) {
15
14
  return false;
16
15
  }
17
-
18
16
  var cellSelection = selectionCell(state.selection);
19
-
20
17
  if (!cellSelection) {
21
18
  return false;
22
19
  }
23
-
24
20
  var cell = findNextCell(cellSelection, direction);
25
-
26
21
  if (cell === undefined) {
27
22
  return false;
28
23
  }
29
-
30
24
  if (dispatch) {
31
25
  var $cell = state.doc.resolve(cell);
32
26
  dispatch(state.tr.setSelection(TextSelection.between($cell, moveCellForward($cell))).scrollIntoView());
33
27
  }
34
-
35
28
  return true;
36
29
  };
37
30
  }
38
-
39
31
  function findNextCell($cell, direction) {
40
32
  if (direction === -1) {
41
33
  var before = $cell.nodeBefore;
42
-
43
34
  if (before) {
44
35
  return $cell.pos - before.nodeSize;
45
36
  }
46
-
47
37
  for (var row = $cell.index(-1) - 1, rowEnd = $cell.before(); row >= 0; row--) {
48
38
  var rowNode = $cell.node(-1).child(row);
49
-
50
39
  if (rowNode.childCount && rowNode.lastChild) {
51
40
  return rowEnd - 1 - rowNode.lastChild.nodeSize;
52
41
  }
53
-
54
42
  rowEnd -= rowNode.nodeSize;
55
43
  }
56
44
  } else {
57
45
  if ($cell.index() < $cell.parent.childCount - 1 && $cell.nodeAfter) {
58
46
  return $cell.pos + $cell.nodeAfter.nodeSize;
59
47
  }
60
-
61
48
  var table = $cell.node(-1);
62
-
63
49
  for (var _row = $cell.indexAfter(-1), rowStart = $cell.after(); _row < table.childCount; _row++) {
64
50
  var _rowNode = table.child(_row);
65
-
66
51
  if (_rowNode.childCount) {
67
52
  return rowStart + 1;
68
53
  }
69
-
70
54
  rowStart += _rowNode.nodeSize;
71
55
  }
72
56
  }