forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableEmbed.ts
239 lines (205 loc) · 6.46 KB
/
tableEmbed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import Delta, { OpIterator } from 'quill-delta';
import Op from 'quill-delta/dist/Op';
import Module from '../core/module';
export type CellData = {
content?: Delta['ops'];
attributes?: Record<string, unknown>;
};
export type TableRowColumnOp = Omit<Op, 'insert'> & {
insert?: { id: string };
};
export interface TableData {
rows?: Delta['ops'];
columns?: Delta['ops'];
cells?: Record<string, CellData>;
}
const parseCellIdentity = (identity: string) => {
const parts = identity.split(':');
return [Number(parts[0]) - 1, Number(parts[1]) - 1];
};
const stringifyCellIdentity = (row: number, column: number) =>
`${row + 1}:${column + 1}`;
export const composePosition = (delta: Delta, index: number) => {
let newIndex = index;
const thisIter = new OpIterator(delta.ops);
let offset = 0;
while (thisIter.hasNext() && offset <= newIndex) {
const length = thisIter.peekLength();
const nextType = thisIter.peekType();
thisIter.next();
switch (nextType) {
case 'delete':
if (length > newIndex - offset) {
return null;
}
newIndex -= length;
break;
case 'insert':
newIndex += length;
offset += length;
break;
default:
offset += length;
break;
}
}
return newIndex;
};
const compactCellData = ({ content, attributes }) => {
const data: CellData = {};
if (content.length() > 0) {
data.content = content.ops;
}
if (attributes && Object.keys(attributes).length > 0) {
data.attributes = attributes;
}
return Object.keys(data).length > 0 ? data : null;
};
const compactTableData = ({ rows, columns, cells }) => {
const data: TableData = {};
if (rows.length() > 0) {
data.rows = rows.ops;
}
if (columns.length() > 0) {
data.columns = columns.ops;
}
if (Object.keys(cells).length) {
data.cells = cells;
}
return data;
};
const reindexCellIdentities = (cells, { rows, columns }) => {
const reindexedCells = {};
Object.keys(cells).forEach(identity => {
let [row, column] = parseCellIdentity(identity);
row = composePosition(rows, row);
column = composePosition(columns, column);
if (row !== null && column !== null) {
const newPosition = stringifyCellIdentity(row, column);
reindexedCells[newPosition] = cells[identity];
}
}, false);
return reindexedCells;
};
export const tableHandler = {
compose(a: TableData, b: TableData, keepNull?: boolean) {
const rows = new Delta(a.rows || []).compose(new Delta(b.rows || []));
const columns = new Delta(a.columns || []).compose(
new Delta(b.columns || []),
);
const cells = reindexCellIdentities(a.cells || {}, {
rows: new Delta(b.rows || []),
columns: new Delta(b.columns || []),
});
Object.keys(b.cells || {}).forEach(identity => {
const aCell = cells[identity] || {};
const bCell = b.cells[identity];
const content = new Delta(aCell.content || []).compose(
new Delta(bCell.content || []),
);
const attributes = Delta.AttributeMap.compose(
aCell.attributes,
bCell.attributes,
keepNull,
);
const cell = compactCellData({ content, attributes });
if (cell) {
cells[identity] = cell;
} else {
delete cells[identity];
}
});
return compactTableData({ rows, columns, cells });
},
transform(a: TableData, b: TableData, priority: boolean) {
const aDeltas = {
rows: new Delta(a.rows || []),
columns: new Delta(a.columns || []),
};
const bDeltas = {
rows: new Delta(b.rows || []),
columns: new Delta(b.columns || []),
};
const rows = aDeltas.rows.transform(bDeltas.rows, priority);
const columns = aDeltas.columns.transform(bDeltas.columns, priority);
const cells = reindexCellIdentities(b.cells || {}, {
rows: bDeltas.rows.transform(aDeltas.rows, !priority),
columns: bDeltas.columns.transform(aDeltas.columns, !priority),
});
Object.keys(a.cells || {}).forEach(identity => {
let [row, column] = parseCellIdentity(identity);
row = composePosition(rows, row);
column = composePosition(columns, column);
if (row !== null && column !== null) {
const newIdentity = stringifyCellIdentity(row, column);
const aCell = a.cells[identity];
const bCell = cells[newIdentity];
if (bCell) {
const content = new Delta(aCell.content || []).transform(
new Delta(bCell.content || []),
priority,
);
const attributes = Delta.AttributeMap.transform(
aCell.attributes,
bCell.attributes,
priority,
);
const cell = compactCellData({ content, attributes });
if (cell) {
cells[newIdentity] = cell;
} else {
delete cells[newIdentity];
}
}
}
});
return compactTableData({ rows, columns, cells });
},
invert(change: TableData, base: TableData) {
const rows = new Delta(change.rows || []).invert(
new Delta(base.rows || []),
);
const columns = new Delta(change.columns || []).invert(
new Delta(base.columns || []),
);
const cells = reindexCellIdentities(change.cells || {}, {
rows,
columns,
});
Object.keys(cells).forEach(identity => {
const changeCell = cells[identity] || {};
const baseCell = (base.cells || {})[identity] || {};
const content = new Delta(changeCell.content || []).invert(
new Delta(baseCell.content || []),
);
const attributes = Delta.AttributeMap.invert(
changeCell.attributes,
baseCell.attributes,
);
const cell = compactCellData({ content, attributes });
if (cell) {
cells[identity] = cell;
} else {
delete cells[identity];
}
});
// Cells may be removed when their row or column is removed
// by row/column deltas. We should add them back.
Object.keys(base.cells || {}).forEach(identity => {
const [row, column] = parseCellIdentity(identity);
if (
composePosition(new Delta(change.rows || []), row) === null ||
composePosition(new Delta(change.columns || []), column) === null
) {
cells[identity] = base.cells[identity];
}
});
return compactTableData({ rows, columns, cells });
},
};
class TableEmbed extends Module {
static register() {
Delta.registerEmbed('table-embed', tableHandler);
}
}
export default TableEmbed;