-
Notifications
You must be signed in to change notification settings - Fork 3k
/
customBorders.js
832 lines (712 loc) · 24.1 KB
/
customBorders.js
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
import { BasePlugin } from '../base';
import { hasOwnProperty, deepClone } from '../../helpers/object';
import { rangeEach } from '../../helpers/number';
import { arrayEach, arrayReduce, arrayMap } from '../../helpers/array';
import * as C from '../../i18n/constants';
import {
top as menuItemTop,
bottom as menuItemBottom,
left as menuItemLeft,
right as menuItemRight,
noBorders as menuItemNoBorders,
} from './contextMenuItem';
import {
createId,
createDefaultCustomBorder,
createSingleEmptyBorder,
createEmptyBorders,
extendDefaultBorder,
hasLeftRightTypeOptions,
hasStartEndTypeOptions,
toInlinePropName,
normalizeBorder,
denormalizeBorder,
} from './utils';
import { detectSelectionType, normalizeSelectionFactory } from '../../selection';
export const PLUGIN_KEY = 'customBorders';
export const PLUGIN_PRIORITY = 90;
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @plugin CustomBorders
* @class CustomBorders
*
* @description
* This plugin enables an option to apply custom borders through the context menu (configurable with context menu key
* `borders`).
*
* To initialize Handsontable with predefined custom borders, provide cell coordinates and border styles in a form
* of an array.
*
* See [`customBorders` configuration option](@/api/options.md#customBorders) or go to
* [Custom cell borders demo](@/guides/cell-features/formatting-cells.md#custom-cell-borders) for more examples.
*
* @example
* ```js
* customBorders: [
* {
* range: {
* from: {
* row: 1,
* col: 1
* },
* to: {
* row: 3,
* col: 4
* },
* },
* start: {},
* end: {},
* top: {},
* bottom: {},
* },
* ],
*
* // or
* customBorders: [
* { row: 2,
* col: 2,
* start: {
* width: 2,
* color: 'red',
* },
* end: {
* width: 1,
* color: 'green',
* },
* top: '',
* bottom: '',
* }
* ],
* ```
*/
export class CustomBorders extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
/**
* Saved borders.
*
* @private
* @type {Array}
*/
savedBorders = [];
/**
* Checks if the plugin is enabled in the handsontable settings. This method is executed in {@link Hooks#beforeInit}
* hook and if it returns `true` then the {@link CustomBorders#enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return !!this.hot.getSettings()[PLUGIN_KEY];
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
this.addHook('afterContextMenuDefaultOptions', options => this.#onAfterContextMenuDefaultOptions(options));
this.addHook('init', () => this.#onAfterInit());
super.enablePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
this.hideBorders();
super.disablePlugin();
}
/**
* Updates the plugin's state.
*
* This method is executed when [`updateSettings()`](@/api/core.md#updatesettings) is invoked with any of the following configuration options:
* - [`customBorders`](@/api/options.md#customborders)
*/
updatePlugin() {
this.disablePlugin();
this.enablePlugin();
this.changeBorderSettings();
super.updatePlugin();
}
/**
* Set custom borders.
*
* @example
* ```js
* const customBordersPlugin = hot.getPlugin('customBorders');
*
* // Using an array of arrays (produced by `.getSelected()` method).
* customBordersPlugin.setBorders([[1, 1, 2, 2], [6, 2, 0, 2]], {start: {width: 2, color: 'blue'}});
*
* // Using an array of CellRange objects (produced by `.getSelectedRange()` method).
* // Selecting a cell range.
* hot.selectCell(0, 0, 2, 2);
* // Returning selected cells' range with the getSelectedRange method.
* customBordersPlugin.setBorders(hot.getSelectedRange(), {start: {hide: false, width: 2, color: 'blue'}});
* ```
*
* @param {Array[]|CellRange[]} selectionRanges Array of selection ranges.
* @param {object} borderObject Object with `top`, `right`, `bottom` and `start` properties.
*/
setBorders(selectionRanges, borderObject) {
let borderKeys = ['top', 'bottom', 'start', 'end'];
let normBorder = null;
if (borderObject) {
this.checkSettingsCohesion([borderObject]);
borderKeys = Object.keys(borderObject);
normBorder = normalizeBorder(borderObject);
}
const selectionType = detectSelectionType(selectionRanges);
const selectionSchemaNormalizer = normalizeSelectionFactory(selectionType, {
createCellCoords: this.hot._createCellCoords.bind(this.hot),
createCellRange: this.hot._createCellRange.bind(this.hot),
});
arrayEach(selectionRanges, (selection) => {
selectionSchemaNormalizer(selection).forAll((row, col) => {
arrayEach(borderKeys, (borderKey) => {
this.prepareBorderFromCustomAdded(row, col, normBorder, toInlinePropName(borderKey));
});
});
});
/*
The line below triggers a re-render of Handsontable. This will be a "fastDraw"
render, because that is the default for the TableView class.
The re-render is needed for borders on cells that did not have a border before.
The way this call works is that it calls Table.refreshSelections, which calls
Selection.getBorder, which creates a new instance of Border.
Seems wise to keep this single-direction flow of creating new Borders
*/
this.hot.view.render();
}
/**
* Get custom borders.
*
* @example
* ```js
* const customBordersPlugin = hot.getPlugin('customBorders');
*
* // Using an array of arrays (produced by `.getSelected()` method).
* customBordersPlugin.getBorders([[1, 1, 2, 2], [6, 2, 0, 2]]);
* // Using an array of CellRange objects (produced by `.getSelectedRange()` method).
* customBordersPlugin.getBorders(hot.getSelectedRange());
* // Using without param - return all customBorders.
* customBordersPlugin.getBorders();
* ```
*
* @param {Array[]|CellRange[]} selectionRanges Array of selection ranges.
* @returns {object[]} Returns array of border objects.
*/
getBorders(selectionRanges) {
if (!Array.isArray(selectionRanges)) {
return this.savedBorders;
}
const selectionType = detectSelectionType(selectionRanges);
const selectionSchemaNormalizer = normalizeSelectionFactory(selectionType, {
createCellCoords: this.hot._createCellCoords.bind(this.hot),
createCellRange: this.hot._createCellRange.bind(this.hot),
});
const selectedBorders = [];
arrayEach(selectionRanges, (selection) => {
selectionSchemaNormalizer(selection).forAll((row, col) => {
arrayEach(this.savedBorders, (border) => {
if (border.row === row && border.col === col) {
selectedBorders.push(denormalizeBorder(border));
}
});
});
});
return selectedBorders;
}
/**
* Clear custom borders.
*
* @example
* ```js
* const customBordersPlugin = hot.getPlugin('customBorders');
*
* // Using an array of arrays (produced by `.getSelected()` method).
* customBordersPlugin.clearBorders([[1, 1, 2, 2], [6, 2, 0, 2]]);
* // Using an array of CellRange objects (produced by `.getSelectedRange()` method).
* customBordersPlugin.clearBorders(hot.getSelectedRange());
* // Using without param - clear all customBorders.
* customBordersPlugin.clearBorders();
* ```
*
* @param {Array[]|CellRange[]} selectionRanges Array of selection ranges.
*/
clearBorders(selectionRanges) {
if (selectionRanges) {
this.setBorders(selectionRanges);
} else {
arrayEach(this.savedBorders, (border) => {
this.clearBordersFromSelectionSettings(border.id);
this.clearNullCellRange();
this.hot.removeCellMeta(border.row, border.col, 'borders');
});
this.savedBorders.length = 0;
}
}
/**
* Insert WalkontableSelection instance into Walkontable settings.
*
* @private
* @param {object} border Object with `row` and `col`, `start`, `end`, `top` and `bottom`, `id` and `border` ({Object} with `color`, `width` and `cornerVisible` property) properties.
* @param {string} [place] Coordinate where add/remove border - `top`, `bottom`, `start`, `end`.
*/
insertBorderIntoSettings(border, place) {
const hasSavedBorders = this.checkSavedBorders(border);
if (!hasSavedBorders) {
this.savedBorders.push(border);
}
const visualCellRange = this.hot._createCellRange(this.hot._createCellCoords(border.row, border.col));
const hasCustomSelections = this.checkCustomSelections(border, visualCellRange, place);
if (!hasCustomSelections) {
this.hot.selection.highlight.addCustomSelection({ border, visualCellRange });
}
}
/**
* Prepare borders from setting (single cell).
*
* @private
* @param {number} row Visual row index.
* @param {number} column Visual column index.
* @param {object} borderDescriptor Object with `row` and `col`, `start`, `end`, `top` and `bottom` properties.
* @param {string} [place] Coordinate where add/remove border - `top`, `bottom`, `start`, `end`.
*/
prepareBorderFromCustomAdded(row, column, borderDescriptor, place) {
const nrOfRows = this.hot.countRows();
const nrOfColumns = this.hot.countCols();
if (row >= nrOfRows || column >= nrOfColumns) {
return;
}
let border = createEmptyBorders(row, column);
if (borderDescriptor) {
border = extendDefaultBorder(border, borderDescriptor);
arrayEach(this.hot.selection.highlight.customSelections, (customSelection) => {
if (border.id === customSelection.settings.id) {
Object.assign(customSelection.settings, borderDescriptor);
border.id = customSelection.settings.id;
border.top = customSelection.settings.top;
border.bottom = customSelection.settings.bottom;
border.start = customSelection.settings.start;
border.end = customSelection.settings.end;
return false; // breaks forAll
}
});
}
this.hot.setCellMeta(row, column, 'borders', denormalizeBorder(border));
this.insertBorderIntoSettings(border, place);
}
/**
* Prepare borders from setting (object).
*
* @private
* @param {object} range {CellRange} The CellRange object.
* @param {object} customBorder Object with `start`, `end`, `top` and `bottom` properties.
*/
prepareBorderFromCustomAddedRange(range, customBorder) {
const lastRowIndex = Math.min(range.to.row, this.hot.countRows() - 1);
const lastColumnIndex = Math.min(range.to.col, this.hot.countCols() - 1);
rangeEach(range.from.row, lastRowIndex, (rowIndex) => {
rangeEach(range.from.col, lastColumnIndex, (colIndex) => {
const border = createEmptyBorders(rowIndex, colIndex);
let add = 0;
if (rowIndex === range.from.row) {
if (hasOwnProperty(customBorder, 'top')) {
add += 1;
border.top = customBorder.top;
}
}
// Please keep in mind that `range.to.row` may be beyond the table boundaries. The border won't be rendered.
if (rowIndex === range.to.row) {
if (hasOwnProperty(customBorder, 'bottom')) {
add += 1;
border.bottom = customBorder.bottom;
}
}
if (colIndex === range.from.col) {
if (hasOwnProperty(customBorder, 'start')) {
add += 1;
border.start = customBorder.start;
}
}
// Please keep in mind that `range.to.col` may be beyond the table boundaries. The border won't be rendered.
if (colIndex === range.to.col) {
if (hasOwnProperty(customBorder, 'end')) {
add += 1;
border.end = customBorder.end;
}
}
if (add > 0) {
this.hot.setCellMeta(rowIndex, colIndex, 'borders', denormalizeBorder(border));
this.insertBorderIntoSettings(border);
} else {
// TODO sometimes it enters here. Why?
}
});
});
}
/**
* Remove border (triggered from context menu).
*
* @private
* @param {number} row Visual row index.
* @param {number} column Visual column index.
*/
removeAllBorders(row, column) {
const borderId = createId(row, column);
this.spliceBorder(borderId);
this.clearBordersFromSelectionSettings(borderId);
this.clearNullCellRange();
this.hot.removeCellMeta(row, column, 'borders');
}
/**
* Set borders for each cell re. To border position.
*
* @private
* @param {number} row Visual row index.
* @param {number} column Visual column index.
* @param {string} place Coordinate where add/remove border - `top`, `bottom`, `start`, `end` and `noBorders`.
* @param {boolean} remove True when remove borders, and false when add borders.
*/
setBorder(row, column, place, remove) {
let bordersMeta = this.hot.getCellMeta(row, column).borders;
if (!bordersMeta || bordersMeta.border === undefined) {
bordersMeta = createEmptyBorders(row, column);
} else {
bordersMeta = normalizeBorder(bordersMeta);
}
if (remove) {
bordersMeta[place] = createSingleEmptyBorder();
const hideCount = this.countHide(bordersMeta);
if (hideCount === 4) {
this.removeAllBorders(row, column);
} else {
const customSelectionsChecker = this.checkCustomSelectionsFromContextMenu(bordersMeta, place, remove);
if (!customSelectionsChecker) {
this.insertBorderIntoSettings(bordersMeta);
}
this.hot.setCellMeta(row, column, 'borders', denormalizeBorder(bordersMeta));
}
} else {
bordersMeta[place] = createDefaultCustomBorder();
const customSelectionsChecker = this.checkCustomSelectionsFromContextMenu(bordersMeta, place, remove);
if (!customSelectionsChecker) {
this.insertBorderIntoSettings(bordersMeta);
}
this.hot.setCellMeta(row, column, 'borders', denormalizeBorder(bordersMeta));
}
}
/**
* Prepare borders based on cell and border position.
*
* @private
* @param {CellRange[]} selected An array of CellRange objects.
* @param {string} place Coordinate where add/remove border - `top`, `bottom`, `left`, `right` and `noBorders`.
* @param {boolean} remove True when remove borders, and false when add borders.
*/
prepareBorder(selected, place, remove) {
arrayEach(selected, ({ start, end }) => {
if (start.row === end.row && start.col === end.col) {
if (place === 'noBorders') {
this.removeAllBorders(start.row, start.col);
} else {
this.setBorder(start.row, start.col, place, remove);
}
} else {
switch (place) {
case 'noBorders':
rangeEach(start.col, end.col, (colIndex) => {
rangeEach(start.row, end.row, (rowIndex) => {
this.removeAllBorders(rowIndex, colIndex);
});
});
break;
case 'top':
rangeEach(start.col, end.col, (topCol) => {
this.setBorder(start.row, topCol, place, remove);
});
break;
case 'bottom':
rangeEach(start.col, end.col, (bottomCol) => {
this.setBorder(end.row, bottomCol, place, remove);
});
break;
case 'start':
rangeEach(start.row, end.row, (rowStart) => {
this.setBorder(rowStart, start.col, place, remove);
});
break;
case 'end':
rangeEach(start.row, end.row, (rowEnd) => {
this.setBorder(rowEnd, end.col, place, remove);
});
break;
default:
break;
}
}
});
}
/**
* Create borders from settings.
*
* @private
* @param {Array} customBorders Object with `row` and `col`, `start`, `end`, `top` and `bottom` properties.
*/
createCustomBorders(customBorders) {
arrayEach(customBorders, (customBorder) => {
const normCustomBorder = normalizeBorder(customBorder);
if (customBorder.range) {
this.prepareBorderFromCustomAddedRange(customBorder.range, normCustomBorder);
} else {
this.prepareBorderFromCustomAdded(customBorder.row, customBorder.col, normCustomBorder);
}
});
}
/**
* Count hide property in border object.
*
* @private
* @param {object} border Object with `row` and `col`, `start`, `end`, `top` and `bottom`, `id` and
* `border` ({Object} with `color`, `width` and `cornerVisible` property) properties.
* @returns {number}
*/
countHide(border) {
const { top, bottom, start, end } = border;
const values = [top, bottom, start, end];
return arrayReduce(values, (accumulator, value) => {
let result = accumulator;
if (value && value.hide) {
result += 1;
}
return result;
}, 0);
}
/**
* Clear borders settings from custom selections.
*
* @private
* @param {string} borderId Border id name as string.
*/
clearBordersFromSelectionSettings(borderId) {
const index = arrayMap(
this.hot.selection.highlight.customSelections,
customSelection => customSelection.settings.id
).indexOf(borderId);
if (index > -1) {
this.hot.selection.highlight.customSelections[index].clear();
}
}
/**
* Clear cellRange with null value.
*
* @private
*/
clearNullCellRange() {
arrayEach(this.hot.selection.highlight.customSelections, (customSelection, index) => {
if (customSelection.cellRange === null) {
this.hot.selection.highlight.customSelections[index].destroy();
this.hot.selection.highlight.customSelections.splice(index, 1);
return false; // breaks forAll
}
});
}
/**
* Hide custom borders.
*
* @private
*/
hideBorders() {
arrayEach(this.savedBorders, (border) => {
this.clearBordersFromSelectionSettings(border.id);
this.clearNullCellRange();
});
}
/**
* Splice border from savedBorders.
*
* @private
* @param {string} borderId Border id name as string.
*/
spliceBorder(borderId) {
const index = arrayMap(this.savedBorders, border => border.id).indexOf(borderId);
if (index > -1) {
this.savedBorders.splice(index, 1);
}
}
/**
* Check if an border already exists in the savedBorders array, and if true update border in savedBorders.
*
* @private
* @param {object} border Object with `row` and `col`, `start`, `end`, `top` and `bottom`, `id` and
* `border` ({Object} with `color`, `width` and `cornerVisible` property) properties.
*
* @returns {boolean}
*/
checkSavedBorders(border) {
let check = false;
const hideCount = this.countHide(border);
if (hideCount === 4) {
this.spliceBorder(border.id);
check = true;
} else {
arrayEach(this.savedBorders, (savedBorder, index) => {
if (border.id === savedBorder.id) {
this.savedBorders[index] = border;
check = true;
return false; // breaks forAll
}
});
}
return check;
}
/**
* Check if an border already exists in the customSelections, and if true call toggleHiddenClass method.
*
* @private
* @param {object} border Object with `row` and `col`, `start`, `end`, `top` and `bottom`, `id` and
* `border` ({Object} with `color`, `width` and `cornerVisible` property) properties.
* @param {string} place Coordinate where add/remove border - `top`, `bottom`, `start`, `end` and `noBorders`.
* @param {boolean} remove True when remove borders, and false when add borders.
*
* @returns {boolean}
*/
checkCustomSelectionsFromContextMenu(border, place, remove) {
let check = false;
arrayEach(this.hot.selection.highlight.customSelections, (customSelection) => {
if (border.id === customSelection.settings.id) {
const borders = this.hot.view._wt.selectionManager.getBorderInstances(customSelection);
arrayEach(borders, (borderObject) => {
borderObject.toggleHiddenClass(place, remove); // TODO this also bad?
});
check = true;
return false; // breaks forAll
}
});
return check;
}
/**
* Check if an border already exists in the customSelections, and if true reset cellRange.
*
* @private
* @param {object} border Object with `row` and `col`, `start`, `end`, `top` and `bottom`, `id` and
* `border` ({Object} with `color`, `width` and `cornerVisible` property) properties.
* @param {CellRange} cellRange The selection range to check.
* @param {string} [place] Coordinate where add/remove border - `top`, `bottom`, `start`, `end`.
* @returns {boolean}
*/
checkCustomSelections(border, cellRange, place) {
const hideCount = this.countHide(border);
let check = false;
if (hideCount === 4) {
this.removeAllBorders(border.row, border.col);
check = true;
} else {
arrayEach(this.hot.selection.highlight.customSelections, (customSelection) => {
if (border.id === customSelection.settings.id) {
customSelection.visualCellRange = cellRange;
customSelection.commit();
if (place) {
const borders = this.hot.view._wt.selectionManager.getBorderInstances(customSelection);
arrayEach(borders, (borderObject) => {
borderObject.changeBorderStyle(place, border);
});
}
check = true;
return false; // breaks forAll
}
});
}
return check;
}
/**
* Change borders from settings.
*
* @private
*/
changeBorderSettings() {
const customBorders = this.hot.getSettings()[PLUGIN_KEY];
if (Array.isArray(customBorders)) {
const bordersClone = deepClone(customBorders);
this.checkSettingsCohesion(bordersClone);
if (!bordersClone.length) {
this.savedBorders = bordersClone;
}
this.createCustomBorders(bordersClone);
} else if (customBorders !== undefined) {
this.createCustomBorders(this.savedBorders);
}
}
/**
* Checks the settings cohesion. The properties such like "left"/"right" are supported only
* in the LTR mode and the "left"/"right" options can not be used together with "start"/"end" properties.
*
* @private
* @param {object[]} customBorders The user defined custom border objects array.
*/
checkSettingsCohesion(customBorders) {
const hasLeftOrRight = hasLeftRightTypeOptions(customBorders);
const hasStartOrEnd = hasStartEndTypeOptions(customBorders);
if (hasLeftOrRight && hasStartOrEnd) {
throw new Error('The "left"/"right" and "start"/"end" options should not be used together. ' +
'Please use only the option "start"/"end".');
}
if (this.hot.isRtl() && hasLeftOrRight) {
throw new Error('The "left"/"right" properties are not supported for RTL. Please use option "start"/"end".');
}
}
/**
* Add border options to context menu.
*
* @param {object} defaultOptions Context menu items.
*/
#onAfterContextMenuDefaultOptions(defaultOptions) {
if (!this.hot.getSettings()[PLUGIN_KEY]) {
return;
}
defaultOptions.items.push({
name: '---------',
}, {
key: 'borders',
name() {
return this.getTranslatedPhrase(C.CONTEXTMENU_ITEMS_BORDERS);
},
disabled() {
const range = this.getSelectedRangeLast();
if (!range) {
return true;
}
if (range.isSingleHeader()) {
return true;
}
return this.selection.isSelectedByCorner();
},
submenu: {
items: [
menuItemTop(this),
menuItemRight(this),
menuItemBottom(this),
menuItemLeft(this),
menuItemNoBorders(this)
]
}
});
}
/**
* `afterInit` hook callback.
*/
#onAfterInit() {
this.changeBorderSettings();
}
/**
* Destroys the plugin instance.
*/
destroy() {
super.destroy();
}
}