@tanstack/svelte-virtual 3.0.3 → 3.1.3

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