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