@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
@@ -4,7 +4,9 @@ import { drawCellSelection } from '../utils/draw-cell-selection';
4
4
  import { fixTables } from '../utils/fix-tables';
5
5
  import { normalizeSelection } from '../utils/normalize-selection';
6
6
  import { handleKeyDown, handleMouseDown, handleTripleClick } from './input';
7
- import { tableEditingKey } from './plugin-key'; // :: () → Plugin
7
+ import { tableEditingKey } from './plugin-key';
8
+
9
+ // :: () → Plugin
8
10
  //
9
11
  // Creates a [plugin](http://prosemirror.net/docs/ref/#state.Plugin)
10
12
  // that, when added to an editor, enables cell-selection, handles
@@ -16,13 +18,11 @@ import { tableEditingKey } from './plugin-key'; // :: () → Plugin
16
18
  // rather broadly, and other plugins, like the gap cursor or the
17
19
  // column-width dragging plugin, might want to get a turn first to
18
20
  // perform more specific behavior.
19
-
20
21
  export function tableEditing() {
21
22
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
22
- _ref$allowTableNodeSe = _ref.allowTableNodeSelection,
23
- allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe,
24
- reportFixedTable = _ref.reportFixedTable;
25
-
23
+ _ref$allowTableNodeSe = _ref.allowTableNodeSelection,
24
+ allowTableNodeSelection = _ref$allowTableNodeSe === void 0 ? false : _ref$allowTableNodeSe,
25
+ reportFixedTable = _ref.reportFixedTable;
26
26
  return new Plugin({
27
27
  key: tableEditingKey,
28
28
  // This piece of state is used to remember when a mouse-drag
@@ -35,19 +35,15 @@ export function tableEditing() {
35
35
  apply: function apply(unsafeTr, cur) {
36
36
  var tr = unsafeTr;
37
37
  var set = tr.getMeta(tableEditingKey);
38
-
39
38
  if (set != null) {
40
39
  return set === -1 ? null : set;
41
40
  }
42
-
43
41
  if (cur == null || !tr.docChanged) {
44
42
  return cur;
45
43
  }
46
-
47
44
  var _tr$mapping$mapResult = tr.mapping.mapResult(cur),
48
- deleted = _tr$mapping$mapResult.deleted,
49
- pos = _tr$mapping$mapResult.pos;
50
-
45
+ deleted = _tr$mapping$mapResult.deleted,
46
+ pos = _tr$mapping$mapResult.pos;
51
47
  return deleted ? null : pos;
52
48
  }
53
49
  },
@@ -60,7 +56,6 @@ export function tableEditing() {
60
56
  if (tableEditingKey.getState(view.state) != null) {
61
57
  return view.state.selection;
62
58
  }
63
-
64
59
  return null;
65
60
  },
66
61
  handleTripleClick: handleTripleClick,
@@ -10,16 +10,14 @@ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
10
10
  // document-relative positions. So code that uses them will typically
11
11
  // compute the start position of the table and offset positions passed
12
12
  // to or gotten from this structure by that amount.
13
- var readFromCache, addToCache; // Prefer using a weak map to cache table maps. Fall back on a
13
+ var readFromCache, addToCache;
14
+ // Prefer using a weak map to cache table maps. Fall back on a
14
15
  // fixed-size cache if that's not supported.
15
-
16
16
  if (typeof WeakMap !== 'undefined') {
17
17
  var cache = new WeakMap();
18
-
19
18
  readFromCache = function readFromCache(key) {
20
19
  return cache.get(key);
21
20
  };
22
-
23
21
  addToCache = function addToCache(key, value) {
24
22
  cache.set(key, value);
25
23
  return value;
@@ -28,7 +26,6 @@ if (typeof WeakMap !== 'undefined') {
28
26
  var _cache = [];
29
27
  var cacheSize = 10;
30
28
  var cachePos = 0;
31
-
32
29
  readFromCache = function readFromCache(key) {
33
30
  for (var i = 0; i < _cache.length; i += 2) {
34
31
  if (_cache[i] === key) {
@@ -36,90 +33,81 @@ if (typeof WeakMap !== 'undefined') {
36
33
  }
37
34
  }
38
35
  };
39
-
40
36
  addToCache = function addToCache(key, value) {
41
37
  if (cachePos === cacheSize) {
42
38
  cachePos = 0;
43
39
  }
44
-
45
40
  _cache[cachePos++] = key;
46
41
  return _cache[cachePos++] = value;
47
42
  };
48
43
  }
49
-
50
44
  export var Rect = /*#__PURE__*/_createClass(function Rect(left, top, right, bottom) {
51
45
  _classCallCheck(this, Rect);
52
-
53
46
  this.left = left;
54
47
  this.top = top;
55
48
  this.right = right;
56
49
  this.bottom = bottom;
57
50
  });
58
- export var TableProblemTypes;
59
-
60
- (function (TableProblemTypes) {
51
+ export var TableProblemTypes = /*#__PURE__*/function (TableProblemTypes) {
61
52
  TableProblemTypes["COLLISION"] = "collision";
62
53
  TableProblemTypes["OVERLONG_ROWSPAN"] = "overlong_rowspan";
63
54
  TableProblemTypes["MISSING"] = "missing";
64
55
  TableProblemTypes["COLWIDTH_MISMATCH"] = "colwidth mismatch";
65
- })(TableProblemTypes || (TableProblemTypes = {}));
66
-
56
+ return TableProblemTypes;
57
+ }({});
67
58
  // Ideally tableNewColumnMinWidth should be imported
68
59
  // from '@atlaskit/editor-common/styles';
69
60
  // We don't want to introduce a new dependency.
70
61
  // Thus we define the constant here.
71
- export var tableNewColumnMinWidth = 140; // ::- A table map describes the structore of a given table. To avoid
62
+ export var tableNewColumnMinWidth = 140;
63
+
64
+ // ::- A table map describes the structore of a given table. To avoid
72
65
  // recomputing them all the time, they are cached per table node. To
73
66
  // be able to do that, positions saved in the map are relative to the
74
67
  // start of the table, rather than the start of the document.
75
-
76
68
  export var TableMap = /*#__PURE__*/function () {
77
69
  // The width of the table
70
+
78
71
  // The table's height
72
+
79
73
  function TableMap(width, height, map, problems) {
80
74
  _classCallCheck(this, TableMap);
81
-
82
75
  this.width = width;
83
- this.height = height; // :: [number] A width * height array with the start position of
76
+ this.height = height;
77
+ // :: [number] A width * height array with the start position of
84
78
  // the cell covering that part of the table in each slot
85
-
86
- this.map = map; // An optional array of problems (cell overlap or non-rectangular
79
+ this.map = map;
80
+ // An optional array of problems (cell overlap or non-rectangular
87
81
  // shape) for the table, used by the table normalizer.
88
-
89
82
  this.problems = problems;
90
- } // :: (number) → Rect
91
- // Find the dimensions of the cell at the given position.
92
-
83
+ }
93
84
 
85
+ // :: (number) → Rect
86
+ // Find the dimensions of the cell at the given position.
94
87
  _createClass(TableMap, [{
95
88
  key: "findCell",
96
89
  value: function findCell(pos) {
97
90
  for (var i = 0; i < this.map.length; i++) {
98
91
  var curPos = this.map[i];
99
-
100
92
  if (curPos !== pos) {
101
93
  continue;
102
94
  }
103
-
104
95
  var left = i % this.width;
105
96
  var top = i / this.width | 0;
106
97
  var right = left + 1;
107
98
  var bottom = top + 1;
108
-
109
99
  for (var j = 1; right < this.width && this.map[i + j] === curPos; j++) {
110
100
  right++;
111
101
  }
112
-
113
102
  for (var _j = 1; bottom < this.height && this.map[i + this.width * _j] === curPos; _j++) {
114
103
  bottom++;
115
104
  }
116
-
117
105
  return new Rect(left, top, right, bottom);
118
106
  }
119
-
120
107
  throw new RangeError('No cell with offset ' + pos + ' found');
121
- } // Find the left side of the cell at the given position.
108
+ }
122
109
 
110
+ // Find the left side of the cell at the given position.
123
111
  }, {
124
112
  key: "colCount",
125
113
  value: function colCount(pos) {
@@ -128,23 +116,21 @@ export var TableMap = /*#__PURE__*/function () {
128
116
  return i % this.width;
129
117
  }
130
118
  }
131
-
132
119
  throw new RangeError('No cell with offset ' + pos + ' found');
133
- } // Find the top side of the cell at the given position.
120
+ }
134
121
 
122
+ // Find the top side of the cell at the given position.
135
123
  }, {
136
124
  key: "rowCount",
137
125
  value: function rowCount(pos) {
138
126
  if (this.width <= 0) {
139
127
  throw new RangeError('Wrong table width found');
140
128
  }
141
-
142
129
  for (var i = 0; i < this.map.length; i++) {
143
130
  if (this.map[i] === pos) {
144
131
  return Math.floor(i / this.width);
145
132
  }
146
133
  }
147
-
148
134
  throw new RangeError('No cell with offset ' + pos + ' found');
149
135
  }
150
136
  }, {
@@ -153,100 +139,91 @@ export var TableMap = /*#__PURE__*/function () {
153
139
  return this.map.filter(function (cellPos) {
154
140
  return cellPos === pos;
155
141
  }).length > 1;
156
- } // :: (number, string, number) → ?number
142
+ }
143
+
144
+ // :: (number, string, number) → ?number
157
145
  // Find the next cell in the given direction, starting from the cell
158
146
  // at `pos`, if any.
159
-
160
147
  }, {
161
148
  key: "nextCell",
162
149
  value: function nextCell(pos, axis, dir) {
163
150
  var _this$findCell = this.findCell(pos),
164
- left = _this$findCell.left,
165
- right = _this$findCell.right,
166
- top = _this$findCell.top,
167
- bottom = _this$findCell.bottom;
168
-
151
+ left = _this$findCell.left,
152
+ right = _this$findCell.right,
153
+ top = _this$findCell.top,
154
+ bottom = _this$findCell.bottom;
169
155
  if (axis === 'horiz') {
170
156
  if (dir < 0 ? left === 0 : right === this.width) {
171
157
  return null;
172
158
  }
173
-
174
159
  return this.map[top * this.width + (dir < 0 ? left - 1 : right)];
175
160
  } else {
176
161
  if (dir < 0 ? top === 0 : bottom === this.height) {
177
162
  return null;
178
163
  }
179
-
180
164
  return this.map[left + this.width * (dir < 0 ? top - 1 : bottom)];
181
165
  }
182
- } // :: (number, number) → Rect
183
- // Get the rectangle spanning the two given cells.
166
+ }
184
167
 
168
+ // :: (number, number) → Rect
169
+ // Get the rectangle spanning the two given cells.
185
170
  }, {
186
171
  key: "rectBetween",
187
172
  value: function rectBetween(a, b) {
188
173
  var _this$findCell2 = this.findCell(a),
189
- leftA = _this$findCell2.left,
190
- rightA = _this$findCell2.right,
191
- topA = _this$findCell2.top,
192
- bottomA = _this$findCell2.bottom;
193
-
174
+ leftA = _this$findCell2.left,
175
+ rightA = _this$findCell2.right,
176
+ topA = _this$findCell2.top,
177
+ bottomA = _this$findCell2.bottom;
194
178
  var _this$findCell3 = this.findCell(b),
195
- leftB = _this$findCell3.left,
196
- rightB = _this$findCell3.right,
197
- topB = _this$findCell3.top,
198
- bottomB = _this$findCell3.bottom;
199
-
179
+ leftB = _this$findCell3.left,
180
+ rightB = _this$findCell3.right,
181
+ topB = _this$findCell3.top,
182
+ bottomB = _this$findCell3.bottom;
200
183
  return new Rect(Math.min(leftA, leftB), Math.min(topA, topB), Math.max(rightA, rightB), Math.max(bottomA, bottomB));
201
- } // :: (Rect) → [number]
184
+ }
185
+
186
+ // :: (Rect) → [number]
202
187
  // Return the position of all cells that have the top left corner in
203
188
  // the given rectangle.
204
-
205
189
  }, {
206
190
  key: "cellsInRect",
207
191
  value: function cellsInRect(rect) {
208
192
  var result = [];
209
193
  var seen = {};
210
-
211
194
  for (var row = rect.top; row < rect.bottom; row++) {
212
195
  for (var col = rect.left; col < rect.right; col++) {
213
196
  var index = row * this.width + col;
214
197
  var pos = this.map[index];
215
-
216
198
  if (seen[pos]) {
217
199
  continue;
218
200
  }
219
-
220
201
  seen[pos] = true;
221
-
222
202
  if ((col !== rect.left || !col || this.map[index - 1] !== pos) && (row !== rect.top || !row || this.map[index - this.width] !== pos)) {
223
203
  result.push(pos);
224
204
  }
225
205
  }
226
206
  }
227
-
228
207
  return result;
229
- } // :: (number, number, Node) → number
208
+ }
209
+
210
+ // :: (number, number, Node) → number
230
211
  // Return the position at which the cell at the given row and column
231
212
  // starts, or would start, if a cell started there.
232
-
233
213
  }, {
234
214
  key: "positionAt",
235
215
  value: function positionAt(row, col, table) {
236
216
  for (var i = 0, rowStart = 0;; i++) {
237
217
  var rowEnd = rowStart + table.child(i).nodeSize;
238
-
239
218
  if (i === row) {
240
219
  var index = col + row * this.width;
241
- var rowEndIndex = (row + 1) * this.width; // Skip past cells from previous rows (via rowspan)
242
-
220
+ var rowEndIndex = (row + 1) * this.width;
221
+ // Skip past cells from previous rows (via rowspan)
243
222
  while (index < rowEndIndex && this.map[index] < rowStart) {
244
223
  index++;
245
224
  }
246
-
247
225
  return index === rowEndIndex ? rowEnd - 1 : this.map[index];
248
226
  }
249
-
250
227
  rowStart = rowEnd;
251
228
  }
252
229
  }
@@ -254,58 +231,51 @@ export var TableMap = /*#__PURE__*/function () {
254
231
  key: "getMaxColInRow",
255
232
  value: function getMaxColInRow(pos) {
256
233
  var parentRowNode = pos.parent;
257
-
258
234
  if (parentRowNode.type.name === 'tableRow') {
259
235
  return parentRowNode.childCount;
260
236
  }
261
- } // :: (Node) → TableMap
262
- // Find the table map for the given table node.
237
+ }
263
238
 
239
+ // :: (Node) → TableMap
240
+ // Find the table map for the given table node.
264
241
  }], [{
265
242
  key: "get",
266
243
  value: function get(table) {
267
244
  return readFromCache(table) || addToCache(table, computeMap(table));
268
245
  }
269
246
  }]);
270
-
271
247
  return TableMap;
272
- }(); // Compute a table map.
248
+ }();
273
249
 
250
+ // Compute a table map.
274
251
  function computeMap(table) {
275
252
  if (table.type.spec.tableRole !== 'table') {
276
253
  throw new RangeError('Not a table node: ' + table.type.name);
277
254
  }
278
-
279
255
  var width = findWidth(table);
280
256
  var height = table.childCount;
281
257
  var map = [],
282
- mapPos = 0,
283
- problems = null,
284
- colWidths = [];
285
-
258
+ mapPos = 0,
259
+ problems = null,
260
+ colWidths = [];
286
261
  for (var i = 0, e = width * height; i < e; i++) {
287
262
  map[i] = 0;
288
263
  }
289
-
290
264
  for (var row = 0, pos = 0; row < height; row++) {
291
265
  var rowNode = table.child(row);
292
266
  pos++;
293
-
294
267
  for (var _i = 0;; _i++) {
295
268
  while (mapPos < map.length && map[mapPos] !== 0) {
296
269
  mapPos++;
297
270
  }
298
-
299
271
  if (_i === rowNode.childCount) {
300
272
  break;
301
273
  }
302
-
303
274
  var cellNode = rowNode.child(_i),
304
- _cellNode$attrs = cellNode.attrs,
305
- colspan = _cellNode$attrs.colspan,
306
- rowspan = _cellNode$attrs.rowspan,
307
- colwidth = _cellNode$attrs.colwidth;
308
-
275
+ _cellNode$attrs = cellNode.attrs,
276
+ colspan = _cellNode$attrs.colspan,
277
+ rowspan = _cellNode$attrs.rowspan,
278
+ colwidth = _cellNode$attrs.colwidth;
309
279
  for (var h = 0; h < rowspan; h++) {
310
280
  if (h + row >= height) {
311
281
  (problems || (problems = [])).push({
@@ -315,9 +285,7 @@ function computeMap(table) {
315
285
  });
316
286
  break;
317
287
  }
318
-
319
288
  var start = mapPos + h * width;
320
-
321
289
  for (var w = 0; w < colspan; w++) {
322
290
  if (map[start + w] === 0) {
323
291
  map[start + w] = pos;
@@ -329,13 +297,10 @@ function computeMap(table) {
329
297
  n: colspan - w
330
298
  });
331
299
  }
332
-
333
300
  var colW = colwidth && colwidth[w];
334
-
335
301
  if (colW) {
336
302
  var widthIndex = (start + w) % width * 2,
337
- prev = colWidths[widthIndex];
338
-
303
+ prev = colWidths[widthIndex];
339
304
  if (prev == null || prev !== colW && colWidths[widthIndex + 1] === 1) {
340
305
  colWidths[widthIndex] = colW;
341
306
  colWidths[widthIndex + 1] = 1;
@@ -345,20 +310,16 @@ function computeMap(table) {
345
310
  }
346
311
  }
347
312
  }
348
-
349
313
  mapPos += colspan;
350
314
  pos += cellNode.nodeSize;
351
315
  }
352
-
353
316
  var expectedPos = (row + 1) * width;
354
317
  var missing = 0;
355
-
356
318
  while (mapPos < expectedPos) {
357
319
  if (map[mapPos++] === 0) {
358
320
  missing++;
359
321
  }
360
322
  }
361
-
362
323
  if (missing) {
363
324
  (problems || (problems = [])).push({
364
325
  type: TableProblemTypes.MISSING,
@@ -366,110 +327,88 @@ function computeMap(table) {
366
327
  n: missing
367
328
  });
368
329
  }
369
-
370
330
  pos++;
371
331
  }
372
-
373
332
  var tableMap = new TableMap(width, height, map, problems);
374
- var badWidths = false; // For columns that have defined widths, but whose widths disagree
333
+ var badWidths = false;
334
+
335
+ // For columns that have defined widths, but whose widths disagree
375
336
  // between rows, fix up the cells whose width doesn't match the
376
337
  // computed one.
377
-
378
338
  for (var _i2 = 0; !badWidths && _i2 < colWidths.length; _i2 += 2) {
379
339
  if (colWidths[_i2] != null && colWidths[_i2 + 1] < height) {
380
340
  badWidths = true;
381
341
  }
382
- } // colWidths is an array of numbers, it can look like this
342
+ }
343
+
344
+ // colWidths is an array of numbers, it can look like this
383
345
  // const colWidths = [255, 3, 125, 3, 150, 2, 130, 1];
384
346
  // 255 is a colWidth and 3 is a number of cells with this colwidth.
385
347
  // This check exists to make sure that the table has been resized,
386
348
  // which means there will be elements in the colWidths array.
387
-
388
-
389
349
  if (colWidths.length > 0 && colWidths.length !== width * 2) {
390
350
  for (var _i3 = 0; _i3 < width * 2 - colWidths.length; _i3++) {
391
351
  colWidths.push(tableNewColumnMinWidth, 0);
392
352
  }
393
-
394
353
  badWidths = true;
395
354
  }
396
-
397
355
  if (badWidths) {
398
356
  findBadColWidths(tableMap, colWidths, table);
399
357
  }
400
-
401
358
  return tableMap;
402
359
  }
403
-
404
360
  function findWidth(table) {
405
361
  var width = -1;
406
362
  var hasRowSpan = false;
407
-
408
363
  for (var row = 0; row < table.childCount; row++) {
409
364
  var rowNode = table.child(row);
410
365
  var rowWidth = 0;
411
-
412
366
  if (hasRowSpan) {
413
367
  for (var j = 0; j < row; j++) {
414
368
  var prevRow = table.child(j);
415
-
416
369
  for (var i = 0; i < prevRow.childCount; i++) {
417
370
  var cell = prevRow.child(i);
418
-
419
371
  if (j + cell.attrs.rowspan > row) {
420
372
  rowWidth += cell.attrs.colspan;
421
373
  }
422
374
  }
423
375
  }
424
376
  }
425
-
426
377
  for (var _i4 = 0; _i4 < rowNode.childCount; _i4++) {
427
378
  var _cell = rowNode.child(_i4);
428
-
429
379
  rowWidth += _cell.attrs.colspan;
430
-
431
380
  if (_cell.attrs.rowspan > 1) {
432
381
  hasRowSpan = true;
433
382
  }
434
383
  }
435
-
436
384
  if (width === -1) {
437
385
  width = rowWidth;
438
386
  } else if (width !== rowWidth) {
439
387
  width = Math.max(width, rowWidth);
440
388
  }
441
389
  }
442
-
443
390
  return width;
444
391
  }
445
-
446
392
  function findBadColWidths(map, colWidths, table) {
447
393
  if (!map.problems) {
448
394
  map.problems = [];
449
395
  }
450
-
451
396
  var seen = {};
452
-
453
397
  for (var i = 0; i < map.map.length; i++) {
454
398
  var pos = map.map[i];
455
-
456
399
  if (seen[pos]) {
457
400
  continue;
458
401
  }
459
-
460
402
  seen[pos] = true;
461
403
  var node = table.nodeAt(pos),
462
- updated = null;
463
-
404
+ updated = null;
464
405
  for (var j = 0; j < node.attrs.colspan; j++) {
465
406
  var col = (i + j) % map.width,
466
- colWidth = colWidths[col * 2];
467
-
407
+ colWidth = colWidths[col * 2];
468
408
  if (colWidth != null && (!node.attrs.colwidth || node.attrs.colwidth[j] !== colWidth)) {
469
409
  (updated || (updated = freshColWidth(node.attrs)))[j] = colWidth;
470
410
  }
471
411
  }
472
-
473
412
  if (updated) {
474
413
  map.problems.unshift({
475
414
  type: TableProblemTypes.COLWIDTH_MISMATCH,
@@ -479,17 +418,13 @@ function findBadColWidths(map, colWidths, table) {
479
418
  }
480
419
  }
481
420
  }
482
-
483
421
  function freshColWidth(attrs) {
484
422
  if (attrs.colwidth) {
485
423
  return attrs.colwidth.slice();
486
424
  }
487
-
488
425
  var result = [];
489
-
490
426
  for (var i = 0; i < attrs.colspan; i++) {
491
427
  result.push(0);
492
428
  }
493
-
494
429
  return result;
495
430
  }
@@ -1,15 +1,14 @@
1
1
  import { TableMap } from '../table-map';
2
2
  import { addColumn } from './add-column';
3
3
  import { cloneTr } from './clone-tr';
4
- import { findTable } from './find'; // Returns a new transaction that adds a new column at index `columnIndex`.
4
+ import { findTable } from './find';
5
5
 
6
+ // Returns a new transaction that adds a new column at index `columnIndex`.
6
7
  export var addColumnAt = function addColumnAt(columnIndex) {
7
8
  return function (tr) {
8
9
  var table = findTable(tr.selection);
9
-
10
10
  if (table) {
11
11
  var map = TableMap.get(table.node);
12
-
13
12
  if (columnIndex >= 0 && columnIndex <= map.width) {
14
13
  return cloneTr(addColumn(tr, {
15
14
  map: map,
@@ -18,7 +17,6 @@ export var addColumnAt = function addColumnAt(columnIndex) {
18
17
  }, columnIndex));
19
18
  }
20
19
  }
21
-
22
20
  return tr;
23
21
  };
24
22
  };
@@ -2,66 +2,54 @@ import { addColSpan, assertColspan } from './colspan';
2
2
  import { tableNodeTypes } from './table-node-types';
3
3
  export function columnIsHeader(map, table, col) {
4
4
  var headerCell = tableNodeTypes(table.type.schema).header_cell;
5
-
6
5
  for (var row = 0; row < map.height; row++) {
7
6
  var cell = table.nodeAt(map.map[col + row * map.width]);
8
-
9
7
  if (cell && cell.type !== headerCell) {
10
8
  return false;
11
9
  }
12
10
  }
13
-
14
11
  return true;
15
- } // Add a column at the given position in a table.
12
+ }
16
13
 
14
+ // Add a column at the given position in a table.
17
15
  export function addColumn(tr, _ref, col) {
18
16
  var map = _ref.map,
19
- tableStart = _ref.tableStart,
20
- table = _ref.table;
17
+ tableStart = _ref.tableStart,
18
+ table = _ref.table;
21
19
  var refColumn = col > 0 ? -1 : 0;
22
-
23
20
  if (columnIsHeader(map, table, col + refColumn)) {
24
21
  refColumn = col === 0 || col === map.width ? null : 0;
25
22
  }
26
-
27
23
  for (var row = 0; row < map.height; row++) {
28
- var index = row * map.width + col; // If this position falls inside a col-spanning cell
24
+ var index = row * map.width + col;
29
25
 
26
+ // If this position falls inside a col-spanning cell
30
27
  if (col > 0 && col < map.width && map.map[index - 1] === map.map[index]) {
31
28
  var pos = map.map[index];
32
29
  var cell = table.nodeAt(pos);
33
-
34
30
  if (!cell) {
35
31
  throw new Error("addColumn: invalid cell for pos ".concat(pos));
36
32
  }
37
-
38
33
  var attributes = cell.attrs;
39
34
  assertColspan(attributes);
40
- tr.setNodeMarkup(tr.mapping.map(tableStart + pos), undefined, addColSpan(attributes, col - map.colCount(pos))); // Skip ahead if rowspan > 1
41
-
35
+ tr.setNodeMarkup(tr.mapping.map(tableStart + pos), undefined, addColSpan(attributes, col - map.colCount(pos)));
36
+ // Skip ahead if rowspan > 1
42
37
  row += attributes.rowspan - 1;
43
38
  } else {
44
39
  var type = void 0;
45
-
46
40
  if (refColumn === null) {
47
41
  type = tableNodeTypes(table.type.schema).cell;
48
42
  } else {
49
43
  var mappedPos = map.map[index + refColumn];
50
-
51
44
  var _cell = table.nodeAt(mappedPos);
52
-
53
45
  if (!_cell) {
54
46
  throw new Error("addColumn: invalid node at mapped pos ".concat(mappedPos));
55
47
  }
56
-
57
48
  type = _cell.type;
58
49
  }
59
-
60
50
  var _pos = map.positionAt(row, col, table);
61
-
62
51
  tr.insert(tr.mapping.map(tableStart + _pos), type.createAndFill());
63
52
  }
64
53
  }
65
-
66
54
  return tr;
67
55
  }