-
Notifications
You must be signed in to change notification settings - Fork 3k
/
autoColumnSize.js
686 lines (592 loc) · 20.5 KB
/
autoColumnSize.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
import { BasePlugin } from '../base';
import { arrayEach, arrayFilter, arrayReduce, arrayMap } from '../../helpers/array';
import { cancelAnimationFrame, requestAnimationFrame } from '../../helpers/feature';
import GhostTable from '../../utils/ghostTable';
import Hooks from '../../pluginHooks';
import { isObject, hasOwnProperty } from '../../helpers/object';
import { valueAccordingPercent, rangeEach } from '../../helpers/number';
import SamplesGenerator from '../../utils/samplesGenerator';
import { isPercentValue } from '../../helpers/string';
import { ViewportColumnsCalculator } from '../../3rdparty/walkontable/src';
import { PhysicalIndexToValueMap as IndexToValueMap } from '../../translations';
import { isDefined } from '../../helpers/mixed';
Hooks.getSingleton().register('modifyAutoColumnSizeSeed');
export const PLUGIN_KEY = 'autoColumnSize';
export const PLUGIN_PRIORITY = 10;
const privatePool = new WeakMap();
const COLUMN_SIZE_MAP_NAME = 'autoColumnSize';
/* eslint-disable jsdoc/require-description-complete-sentence */
/**
* @plugin AutoColumnSize
* @class AutoColumnSize
*
* @description
* This plugin allows to set column widths based on their widest cells.
*
* By default, the plugin is declared as `undefined`, which makes it enabled (same as if it was declared as `true`).
* Enabling this plugin may decrease the overall table performance, as it needs to calculate the widths of all cells to
* resize the columns accordingly.
* If you experience problems with the performance, try turning this feature off and declaring the column widths manually.
*
* Column width calculations are divided into sync and async part. Each of this parts has their own advantages and
* disadvantages. Synchronous calculations are faster but they block the browser UI, while the slower asynchronous
* operations don't block the browser UI.
*
* To configure the sync/async distribution, you can pass an absolute value (number of columns) or a percentage value to a config object:
*
* ```js
* // as a number (300 columns in sync, rest async)
* autoColumnSize: {syncLimit: 300},
*
* // as a string (percent)
* autoColumnSize: {syncLimit: '40%'},
* ```
*
* The plugin uses {@link GhostTable} and {@link SamplesGenerator} for calculations.
* First, {@link SamplesGenerator} prepares samples of data with its coordinates.
* Next {@link GhostTable} uses coordinates to get cells' renderers and append all to the DOM through DocumentFragment.
*
* Sampling accepts additional options:
* - *samplingRatio* - Defines how many samples for the same length will be used to calculate. Default is `3`.
*
* ```js
* autoColumnSize: {
* samplingRatio: 10,
* }
* ```
*
* - *allowSampleDuplicates* - Defines if duplicated values might be used in sampling. Default is `false`.
*
* ```js
* autoColumnSize: {
* allowSampleDuplicates: true,
* }
* ```
*
* To configure this plugin see {@link Options#autoColumnSize}.
*
* @example
*
* ::: only-for javascript
* ```js
* const hot = new Handsontable(document.getElementById('example'), {
* data: getData(),
* autoColumnSize: true
* });
* // Access to plugin instance:
* const plugin = hot.getPlugin('autoColumnSize');
*
* plugin.getColumnWidth(4);
*
* if (plugin.isEnabled()) {
* // code...
* }
* ```
* :::
*
* ::: only-for react
* ```jsx
* const hotRef = useRef(null);
*
* ...
*
* // First, let's contruct Handsontable
* <HotTable
* ref={hotRef}
* data={getData()}
* autoColumnSize={true}
* />
*
* ...
*
* // Access to plugin instance:
* const hot = hotRef.current.hotInstance;
* const plugin = hot.getPlugin('autoColumnSize');
*
* plugin.getColumnWidth(4);
*
* if (plugin.isEnabled()) {
* // code...
* }
* ```
* :::
*/
/* eslint-enable jsdoc/require-description-complete-sentence */
export class AutoColumnSize extends BasePlugin {
static get PLUGIN_KEY() {
return PLUGIN_KEY;
}
static get PLUGIN_PRIORITY() {
return PLUGIN_PRIORITY;
}
static get SETTING_KEYS() {
return true;
}
static get CALCULATION_STEP() {
return 50;
}
static get SYNC_CALCULATION_LIMIT() {
return 50;
}
constructor(hotInstance) {
super(hotInstance);
privatePool.set(this, {
/**
* Cached column header names. It is used to diff current column headers with previous state and detect which
* columns width should be updated.
*
* @private
* @type {Array}
*/
cachedColumnHeaders: [],
});
/**
* Instance of {@link GhostTable} for rows and columns size calculations.
*
* @private
* @type {GhostTable}
*/
this.ghostTable = new GhostTable(this.hot);
/**
* Instance of {@link SamplesGenerator} for generating samples necessary for columns width calculations.
*
* @private
* @type {SamplesGenerator}
* @fires Hooks#modifyAutoColumnSizeSeed
*/
this.samplesGenerator = new SamplesGenerator((row, column) => {
const cellMeta = this.hot.getCellMeta(row, column);
let cellValue = '';
if (!cellMeta.spanned) {
cellValue = this.hot.getDataAtCell(row, column);
}
let bundleSeed = '';
if (this.hot.hasHook('modifyAutoColumnSizeSeed')) {
bundleSeed = this.hot.runHooks('modifyAutoColumnSizeSeed', bundleSeed, cellMeta, cellValue);
}
return { value: cellValue, bundleSeed };
});
/**
* `true` only if the first calculation was performed.
*
* @private
* @type {boolean}
*/
this.firstCalculation = true;
/**
* `true` if the size calculation is in progress.
*
* @type {boolean}
*/
this.inProgress = false;
/**
* Number of already measured columns (we already know their sizes).
*
* @type {number}
*/
this.measuredColumns = 0;
/**
* PhysicalIndexToValueMap to keep and track widths for physical column indexes.
*
* @private
* @type {PhysicalIndexToValueMap}
*/
this.columnWidthsMap = new IndexToValueMap();
this.hot.columnIndexMapper.registerMap(COLUMN_SIZE_MAP_NAME, this.columnWidthsMap);
// Leave the listener active to allow auto-sizing the columns when the plugin is disabled.
// This is necessary for width recalculation for resize handler doubleclick (ManualColumnResize).
this.addHook('beforeColumnResize',
(size, column, isDblClick) => this.onBeforeColumnResize(size, column, isDblClick));
}
/**
* 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 #enablePlugin} method is called.
*
* @returns {boolean}
*/
isEnabled() {
return this.hot.getSettings()[PLUGIN_KEY] !== false && !this.hot.getSettings().colWidths;
}
/**
* Enables the plugin functionality for this Handsontable instance.
*/
enablePlugin() {
if (this.enabled) {
return;
}
const setting = this.hot.getSettings()[PLUGIN_KEY];
if (setting && setting.useHeaders !== null && setting.useHeaders !== void 0) {
this.ghostTable.setSetting('useHeaders', setting.useHeaders);
}
this.setSamplingOptions();
this.addHook('afterLoadData', (...args) => this.onAfterLoadData(...args));
this.addHook('beforeChangeRender', changes => this.onBeforeChange(changes));
this.addHook('afterFormulasValuesUpdate', changes => this.onAfterFormulasValuesUpdate(changes));
this.addHook('beforeViewRender', force => this.onBeforeViewRender(force));
this.addHook('modifyColWidth', (width, col) => this.getColumnWidth(col, width));
this.addHook('afterInit', () => this.onAfterInit());
super.enablePlugin();
}
/**
* Updates the plugin's state. This method is executed when {@link Core#updateSettings} is invoked.
*/
updatePlugin() {
const changedColumns = this.findColumnsWhereHeaderWasChanged();
if (changedColumns.length) {
this.clearCache(changedColumns);
this.calculateVisibleColumnsWidth();
}
super.updatePlugin();
}
/**
* Disables the plugin functionality for this Handsontable instance.
*/
disablePlugin() {
super.disablePlugin();
// Leave the listener active to allow auto-sizing the columns when the plugin is disabled.
// This is necessary for width recalculation for resize handler doubleclick (ManualColumnResize).
this.addHook('beforeColumnResize',
(size, column, isDblClick) => this.onBeforeColumnResize(size, column, isDblClick));
}
/**
* Calculates visible columns width.
*/
calculateVisibleColumnsWidth() {
const rowsCount = this.hot.countRows();
// Keep last column widths unchanged for situation when all rows was deleted or trimmed (pro #6)
if (!rowsCount) {
return;
}
const force = this.hot.renderCall;
const firstVisibleColumn = this.getFirstVisibleColumn();
const lastVisibleColumn = this.getLastVisibleColumn();
if (firstVisibleColumn === -1 || lastVisibleColumn === -1) {
return;
}
this.calculateColumnsWidth({ from: firstVisibleColumn, to: lastVisibleColumn }, void 0, force);
}
/**
* Calculates a columns width.
*
* @param {number|object} colRange Visual column index or an object with `from` and `to` visual indexes as a range.
* @param {number|object} rowRange Visual row index or an object with `from` and `to` visual indexes as a range.
* @param {boolean} [force=false] If `true` the calculation will be processed regardless of whether the width exists in the cache.
*/
calculateColumnsWidth(colRange = { from: 0, to: this.hot.countCols() - 1 }, rowRange = { from: 0, to: this.hot.countRows() - 1 }, force = false) { // eslint-disable-line max-len
const columnsRange = typeof colRange === 'number' ? { from: colRange, to: colRange } : colRange;
const rowsRange = typeof rowRange === 'number' ? { from: rowRange, to: rowRange } : rowRange;
rangeEach(columnsRange.from, columnsRange.to, (visualColumn) => {
let physicalColumn = this.hot.toPhysicalColumn(visualColumn);
if (physicalColumn === null) {
physicalColumn = visualColumn;
}
if (force || (this.columnWidthsMap.getValueAtIndex(physicalColumn) === null &&
!this.hot._getColWidthFromSettings(physicalColumn))) {
const samples = this.samplesGenerator.generateColumnSamples(visualColumn, rowsRange);
arrayEach(samples, ([column, sample]) => this.ghostTable.addColumn(column, sample));
}
});
if (this.ghostTable.columns.length) {
this.hot.batchExecution(() => {
this.ghostTable.getWidths((visualColumn, width) => {
const physicalColumn = this.hot.toPhysicalColumn(visualColumn);
this.columnWidthsMap.setValueAtIndex(physicalColumn, width);
});
}, true);
this.measuredColumns = columnsRange.to + 1;
this.ghostTable.clean();
}
}
/**
* Calculates all columns width. The calculated column will be cached in the {@link AutoColumnSize#widths} property.
* To retrieve width for specified column use {@link AutoColumnSize#getColumnWidth} method.
*
* @param {object|number} rowRange Row index or an object with `from` and `to` properties which define row range.
*/
calculateAllColumnsWidth(rowRange = { from: 0, to: this.hot.countRows() - 1 }) {
let current = 0;
const length = this.hot.countCols() - 1;
let timer = null;
this.inProgress = true;
const loop = () => {
// When hot was destroyed after calculating finished cancel frame
if (!this.hot) {
cancelAnimationFrame(timer);
this.inProgress = false;
return;
}
this.calculateColumnsWidth({
from: current,
to: Math.min(current + AutoColumnSize.CALCULATION_STEP, length)
}, rowRange);
current = current + AutoColumnSize.CALCULATION_STEP + 1;
if (current < length) {
timer = requestAnimationFrame(loop);
} else {
cancelAnimationFrame(timer);
this.inProgress = false;
// @TODO Should call once per render cycle, currently fired separately in different plugins
this.hot.view.adjustElementsSize();
}
};
const syncLimit = this.getSyncCalculationLimit();
// sync
if (this.firstCalculation && syncLimit >= 0) {
this.calculateColumnsWidth({ from: 0, to: syncLimit }, rowRange);
this.firstCalculation = false;
current = syncLimit + 1;
}
// async
if (current < length) {
loop();
} else {
this.inProgress = false;
}
}
/**
* Sets the sampling options.
*
* @private
*/
setSamplingOptions() {
const setting = this.hot.getSettings()[PLUGIN_KEY];
const samplingRatio = setting && hasOwnProperty(setting, 'samplingRatio') ?
setting.samplingRatio : void 0;
const allowSampleDuplicates = setting && hasOwnProperty(setting, 'allowSampleDuplicates') ?
setting.allowSampleDuplicates : void 0;
if (samplingRatio && !isNaN(samplingRatio)) {
this.samplesGenerator.setSampleCount(parseInt(samplingRatio, 10));
}
if (allowSampleDuplicates) {
this.samplesGenerator.setAllowDuplicates(allowSampleDuplicates);
}
}
/**
* Recalculates all columns width (overwrite cache values).
*/
recalculateAllColumnsWidth() {
if (this.hot.view && this.hot.view._wt.wtTable.isVisible()) {
this.clearCache();
this.calculateAllColumnsWidth();
}
}
/**
* Gets value which tells how many columns should be calculated synchronously (rest of the columns will be calculated
* asynchronously). The limit is calculated based on `syncLimit` set to `autoColumnSize` option (see {@link Options#autoColumnSize}).
*
* @returns {number}
*/
getSyncCalculationLimit() {
const settings = this.hot.getSettings()[PLUGIN_KEY];
/* eslint-disable no-bitwise */
let limit = AutoColumnSize.SYNC_CALCULATION_LIMIT;
const colsLimit = this.hot.countCols() - 1;
if (isObject(settings)) {
limit = settings.syncLimit;
if (isPercentValue(limit)) {
limit = valueAccordingPercent(colsLimit, limit);
} else {
// Force to Number
limit >>= 0;
}
}
return Math.min(limit, colsLimit);
}
/**
* Gets the calculated column width.
*
* @param {number} column Visual column index.
* @param {number} [defaultWidth] Default column width. It will be picked up if no calculated width found.
* @param {boolean} [keepMinimum=true] If `true` then returned value won't be smaller then 50 (default column width).
* @returns {number}
*/
getColumnWidth(column, defaultWidth = void 0, keepMinimum = true) {
let width = defaultWidth;
if (width === void 0) {
width = this.columnWidthsMap.getValueAtIndex(this.hot.toPhysicalColumn(column));
if (keepMinimum && typeof width === 'number') {
width = Math.max(width, ViewportColumnsCalculator.DEFAULT_WIDTH);
}
}
return width;
}
/**
* Gets the first visible column.
*
* @returns {number} Returns visual column index, -1 if table is not rendered or if there are no columns to base the the calculations on.
*/
getFirstVisibleColumn() {
const wot = this.hot.view._wt;
if (wot.wtViewport.columnsVisibleCalculator) {
// Fist fully visible column is stored as renderable index.
const firstFullyVisibleColumn = wot.wtTable.getFirstVisibleColumn();
if (firstFullyVisibleColumn !== -1) {
return this.hot.columnIndexMapper.getVisualFromRenderableIndex(firstFullyVisibleColumn);
}
}
if (wot.wtViewport.columnsRenderCalculator) {
const firstRenderedColumn = wot.wtTable.getFirstRenderedColumn();
// There are no rendered column.
if (firstRenderedColumn !== -1) {
return this.hot.columnIndexMapper.getVisualFromRenderableIndex(firstRenderedColumn);
}
}
return -1;
}
/**
* Gets the last visible column.
*
* @returns {number} Returns visual column index or -1 if table is not rendered.
*/
getLastVisibleColumn() {
const wot = this.hot.view._wt;
if (wot.wtViewport.columnsVisibleCalculator) {
// Last fully visible column is stored as renderable index.
const lastFullyVisibleColumn = wot.wtTable.getLastVisibleColumn();
if (lastFullyVisibleColumn !== -1) {
return this.hot.columnIndexMapper.getVisualFromRenderableIndex(lastFullyVisibleColumn);
}
}
if (wot.wtViewport.columnsRenderCalculator) {
// Last fully visible column is stored as renderable index.
const lastRenderedColumn = wot.wtTable.getLastRenderedColumn();
// There are no rendered columns.
if (lastRenderedColumn !== -1) {
return this.hot.columnIndexMapper.getVisualFromRenderableIndex(lastRenderedColumn);
}
}
return -1;
}
/**
* Collects all columns which titles has been changed in comparison to the previous state.
*
* @private
* @returns {Array} It returns an array of physical column indexes.
*/
findColumnsWhereHeaderWasChanged() {
const columnHeaders = this.hot.getColHeader();
const { cachedColumnHeaders } = privatePool.get(this);
const changedColumns = arrayReduce(columnHeaders, (acc, columnTitle, physicalColumn) => {
const cachedColumnsLength = cachedColumnHeaders.length;
if (cachedColumnsLength - 1 < physicalColumn || cachedColumnHeaders[physicalColumn] !== columnTitle) {
acc.push(physicalColumn);
}
if (cachedColumnsLength - 1 < physicalColumn) {
cachedColumnHeaders.push(columnTitle);
} else {
cachedColumnHeaders[physicalColumn] = columnTitle;
}
return acc;
}, []);
return changedColumns;
}
/**
* Clears cache of calculated column widths. If you want to clear only selected columns pass an array with their indexes.
* Otherwise whole cache will be cleared.
*
* @param {number[]} [columns] List of physical column indexes to clear.
*/
clearCache(columns = []) {
if (columns.length) {
this.hot.batchExecution(() => {
arrayEach(columns, (physicalIndex) => {
this.columnWidthsMap.setValueAtIndex(physicalIndex, null);
});
}, true);
} else {
this.columnWidthsMap.clear();
}
}
/**
* Checks if all widths were calculated. If not then return `true` (need recalculate).
*
* @returns {boolean}
*/
isNeedRecalculate() {
return !!arrayFilter(this.columnWidthsMap.getValues()
.slice(0, this.measuredColumns), item => (item === null)).length;
}
/**
* On before view render listener.
*
* @private
*/
onBeforeViewRender() {
this.calculateVisibleColumnsWidth();
if (this.isNeedRecalculate() && !this.inProgress) {
this.calculateAllColumnsWidth();
}
}
/**
* On after load data listener.
*
* @private
*/
onAfterLoadData() {
if (this.hot.view) {
this.recalculateAllColumnsWidth();
} else {
// first load - initialization
setTimeout(() => {
if (this.hot) {
this.recalculateAllColumnsWidth();
}
}, 0);
}
}
/**
* On before change listener.
*
* @private
* @param {Array} changes An array of modified data.
*/
onBeforeChange(changes) {
const changedColumns = arrayMap(changes, ([, columnProperty]) => {
return this.hot.toPhysicalColumn(this.hot.propToCol(columnProperty));
});
this.clearCache(Array.from(new Set(changedColumns)));
}
/**
* On before column resize listener.
*
* @private
* @param {number} size Calculated new column width.
* @param {number} column Visual index of the resized column.
* @param {boolean} isDblClick Flag that determines whether there was a double-click.
* @returns {number}
*/
onBeforeColumnResize(size, column, isDblClick) {
let newSize = size;
if (isDblClick) {
this.calculateColumnsWidth(column, void 0, true);
newSize = this.getColumnWidth(column, void 0, false);
}
return newSize;
}
/**
* On after Handsontable init fill plugin with all necessary values.
*
* @private
*/
onAfterInit() {
privatePool.get(this).cachedColumnHeaders = this.hot.getColHeader();
}
/**
* After formulas values updated listener.
*
* @private
* @param {Array} changes An array of modified data.
*/
onAfterFormulasValuesUpdate(changes) {
const filteredChanges = arrayFilter(changes, change => isDefined(change.address?.col));
const changedColumns = arrayMap(filteredChanges, change => change.address.col);
this.clearCache(Array.from(new Set(changedColumns)));
}
/**
* Destroys the plugin instance.
*/
destroy() {
this.ghostTable.clean();
super.destroy();
}
}