@atlaskit/adf-schema 29.1.2 → 30.0.0
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.
- package/CHANGELOG.md +12 -0
- package/dist/cjs/schema/default-schema.js +0 -1
- package/dist/cjs/schema/nodes/tableNodes.js +9 -8
- package/dist/cjs/steps.js +0 -21
- package/dist/es2019/schema/default-schema.js +1 -2
- package/dist/es2019/schema/nodes/tableNodes.js +10 -9
- package/dist/es2019/steps.js +0 -3
- package/dist/esm/schema/default-schema.js +1 -2
- package/dist/esm/schema/nodes/tableNodes.js +9 -8
- package/dist/esm/steps.js +0 -3
- package/dist/json-schema/v1/full.json +3 -0
- package/dist/types/schema/nodes/tableNodes.d.ts +8 -5
- package/dist/types/steps.d.ts +0 -4
- package/json-schema/v1/full.json +3 -0
- package/package.json +1 -2
- package/dist/cjs/steps/table/add-column.js +0 -381
- package/dist/cjs/steps/table/constants.js +0 -12
- package/dist/cjs/steps/table/sort-column.js +0 -68
- package/dist/cjs/steps/table/types.js +0 -5
- package/dist/cjs/steps/table/utils/cell-step.js +0 -202
- package/dist/cjs/steps/table/utils/cells-at-column.js +0 -73
- package/dist/cjs/steps/table/utils/find-column.js +0 -90
- package/dist/cjs/steps/table/utils/get-table-rect-from-doc.js +0 -36
- package/dist/cjs/steps/table/utils/side-effects/rows.js +0 -315
- package/dist/cjs/steps/table/utils/side-effects/side-effects.js +0 -83
- package/dist/cjs/steps/table/utils/side-effects/table.js +0 -110
- package/dist/cjs/steps/table/utils/side-effects/types.js +0 -5
- package/dist/cjs/steps/table/utils/table-map.js +0 -30
- package/dist/es2019/steps/table/add-column.js +0 -306
- package/dist/es2019/steps/table/constants.js +0 -5
- package/dist/es2019/steps/table/sort-column.js +0 -34
- package/dist/es2019/steps/table/types.js +0 -1
- package/dist/es2019/steps/table/utils/cell-step.js +0 -187
- package/dist/es2019/steps/table/utils/cells-at-column.js +0 -52
- package/dist/es2019/steps/table/utils/find-column.js +0 -83
- package/dist/es2019/steps/table/utils/get-table-rect-from-doc.js +0 -31
- package/dist/es2019/steps/table/utils/side-effects/rows.js +0 -257
- package/dist/es2019/steps/table/utils/side-effects/side-effects.js +0 -60
- package/dist/es2019/steps/table/utils/side-effects/table.js +0 -82
- package/dist/es2019/steps/table/utils/side-effects/types.js +0 -1
- package/dist/es2019/steps/table/utils/table-map.js +0 -19
- package/dist/esm/steps/table/add-column.js +0 -373
- package/dist/esm/steps/table/constants.js +0 -5
- package/dist/esm/steps/table/sort-column.js +0 -60
- package/dist/esm/steps/table/types.js +0 -1
- package/dist/esm/steps/table/utils/cell-step.js +0 -192
- package/dist/esm/steps/table/utils/cells-at-column.js +0 -66
- package/dist/esm/steps/table/utils/find-column.js +0 -84
- package/dist/esm/steps/table/utils/get-table-rect-from-doc.js +0 -31
- package/dist/esm/steps/table/utils/side-effects/rows.js +0 -307
- package/dist/esm/steps/table/utils/side-effects/side-effects.js +0 -75
- package/dist/esm/steps/table/utils/side-effects/table.js +0 -102
- package/dist/esm/steps/table/utils/side-effects/types.js +0 -1
- package/dist/esm/steps/table/utils/table-map.js +0 -19
- package/dist/types/steps/table/add-column.d.ts +0 -88
- package/dist/types/steps/table/constants.d.ts +0 -4
- package/dist/types/steps/table/sort-column.d.ts +0 -18
- package/dist/types/steps/table/types.d.ts +0 -39
- package/dist/types/steps/table/utils/cell-step.d.ts +0 -47
- package/dist/types/steps/table/utils/cells-at-column.d.ts +0 -20
- package/dist/types/steps/table/utils/find-column.d.ts +0 -8
- package/dist/types/steps/table/utils/get-table-rect-from-doc.d.ts +0 -8
- package/dist/types/steps/table/utils/side-effects/rows.d.ts +0 -25
- package/dist/types/steps/table/utils/side-effects/side-effects.d.ts +0 -15
- package/dist/types/steps/table/utils/side-effects/table.d.ts +0 -16
- package/dist/types/steps/table/utils/side-effects/types.d.ts +0 -33
- package/dist/types/steps/table/utils/table-map.d.ts +0 -6
@@ -1,110 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
5
|
-
value: true
|
6
|
-
});
|
7
|
-
exports.TableSideEffectHandler = void 0;
|
8
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
9
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
10
|
-
var _transform = require("@atlaskit/editor-prosemirror/transform");
|
11
|
-
var TableSideEffectHandler = /*#__PURE__*/function () {
|
12
|
-
function TableSideEffectHandler(tableSideEffect) {
|
13
|
-
(0, _classCallCheck2.default)(this, TableSideEffectHandler);
|
14
|
-
if (tableSideEffect) {
|
15
|
-
this.table = tableSideEffect;
|
16
|
-
}
|
17
|
-
}
|
18
|
-
(0, _createClass2.default)(TableSideEffectHandler, [{
|
19
|
-
key: "addTableSideEffect",
|
20
|
-
value: function addTableSideEffect(from, to, node) {
|
21
|
-
this.table = {
|
22
|
-
from: from,
|
23
|
-
to: to,
|
24
|
-
node: node
|
25
|
-
};
|
26
|
-
}
|
27
|
-
}, {
|
28
|
-
key: "handleAddTable",
|
29
|
-
value: function handleAddTable(tr, isDelete) {
|
30
|
-
if (isDelete || !this.table) {
|
31
|
-
return false;
|
32
|
-
}
|
33
|
-
tr.insert(this.table.from, this.table.node);
|
34
|
-
return true;
|
35
|
-
}
|
36
|
-
}, {
|
37
|
-
key: "handleRemoveTable",
|
38
|
-
value: function handleRemoveTable(tr, tablePos, tableRect, column, isDelete) {
|
39
|
-
if (isDelete && tableRect.map.width === 1 && column === 0) {
|
40
|
-
// Add side effect
|
41
|
-
this.addTableSideEffect(tablePos, tablePos + tableRect.table.nodeSize, tableRect.table.copy(tableRect.table.content));
|
42
|
-
tr.delete(tablePos, tablePos + tableRect.table.nodeSize);
|
43
|
-
return true;
|
44
|
-
}
|
45
|
-
return false;
|
46
|
-
}
|
47
|
-
}, {
|
48
|
-
key: "getTableMap",
|
49
|
-
value: function getTableMap(isDelete) {
|
50
|
-
if (!this.table) {
|
51
|
-
return;
|
52
|
-
}
|
53
|
-
var _this$table = this.table,
|
54
|
-
from = _this$table.from,
|
55
|
-
to = _this$table.to;
|
56
|
-
if (isDelete) {
|
57
|
-
return new _transform.StepMap([from, to - from, 0]);
|
58
|
-
}
|
59
|
-
return new _transform.StepMap([from, 0, to - from]);
|
60
|
-
}
|
61
|
-
}, {
|
62
|
-
key: "map",
|
63
|
-
value: function map(mapping) {
|
64
|
-
if (!this.table) {
|
65
|
-
return;
|
66
|
-
}
|
67
|
-
return {
|
68
|
-
from: mapping.map(this.table.from),
|
69
|
-
to: mapping.map(this.table.to),
|
70
|
-
node: this.table.node
|
71
|
-
};
|
72
|
-
}
|
73
|
-
}, {
|
74
|
-
key: "invert",
|
75
|
-
value: function invert(doc) {
|
76
|
-
if (!this.table) {
|
77
|
-
return;
|
78
|
-
}
|
79
|
-
var tableNode = doc.nodeAt(this.table.from);
|
80
|
-
return {
|
81
|
-
from: this.table.from,
|
82
|
-
to: this.table.from + tableNode.nodeSize,
|
83
|
-
node: tableNode.copy(tableNode.content)
|
84
|
-
};
|
85
|
-
}
|
86
|
-
}, {
|
87
|
-
key: "toJSON",
|
88
|
-
value: function toJSON() {
|
89
|
-
if (!this.table) {
|
90
|
-
return;
|
91
|
-
}
|
92
|
-
return {
|
93
|
-
from: this.table.from,
|
94
|
-
to: this.table.to,
|
95
|
-
node: this.table.node.toJSON()
|
96
|
-
};
|
97
|
-
}
|
98
|
-
}], [{
|
99
|
-
key: "fromJSON",
|
100
|
-
value: function fromJSON(schema, json) {
|
101
|
-
return {
|
102
|
-
from: json.from,
|
103
|
-
to: json.to,
|
104
|
-
node: schema.nodeFromJSON(json.node)
|
105
|
-
};
|
106
|
-
}
|
107
|
-
}]);
|
108
|
-
return TableSideEffectHandler;
|
109
|
-
}();
|
110
|
-
exports.TableSideEffectHandler = TableSideEffectHandler;
|
@@ -1,30 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.getCellIndex = void 0;
|
7
|
-
exports.hasMergedColumns = hasMergedColumns;
|
8
|
-
exports.hasMergedRows = hasMergedRows;
|
9
|
-
exports.isRootCol = isRootCol;
|
10
|
-
exports.isRootRow = isRootRow;
|
11
|
-
var getCellIndex = function getCellIndex(map, row, col) {
|
12
|
-
return row * map.width + col;
|
13
|
-
};
|
14
|
-
exports.getCellIndex = getCellIndex;
|
15
|
-
function hasMergedColumns(map, row, col) {
|
16
|
-
var index = getCellIndex(map, row, col);
|
17
|
-
return col > 0 && map.map[index - 1] === map.map[index] || col < map.width - 1 && map.map[index + 1] === map.map[index];
|
18
|
-
}
|
19
|
-
function hasMergedRows(map, row, col) {
|
20
|
-
var index = getCellIndex(map, row, col);
|
21
|
-
return row > 0 && map.map[index - map.width] !== map.map[index] || row < map.height - 1 && map.map[index + map.width] === map.map[index];
|
22
|
-
}
|
23
|
-
function isRootRow(map, row, col) {
|
24
|
-
var index = getCellIndex(map, row, col);
|
25
|
-
return row > 0 ? map.map[index - map.width] !== map.map[index] : true;
|
26
|
-
}
|
27
|
-
function isRootCol(map, row, col) {
|
28
|
-
var index = getCellIndex(map, row, col);
|
29
|
-
return row > 0 ? map.map[index - 1] !== map.map[index] : true;
|
30
|
-
}
|
@@ -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,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
|
-
}
|