@atlaskit/adf-schema 29.2.0 → 30.0.1

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 (59) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/steps.js +0 -21
  3. package/dist/es2019/steps.js +0 -3
  4. package/dist/esm/steps.js +0 -3
  5. package/dist/types/schema/nodes/tableNodes.d.ts +6 -2
  6. package/dist/types/steps.d.ts +0 -4
  7. package/package.json +1 -3
  8. package/dist/cjs/steps/table/add-column.js +0 -381
  9. package/dist/cjs/steps/table/constants.js +0 -12
  10. package/dist/cjs/steps/table/sort-column.js +0 -68
  11. package/dist/cjs/steps/table/types.js +0 -5
  12. package/dist/cjs/steps/table/utils/cell-step.js +0 -202
  13. package/dist/cjs/steps/table/utils/cells-at-column.js +0 -73
  14. package/dist/cjs/steps/table/utils/find-column.js +0 -90
  15. package/dist/cjs/steps/table/utils/get-table-rect-from-doc.js +0 -36
  16. package/dist/cjs/steps/table/utils/side-effects/rows.js +0 -315
  17. package/dist/cjs/steps/table/utils/side-effects/side-effects.js +0 -83
  18. package/dist/cjs/steps/table/utils/side-effects/table.js +0 -110
  19. package/dist/cjs/steps/table/utils/side-effects/types.js +0 -5
  20. package/dist/cjs/steps/table/utils/table-map.js +0 -30
  21. package/dist/es2019/steps/table/add-column.js +0 -306
  22. package/dist/es2019/steps/table/constants.js +0 -5
  23. package/dist/es2019/steps/table/sort-column.js +0 -34
  24. package/dist/es2019/steps/table/types.js +0 -1
  25. package/dist/es2019/steps/table/utils/cell-step.js +0 -187
  26. package/dist/es2019/steps/table/utils/cells-at-column.js +0 -52
  27. package/dist/es2019/steps/table/utils/find-column.js +0 -83
  28. package/dist/es2019/steps/table/utils/get-table-rect-from-doc.js +0 -31
  29. package/dist/es2019/steps/table/utils/side-effects/rows.js +0 -257
  30. package/dist/es2019/steps/table/utils/side-effects/side-effects.js +0 -60
  31. package/dist/es2019/steps/table/utils/side-effects/table.js +0 -82
  32. package/dist/es2019/steps/table/utils/side-effects/types.js +0 -1
  33. package/dist/es2019/steps/table/utils/table-map.js +0 -19
  34. package/dist/esm/steps/table/add-column.js +0 -373
  35. package/dist/esm/steps/table/constants.js +0 -5
  36. package/dist/esm/steps/table/sort-column.js +0 -60
  37. package/dist/esm/steps/table/types.js +0 -1
  38. package/dist/esm/steps/table/utils/cell-step.js +0 -192
  39. package/dist/esm/steps/table/utils/cells-at-column.js +0 -66
  40. package/dist/esm/steps/table/utils/find-column.js +0 -84
  41. package/dist/esm/steps/table/utils/get-table-rect-from-doc.js +0 -31
  42. package/dist/esm/steps/table/utils/side-effects/rows.js +0 -307
  43. package/dist/esm/steps/table/utils/side-effects/side-effects.js +0 -75
  44. package/dist/esm/steps/table/utils/side-effects/table.js +0 -102
  45. package/dist/esm/steps/table/utils/side-effects/types.js +0 -1
  46. package/dist/esm/steps/table/utils/table-map.js +0 -19
  47. package/dist/types/steps/table/add-column.d.ts +0 -88
  48. package/dist/types/steps/table/constants.d.ts +0 -4
  49. package/dist/types/steps/table/sort-column.d.ts +0 -18
  50. package/dist/types/steps/table/types.d.ts +0 -39
  51. package/dist/types/steps/table/utils/cell-step.d.ts +0 -47
  52. package/dist/types/steps/table/utils/cells-at-column.d.ts +0 -20
  53. package/dist/types/steps/table/utils/find-column.d.ts +0 -8
  54. package/dist/types/steps/table/utils/get-table-rect-from-doc.d.ts +0 -8
  55. package/dist/types/steps/table/utils/side-effects/rows.d.ts +0 -25
  56. package/dist/types/steps/table/utils/side-effects/side-effects.d.ts +0 -15
  57. package/dist/types/steps/table/utils/side-effects/table.d.ts +0 -16
  58. package/dist/types/steps/table/utils/side-effects/types.d.ts +0 -33
  59. package/dist/types/steps/table/utils/table-map.d.ts +0 -6
@@ -1,306 +0,0 @@
1
- import { Step, StepMap, StepResult, Transform } from '@atlaskit/editor-prosemirror/transform';
2
- import { cellsAtColumn } from './utils/cells-at-column';
3
- import { findColumn } from './utils/find-column';
4
- import { getTableRectFromDoc } from './utils/get-table-rect-from-doc';
5
- import { applyCellStep, createCellStep, getMapFromCellStep, invertCellStep } from './utils/cell-step';
6
- import { SideEffectsHandler } from './utils/side-effects/side-effects';
7
- import memoizeOne from 'memoize-one';
8
- const ADD_COLUMN_STEP = 'ak-add-column';
9
- function printColumnInfo(columnInfo) {
10
- let cellsFrom = [];
11
- for (const cellInfo of columnInfo.values()) {
12
- cellsFrom.push(cellInfo.from);
13
- }
14
- return `[${cellsFrom.join(',')}]`;
15
- }
16
- function createColumnInfo(cellsInfo) {
17
- return new Map(cellsInfo.map(cellInfo => [cellInfo.from, cellInfo]));
18
- }
19
- const getTableRectAndColumnFactory = (doc, pos, columnInfo) => {
20
- return memoizeOne(() => {
21
- const rect = getTableRectFromDoc(doc, pos);
22
- const column = findColumn(columnInfo, rect);
23
- if (column === null) {
24
- throw new Error('no column');
25
- }
26
- return {
27
- rect,
28
- column
29
- };
30
- });
31
- };
32
-
33
- /**
34
- * Index and positions looks like
35
- * 0 1 2 3 -> Add Column Index
36
- * | 5 | 10 | 15 | -> Table with Positions
37
- * | 20 | 25 | 30 |
38
- * 0 1 2 x -> Remove Column Index
39
- *
40
- */
41
- export class AddColumnStep extends Step {
42
- constructor(tablePos, addColumnStepInfo, isDelete = false) {
43
- super();
44
- this.tablePos = tablePos;
45
- this.isDelete = isDelete;
46
- this.sideEffectsHandler = new SideEffectsHandler(addColumnStepInfo.sideEffects);
47
- this.columnInfo = createColumnInfo(addColumnStepInfo.cells);
48
- }
49
-
50
- /**
51
- * Detect the column based on all the cells step in column info.
52
- * Recreate columnInfo based on the current document. We might need to add new cells added by insert row or unmerge cells.
53
- * If isDelete
54
- * Decrease colspan if one row has merged cell
55
- * Remove all the cells using columnInfo.cellStep[].from
56
- * else
57
- * Increase colspan if one row had merged cell
58
- * Add all new cells at columnInfo.cellStep[].from,
59
- * if there is columnInfo.cellStep[].newCell use it
60
- * else create an empty cell
61
- *
62
- * @param doc Current document
63
- */
64
- apply(doc) {
65
- const {
66
- tablePos
67
- } = this;
68
- // Create transform base on the doc
69
- const tr = new Transform(doc);
70
- if (this.sideEffectsHandler.table.handleAddTable(tr, this.isDelete)) {
71
- return StepResult.ok(tr.doc);
72
- }
73
- let tableRect;
74
- try {
75
- tableRect = getTableRectFromDoc(doc, tablePos);
76
- } catch (e) {
77
- return StepResult.fail(e.message);
78
- }
79
- const column = findColumn(this.columnInfo, tableRect);
80
- if (column === null) {
81
- return StepResult.fail(`No column for this cells "${printColumnInfo(this.columnInfo)}" in table at position "${tablePos}".`);
82
- }
83
- if (this.sideEffectsHandler.table.handleRemoveTable(tr, this.tablePos, tableRect, column, this.isDelete)) {
84
- return StepResult.ok(tr.doc);
85
- }
86
- this.columnInfo = this.applyCellSteps(tr, tableRect, column, this.isDelete);
87
-
88
- // Return the document modified.
89
- return StepResult.ok(tr.doc);
90
- }
91
-
92
- /**
93
- * Update tablePos with the new position. If tablePos doesnt exist any more remove the step
94
- * Update all the cellStep inside columnInfo. If cellStep.from position gets deleted removed it from column info
95
- * if cellStep.length === 0 remove the step
96
- * Create a new step with all the position updated
97
- * @param mapping
98
- */
99
- map(mapping) {
100
- const tablePosResult = mapping.mapResult(this.tablePos);
101
- if (tablePosResult.deleted) {
102
- // If table was deleted remove the step
103
- return null;
104
- }
105
- const cellSteps = [];
106
- for (const oldCellStep of this.columnInfo.values()) {
107
- const fromResult = mapping.mapResult(oldCellStep.from);
108
- const toResult = mapping.mapResult(oldCellStep.to);
109
- if (fromResult.deleted && toResult.deleted) {
110
- continue;
111
- }
112
- const cellStep = {
113
- ...oldCellStep,
114
- from: fromResult.pos,
115
- to: toResult.pos
116
- };
117
- if (oldCellStep.mergeWith !== undefined) {
118
- cellStep.mergeWith = mapping.map(oldCellStep.mergeWith);
119
- }
120
- cellSteps.push(cellStep);
121
- }
122
- if (cellSteps.length === 0) {
123
- return null;
124
- }
125
- const sideEffects = this.sideEffectsHandler.map(mapping);
126
- return new AddColumnStep(tablePosResult.pos, {
127
- cells: cellSteps,
128
- sideEffects
129
- }, this.isDelete);
130
- }
131
-
132
- /**
133
- * if isDelete
134
- * Get the original cell node at columnInfo.cellStep[].from to columnInfo.cellStep[].to
135
- * Create a copy of the node
136
- * Create a new cellStep with the same positions but with the clone node as a content
137
- * return new step inverted
138
- * else
139
- * Remove the content from each columnInfo.cellStep[].content
140
- * return new step inverted
141
- * @param originalDoc
142
- */
143
- invert(originalDoc) {
144
- const stepMap = this.getMap();
145
-
146
- // Memoize function to be called only on delete scenarios
147
- const getTableRectAndColumn = getTableRectAndColumnFactory(originalDoc, this.tablePos, this.columnInfo);
148
- // This is needed because the real pos of the cell in the generated document is affected by the previous operations.
149
- const newCellSteps = Array.from(this.columnInfo.values(), oldCellStep => {
150
- const newCellStep = invertCellStep(originalDoc, getTableRectAndColumn, oldCellStep, this.isDelete, stepMap);
151
- return newCellStep;
152
- });
153
- const sideEffects = this.sideEffectsHandler.invert(originalDoc, this.isDelete, stepMap);
154
- return new AddColumnStep(this.tablePos, {
155
- cells: newCellSteps,
156
- sideEffects
157
- }, !this.isDelete);
158
- }
159
-
160
- /**
161
- * StepMap is created based on columnInfo.
162
- * ColumnInfo is created on constructor and once is applied (the document could have new cells that weren't part of the original set)
163
- * if isDelete
164
- * Create range array based on cell info where each range is [cellStep.from, cellStep.from - cellStep.to, 0]
165
- * else
166
- * Create range array base on cell info where each range is [cellStep.from, 0, cellStep.content ? cellStep.content.nodeSize : defaultEmptyCellNodeSize]
167
- *
168
- * Ranges in ProseMirror are represented by each 3 elements in an array.
169
- * As [pos, currentSize, newSize, pos2, currentSize2, newSize2] where:
170
- * pos: Position in the document
171
- * currentSize: Represent the affected range, this will be pos + currentSize
172
- * newSize: Represent the new values, pos + newSize
173
- */
174
- getMap() {
175
- const tableMap = this.sideEffectsHandler.getTableMap(this.isDelete);
176
- if (tableMap) {
177
- return tableMap;
178
- }
179
- let ranges = [];
180
- for (const cellStep of this.columnInfo.values()) {
181
- ranges.push(...getMapFromCellStep(cellStep, this.isDelete));
182
- }
183
- ranges = this.sideEffectsHandler.rows.addRowRanges(ranges, this.isDelete);
184
-
185
- // If no steps, I create am empty stepMap
186
- return new StepMap(ranges);
187
- }
188
-
189
- /**
190
- * Try to merge this step with another one, to be applied directly
191
- * after it. Returns the merged step when possible, null if the
192
- * steps can't be merged.
193
- */
194
- merge(other) {
195
- // We cannot merge add column step at the moment
196
- return null;
197
- }
198
-
199
- /**
200
- * Create a JSON-serializeable representation of this step. When
201
- * defining this for a custom subclass, make sure the result object
202
- * includes the step type's [JSON id](#transform.Step^jsonID) under
203
- * the `stepType` property.
204
- */
205
- toJSON() {
206
- const addColumnStepJson = {
207
- stepType: ADD_COLUMN_STEP,
208
- tablePos: this.tablePos,
209
- cells: Array.from(this.columnInfo.values(), cellStep => {
210
- const cellStepJson = {
211
- from: cellStep.from,
212
- to: cellStep.to
213
- };
214
- if (cellStep.mergeWith !== undefined) {
215
- cellStepJson.mergeWith = cellStep.mergeWith;
216
- }
217
- if (cellStep.newCell !== undefined) {
218
- cellStepJson.newCell = cellStep.newCell.toJSON();
219
- }
220
- return cellStepJson;
221
- }),
222
- isDelete: this.isDelete
223
- };
224
- const sideEffectsJSON = this.sideEffectsHandler.toJSON();
225
- if (sideEffectsJSON) {
226
- addColumnStepJson.sideEffects = sideEffectsJSON;
227
- }
228
- return addColumnStepJson;
229
- }
230
-
231
- /**
232
- * Deserialize a step from its JSON representation. Will call
233
- * through to the step class' own implementation of this method.
234
- */
235
- static fromJSON(schema, json) {
236
- // TODO: Add validation. Return null if it is invalid. Check in review if this is necessary
237
- const cells = json.cells.map(cellsJson => {
238
- const cell = {
239
- ...cellsJson,
240
- newCell: cellsJson.newCell ? schema.nodeFromJSON(cellsJson.newCell) : undefined
241
- };
242
- return cell;
243
- });
244
- let sideEffects;
245
- if (json.sideEffects) {
246
- sideEffects = SideEffectsHandler.fromJSON(schema, json.sideEffects);
247
- }
248
- return new AddColumnStep(json.tablePos, {
249
- cells,
250
- sideEffects
251
- }, json.isDelete);
252
- }
253
- static create(doc, tablePos, column, isDelete = false) {
254
- const tableRect = getTableRectFromDoc(doc, tablePos);
255
-
256
- // By default add column will rely on default behaviour (add empty cell).
257
- // There is no need to add content
258
- const cells = [];
259
- const iter = cellsAtColumn(tableRect, column);
260
- let next = iter.next();
261
- while (!next.done) {
262
- const cell = next.value;
263
- cells.push(createCellStep(cell, column, isDelete));
264
- let skipRows = 0;
265
- if (cell.attrs && cell.attrs.rowspan) {
266
- skipRows = cell.attrs.rowspan - 1;
267
- }
268
- next = iter.next(skipRows);
269
- }
270
- return new AddColumnStep(tablePos, {
271
- cells
272
- }, isDelete);
273
- }
274
- applyCellSteps(tr, tableRect, column, isDelete) {
275
- const newColumnInfo = new Map();
276
- const rowsHandler = this.sideEffectsHandler.rows.start(this.isDelete);
277
- const iter = cellsAtColumn(tableRect, column);
278
- let next = iter.next();
279
- // Iterate for all the cells in the current document
280
- while (!next.done) {
281
- const cell = next.value;
282
- const previousCellStep = this.columnInfo.get(cell.from);
283
- const newCellStep = createCellStep(cell, column, isDelete, previousCellStep);
284
-
285
- // If is the last cell in the row and doesnt have colspan I need to remove the whole row.
286
- const removeRowResult = rowsHandler.handle(tr, tableRect, cell.row, column, cell);
287
- if (removeRowResult.handled) {
288
- next = iter.next(removeRowResult.skipRows);
289
- continue;
290
- }
291
-
292
- // Apply the step, to the pseudo document, get rows to skip, and the cellstep (might be modified, for example, a merge cell that remove the cell instead)
293
- const {
294
- skipRows,
295
- cellStep
296
- } = applyCellStep(tr, tableRect, cell, newCellStep, isDelete, column);
297
-
298
- // Store the new cell step. This could be an existing one or a new cell.
299
- newColumnInfo.set(newCellStep.from, cellStep);
300
- next = iter.next(skipRows);
301
- }
302
- rowsHandler.end(tr, tableRect, column);
303
- return newColumnInfo;
304
- }
305
- }
306
- Step.jsonID(ADD_COLUMN_STEP, AddColumnStep);
@@ -1,5 +0,0 @@
1
- export let SortOrder = /*#__PURE__*/function (SortOrder) {
2
- SortOrder["ASC"] = "asc";
3
- SortOrder["DESC"] = "desc";
4
- return SortOrder;
5
- }({});
@@ -1,34 +0,0 @@
1
- import { Slice } from '@atlaskit/editor-prosemirror/model';
2
- import { ReplaceStep, Step, StepMap, StepResult } from '@atlaskit/editor-prosemirror/transform';
3
- export const tableSortingStepType = 'atlaskit-table-sorting-ordering';
4
- export class TableSortStep extends Step {
5
- constructor(pos, prev, next) {
6
- super();
7
- this.prev = prev;
8
- this.next = next;
9
- this.pos = pos;
10
- }
11
- invert() {
12
- return new TableSortStep(this.pos, this.next, this.prev);
13
- }
14
- apply(doc) {
15
- return StepResult.ok(doc);
16
- }
17
- map() {
18
- return null;
19
- }
20
- getMap() {
21
- return new StepMap([0, 0, 0]);
22
- }
23
- toJSON() {
24
- return {
25
- stepType: tableSortingStepType
26
- };
27
- }
28
- static fromJSON() {
29
- return new ReplaceStep(0, 0, Slice.empty);
30
- }
31
- }
32
-
33
- /** Register this step with Prosemirror */
34
- Step.jsonID(tableSortingStepType, TableSortStep);
@@ -1 +0,0 @@
1
- export {};
@@ -1,187 +0,0 @@
1
- import { addColSpan, removeColSpan } from '@atlaskit/editor-tables/utils';
2
- const EmptyCellNodeSize = 4;
3
- function calculateRowsToSkip(attrs) {
4
- if (attrs && attrs.rowspan) {
5
- return attrs.rowspan - 1;
6
- }
7
- return 0;
8
- }
9
-
10
- /**
11
- * Given a cell step, this function has to do the specific transformation to executed that step.
12
- * It returns the applied cellStep and rows that needs to skip (because it was already handled).
13
- * @param tr
14
- * @param tableRect
15
- * @param cell
16
- * @param cellStep
17
- * @param isDelete
18
- * @param column
19
- */
20
- export function applyCellStep(tr, tableRect, cell, cellStep, isDelete, column) {
21
- // Apply the merge actions,
22
- if (cellStep.mergeWith !== undefined) {
23
- let cellNode = tr.doc.nodeAt(tr.mapping.map(cellStep.mergeWith));
24
- const columns = column - tableRect.map.colCount(cellStep.mergeWith - tableRect.tableStart);
25
- const cellAttrs = isDelete ? removeColSpan(cellNode.attrs, columns) : addColSpan(cellNode.attrs, columns);
26
- if (cellAttrs.colspan > 0) {
27
- // When colspan is 0 should remove the cell
28
- tr.setNodeMarkup(tr.mapping.map(cellStep.mergeWith), undefined, cellAttrs);
29
- return {
30
- tr,
31
- skipRows: calculateRowsToSkip(cellAttrs),
32
- cellStep
33
- };
34
- }
35
-
36
- // When the new colspan is 0, I need to change the operation to a delete operation
37
- // Update cellStep with the proper data
38
- cellStep.from = cellStep.mergeWith;
39
- cellStep.to = cellStep.from + cellNode.nodeSize;
40
- cellStep.mergeWith = undefined;
41
- }
42
- let skipRows = 0;
43
- // Modify temporary document
44
- if (isDelete) {
45
- let cellNode = tr.doc.nodeAt(tr.mapping.map(cellStep.from));
46
- skipRows = calculateRowsToSkip(cellNode.attrs);
47
- tr.delete(tr.mapping.map(cellStep.from), tr.mapping.map(cellStep.to));
48
- } else {
49
- if (cellStep.newCell) {
50
- tr.insert(tr.mapping.map(cellStep.from), cellStep.newCell);
51
- skipRows = calculateRowsToSkip(cellStep.newCell.attrs);
52
- } else {
53
- tr.insert(tr.mapping.map(cellStep.from), cell.type.createAndFill());
54
- }
55
- }
56
- return {
57
- tr,
58
- skipRows,
59
- cellStep
60
- };
61
- }
62
-
63
- /**
64
- * Given a cell step, this functions return un StepMap representing this action.
65
- * [position, oldSize, newSize]
66
- * @param cellStep
67
- * @param isDelete
68
- */
69
- export function getMapFromCellStep(cellStep, isDelete) {
70
- if (cellStep.mergeWith !== undefined) {
71
- return [cellStep.mergeWith, 1, 1];
72
- }
73
- if (isDelete) {
74
- return [cellStep.from, cellStep.to - cellStep.from, 0];
75
- } else {
76
- if (cellStep.newCell) {
77
- return [cellStep.from, 0, cellStep.newCell.nodeSize];
78
- }
79
- return [cellStep.from, 0, EmptyCellNodeSize];
80
- }
81
- }
82
-
83
- /**
84
- * Helper to calculate the offset of the inverted cells.
85
- * When you delete consecutive rows in a single step, the position in the generated document
86
- * are skipped by the all the changes except your own. (StepMap.map is not valid)
87
- * @param map
88
- * @param cellStep
89
- * @param isDelete
90
- */
91
- function getOffset(map, cellStep, isDelete) {
92
- if (isDelete) {
93
- return map.map(cellStep.from) - cellStep.from;
94
- }
95
- const [, oldSize, newSize] = getMapFromCellStep(cellStep, isDelete);
96
- return map.map(cellStep.from) - cellStep.from - (newSize - oldSize);
97
- }
98
-
99
- /**
100
- * Given a cell step, this function invert that step.
101
- * @param doc
102
- * @param getTableRectAndColumn
103
- * @param cellStep
104
- * @param isDelete
105
- * @param stepMap
106
- */
107
- export function invertCellStep(doc, getTableRectAndColumn, cellStep, isDelete, stepMap) {
108
- /**
109
- * We need a correct map when a cell is added
110
- * We need the normal map position minus the size of the cell you added it. Why?
111
- * Having a table 3x3 and we add a new column at 2 creates this ranges
112
- * [
113
- * 10, 0, 4,
114
- * 20, 0, 4,
115
- * 30, 0, 4,
116
- * ]
117
- * Where:
118
- * * [10, 20, 30] are the original cell positions where we add the cells
119
- * * [0, 0, 0] are the old size. We are adding new cells, so it's always zero
120
- * * [4, 4, 4] are the new size. In this case, we are adding empty cell and has size 4, this will be different for prefill cells.
121
- * In the document generated the cells that I want to delete (if I invert this step) are [10, 24, 38]
122
- * this is calculated in the given way
123
- * * Map the position using this step mapping function, this will return the cell in the next column
124
- * * Remove the diff (4 - 0 in this case) of the current position.
125
- * For a delete action this not happen, it will always return the right value
126
- */
127
- const offset = getOffset(stepMap, cellStep, isDelete);
128
- const newCellStepInfo = {
129
- ...cellStep,
130
- // Map the position to position of the generated document
131
- from: cellStep.from + offset,
132
- to: cellStep.to + offset
133
- };
134
- if (cellStep.mergeWith !== undefined) {
135
- newCellStepInfo.mergeWith = cellStep.mergeWith + offset;
136
- }
137
- if (isDelete) {
138
- // Add the removed cell as the new cell of the inverted step
139
- const removedCell = doc.nodeAt(cellStep.from);
140
- newCellStepInfo.newCell = removedCell.copy(removedCell.content);
141
-
142
- // When we delete a column we can end in a position that doesnt represent the right column.
143
- // This only happens on merged cell that ends in the deleted column.
144
- // We need to remap this position to the "next" cell (AKA cellStep.to)
145
- const {
146
- column,
147
- rect
148
- } = getTableRectAndColumn();
149
- if (column < rect.map.width) {
150
- const isAtTheEnd = rect.map.colCount(cellStep.from - rect.tableStart) + removedCell.attrs.colspan - 1 === column;
151
- if (cellStep.mergeWith !== undefined && isAtTheEnd) {
152
- newCellStepInfo.mergeWith = newCellStepInfo.from;
153
- newCellStepInfo.from = newCellStepInfo.to;
154
- }
155
- }
156
- }
157
- return newCellStepInfo;
158
- }
159
-
160
- /**
161
- * Create a cell step based on the current cell and operation (add/delete)
162
- * @param cell
163
- * @param column
164
- * @param isDelete
165
- * @param previousCellStep
166
- */
167
- export function createCellStep(cell, column, isDelete, previousCellStep) {
168
- const newCellStepInfo = {
169
- from: cell.from,
170
- to: cell.to
171
- };
172
- if (cell.hasMergedCells) {
173
- // Check what column has to merge
174
- if (column !== cell.col || isDelete) {
175
- newCellStepInfo.mergeWith = cell.from;
176
- }
177
- }
178
- if (previousCellStep) {
179
- if (previousCellStep.mergeWith !== undefined) {
180
- newCellStepInfo.mergeWith = previousCellStep.mergeWith;
181
- }
182
- if (previousCellStep.newCell) {
183
- newCellStepInfo.newCell = previousCellStep.newCell;
184
- }
185
- }
186
- return newCellStepInfo;
187
- }
@@ -1,52 +0,0 @@
1
- import { columnIsHeader, tableNodeTypes } from '@atlaskit/editor-tables/utils';
2
- import { getCellIndex, hasMergedColumns, isRootRow } from './table-map';
3
- /**
4
- * Helper to have a consistent way to iterate for all the cells in a column.
5
- * You can skip rows by passing the rows to skipped in the next arguments.
6
- * For example: `iter.next(1)` to skip the next row
7
- * @param rect
8
- * @param col
9
- */
10
- export function* cellsAtColumn(rect, col) {
11
- const {
12
- map,
13
- tableStart,
14
- table
15
- } = rect;
16
- let refColumn = col > 0 ? -1 : 0;
17
- if (columnIsHeader(map, table, col + refColumn)) {
18
- refColumn = col === 0 || col === map.width ? null : 0;
19
- }
20
- for (let row = 0; row < map.height; row++) {
21
- let index = getCellIndex(rect.map, row, col);
22
- let pos = map.map[index];
23
-
24
- // We only consider to has merged cell to the first cell in a rowspan.
25
- const hasMergedCells = hasMergedColumns(rect.map, row, col) && isRootRow(rect.map, row, col);
26
-
27
- // If this position falls inside a col-spanning cell
28
- let type = refColumn == null ? tableNodeTypes(table.type.schema).cell : table.nodeAt(map.map[index + refColumn]).type;
29
- if (!hasMergedCells) {
30
- pos = map.positionAt(row, col, table);
31
- }
32
- let cell = table.nodeAt(pos);
33
- const cellInfo = {
34
- from: tableStart + pos,
35
- to: tableStart + pos,
36
- row,
37
- col: hasMergedCells ? map.colCount(pos) : col,
38
- type,
39
- hasMergedCells
40
- };
41
- if (cell) {
42
- cellInfo.attrs = cell.attrs;
43
- cellInfo.to = tableStart + pos + cell.nodeSize;
44
- }
45
-
46
- // We let the consumer to pass the rows that we want to skip
47
- const skippedRows = yield cellInfo;
48
- if (skippedRows && skippedRows > 0) {
49
- row += skippedRows;
50
- }
51
- }
52
- }
@@ -1,83 +0,0 @@
1
- /**
2
- * Try to find the right column based on the cell steps in column info..
3
- * @param columnInfo - Map of cell positions sorted from lower to greather
4
- * @param rect - map rect
5
- */
6
- export function findColumn(columnInfo, rect) {
7
- /**
8
- * Algorithm explained
9
- * Given a table like this:
10
- * | 5 | 10 |
11
- * | 15 | 20 |
12
- * | 25 | 30 | 35 |
13
- * Represented by a table map like this:
14
- * rect.map = [5, 10, 10,
15
- * 15, 15, 20,
16
- * 25, 30, 35]
17
- * And a have this inputs:
18
- * columnInfo[].from = [10, 15, 30]
19
- *
20
- * Algorithm:
21
- * * Initial state
22
- * start = 0;
23
- * end = 2 (rect.width - 1 === 3 - 1)
24
- * * Iterate until find first cell position
25
- * start = 1
26
- * end = 2 (min(start column + colspan - 1, start column + end column))
27
- * * Iterate until find second cell position
28
- * start = 1
29
- * end = 1
30
- * * Start === end return 1
31
- */
32
-
33
- // Initial range (start and end column) to search for each row
34
- let [start, end] = [0, rect.map.width - 1];
35
- const iter = columnInfo.values();
36
- let next = iter.next();
37
-
38
- // Iterate for each row
39
- for (let row = 0; row < rect.map.height; row++) {
40
- if (next.done) {
41
- break;
42
- }
43
- // Iterate for the column. Starting with the current start range
44
- for (let col = start; col <= end; col++) {
45
- const i = row * rect.map.width + col;
46
- const cell = rect.map.map[i];
47
- const cellInfo = next.value.from - rect.tableStart;
48
-
49
- // When cell is found update range with the new values
50
- if (cell === cellInfo) {
51
- start = col; // Start column will be the current column
52
- // Try to find the end column. End column will be different that start when has merged cells.
53
- const endIndex = end - start + i;
54
- for (let j = i; j <= endIndex; j++) {
55
- if (rect.map.map[j] !== cell) {
56
- break;
57
- }
58
-
59
- // merged columns
60
- end = start + j - i; // Update the end column with the new position
61
- }
62
-
63
- if (start === end) {
64
- // We found the right column only when start and end columns are the same.
65
- return start;
66
- }
67
- next = iter.next();
68
- break;
69
- }
70
-
71
- // Sometimes I want to find a column at the end of the table (It doesn't exist, but we can add a new cell there).
72
- // This is represented by the end position of the last cell in the column.
73
- // In this case return, table width
74
- if (col === rect.map.width - 1) {
75
- const cellNode = rect.table.nodeAt(cell);
76
- if (cell + cellNode.nodeSize === cellInfo) {
77
- return rect.map.width;
78
- }
79
- }
80
- }
81
- }
82
- return null;
83
- }