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