@tanstack/virtual-core 3.0.4 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/cjs/index.cjs +645 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/cjs/index.d.cts +126 -0
  4. package/dist/cjs/utils.cjs +59 -0
  5. package/dist/cjs/utils.cjs.map +1 -0
  6. package/{build/lib → dist/esm}/index.d.ts +1 -1
  7. package/dist/esm/index.js +645 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/dist/esm/utils.d.ts +10 -0
  10. package/dist/esm/utils.js +59 -0
  11. package/dist/esm/utils.js.map +1 -0
  12. package/package.json +33 -23
  13. package/src/index.ts +18 -21
  14. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +0 -27
  15. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
  16. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -31
  17. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  18. package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs +0 -27
  19. package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs.map +0 -1
  20. package/build/lib/index.esm.js +0 -653
  21. package/build/lib/index.esm.js.map +0 -1
  22. package/build/lib/index.js +0 -668
  23. package/build/lib/index.js.map +0 -1
  24. package/build/lib/index.mjs +0 -653
  25. package/build/lib/index.mjs.map +0 -1
  26. package/build/lib/utils.esm.js +0 -58
  27. package/build/lib/utils.esm.js.map +0 -1
  28. package/build/lib/utils.js +0 -64
  29. package/build/lib/utils.js.map +0 -1
  30. package/build/lib/utils.mjs +0 -58
  31. package/build/lib/utils.mjs.map +0 -1
  32. package/build/umd/index.development.js +0 -732
  33. package/build/umd/index.development.js.map +0 -1
  34. package/build/umd/index.production.js +0 -12
  35. package/build/umd/index.production.js.map +0 -1
  36. /package/{build/lib/utils.d.ts → dist/cjs/utils.d.cts} +0 -0
@@ -1,668 +0,0 @@
1
- /**
2
- * virtual-core
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
16
- var utils = require('./utils.js');
17
-
18
- //
19
-
20
- //
21
-
22
- var defaultKeyExtractor = function defaultKeyExtractor(index) {
23
- return index;
24
- };
25
- var defaultRangeExtractor = function defaultRangeExtractor(range) {
26
- var start = Math.max(range.startIndex - range.overscan, 0);
27
- var end = Math.min(range.endIndex + range.overscan, range.count - 1);
28
- var arr = [];
29
- for (var _i = start; _i <= end; _i++) {
30
- arr.push(_i);
31
- }
32
- return arr;
33
- };
34
- var observeElementRect = function observeElementRect(instance, cb) {
35
- var element = instance.scrollElement;
36
- if (!element) {
37
- return;
38
- }
39
- var handler = function handler(rect) {
40
- var width = rect.width,
41
- height = rect.height;
42
- cb({
43
- width: Math.round(width),
44
- height: Math.round(height)
45
- });
46
- };
47
- handler(element.getBoundingClientRect());
48
- var observer = new ResizeObserver(function (entries) {
49
- var entry = entries[0];
50
- if (entry != null && entry.borderBoxSize) {
51
- var box = entry.borderBoxSize[0];
52
- if (box) {
53
- handler({
54
- width: box.inlineSize,
55
- height: box.blockSize
56
- });
57
- return;
58
- }
59
- }
60
- handler(element.getBoundingClientRect());
61
- });
62
- observer.observe(element, {
63
- box: 'border-box'
64
- });
65
- return function () {
66
- observer.unobserve(element);
67
- };
68
- };
69
- var observeWindowRect = function observeWindowRect(instance, cb) {
70
- var element = instance.scrollElement;
71
- if (!element) {
72
- return;
73
- }
74
- var handler = function handler() {
75
- cb({
76
- width: element.innerWidth,
77
- height: element.innerHeight
78
- });
79
- };
80
- handler();
81
- element.addEventListener('resize', handler, {
82
- passive: true
83
- });
84
- return function () {
85
- element.removeEventListener('resize', handler);
86
- };
87
- };
88
- var observeElementOffset = function observeElementOffset(instance, cb) {
89
- var element = instance.scrollElement;
90
- if (!element) {
91
- return;
92
- }
93
- var handler = function handler() {
94
- cb(element[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
95
- };
96
- handler();
97
- element.addEventListener('scroll', handler, {
98
- passive: true
99
- });
100
- return function () {
101
- element.removeEventListener('scroll', handler);
102
- };
103
- };
104
- var observeWindowOffset = function observeWindowOffset(instance, cb) {
105
- var element = instance.scrollElement;
106
- if (!element) {
107
- return;
108
- }
109
- var handler = function handler() {
110
- cb(element[instance.options.horizontal ? 'scrollX' : 'scrollY']);
111
- };
112
- handler();
113
- element.addEventListener('scroll', handler, {
114
- passive: true
115
- });
116
- return function () {
117
- element.removeEventListener('scroll', handler);
118
- };
119
- };
120
- var measureElement = function measureElement(element, entry, instance) {
121
- if (entry != null && entry.borderBoxSize) {
122
- var box = entry.borderBoxSize[0];
123
- if (box) {
124
- var size = Math.round(box[instance.options.horizontal ? 'inlineSize' : 'blockSize']);
125
- return size;
126
- }
127
- }
128
- return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
129
- };
130
- var windowScroll = function windowScroll(offset, _ref, instance) {
131
- var _instance$scrollEleme, _instance$scrollEleme2;
132
- var _ref$adjustments = _ref.adjustments,
133
- adjustments = _ref$adjustments === void 0 ? 0 : _ref$adjustments,
134
- behavior = _ref.behavior;
135
- var toOffset = offset + adjustments;
136
- (_instance$scrollEleme = instance.scrollElement) == null || _instance$scrollEleme.scrollTo == null || _instance$scrollEleme.scrollTo((_instance$scrollEleme2 = {}, _instance$scrollEleme2[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme2.behavior = behavior, _instance$scrollEleme2));
137
- };
138
- var elementScroll = function elementScroll(offset, _ref2, instance) {
139
- var _instance$scrollEleme3, _instance$scrollEleme4;
140
- var _ref2$adjustments = _ref2.adjustments,
141
- adjustments = _ref2$adjustments === void 0 ? 0 : _ref2$adjustments,
142
- behavior = _ref2.behavior;
143
- var toOffset = offset + adjustments;
144
- (_instance$scrollEleme3 = instance.scrollElement) == null || _instance$scrollEleme3.scrollTo == null || _instance$scrollEleme3.scrollTo((_instance$scrollEleme4 = {}, _instance$scrollEleme4[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme4.behavior = behavior, _instance$scrollEleme4));
145
- };
146
- var Virtualizer = function Virtualizer(_opts) {
147
- var _this = this;
148
- this.unsubs = [];
149
- this.scrollElement = null;
150
- this.isScrolling = false;
151
- this.isScrollingTimeoutId = null;
152
- this.scrollToIndexTimeoutId = null;
153
- this.measurementsCache = [];
154
- this.itemSizeCache = new Map();
155
- this.pendingMeasuredCacheIndexes = [];
156
- this.scrollDirection = null;
157
- this.scrollAdjustments = 0;
158
- this.measureElementCache = new Map();
159
- this.observer = function () {
160
- var _ro = null;
161
- var get = function get() {
162
- if (_ro) {
163
- return _ro;
164
- } else if (typeof ResizeObserver !== 'undefined') {
165
- return _ro = new ResizeObserver(function (entries) {
166
- entries.forEach(function (entry) {
167
- _this._measureElement(entry.target, entry);
168
- });
169
- });
170
- } else {
171
- return null;
172
- }
173
- };
174
- return {
175
- disconnect: function disconnect() {
176
- var _get;
177
- return (_get = get()) == null ? void 0 : _get.disconnect();
178
- },
179
- observe: function observe(target) {
180
- var _get2;
181
- return (_get2 = get()) == null ? void 0 : _get2.observe(target, {
182
- box: 'border-box'
183
- });
184
- },
185
- unobserve: function unobserve(target) {
186
- var _get3;
187
- return (_get3 = get()) == null ? void 0 : _get3.unobserve(target);
188
- }
189
- };
190
- }();
191
- this.range = null;
192
- this.setOptions = function (opts) {
193
- Object.entries(opts).forEach(function (_ref3) {
194
- var key = _ref3[0],
195
- value = _ref3[1];
196
- if (typeof value === 'undefined') delete opts[key];
197
- });
198
- _this.options = _rollupPluginBabelHelpers["extends"]({
199
- debug: false,
200
- initialOffset: 0,
201
- overscan: 1,
202
- paddingStart: 0,
203
- paddingEnd: 0,
204
- scrollPaddingStart: 0,
205
- scrollPaddingEnd: 0,
206
- horizontal: false,
207
- getItemKey: defaultKeyExtractor,
208
- rangeExtractor: defaultRangeExtractor,
209
- onChange: function onChange() {},
210
- measureElement: measureElement,
211
- initialRect: {
212
- width: 0,
213
- height: 0
214
- },
215
- scrollMargin: 0,
216
- scrollingDelay: 150,
217
- indexAttribute: 'data-index',
218
- initialMeasurementsCache: [],
219
- lanes: 1
220
- }, opts);
221
- };
222
- this.notify = function (sync) {
223
- _this.options.onChange == null || _this.options.onChange(_this, sync);
224
- };
225
- this.maybeNotify = utils.memo(function () {
226
- _this.calculateRange();
227
- return [_this.isScrolling, _this.range ? _this.range.startIndex : null, _this.range ? _this.range.endIndex : null];
228
- }, function (isScrolling) {
229
- _this.notify(isScrolling);
230
- }, {
231
- key: process.env.NODE_ENV !== 'production' && 'maybeNotify',
232
- debug: function debug() {
233
- return _this.options.debug;
234
- },
235
- initialDeps: [this.isScrolling, this.range ? this.range.startIndex : null, this.range ? this.range.endIndex : null]
236
- });
237
- this.cleanup = function () {
238
- _this.unsubs.filter(Boolean).forEach(function (d) {
239
- return d();
240
- });
241
- _this.unsubs = [];
242
- _this.scrollElement = null;
243
- };
244
- this._didMount = function () {
245
- _this.measureElementCache.forEach(_this.observer.observe);
246
- return function () {
247
- _this.observer.disconnect();
248
- _this.cleanup();
249
- };
250
- };
251
- this._willUpdate = function () {
252
- var scrollElement = _this.options.getScrollElement();
253
- if (_this.scrollElement !== scrollElement) {
254
- _this.cleanup();
255
- _this.scrollElement = scrollElement;
256
- _this._scrollToOffset(_this.scrollOffset, {
257
- adjustments: undefined,
258
- behavior: undefined
259
- });
260
- _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
261
- _this.scrollRect = rect;
262
- _this.maybeNotify();
263
- }));
264
- _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
265
- _this.scrollAdjustments = 0;
266
- if (_this.scrollOffset === offset) {
267
- return;
268
- }
269
- if (_this.isScrollingTimeoutId !== null) {
270
- clearTimeout(_this.isScrollingTimeoutId);
271
- _this.isScrollingTimeoutId = null;
272
- }
273
- _this.isScrolling = true;
274
- _this.scrollDirection = _this.scrollOffset < offset ? 'forward' : 'backward';
275
- _this.scrollOffset = offset;
276
- _this.maybeNotify();
277
- _this.isScrollingTimeoutId = setTimeout(function () {
278
- _this.isScrollingTimeoutId = null;
279
- _this.isScrolling = false;
280
- _this.scrollDirection = null;
281
- _this.maybeNotify();
282
- }, _this.options.scrollingDelay);
283
- }));
284
- }
285
- };
286
- this.getSize = function () {
287
- return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
288
- };
289
- this.memoOptions = utils.memo(function () {
290
- return [_this.options.count, _this.options.paddingStart, _this.options.scrollMargin, _this.options.getItemKey];
291
- }, function (count, paddingStart, scrollMargin, getItemKey) {
292
- _this.pendingMeasuredCacheIndexes = [];
293
- return {
294
- count: count,
295
- paddingStart: paddingStart,
296
- scrollMargin: scrollMargin,
297
- getItemKey: getItemKey
298
- };
299
- }, {
300
- key: false
301
- });
302
- this.getFurthestMeasurement = function (measurements, index) {
303
- var furthestMeasurementsFound = new Map();
304
- var furthestMeasurements = new Map();
305
- for (var m = index - 1; m >= 0; m--) {
306
- var measurement = measurements[m];
307
- if (furthestMeasurementsFound.has(measurement.lane)) {
308
- continue;
309
- }
310
- var previousFurthestMeasurement = furthestMeasurements.get(measurement.lane);
311
- if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
312
- furthestMeasurements.set(measurement.lane, measurement);
313
- } else if (measurement.end < previousFurthestMeasurement.end) {
314
- furthestMeasurementsFound.set(measurement.lane, true);
315
- }
316
- if (furthestMeasurementsFound.size === _this.options.lanes) {
317
- break;
318
- }
319
- }
320
- return furthestMeasurements.size === _this.options.lanes ? Array.from(furthestMeasurements.values()).sort(function (a, b) {
321
- if (a.end === b.end) {
322
- return a.index - b.index;
323
- }
324
- return a.end - b.end;
325
- })[0] : undefined;
326
- };
327
- this.getMeasurements = utils.memo(function () {
328
- return [_this.memoOptions(), _this.itemSizeCache];
329
- }, function (_ref4, itemSizeCache) {
330
- var count = _ref4.count,
331
- paddingStart = _ref4.paddingStart,
332
- scrollMargin = _ref4.scrollMargin,
333
- getItemKey = _ref4.getItemKey;
334
- var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
335
- _this.pendingMeasuredCacheIndexes = [];
336
- var measurements = _this.measurementsCache.slice(0, min);
337
- for (var _i2 = min; _i2 < count; _i2++) {
338
- var key = getItemKey(_i2);
339
- var furthestMeasurement = _this.options.lanes === 1 ? measurements[_i2 - 1] : _this.getFurthestMeasurement(measurements, _i2);
340
- var start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
341
- var measuredSize = itemSizeCache.get(key);
342
- var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
343
- var end = start + size;
344
- var lane = furthestMeasurement ? furthestMeasurement.lane : _i2 % _this.options.lanes;
345
- measurements[_i2] = {
346
- index: _i2,
347
- start: start,
348
- size: size,
349
- end: end,
350
- key: key,
351
- lane: lane
352
- };
353
- }
354
- _this.measurementsCache = measurements;
355
- return measurements;
356
- }, {
357
- key: process.env.NODE_ENV !== 'production' && 'getMeasurements',
358
- debug: function debug() {
359
- return _this.options.debug;
360
- }
361
- });
362
- this.calculateRange = utils.memo(function () {
363
- return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
364
- }, function (measurements, outerSize, scrollOffset) {
365
- return _this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
366
- measurements: measurements,
367
- outerSize: outerSize,
368
- scrollOffset: scrollOffset
369
- }) : null;
370
- }, {
371
- key: process.env.NODE_ENV !== 'production' && 'calculateRange',
372
- debug: function debug() {
373
- return _this.options.debug;
374
- }
375
- });
376
- this.getIndexes = utils.memo(function () {
377
- return [_this.options.rangeExtractor, _this.calculateRange(), _this.options.overscan, _this.options.count];
378
- }, function (rangeExtractor, range, overscan, count) {
379
- return range === null ? [] : rangeExtractor(_rollupPluginBabelHelpers["extends"]({}, range, {
380
- overscan: overscan,
381
- count: count
382
- }));
383
- }, {
384
- key: process.env.NODE_ENV !== 'production' && 'getIndexes',
385
- debug: function debug() {
386
- return _this.options.debug;
387
- }
388
- });
389
- this.indexFromElement = function (node) {
390
- var attributeName = _this.options.indexAttribute;
391
- var indexStr = node.getAttribute(attributeName);
392
- if (!indexStr) {
393
- console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
394
- return -1;
395
- }
396
- return parseInt(indexStr, 10);
397
- };
398
- this._measureElement = function (node, entry) {
399
- var item = _this.measurementsCache[_this.indexFromElement(node)];
400
- if (!item || !node.isConnected) {
401
- _this.measureElementCache.forEach(function (cached, key) {
402
- if (cached === node) {
403
- _this.observer.unobserve(node);
404
- _this.measureElementCache["delete"](key);
405
- }
406
- });
407
- return;
408
- }
409
- var prevNode = _this.measureElementCache.get(item.key);
410
- if (prevNode !== node) {
411
- if (prevNode) {
412
- _this.observer.unobserve(prevNode);
413
- }
414
- _this.observer.observe(node);
415
- _this.measureElementCache.set(item.key, node);
416
- }
417
- var measuredItemSize = _this.options.measureElement(node, entry, _this);
418
- _this.resizeItem(item, measuredItemSize);
419
- };
420
- this.resizeItem = function (item, size) {
421
- var _this$itemSizeCache$g;
422
- var itemSize = (_this$itemSizeCache$g = _this.itemSizeCache.get(item.key)) != null ? _this$itemSizeCache$g : item.size;
423
- var delta = size - itemSize;
424
- if (delta !== 0) {
425
- if (item.start < _this.scrollOffset + _this.scrollAdjustments) {
426
- if (process.env.NODE_ENV !== 'production' && _this.options.debug) {
427
- console.info('correction', delta);
428
- }
429
- _this._scrollToOffset(_this.scrollOffset, {
430
- adjustments: _this.scrollAdjustments += delta,
431
- behavior: undefined
432
- });
433
- }
434
- _this.pendingMeasuredCacheIndexes.push(item.index);
435
- _this.itemSizeCache = new Map(_this.itemSizeCache.set(item.key, size));
436
- _this.notify(false);
437
- }
438
- };
439
- this.measureElement = function (node) {
440
- if (!node) {
441
- return;
442
- }
443
- _this._measureElement(node, undefined);
444
- };
445
- this.getVirtualItems = utils.memo(function () {
446
- return [_this.getIndexes(), _this.getMeasurements()];
447
- }, function (indexes, measurements) {
448
- var virtualItems = [];
449
- for (var k = 0, len = indexes.length; k < len; k++) {
450
- var _i3 = indexes[k];
451
- var measurement = measurements[_i3];
452
- virtualItems.push(measurement);
453
- }
454
- return virtualItems;
455
- }, {
456
- key: process.env.NODE_ENV !== 'production' && 'getIndexes',
457
- debug: function debug() {
458
- return _this.options.debug;
459
- }
460
- });
461
- this.getVirtualItemForOffset = function (offset) {
462
- var measurements = _this.getMeasurements();
463
- return utils.notUndefined(measurements[findNearestBinarySearch(0, measurements.length - 1, function (index) {
464
- return utils.notUndefined(measurements[index]).start;
465
- }, offset)]);
466
- };
467
- this.getOffsetForAlignment = function (toOffset, align) {
468
- var size = _this.getSize();
469
- if (align === 'auto') {
470
- if (toOffset <= _this.scrollOffset) {
471
- align = 'start';
472
- } else if (toOffset >= _this.scrollOffset + size) {
473
- align = 'end';
474
- } else {
475
- align = 'start';
476
- }
477
- }
478
- if (align === 'start') {
479
- toOffset = toOffset;
480
- } else if (align === 'end') {
481
- toOffset = toOffset - size;
482
- } else if (align === 'center') {
483
- toOffset = toOffset - size / 2;
484
- }
485
- var scrollSizeProp = _this.options.horizontal ? 'scrollWidth' : 'scrollHeight';
486
- var scrollSize = _this.scrollElement ? 'document' in _this.scrollElement ? _this.scrollElement.document.documentElement[scrollSizeProp] : _this.scrollElement[scrollSizeProp] : 0;
487
- var maxOffset = scrollSize - _this.getSize();
488
- return Math.max(Math.min(maxOffset, toOffset), 0);
489
- };
490
- this.getOffsetForIndex = function (index, align) {
491
- if (align === void 0) {
492
- align = 'auto';
493
- }
494
- index = Math.max(0, Math.min(index, _this.options.count - 1));
495
- var measurement = utils.notUndefined(_this.getMeasurements()[index]);
496
- if (align === 'auto') {
497
- if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) {
498
- align = 'end';
499
- } else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) {
500
- align = 'start';
501
- } else {
502
- return [_this.scrollOffset, align];
503
- }
504
- }
505
- var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
506
- return [_this.getOffsetForAlignment(toOffset, align), align];
507
- };
508
- this.isDynamicMode = function () {
509
- return _this.measureElementCache.size > 0;
510
- };
511
- this.cancelScrollToIndex = function () {
512
- if (_this.scrollToIndexTimeoutId !== null) {
513
- clearTimeout(_this.scrollToIndexTimeoutId);
514
- _this.scrollToIndexTimeoutId = null;
515
- }
516
- };
517
- this.scrollToOffset = function (toOffset, _temp) {
518
- var _ref5 = _temp === void 0 ? {} : _temp,
519
- _ref5$align = _ref5.align,
520
- align = _ref5$align === void 0 ? 'start' : _ref5$align,
521
- behavior = _ref5.behavior;
522
- _this.cancelScrollToIndex();
523
- if (behavior === 'smooth' && _this.isDynamicMode()) {
524
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
525
- }
526
- _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), {
527
- adjustments: undefined,
528
- behavior: behavior
529
- });
530
- };
531
- this.scrollToIndex = function (index, _temp2) {
532
- var _ref6 = _temp2 === void 0 ? {} : _temp2,
533
- _ref6$align = _ref6.align,
534
- initialAlign = _ref6$align === void 0 ? 'auto' : _ref6$align,
535
- behavior = _ref6.behavior;
536
- index = Math.max(0, Math.min(index, _this.options.count - 1));
537
- _this.cancelScrollToIndex();
538
- if (behavior === 'smooth' && _this.isDynamicMode()) {
539
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
540
- }
541
- var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign),
542
- toOffset = _this$getOffsetForInd[0],
543
- align = _this$getOffsetForInd[1];
544
- _this._scrollToOffset(toOffset, {
545
- adjustments: undefined,
546
- behavior: behavior
547
- });
548
- if (behavior !== 'smooth' && _this.isDynamicMode()) {
549
- _this.scrollToIndexTimeoutId = setTimeout(function () {
550
- _this.scrollToIndexTimeoutId = null;
551
- var elementInDOM = _this.measureElementCache.has(_this.options.getItemKey(index));
552
- if (elementInDOM) {
553
- var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align),
554
- _toOffset = _this$getOffsetForInd2[0];
555
- if (!utils.approxEqual(_toOffset, _this.scrollOffset)) {
556
- _this.scrollToIndex(index, {
557
- align: align,
558
- behavior: behavior
559
- });
560
- }
561
- } else {
562
- _this.scrollToIndex(index, {
563
- align: align,
564
- behavior: behavior
565
- });
566
- }
567
- });
568
- }
569
- };
570
- this.scrollBy = function (delta, _temp3) {
571
- var _ref7 = _temp3 === void 0 ? {} : _temp3,
572
- behavior = _ref7.behavior;
573
- _this.cancelScrollToIndex();
574
- if (behavior === 'smooth' && _this.isDynamicMode()) {
575
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
576
- }
577
- _this._scrollToOffset(_this.scrollOffset + delta, {
578
- adjustments: undefined,
579
- behavior: behavior
580
- });
581
- };
582
- this.getTotalSize = function () {
583
- var measurements = _this.getMeasurements();
584
- var end;
585
- // If there are no measurements, set the end to paddingStart
586
- if (measurements.length === 0) {
587
- end = _this.options.paddingStart;
588
- } else {
589
- var _measurements$end, _measurements;
590
- // If lanes is 1, use the last measurement's end, otherwise find the maximum end value among all measurements
591
- end = _this.options.lanes === 1 ? (_measurements$end = (_measurements = measurements[measurements.length - 1]) == null ? void 0 : _measurements.end) != null ? _measurements$end : 0 : Math.max.apply(Math, measurements.slice(-_this.options.lanes).map(function (m) {
592
- return m.end;
593
- }));
594
- }
595
- return end - _this.options.scrollMargin + _this.options.paddingEnd;
596
- };
597
- this._scrollToOffset = function (offset, _ref8) {
598
- var adjustments = _ref8.adjustments,
599
- behavior = _ref8.behavior;
600
- _this.options.scrollToFn(offset, {
601
- behavior: behavior,
602
- adjustments: adjustments
603
- }, _this);
604
- };
605
- this.measure = function () {
606
- _this.itemSizeCache = new Map();
607
- _this.notify(false);
608
- };
609
- this.setOptions(_opts);
610
- this.scrollRect = this.options.initialRect;
611
- this.scrollOffset = this.options.initialOffset;
612
- this.measurementsCache = this.options.initialMeasurementsCache;
613
- this.measurementsCache.forEach(function (item) {
614
- _this.itemSizeCache.set(item.key, item.size);
615
- });
616
- this.maybeNotify();
617
- };
618
- var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
619
- while (low <= high) {
620
- var middle = (low + high) / 2 | 0;
621
- var currentValue = getCurrentValue(middle);
622
- if (currentValue < value) {
623
- low = middle + 1;
624
- } else if (currentValue > value) {
625
- high = middle - 1;
626
- } else {
627
- return middle;
628
- }
629
- }
630
- if (low > 0) {
631
- return low - 1;
632
- } else {
633
- return 0;
634
- }
635
- };
636
- function calculateRange(_ref9) {
637
- var measurements = _ref9.measurements,
638
- outerSize = _ref9.outerSize,
639
- scrollOffset = _ref9.scrollOffset;
640
- var count = measurements.length - 1;
641
- var getOffset = function getOffset(index) {
642
- return measurements[index].start;
643
- };
644
- var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
645
- var endIndex = startIndex;
646
- while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
647
- endIndex++;
648
- }
649
- return {
650
- startIndex: startIndex,
651
- endIndex: endIndex
652
- };
653
- }
654
-
655
- exports.approxEqual = utils.approxEqual;
656
- exports.memo = utils.memo;
657
- exports.notUndefined = utils.notUndefined;
658
- exports.Virtualizer = Virtualizer;
659
- exports.defaultKeyExtractor = defaultKeyExtractor;
660
- exports.defaultRangeExtractor = defaultRangeExtractor;
661
- exports.elementScroll = elementScroll;
662
- exports.measureElement = measureElement;
663
- exports.observeElementOffset = observeElementOffset;
664
- exports.observeElementRect = observeElementRect;
665
- exports.observeWindowOffset = observeWindowOffset;
666
- exports.observeWindowRect = observeWindowRect;
667
- exports.windowScroll = windowScroll;
668
- //# sourceMappingURL=index.js.map