-
Notifications
You must be signed in to change notification settings - Fork 32
/
borders.ts
604 lines (574 loc) · 20.3 KB
/
borders.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import { DEFAULT_BORDER_DESC } from "../../constants";
import {
deepEquals,
getItemId,
isDefined,
range,
toCartesian,
toXC,
toZone,
} from "../../helpers/index";
import {
AddColumnsRowsCommand,
Border,
BorderDescr,
BorderDescription,
BorderPosition,
CellPosition,
Color,
CommandResult,
CoreCommand,
ExcelWorkbookData,
HeaderIndex,
SetBorderCommand,
UID,
WorkbookData,
Zone,
} from "../../types/index";
import { CorePlugin } from "../core_plugin";
interface BordersPluginState {
readonly borders: Record<UID, (BorderDescription[] | undefined)[] | undefined>;
}
/**
* Formatting plugin.
*
* This plugin manages all things related to a cell look:
* - borders
*/
export class BordersPlugin extends CorePlugin<BordersPluginState> implements BordersPluginState {
static getters = ["getCellBorder", "getBordersColors"] as const;
public readonly borders: BordersPluginState["borders"] = {};
// ---------------------------------------------------------------------------
// Command Handling
// ---------------------------------------------------------------------------
allowDispatch(cmd: CoreCommand) {
switch (cmd.type) {
case "SET_BORDER":
return this.checkBordersUnchanged(cmd);
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":
const borders = this.borders[cmd.sheetId];
if (borders) {
// borders is a sparse 2D array.
// map and slice preserve empty values and do not set `undefined` instead
const bordersCopy = borders
.slice()
.map((col) => col?.slice().map((border) => ({ ...border })));
this.history.update("borders", cmd.sheetIdTo, bordersCopy);
}
break;
case "DELETE_SHEET":
const allBorders = { ...this.borders };
delete allBorders[cmd.sheetId];
this.history.update("borders", allBorders);
break;
case "SET_BORDER":
this.setBorder(cmd.sheetId, cmd.col, cmd.row, cmd.border);
break;
case "SET_ZONE_BORDERS":
if (cmd.border) {
const target = cmd.target.map((zone) => this.getters.expandZone(cmd.sheetId, zone));
this.setBorders(
cmd.sheetId,
target,
cmd.border.position,
cmd.border.color === ""
? undefined
: {
style: cmd.border.style || DEFAULT_BORDER_DESC.style,
color: cmd.border.color || DEFAULT_BORDER_DESC.color,
}
);
}
break;
case "CLEAR_FORMATTING":
this.clearBorders(cmd.sheetId, cmd.target);
break;
case "REMOVE_COLUMNS_ROWS":
for (let el of [...cmd.elements].sort((a, b) => b - a)) {
if (cmd.dimension === "COL") {
this.shiftBordersHorizontally(cmd.sheetId, el + 1, -1);
} else {
this.shiftBordersVertically(cmd.sheetId, el + 1, -1);
}
}
break;
case "ADD_COLUMNS_ROWS":
if (cmd.dimension === "COL") {
this.handleAddColumns(cmd);
} else {
this.handleAddRows(cmd);
}
break;
}
}
/**
* Move borders according to the inserted columns.
* Ensure borders continuity.
*/
private handleAddColumns(cmd: AddColumnsRowsCommand) {
// The new columns have already been inserted in the sheet at this point.
let colLeftOfInsertion: HeaderIndex;
let colRightOfInsertion: HeaderIndex;
if (cmd.position === "before") {
this.shiftBordersHorizontally(cmd.sheetId, cmd.base, cmd.quantity, {
moveFirstLeftBorder: true,
});
colLeftOfInsertion = cmd.base - 1;
colRightOfInsertion = cmd.base + cmd.quantity;
} else {
this.shiftBordersHorizontally(cmd.sheetId, cmd.base + 1, cmd.quantity, {
moveFirstLeftBorder: false,
});
colLeftOfInsertion = cmd.base;
colRightOfInsertion = cmd.base + cmd.quantity + 1;
}
this.ensureColumnBorderContinuity(cmd.sheetId, colLeftOfInsertion, colRightOfInsertion);
}
/**
* Move borders according to the inserted rows.
* Ensure borders continuity.
*/
private handleAddRows(cmd: AddColumnsRowsCommand) {
// The new rows have already been inserted at this point.
let rowAboveInsertion: HeaderIndex;
let rowBelowInsertion: HeaderIndex;
if (cmd.position === "before") {
this.shiftBordersVertically(cmd.sheetId, cmd.base, cmd.quantity, {
moveFirstTopBorder: true,
});
rowAboveInsertion = cmd.base - 1;
rowBelowInsertion = cmd.base + cmd.quantity;
} else {
this.shiftBordersVertically(cmd.sheetId, cmd.base + 1, cmd.quantity, {
moveFirstTopBorder: false,
});
rowAboveInsertion = cmd.base;
rowBelowInsertion = cmd.base + cmd.quantity + 1;
}
this.ensureRowBorderContinuity(cmd.sheetId, rowAboveInsertion, rowBelowInsertion);
}
// ---------------------------------------------------------------------------
// Getters
// ---------------------------------------------------------------------------
getCellBorder({ sheetId, col, row }: CellPosition): Border | null {
const border = {
top: this.borders[sheetId]?.[col]?.[row]?.horizontal,
bottom: this.borders[sheetId]?.[col]?.[row + 1]?.horizontal,
left: this.borders[sheetId]?.[col]?.[row]?.vertical,
right: this.borders[sheetId]?.[col + 1]?.[row]?.vertical,
};
if (!border.bottom && !border.left && !border.right && !border.top) {
return null;
}
return border;
}
getBordersColors(sheetId: UID): Color[] {
const colors: Color[] = [];
const sheetBorders = this.borders[sheetId];
if (sheetBorders) {
for (const borders of sheetBorders.filter(isDefined)) {
for (const cellBorder of borders) {
if (cellBorder?.horizontal) {
colors.push(cellBorder.horizontal.color);
}
if (cellBorder?.vertical) {
colors.push(cellBorder.vertical.color);
}
}
}
}
return colors;
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
/**
* Ensure border continuity between two columns.
* If the two columns have the same borders (at each row respectively),
* the same borders are applied to each cell in between.
*/
private ensureColumnBorderContinuity(
sheetId: UID,
leftColumn: HeaderIndex,
rightColumn: HeaderIndex
) {
const targetCols = range(leftColumn + 1, rightColumn);
for (let row: HeaderIndex = 0; row < this.getters.getNumberRows(sheetId); row++) {
const leftBorder = this.getCellBorder({ sheetId, col: leftColumn, row });
const rightBorder = this.getCellBorder({ sheetId, col: rightColumn, row });
if (leftBorder && rightBorder) {
const commonSides = this.getCommonSides(leftBorder, rightBorder);
for (let col of targetCols) {
this.addBorder(sheetId, col, row, commonSides);
}
}
}
}
/**
* Ensure border continuity between two rows.
* If the two rows have the same borders (at each column respectively),
* the same borders are applied to each cell in between.
*/
private ensureRowBorderContinuity(sheetId: UID, topRow: HeaderIndex, bottomRow: HeaderIndex) {
const targetRows = range(topRow + 1, bottomRow);
for (let col: HeaderIndex = 0; col < this.getters.getNumberCols(sheetId); col++) {
const aboveBorder = this.getCellBorder({ sheetId, col, row: topRow });
const belowBorder = this.getCellBorder({ sheetId, col, row: bottomRow });
if (aboveBorder && belowBorder) {
const commonSides = this.getCommonSides(aboveBorder, belowBorder);
for (let row of targetRows) {
this.addBorder(sheetId, col, row, commonSides);
}
}
}
}
/**
* From two borders, return a new border with sides defined in both borders.
* i.e. the intersection of two borders.
*/
private getCommonSides(border1: Border, border2: Border): Border {
const commonBorder = {};
for (let side of ["top", "bottom", "left", "right"]) {
if (border1[side] && border1[side] === border2[side]) {
commonBorder[side] = border1[side];
}
}
return commonBorder;
}
/**
* Get all the columns which contains at least a border
*/
private getColumnsWithBorders(sheetId: UID): HeaderIndex[] {
const sheetBorders = this.borders[sheetId];
if (!sheetBorders) return [];
return Object.keys(sheetBorders).map((index) => parseInt(index, 10));
}
/**
* Get the range of all the rows in the sheet
*/
private getRowsRange(sheetId: UID): HeaderIndex[] {
const sheetBorders = this.borders[sheetId];
if (!sheetBorders) return [];
return range(0, this.getters.getNumberRows(sheetId) + 1);
}
/**
* Move borders of a sheet horizontally.
* @param sheetId
* @param start starting column (included)
* @param delta how much borders will be moved (negative if moved to the left)
*/
private shiftBordersHorizontally(
sheetId: UID,
start: HeaderIndex,
delta: number,
{ moveFirstLeftBorder }: { moveFirstLeftBorder?: boolean } = {}
) {
const borders = this.borders[sheetId];
if (!borders) return;
if (delta < 0) {
this.moveBordersOfColumn(sheetId, start, delta, "vertical", {
destructive: false,
});
}
this.getColumnsWithBorders(sheetId)
.filter((col) => col >= start)
.sort((a, b) => (delta < 0 ? a - b : b - a)) // start by the end when moving up
.forEach((col) => {
if ((col === start && moveFirstLeftBorder) || col !== start) {
this.moveBordersOfColumn(sheetId, col, delta, "vertical");
}
this.moveBordersOfColumn(sheetId, col, delta, "horizontal");
});
}
/**
* Move borders of a sheet vertically.
* @param sheetId
* @param start starting row (included)
* @param delta how much borders will be moved (negative if moved to the above)
*/
private shiftBordersVertically(
sheetId: UID,
start: HeaderIndex,
delta: number,
{ moveFirstTopBorder }: { moveFirstTopBorder?: boolean } = {}
) {
const borders = this.borders[sheetId];
if (!borders) return;
if (delta < 0) {
this.moveBordersOfRow(sheetId, start, delta, "horizontal", {
destructive: false,
});
}
this.getRowsRange(sheetId)
.filter((row) => row >= start)
.sort((a, b) => (delta < 0 ? a - b : b - a)) // start by the end when moving up
.forEach((row) => {
if ((row === start && moveFirstTopBorder) || row !== start) {
this.moveBordersOfRow(sheetId, row, delta, "horizontal");
}
this.moveBordersOfRow(sheetId, row, delta, "vertical");
});
}
/**
* Moves the borders (left if `vertical` or top if `horizontal` depending on
* `borderDirection`) of all cells in an entire row `delta` rows to the right
* (`delta` > 0) or to the left (`delta` < 0).
* Note that as the left of a cell is the right of the cell-1, if the left is
* moved the right is also moved. However, if `horizontal`, the bottom border
* is not moved.
* It does it by replacing the target border by the moved border. If the
* argument `destructive` is given false, the target border is preserved if
* the moved border is empty
*/
private moveBordersOfRow(
sheetId: UID,
row: HeaderIndex,
delta: number,
borderDirection: "vertical" | "horizontal",
{ destructive }: { destructive: boolean } = { destructive: true }
) {
const borders = this.borders[sheetId];
if (!borders) return;
this.getColumnsWithBorders(sheetId).forEach((col) => {
const targetBorder = borders[col]?.[row + delta]?.[borderDirection];
const movedBorder = borders[col]?.[row]?.[borderDirection];
this.history.update(
"borders",
sheetId,
col,
row + delta,
borderDirection,
destructive ? movedBorder : movedBorder || targetBorder
);
this.history.update("borders", sheetId, col, row, borderDirection, undefined);
});
}
/**
* Moves the borders (left if `vertical` or top if `horizontal` depending on
* `borderDirection`) of all cells in an entire column `delta` columns below
* (`delta` > 0) or above (`delta` < 0).
* Note that as the top of a cell is the bottom of the cell-1, if the top is
* moved the bottom is also moved. However, if `vertical`, the right border
* is not moved.
* It does it by replacing the target border by the moved border. If the
* argument `destructive` is given false, the target border is preserved if
* the moved border is empty
*/
private moveBordersOfColumn(
sheetId: UID,
col: HeaderIndex,
delta: number,
borderDirection: "vertical" | "horizontal",
{ destructive }: { destructive: boolean } = { destructive: true }
) {
const borders = this.borders[sheetId];
if (!borders) return;
this.getRowsRange(sheetId).forEach((row) => {
const targetBorder = borders[col + delta]?.[row]?.[borderDirection];
const movedBorder = borders[col]?.[row]?.[borderDirection];
this.history.update(
"borders",
sheetId,
col + delta,
row,
borderDirection,
destructive ? movedBorder : movedBorder || targetBorder
);
this.history.update("borders", sheetId, col, row, borderDirection, undefined);
});
}
/**
* Set the borders of a cell.
* It overrides the current border if override === true.
*/
private setBorder(
sheetId: UID,
col: HeaderIndex,
row: HeaderIndex,
border?: Border,
override = true
) {
if (override || !this.borders?.[sheetId]?.[col]?.[row]?.vertical) {
this.history.update("borders", sheetId, col, row, "vertical", border?.left);
}
if (override || !this.borders?.[sheetId]?.[col]?.[row]?.horizontal) {
this.history.update("borders", sheetId, col, row, "horizontal", border?.top);
}
if (override || !this.borders?.[sheetId]?.[col + 1]?.[row]?.vertical) {
this.history.update("borders", sheetId, col + 1, row, "vertical", border?.right);
}
if (override || !this.borders?.[sheetId]?.[col]?.[row + 1]?.horizontal) {
this.history.update("borders", sheetId, col, row + 1, "horizontal", border?.bottom);
}
}
/**
* Remove the borders of a zone
*/
private clearBorders(sheetId: UID, zones: Zone[]) {
for (let zone of zones) {
for (let row = zone.top; row <= zone.bottom; row++) {
this.history.update("borders", sheetId, zone.right + 1, row, "vertical", undefined);
for (let col = zone.left; col <= zone.right; col++) {
this.history.update("borders", sheetId, col, row, undefined);
}
}
for (let col = zone.left; col <= zone.right; col++) {
this.history.update("borders", sheetId, col, zone.bottom + 1, "horizontal", undefined);
}
}
}
/**
* Add a border to the existing one to a cell
*/
private addBorder(sheetId: UID, col: HeaderIndex, row: HeaderIndex, border: Border) {
this.setBorder(sheetId, col, row, {
...this.getCellBorder({ sheetId, col, row }),
...border,
});
}
/**
* Set the borders of a zone by computing the borders to add from the given
* command
*/
private setBorders(
sheetId: UID,
zones: Zone[],
position: BorderPosition,
border: BorderDescr | undefined
) {
if (position === "clear") {
return this.clearBorders(sheetId, zones);
}
for (let zone of zones) {
if (position === "h" || position === "hv" || position === "all") {
for (let row = zone.top + 1; row <= zone.bottom; row++) {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, row, { top: border });
}
}
}
if (position === "v" || position === "hv" || position === "all") {
for (let row = zone.top; row <= zone.bottom; row++) {
for (let col = zone.left + 1; col <= zone.right; col++) {
this.addBorder(sheetId, col, row, { left: border });
}
}
}
if (position === "left" || position === "all" || position === "external") {
for (let row = zone.top; row <= zone.bottom; row++) {
this.addBorder(sheetId, zone.left, row, { left: border });
}
}
if (position === "right" || position === "all" || position === "external") {
for (let row = zone.top; row <= zone.bottom; row++) {
this.addBorder(sheetId, zone.right + 1, row, { left: border });
}
}
if (position === "top" || position === "all" || position === "external") {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, zone.top, { top: border });
}
}
if (position === "bottom" || position === "all" || position === "external") {
for (let col = zone.left; col <= zone.right; col++) {
this.addBorder(sheetId, col, zone.bottom + 1, { top: border });
}
}
}
}
/**
* Compute the borders to add to the given zone merged.
*/
private addBordersToMerge(sheetId: UID, zone: Zone) {
const { left, right, top, bottom } = zone;
const bordersTopLeft = this.getCellBorder({ sheetId, col: left, row: top });
const bordersBottomRight = this.getCellBorder({ sheetId, col: right, row: bottom });
this.clearBorders(sheetId, [zone]);
if (bordersTopLeft?.top) {
this.setBorders(sheetId, [{ ...zone, bottom: top }], "top", bordersTopLeft.top);
}
if (bordersTopLeft?.left) {
this.setBorders(sheetId, [{ ...zone, right: left }], "left", bordersTopLeft.left);
}
if (bordersBottomRight?.bottom) {
this.setBorders(sheetId, [{ ...zone, top: bottom }], "bottom", bordersBottomRight.bottom);
} else if (bordersTopLeft?.bottom) {
this.setBorders(sheetId, [{ ...zone, top: bottom }], "bottom", bordersTopLeft.bottom);
}
if (bordersBottomRight?.right) {
this.setBorders(sheetId, [{ ...zone, left: right }], "right", bordersBottomRight.right);
} else if (bordersTopLeft?.right) {
this.setBorders(sheetId, [{ ...zone, left: right }], "right", bordersTopLeft.right);
}
}
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;
}
// ---------------------------------------------------------------------------
// Import/Export
// ---------------------------------------------------------------------------
import(data: WorkbookData) {
// Borders
if (Object.keys(data.borders || {}).length) {
for (let sheet of data.sheets) {
for (const xc in sheet.cells) {
const cell = sheet.cells[xc];
if (cell?.border) {
const border = data.borders[cell.border];
const { col, row } = toCartesian(xc);
this.setBorder(sheet.id, col, row, border, false);
}
}
}
}
// Merges
for (let sheetData of data.sheets) {
if (sheetData.merges) {
for (let merge of sheetData.merges) {
this.addBordersToMerge(sheetData.id, toZone(merge));
}
}
}
}
export(data: WorkbookData) {
const borders: { [borderId: number]: Border } = {};
for (let sheet of data.sheets) {
for (let col: HeaderIndex = 0; col < sheet.colNumber; col++) {
for (let row: HeaderIndex = 0; row < sheet.rowNumber; row++) {
const border = this.getCellBorder({ sheetId: sheet.id, col, row });
if (border) {
const xc = toXC(col, row);
const cell = sheet.cells[xc];
const borderId = getItemId(border, borders);
if (cell) {
cell.border = borderId;
} else {
sheet.cells[xc] = { border: borderId };
}
}
}
}
}
data.borders = borders;
}
exportForExcel(data: ExcelWorkbookData) {
this.export(data);
}
}