-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathborders.ts
More file actions
508 lines (473 loc) · 17 KB
/
borders.ts
File metadata and controls
508 lines (473 loc) · 17 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import { PositionMap } from "../../helpers/cells/position_map";
import {
deepCopy,
deepEquals,
getItemId,
intersection,
positionToZone,
recomputeZones,
removeFalsyAttributes,
toZone,
} from "../../helpers/index";
import { adjacent, overlap, splitIfAdjacent, zoneToXc } from "../../helpers/zones";
import {
ApplyRangeChange,
Border,
BorderData,
BorderDescr,
CellPosition,
Color,
CommandResult,
CoreCommand,
ExcelWorkbookData,
HeaderIndex,
SetBorderCommand,
SetZoneBordersCommand,
UID,
UnboundedZone,
WorkbookData,
Zone,
} from "../../types/index";
import { CorePlugin } from "../core_plugin";
export type ZoneBorderData = {
top?: BorderDescr;
bottom?: BorderDescr;
left?: BorderDescr;
right?: BorderDescr;
vertical?: BorderDescr;
horizontal?: BorderDescr;
};
export type ZoneBorder = {
zone: UnboundedZone;
style: ZoneBorderData;
};
interface BordersPluginState {
readonly borders: Record<UID, ZoneBorder[] | undefined>;
}
export class BordersPlugin extends CorePlugin<BordersPluginState> implements BordersPluginState {
static getters = [
"getCellBorder",
"getBorders",
"getBordersColors",
"getCellBordersInZone",
] as const;
public readonly borders: BordersPluginState["borders"] = {};
// ---------------------------------------------------------------------------
// Command Handling
// ---------------------------------------------------------------------------
allowDispatch(cmd: CoreCommand) {
switch (cmd.type) {
case "SET_BORDER":
return this.checkValidations(cmd, this.checkBordersUnchanged, this.ensureHasBorder);
default:
return CommandResult.Success;
}
}
handle(cmd: CoreCommand) {
switch (cmd.type) {
case "ADD_MERGE":
for (const zone of cmd.target) {
this.addBordersToMerge(cmd.sheetId, zone);
}
break;
case "DUPLICATE_SHEET":
this.history.update("borders", cmd.sheetIdTo, deepCopy(this.borders[cmd.sheetId]));
break;
case "DELETE_SHEET":
const allBorders = { ...this.borders };
delete allBorders[cmd.sheetId];
this.history.update("borders", allBorders);
break;
case "SET_BORDER":
if (cmd.border) this.addBorders(cmd.sheetId, [positionToZone(cmd)], cmd.border);
break;
case "SET_BORDERS_ON_TARGET":
for (const zone of cmd.target) {
for (let col = zone.left; col <= zone.right; col++) {
for (let row = zone.top; row <= zone.bottom; row++) {
this.addBorder(
cmd.sheetId,
{ left: col, right: col, top: row, bottom: row },
cmd.border && Object.keys(cmd.border).length ? cmd.border : undefined
);
}
}
}
break;
case "SET_ZONE_BORDERS":
const target = cmd.target.map((zone) => this.getters.expandZone(cmd.sheetId, zone));
if (cmd.border.position === "clear") {
this.clearBorders(cmd.sheetId, target);
} else {
this.addBorders(cmd.sheetId, target, this.borderDataToNewBorderData(cmd.border));
}
break;
case "CLEAR_FORMATTING":
this.clearBorders(cmd.sheetId, cmd.target);
break;
}
}
beforeHandle(cmd: CoreCommand): void {
if (cmd.type === "REMOVE_COLUMNS_ROWS") {
if (cmd.dimension === "ROW") {
this.onRowRemove(cmd.sheetId, cmd.elements);
} else {
this.onColRemove(cmd.sheetId, cmd.elements);
}
}
}
adaptRanges(applyChange: ApplyRangeChange, sheetId: UID) {
const newBorders: ZoneBorder[] = [];
for (const border of this.borders[sheetId] ?? []) {
const change = applyChange(this.getters.getRangeFromZone(sheetId, border.zone));
switch (change.changeType) {
case "RESIZE":
case "CHANGE":
case "MOVE":
newBorders.push({ ...border, zone: change.range.unboundedZone });
break;
case "NONE":
newBorders.push(border);
break;
}
}
this.history.update(
"borders",
sheetId,
newBorders.filter((border) => !this.borderIsClear(border))
);
}
private onRowRemove(sheetId: UID, rowsIndex: HeaderIndex[]) {
const rows = new Set(rowsIndex);
const newBorders: ZoneBorder[] = [];
for (const border of this.borders[sheetId] ?? []) {
let newBorder = border;
if (rows.has(border.zone.top)) {
newBorder = deepCopy(border);
newBorder.style.top = border.style.horizontal;
}
if (border.zone.bottom !== undefined && rows.has(border.zone.bottom)) {
newBorder = newBorder === border ? deepCopy(border) : newBorder;
newBorder.style.bottom = border.style.horizontal;
}
newBorders.push(newBorder);
}
this.history.update("borders", sheetId, newBorders);
}
private onColRemove(sheetId: UID, colsIndex: HeaderIndex[]) {
const cols = new Set(colsIndex);
const newBorders: ZoneBorder[] = [];
for (const border of this.borders[sheetId] ?? []) {
let newBorder = border;
if (cols.has(border.zone.left)) {
newBorder = deepCopy(border);
newBorder.style.left = border.style.vertical;
}
if (border.zone.right !== undefined && cols.has(border.zone.right)) {
newBorder = newBorder === border ? deepCopy(border) : newBorder;
newBorder.style.right = border.style.vertical;
}
newBorders.push(newBorder);
}
this.history.update(
"borders",
sheetId,
newBorders.filter((border) => !this.borderIsClear(border))
);
}
// ---------------------------------------------------------------------------
// Getters
// ---------------------------------------------------------------------------
getCellBorder(position: CellPosition): Border {
return this.getZoneExternalBorders(position.sheetId, positionToZone(position));
}
private getZoneExternalBorders(sheetId: UID, zone: Zone): ZoneBorderData {
const externalBorders: ZoneBorderData = {};
for (const border of this.borders[sheetId] ?? []) {
if (overlap(border.zone, zone)) {
externalBorders.right =
(zone.right === border.zone.right ? border.style.right : border.style.vertical) ??
externalBorders.right;
externalBorders.left =
(zone.left === border.zone.left ? border.style.left : border.style.vertical) ??
externalBorders.left;
externalBorders.bottom =
(zone.bottom === border.zone.bottom ? border.style.bottom : border.style.horizontal) ??
externalBorders.bottom;
externalBorders.top =
(zone.top === border.zone.top ? border.style.top : border.style.horizontal) ??
externalBorders.top;
}
}
return externalBorders;
}
getCellBordersInZone(sheetId: UID, zone: Zone): PositionMap<Border> {
const borders = new PositionMap<Border>();
for (const border of this.borders[sheetId] ?? []) {
const { zone: bzone, style: bstyle } = border;
const inter = intersection(bzone, zone);
if (!inter) continue;
for (let col = inter.left; col <= inter.right; col++) {
for (let row = inter.top; row <= inter.bottom; row++) {
const cell = borders.get({ sheetId, col, row }) ?? {};
cell.right = (col === bzone.right ? bstyle.right : bstyle.vertical) ?? cell.right;
cell.left = (col === bzone.left ? bstyle.left : bstyle.vertical) ?? cell.left;
cell.bottom = (row === bzone.bottom ? bstyle.bottom : bstyle.horizontal) ?? cell.bottom;
cell.top = (row === bzone.top ? bstyle.top : bstyle.horizontal) ?? cell.top;
borders.set({ sheetId, col, row }, cell);
}
}
}
return borders;
}
getBordersColors(sheetId: UID): Color[] {
const colors: Set<Color> = new Set<Color>();
for (const border of this.borders[sheetId] ?? []) {
for (const style of Object.values(border.style)) {
if (style?.color) colors.add(style.color);
}
}
return [...colors];
}
getBorders(sheetId: UID, zone: Zone): ZoneBorder[] {
const borders: ZoneBorder[] = [];
for (const existingBorder of this.borders[sheetId] ?? []) {
const inter = intersection(existingBorder.zone, zone);
if (!inter) {
continue;
}
borders.push(this.computeBorderFromZone(inter, existingBorder));
}
return borders;
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
private computeBorderFromZone(newZone: UnboundedZone, border: ZoneBorder): ZoneBorder {
const oldPosition = border.style;
const oldZone = border.zone;
const equalSide = {
top: newZone.top === oldZone.top,
bottom: newZone.bottom === oldZone.bottom,
left: newZone.left === oldZone.left,
right: newZone.right === oldZone.right,
};
return {
zone: newZone,
style: {
top: equalSide.top ? oldPosition.top : oldPosition.horizontal,
bottom: equalSide.bottom ? oldPosition.bottom : oldPosition.horizontal,
left: equalSide.left ? oldPosition.left : oldPosition.vertical,
right: equalSide.right ? oldPosition.right : oldPosition.vertical,
vertical: oldPosition.vertical,
horizontal: oldPosition.horizontal,
},
};
}
private borderIsClear(border: ZoneBorder) {
const style = border.style;
if (style.left || style.right || style.bottom || style.top) return false;
const zone = border.zone;
if ((zone.bottom === undefined || zone.top < zone.bottom) && style.horizontal) return false;
if ((zone.right === undefined || zone.left < zone.right) && style.vertical) return false;
return true;
}
private clearBorders(sheetId: UID, zones: Zone[]) {
for (const zone of zones) {
this.removeAndClearAdjacent(sheetId, zone);
}
}
private removeAndClearAdjacent(sheetId: UID, zone: Zone) {
const borders: ZoneBorder[] = [];
for (const existingBorder of this.borders[sheetId] ?? []) {
for (const updatedBorderZone of recomputeZones([existingBorder.zone], [zone])) {
for (const newZone of splitIfAdjacent(updatedBorderZone, zone)) {
const border = this.computeBorderFromZone(newZone, existingBorder);
const adjacentEdge = adjacent(newZone, zone);
switch (adjacentEdge?.position) {
case "left":
border.style.left = undefined;
break;
case "right":
border.style.right = undefined;
break;
case "top":
border.style.top = undefined;
break;
case "bottom":
border.style.bottom = undefined;
break;
}
borders.push(border);
}
}
}
this.history.update("borders", sheetId, borders);
}
private addBorders(sheetId: UID, zones: Zone[], border: ZoneBorderData) {
for (const zone of zones) {
this.addBorder(sheetId, zone, border);
}
}
private addBorder(
sheetId: UID,
zone: Zone,
newBorder: ZoneBorderData | undefined,
force = false
) {
const borders: ZoneBorder[] = [];
const plannedBorder = newBorder ? { zone, style: newBorder } : undefined;
// For each side, decide if we must clear the border on the *adjacent*
// existing cell when we draw on the opposite side of the new zone.
//
// Example:
// - newBorder.right is set → we draw border on the RIGHT side of `zone`
// - the cell on the right may already have a LEFT border on that edge
// In that case we clear that LEFT border, so only the new RIGHT border
// remains on the shared edge.
//
// existingBorderSideToClear[side] = true means we should clear the border on that
// side of the existing adjacent zone before adding the new border.
const existingBorderSideToClear = {
left: !!newBorder?.right,
right: !!newBorder?.left,
top: !!newBorder?.bottom,
bottom: !!newBorder?.top,
};
let editingZone: Zone[] = [zone];
for (const existingBorder of this.borders[sheetId] ?? []) {
const inter = intersection(existingBorder.zone, zone);
if (!inter) {
// Check if the existing border is adjacent to the new zone
const adjacentEdge = adjacent(existingBorder.zone, zone);
if (adjacentEdge && existingBorderSideToClear[adjacentEdge.position]) {
for (const newZone of splitIfAdjacent(existingBorder.zone, zone)) {
const border = this.computeBorderFromZone(newZone, existingBorder);
const adjacentEdge = adjacent(newZone, zone);
// Clear the existing border on the side that touches the new zone
switch (adjacentEdge?.position) {
case "left":
border.style.left = undefined;
break;
case "right":
border.style.right = undefined;
break;
case "top":
border.style.top = undefined;
break;
case "bottom":
border.style.bottom = undefined;
break;
}
borders.push(border);
}
} else {
borders.push(existingBorder);
}
continue;
}
if (plannedBorder) {
let border = this.computeBorderFromZone(inter, plannedBorder).style;
if (!force) {
border = {
...this.computeBorderFromZone(inter, existingBorder).style,
...removeFalsyAttributes(border),
};
}
borders.push({ zone: inter, style: border });
}
editingZone = recomputeZones(editingZone, [inter]);
for (const updatedBorderZone of recomputeZones([existingBorder.zone], [inter])) {
borders.push(this.computeBorderFromZone(updatedBorderZone, existingBorder));
}
}
if (plannedBorder) {
borders.push(...editingZone.map((zone) => this.computeBorderFromZone(zone, plannedBorder)));
}
this.history.update(
"borders",
sheetId,
borders.filter((border) => !this.borderIsClear(border))
);
}
private borderDataToNewBorderData(border: BorderData): ZoneBorderData {
const borderPosition: ZoneBorderData = {};
const borderStyle = { color: border.color ?? "#000000", style: border.style ?? "thin" };
if (["all", "external", "top"].includes(border.position)) {
borderPosition.top = { ...borderStyle };
}
if (["all", "external", "bottom"].includes(border.position)) {
borderPosition.bottom = { ...borderStyle };
}
if (["all", "external", "left"].includes(border.position)) {
borderPosition.left = { ...borderStyle };
}
if (["all", "external", "right"].includes(border.position)) {
borderPosition.right = { ...borderStyle };
}
if (["all", "hv", "v"].includes(border.position)) {
borderPosition.vertical = { ...borderStyle };
}
if (["all", "hv", "h"].includes(border.position)) {
borderPosition.horizontal = { ...borderStyle };
}
return borderPosition;
}
private checkBordersUnchanged(cmd: SetBorderCommand) {
const currentBorder = this.getCellBorder(cmd);
const areAllNewBordersUndefined =
!cmd.border?.bottom && !cmd.border?.left && !cmd.border?.right && !cmd.border?.top;
if ((!currentBorder && areAllNewBordersUndefined) || deepEquals(currentBorder, cmd.border)) {
return CommandResult.NoChanges;
}
return CommandResult.Success;
}
private ensureHasBorder(cmd: SetBorderCommand | SetZoneBordersCommand) {
if (!cmd.border) return CommandResult.NoChanges;
return CommandResult.Success;
}
/**
* Compute the borders to add to the given zone merged.
*/
private addBordersToMerge(sheetId: UID, zone: Zone) {
const border = {
...this.getZoneExternalBorders(sheetId, zone),
...removeFalsyAttributes(this.getCellBorder({ sheetId, col: zone.left, row: zone.top })),
};
this.addBorder(sheetId, zone, border, true);
}
// ---------------------------------------------------------------------------
// Import/Export
// ---------------------------------------------------------------------------
import(data: WorkbookData) {
if (Object.keys(data.borders || {}).length) {
for (const sheet of data.sheets) {
for (const zoneXc in sheet.borders) {
const borderId = sheet.borders[zoneXc];
this.addBorder(sheet.id, toZone(zoneXc), data.borders[borderId]);
}
}
}
// Merges
for (const sheetData of data.sheets) {
if (sheetData.merges) {
for (const merge of sheetData.merges) {
this.addBordersToMerge(sheetData.id, toZone(merge));
}
}
}
}
export(data: WorkbookData) {
const borders: { [borderId: number]: ZoneBorderData } = {};
for (const sheet of data.sheets) {
sheet.borders = {};
for (const border of this.borders[sheet.id] ?? []) {
sheet.borders[zoneToXc(border.zone)] = getItemId(border.style, borders);
}
}
data.borders = borders;
}
exportForExcel(data: ExcelWorkbookData) {
this.export(data);
}
}