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