@tanstack/svelte-virtual 3.0.0-beta.30 → 3.0.0-beta.32

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.
@@ -14,6 +14,46 @@
14
14
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SvelteVirtual = {}, global.SvelteStore));
15
15
  })(this, (function (exports, store) { 'use strict';
16
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
+
17
57
  /**
18
58
  * virtual-core
19
59
  *
@@ -25,36 +65,35 @@
25
65
  * @license MIT
26
66
  */
27
67
  function memo(getDeps, fn, opts) {
28
- let deps = [];
29
- let result;
30
- return () => {
31
- let depTime;
68
+ var deps = [];
69
+ var result;
70
+ return function () {
71
+ var depTime;
32
72
  if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
33
- const newDeps = getDeps();
34
- const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
73
+ var newDeps = getDeps();
74
+ var depsChanged = newDeps.length !== deps.length || newDeps.some(function (dep, index) {
75
+ return deps[index] !== dep;
76
+ });
35
77
  if (!depsChanged) {
36
78
  return result;
37
79
  }
38
80
  deps = newDeps;
39
- let resultTime;
81
+ var resultTime;
40
82
  if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
41
- result = fn(...newDeps);
83
+ result = fn.apply(void 0, newDeps);
42
84
  opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
43
85
  if (opts.key && opts.debug != null && opts.debug()) {
44
- const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
45
- const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
46
- const resultFpsPercentage = resultEndTime / 16;
47
- const pad = (str, num) => {
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) {
48
90
  str = String(str);
49
91
  while (str.length < num) {
50
92
  str = ' ' + str;
51
93
  }
52
94
  return str;
53
95
  };
54
- console.info(`%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`, `
55
- font-size: .6rem;
56
- font-weight: bold;
57
- color: hsl(${Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120))}deg 100% 31%);`, opts == null ? void 0 : opts.key);
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);
58
97
  }
59
98
  return result;
60
99
  };
@@ -75,30 +114,32 @@
75
114
 
76
115
  //
77
116
 
78
- const defaultKeyExtractor = index => index;
79
- const defaultRangeExtractor = range => {
80
- const start = Math.max(range.startIndex - range.overscan, 0);
81
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
82
- const arr = [];
83
- for (let i = start; i <= end; i++) {
84
- arr.push(i);
117
+ var defaultKeyExtractor = function defaultKeyExtractor(index) {
118
+ return index;
119
+ };
120
+ var defaultRangeExtractor = function defaultRangeExtractor(range) {
121
+ var start = Math.max(range.startIndex - range.overscan, 0);
122
+ var end = Math.min(range.endIndex + range.overscan, range.count - 1);
123
+ var arr = [];
124
+ for (var _i = start; _i <= end; _i++) {
125
+ arr.push(_i);
85
126
  }
86
127
  return arr;
87
128
  };
88
- const memoRectCallback = (instance, cb) => {
89
- let prev = {
129
+ var memoRectCallback = function memoRectCallback(instance, cb) {
130
+ var prev = {
90
131
  height: -1,
91
132
  width: -1
92
133
  };
93
- return rect => {
134
+ return function (rect) {
94
135
  if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
95
136
  cb(rect);
96
137
  }
97
138
  prev = rect;
98
139
  };
99
140
  };
100
- const observeElementRect = (instance, cb) => {
101
- const observer = new ResizeObserver(entries => {
141
+ var observeElementRect = function observeElementRect(instance, cb) {
142
+ var observer = new ResizeObserver(function (entries) {
102
143
  var _entries$, _entries$2;
103
144
  cb({
104
145
  width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
@@ -110,16 +151,18 @@
110
151
  }
111
152
  cb(instance.scrollElement.getBoundingClientRect());
112
153
  observer.observe(instance.scrollElement);
113
- return () => {
154
+ return function () {
114
155
  observer.unobserve(instance.scrollElement);
115
156
  };
116
157
  };
117
- const observeWindowRect = (instance, cb) => {
118
- const memoizedCallback = memoRectCallback(instance, cb);
119
- const onResize = () => memoizedCallback({
120
- width: instance.scrollElement.innerWidth,
121
- height: instance.scrollElement.innerHeight
122
- });
158
+ var observeWindowRect = function observeWindowRect(instance, cb) {
159
+ var memoizedCallback = memoRectCallback(instance, cb);
160
+ var onResize = function onResize() {
161
+ return memoizedCallback({
162
+ width: instance.scrollElement.innerWidth,
163
+ height: instance.scrollElement.innerHeight
164
+ });
165
+ };
123
166
  if (!instance.scrollElement) {
124
167
  return;
125
168
  }
@@ -128,32 +171,32 @@
128
171
  capture: false,
129
172
  passive: true
130
173
  });
131
- return () => {
174
+ return function () {
132
175
  instance.scrollElement.removeEventListener('resize', onResize);
133
176
  };
134
177
  };
135
- const scrollProps = {
178
+ var scrollProps = {
136
179
  element: ['scrollLeft', 'scrollTop'],
137
180
  window: ['scrollX', 'scrollY']
138
181
  };
139
- const createOffsetObserver = mode => {
140
- return (instance, cb) => {
182
+ var createOffsetObserver = function createOffsetObserver(mode) {
183
+ return function (instance, cb) {
141
184
  if (!instance.scrollElement) {
142
185
  return;
143
186
  }
144
- const propX = scrollProps[mode][0];
145
- const propY = scrollProps[mode][1];
146
- let prevX = instance.scrollElement[propX];
147
- let prevY = instance.scrollElement[propY];
148
- const scroll = () => {
149
- const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
187
+ var propX = scrollProps[mode][0];
188
+ var propY = scrollProps[mode][1];
189
+ var prevX = instance.scrollElement[propX];
190
+ var prevY = instance.scrollElement[propY];
191
+ var scroll = function scroll() {
192
+ var offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
150
193
  cb(Math.max(0, offset - instance.options.scrollMargin));
151
194
  };
152
195
  scroll();
153
- const onScroll = e => {
154
- const target = e.currentTarget;
155
- const scrollX = target[propX];
156
- const scrollY = target[propY];
196
+ var onScroll = function onScroll(e) {
197
+ var target = e.currentTarget;
198
+ var scrollX = target[propX];
199
+ var scrollY = target[propY];
157
200
  if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
158
201
  scroll();
159
202
  }
@@ -164,390 +207,404 @@
164
207
  capture: false,
165
208
  passive: true
166
209
  });
167
- return () => {
210
+ return function () {
168
211
  instance.scrollElement.removeEventListener('scroll', onScroll);
169
212
  };
170
213
  };
171
214
  };
172
- const observeElementOffset = createOffsetObserver('element');
173
- const observeWindowOffset = createOffsetObserver('window');
174
- const measureElement = (element, instance) => {
215
+ var observeElementOffset = createOffsetObserver('element');
216
+ var observeWindowOffset = createOffsetObserver('window');
217
+ var measureElement = function measureElement(element, instance) {
175
218
  return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
176
219
  };
177
- const windowScroll = (offset, _ref, instance) => {
178
- var _instance$scrollEleme;
179
- let {
180
- adjustments,
181
- behavior,
182
- sync
183
- } = _ref;
184
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
185
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
186
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
187
- behavior
188
- });
220
+ var windowScroll = function windowScroll(offset, _ref, instance) {
221
+ var _instance$scrollEleme, _instance$scrollEleme2;
222
+ var adjustments = _ref.adjustments,
223
+ behavior = _ref.behavior,
224
+ sync = _ref.sync;
225
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
226
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo((_instance$scrollEleme2 = {}, _instance$scrollEleme2[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme2.behavior = behavior, _instance$scrollEleme2));
189
227
  };
190
- const elementScroll = (offset, _ref2, instance) => {
191
- var _instance$scrollEleme2;
192
- let {
193
- adjustments,
194
- behavior,
195
- sync
196
- } = _ref2;
197
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
198
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
199
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
200
- behavior
201
- });
228
+ var elementScroll = function elementScroll(offset, _ref2, instance) {
229
+ var _instance$scrollEleme3, _instance$scrollEleme4;
230
+ var adjustments = _ref2.adjustments,
231
+ behavior = _ref2.behavior,
232
+ sync = _ref2.sync;
233
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
234
+ (_instance$scrollEleme3 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme3.scrollTo == null ? void 0 : _instance$scrollEleme3.scrollTo((_instance$scrollEleme4 = {}, _instance$scrollEleme4[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme4.behavior = behavior, _instance$scrollEleme4));
202
235
  };
203
- class Virtualizer {
204
- constructor(_opts) {
205
- var _this = this;
206
- this.unsubs = [];
207
- this.scrollElement = null;
208
- this.isScrolling = false;
209
- this.isScrollingTimeoutId = null;
210
- this.measurementsCache = [];
211
- this.itemMeasurementsCache = {};
212
- this.pendingMeasuredCacheIndexes = [];
213
- this.scrollAdjustments = 0;
214
- this.measureElementCache = {};
215
- this.pendingScrollToIndexCallback = null;
216
- this.getResizeObserver = (() => {
217
- let _ro = null;
218
- return () => {
219
- if (_ro) {
220
- return _ro;
221
- } else if (typeof ResizeObserver !== 'undefined') {
222
- return _ro = new ResizeObserver(entries => {
223
- entries.forEach(entry => {
224
- this._measureElement(entry.target, false);
225
- });
236
+ var Virtualizer = function Virtualizer(_opts) {
237
+ var _this = this;
238
+ this.unsubs = [];
239
+ this.scrollElement = null;
240
+ this.isScrolling = false;
241
+ this.isScrollingTimeoutId = null;
242
+ this.measurementsCache = [];
243
+ this.itemMeasurementsCache = {};
244
+ this.pendingMeasuredCacheIndexes = [];
245
+ this.scrollAdjustments = 0;
246
+ this.measureElementCache = {};
247
+ this.pendingScrollToIndexCallback = null;
248
+ this.getResizeObserver = function () {
249
+ var _ro = null;
250
+ return function () {
251
+ if (_ro) {
252
+ return _ro;
253
+ } else if (typeof ResizeObserver !== 'undefined') {
254
+ return _ro = new ResizeObserver(function (entries) {
255
+ entries.forEach(function (entry) {
256
+ _this._measureElement(entry.target, false);
226
257
  });
227
- } else {
228
- return null;
229
- }
230
- };
231
- })();
232
- this.range = {
233
- startIndex: 0,
234
- endIndex: 0
235
- };
236
- this.setOptions = opts => {
237
- Object.entries(opts).forEach(_ref3 => {
238
- let [key, value] = _ref3;
239
- if (typeof value === 'undefined') delete opts[key];
240
- });
241
- this.options = {
242
- debug: false,
243
- initialOffset: 0,
244
- overscan: 1,
245
- paddingStart: 0,
246
- paddingEnd: 0,
247
- scrollPaddingStart: 0,
248
- scrollPaddingEnd: 0,
249
- horizontal: false,
250
- getItemKey: defaultKeyExtractor,
251
- rangeExtractor: defaultRangeExtractor,
252
- onChange: () => {},
253
- measureElement,
254
- initialRect: {
255
- width: 0,
256
- height: 0
257
- },
258
- scrollMargin: 0,
259
- scrollingDelay: 150,
260
- indexAttribute: 'data-index',
261
- ...opts
262
- };
263
- };
264
- this.notify = () => {
265
- var _this$options$onChang, _this$options;
266
- (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
267
- };
268
- this.cleanup = () => {
269
- this.unsubs.filter(Boolean).forEach(d => d());
270
- this.unsubs = [];
271
- this.scrollElement = null;
272
- };
273
- this._didMount = () => {
274
- const ro = this.getResizeObserver();
275
- Object.values(this.measureElementCache).forEach(node => ro == null ? void 0 : ro.observe(node));
276
- return () => {
277
- ro == null ? void 0 : ro.disconnect();
278
- this.cleanup();
279
- };
280
- };
281
- this._willUpdate = () => {
282
- var _this$pendingScrollTo;
283
- (_this$pendingScrollTo = this.pendingScrollToIndexCallback) == null ? void 0 : _this$pendingScrollTo.call(this);
284
- const scrollElement = this.options.getScrollElement();
285
- if (this.scrollElement !== scrollElement) {
286
- this.cleanup();
287
- this.scrollElement = scrollElement;
288
- this._scrollToOffset(this.scrollOffset, {
289
- adjustments: undefined,
290
- behavior: undefined,
291
- sync: true
292
258
  });
293
- this.unsubs.push(this.options.observeElementRect(this, rect => {
294
- this.scrollRect = rect;
295
- this.calculateRange();
296
- }));
297
- this.unsubs.push(this.options.observeElementOffset(this, offset => {
298
- if (this.isScrollingTimeoutId !== null) {
299
- clearTimeout(this.isScrollingTimeoutId);
300
- this.isScrollingTimeoutId = null;
301
- }
302
- const onIsScrollingChange = isScrolling => {
303
- if (this.isScrolling !== isScrolling) {
304
- this.isScrolling = isScrolling;
305
- this.notify();
306
- }
307
- };
308
- this.scrollAdjustments = 0;
309
- if (this.scrollOffset !== offset) {
310
- this.scrollOffset = offset;
311
- onIsScrollingChange(true);
312
- }
313
- this.calculateRange();
314
- this.isScrollingTimeoutId = setTimeout(() => {
315
- this.isScrollingTimeoutId = null;
316
- onIsScrollingChange(false);
317
- }, this.options.scrollingDelay);
318
- }));
319
- } else if (!this.isScrolling) {
320
- this.calculateRange();
259
+ } else {
260
+ return null;
321
261
  }
322
262
  };
323
- this.getSize = () => {
324
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
325
- };
326
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
327
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
328
- this.pendingMeasuredCacheIndexes = [];
329
- const measurements = this.measurementsCache.slice(0, min);
330
- for (let i = min; i < count; i++) {
331
- const key = getItemKey(i);
332
- const measuredSize = measurementsCache[key];
333
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
334
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
335
- const end = start + size;
336
- measurements[i] = {
337
- index: i,
338
- start,
339
- size,
340
- end,
341
- key
342
- };
343
- }
344
- this.measurementsCache = measurements;
345
- return measurements;
346
- }, {
347
- key: 'getMeasurements',
348
- debug: () => this.options.debug
263
+ }();
264
+ this.range = {
265
+ startIndex: 0,
266
+ endIndex: 0
267
+ };
268
+ this.setOptions = function (opts) {
269
+ Object.entries(opts).forEach(function (_ref3) {
270
+ var key = _ref3[0],
271
+ value = _ref3[1];
272
+ if (typeof value === 'undefined') delete opts[key];
349
273
  });
350
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
351
- const range = calculateRange({
352
- measurements,
353
- outerSize,
354
- scrollOffset
355
- });
356
- if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
357
- this.range = range;
358
- this.notify();
359
- }
360
- return this.range;
361
- }, {
362
- key: 'calculateRange',
363
- debug: () => this.options.debug
274
+ _this.options = _extends({
275
+ debug: false,
276
+ initialOffset: 0,
277
+ overscan: 1,
278
+ paddingStart: 0,
279
+ paddingEnd: 0,
280
+ scrollPaddingStart: 0,
281
+ scrollPaddingEnd: 0,
282
+ horizontal: false,
283
+ getItemKey: defaultKeyExtractor,
284
+ rangeExtractor: defaultRangeExtractor,
285
+ onChange: function onChange() {},
286
+ measureElement: measureElement,
287
+ initialRect: {
288
+ width: 0,
289
+ height: 0
290
+ },
291
+ scrollMargin: 0,
292
+ scrollingDelay: 150,
293
+ indexAttribute: 'data-index'
294
+ }, opts);
295
+ };
296
+ this.notify = function () {
297
+ _this.options.onChange == null ? void 0 : _this.options.onChange(_this);
298
+ };
299
+ this.cleanup = function () {
300
+ _this.unsubs.filter(Boolean).forEach(function (d) {
301
+ return d();
364
302
  });
365
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
366
- return rangeExtractor({
367
- ...range,
368
- overscan,
369
- count: count
370
- });
371
- }, {
372
- key: 'getIndexes',
373
- debug: () => this.options.debug
303
+ _this.unsubs = [];
304
+ _this.scrollElement = null;
305
+ };
306
+ this._didMount = function () {
307
+ var ro = _this.getResizeObserver();
308
+ Object.values(_this.measureElementCache).forEach(function (node) {
309
+ return ro == null ? void 0 : ro.observe(node);
374
310
  });
375
- this.indexFromElement = node => {
376
- const attributeName = this.options.indexAttribute;
377
- const indexStr = node.getAttribute(attributeName);
378
- if (!indexStr) {
379
- console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
380
- return -1;
381
- }
382
- return parseInt(indexStr, 10);
311
+ return function () {
312
+ ro == null ? void 0 : ro.disconnect();
313
+ _this.cleanup();
383
314
  };
384
- this._measureElement = (node, _sync) => {
385
- const index = this.indexFromElement(node);
386
- const item = this.measurementsCache[index];
387
- if (!item) {
388
- return;
389
- }
390
- const prevNode = this.measureElementCache[item.key];
391
- const ro = this.getResizeObserver();
392
- if (!node.isConnected) {
393
- if (prevNode) {
394
- ro == null ? void 0 : ro.unobserve(prevNode);
395
- delete this.measureElementCache[item.key];
396
- }
397
- return;
398
- }
399
- if (!prevNode || prevNode !== node) {
400
- if (prevNode) {
401
- ro == null ? void 0 : ro.unobserve(prevNode);
315
+ };
316
+ this._willUpdate = function () {
317
+ _this.pendingScrollToIndexCallback == null ? void 0 : _this.pendingScrollToIndexCallback();
318
+ var scrollElement = _this.options.getScrollElement();
319
+ if (_this.scrollElement !== scrollElement) {
320
+ _this.cleanup();
321
+ _this.scrollElement = scrollElement;
322
+ _this._scrollToOffset(_this.scrollOffset, {
323
+ adjustments: undefined,
324
+ behavior: undefined,
325
+ sync: true
326
+ });
327
+ _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
328
+ _this.scrollRect = rect;
329
+ _this.calculateRange();
330
+ }));
331
+ _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
332
+ if (_this.isScrollingTimeoutId !== null) {
333
+ clearTimeout(_this.isScrollingTimeoutId);
334
+ _this.isScrollingTimeoutId = null;
402
335
  }
403
- this.measureElementCache[item.key] = node;
404
- ro == null ? void 0 : ro.observe(node);
405
- }
406
- const measuredItemSize = this.options.measureElement(node, this);
407
- const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
408
- const delta = measuredItemSize - itemSize;
409
- if (delta !== 0) {
410
- if (item.start < this.scrollOffset && this.isScrolling) {
411
- if (this.options.debug) {
412
- console.info('correction', delta);
336
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
337
+ if (_this.isScrolling !== isScrolling) {
338
+ _this.isScrolling = isScrolling;
339
+ _this.notify();
413
340
  }
414
- this._scrollToOffset(this.scrollOffset, {
415
- adjustments: this.scrollAdjustments += delta,
416
- behavior: undefined,
417
- sync: false
418
- });
419
- }
420
- this.pendingMeasuredCacheIndexes.push(index);
421
- this.itemMeasurementsCache = {
422
- ...this.itemMeasurementsCache,
423
- [item.key]: measuredItemSize
424
341
  };
425
- this.notify();
426
- }
427
- };
428
- this.measureElement = node => {
429
- if (!node) {
430
- return;
431
- }
432
- this._measureElement(node, true);
433
- };
434
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
435
- const virtualItems = [];
436
- for (let k = 0, len = indexes.length; k < len; k++) {
437
- const i = indexes[k];
438
- const measurement = measurements[i];
439
- virtualItems.push(measurement);
440
- }
441
- return virtualItems;
442
- }, {
443
- key: 'getIndexes',
444
- debug: () => this.options.debug
445
- });
446
- this.scrollToOffset = function (toOffset, _temp) {
447
- let {
448
- align = 'start',
449
- behavior
450
- } = _temp === void 0 ? {} : _temp;
451
- const offset = _this.scrollOffset;
452
- const size = _this.getSize();
453
- if (align === 'auto') {
454
- if (toOffset <= offset) {
455
- align = 'start';
456
- } else if (toOffset >= offset + size) {
457
- align = 'end';
458
- } else {
459
- align = 'start';
342
+ _this.scrollAdjustments = 0;
343
+ if (_this.scrollOffset !== offset) {
344
+ _this.scrollOffset = offset;
345
+ onIsScrollingChange(true);
460
346
  }
461
- }
462
- const options = {
463
- adjustments: undefined,
464
- behavior,
465
- sync: false
347
+ _this.calculateRange();
348
+ _this.isScrollingTimeoutId = setTimeout(function () {
349
+ _this.isScrollingTimeoutId = null;
350
+ onIsScrollingChange(false);
351
+ }, _this.options.scrollingDelay);
352
+ }));
353
+ } else if (!_this.isScrolling) {
354
+ _this.calculateRange();
355
+ }
356
+ };
357
+ this.getSize = function () {
358
+ return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
359
+ };
360
+ this.getMeasurements = memo(function () {
361
+ return [_this.options.count, _this.options.paddingStart, _this.options.getItemKey, _this.itemMeasurementsCache];
362
+ }, function (count, paddingStart, getItemKey, measurementsCache) {
363
+ var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
364
+ _this.pendingMeasuredCacheIndexes = [];
365
+ var measurements = _this.measurementsCache.slice(0, min);
366
+ for (var _i2 = min; _i2 < count; _i2++) {
367
+ var key = getItemKey(_i2);
368
+ var measuredSize = measurementsCache[key];
369
+ var start = measurements[_i2 - 1] ? measurements[_i2 - 1].end : paddingStart;
370
+ var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
371
+ var end = start + size;
372
+ measurements[_i2] = {
373
+ index: _i2,
374
+ start: start,
375
+ size: size,
376
+ end: end,
377
+ key: key
466
378
  };
467
- if (align === 'start') {
468
- _this._scrollToOffset(toOffset, options);
469
- } else if (align === 'end') {
470
- _this._scrollToOffset(toOffset - size, options);
471
- } else if (align === 'center') {
472
- _this._scrollToOffset(toOffset - size / 2, options);
379
+ }
380
+ _this.measurementsCache = measurements;
381
+ return measurements;
382
+ }, {
383
+ key: 'getMeasurements',
384
+ debug: function debug() {
385
+ return _this.options.debug;
386
+ }
387
+ });
388
+ this.calculateRange = memo(function () {
389
+ return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
390
+ }, function (measurements, outerSize, scrollOffset) {
391
+ var range = calculateRange({
392
+ measurements: measurements,
393
+ outerSize: outerSize,
394
+ scrollOffset: scrollOffset
395
+ });
396
+ if (range.startIndex !== _this.range.startIndex || range.endIndex !== _this.range.endIndex) {
397
+ _this.range = range;
398
+ _this.notify();
399
+ }
400
+ return _this.range;
401
+ }, {
402
+ key: 'calculateRange',
403
+ debug: function debug() {
404
+ return _this.options.debug;
405
+ }
406
+ });
407
+ this.getIndexes = memo(function () {
408
+ return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count];
409
+ }, function (rangeExtractor, range, overscan, count) {
410
+ return rangeExtractor(_extends({}, range, {
411
+ overscan: overscan,
412
+ count: count
413
+ }));
414
+ }, {
415
+ key: 'getIndexes',
416
+ debug: function debug() {
417
+ return _this.options.debug;
418
+ }
419
+ });
420
+ this.indexFromElement = function (node) {
421
+ var attributeName = _this.options.indexAttribute;
422
+ var indexStr = node.getAttribute(attributeName);
423
+ if (!indexStr) {
424
+ console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
425
+ return -1;
426
+ }
427
+ return parseInt(indexStr, 10);
428
+ };
429
+ this._measureElement = function (node, _sync) {
430
+ var _this$itemMeasurement;
431
+ var index = _this.indexFromElement(node);
432
+ var item = _this.measurementsCache[index];
433
+ if (!item) {
434
+ return;
435
+ }
436
+ var prevNode = _this.measureElementCache[item.key];
437
+ var ro = _this.getResizeObserver();
438
+ if (!node.isConnected) {
439
+ if (prevNode) {
440
+ ro == null ? void 0 : ro.unobserve(prevNode);
441
+ delete _this.measureElementCache[item.key];
473
442
  }
474
- };
475
- this.scrollToIndex = function (index, _temp2) {
476
- let {
477
- align = 'auto',
478
- ...rest
479
- } = _temp2 === void 0 ? {} : _temp2;
480
- _this.pendingScrollToIndexCallback = null;
481
- const measurements = _this.getMeasurements();
482
- const offset = _this.scrollOffset;
483
- const size = _this.getSize();
484
- const {
485
- count
486
- } = _this.options;
487
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
488
- if (!measurement) {
489
- return;
443
+ return;
444
+ }
445
+ if (!prevNode || prevNode !== node) {
446
+ if (prevNode) {
447
+ ro == null ? void 0 : ro.unobserve(prevNode);
490
448
  }
491
- if (align === 'auto') {
492
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
493
- align = 'end';
494
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
495
- align = 'start';
496
- } else {
497
- return;
449
+ _this.measureElementCache[item.key] = node;
450
+ ro == null ? void 0 : ro.observe(node);
451
+ }
452
+ var measuredItemSize = _this.options.measureElement(node, _this);
453
+ var itemSize = (_this$itemMeasurement = _this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
454
+ var delta = measuredItemSize - itemSize;
455
+ if (delta !== 0) {
456
+ var _extends2;
457
+ if (item.start < _this.scrollOffset && _this.isScrolling) {
458
+ if (_this.options.debug) {
459
+ console.info('correction', delta);
498
460
  }
461
+ _this._scrollToOffset(_this.scrollOffset, {
462
+ adjustments: _this.scrollAdjustments += delta,
463
+ behavior: undefined,
464
+ sync: false
465
+ });
499
466
  }
500
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
501
- _this.scrollToOffset(toOffset, {
502
- align,
503
- ...rest
504
- });
505
- const isDynamic = Object.keys(_this.measureElementCache).length > 0;
506
- if (isDynamic) {
507
- const didSeen = () => typeof _this.itemMeasurementsCache[_this.options.getItemKey(index)] === 'number';
508
- if (!didSeen()) {
509
- _this.pendingScrollToIndexCallback = () => {
510
- if (didSeen()) {
511
- _this.pendingScrollToIndexCallback = null;
512
- _this.scrollToIndex(index, {
513
- align,
514
- ...rest
515
- });
516
- }
517
- };
518
- }
467
+ _this.pendingMeasuredCacheIndexes.push(index);
468
+ _this.itemMeasurementsCache = _extends({}, _this.itemMeasurementsCache, (_extends2 = {}, _extends2[item.key] = measuredItemSize, _extends2));
469
+ _this.notify();
470
+ }
471
+ };
472
+ this.measureElement = function (node) {
473
+ if (!node) {
474
+ return;
475
+ }
476
+ _this._measureElement(node, true);
477
+ };
478
+ this.getVirtualItems = memo(function () {
479
+ return [_this.getIndexes(), _this.getMeasurements()];
480
+ }, function (indexes, measurements) {
481
+ var virtualItems = [];
482
+ for (var k = 0, len = indexes.length; k < len; k++) {
483
+ var _i3 = indexes[k];
484
+ var measurement = measurements[_i3];
485
+ virtualItems.push(measurement);
486
+ }
487
+ return virtualItems;
488
+ }, {
489
+ key: 'getIndexes',
490
+ debug: function debug() {
491
+ return _this.options.debug;
492
+ }
493
+ });
494
+ this.getOffsetForAlignment = function (toOffset, align) {
495
+ var offset = _this.scrollOffset;
496
+ var size = _this.getSize();
497
+ if (align === 'auto') {
498
+ if (toOffset <= offset) {
499
+ align = 'start';
500
+ } else if (toOffset >= offset + size) {
501
+ align = 'end';
502
+ } else {
503
+ align = 'start';
519
504
  }
505
+ }
506
+ if (align === 'start') {
507
+ return toOffset;
508
+ } else if (align === 'end') {
509
+ return toOffset - size;
510
+ } else if (align === 'center') {
511
+ return toOffset - size / 2;
512
+ }
513
+ return toOffset;
514
+ };
515
+ this.scrollToOffset = function (toOffset, _temp) {
516
+ var _ref4 = _temp === void 0 ? {} : _temp,
517
+ _ref4$align = _ref4.align,
518
+ align = _ref4$align === void 0 ? 'start' : _ref4$align,
519
+ behavior = _ref4.behavior;
520
+ var options = {
521
+ adjustments: undefined,
522
+ behavior: behavior,
523
+ sync: false
520
524
  };
521
- this.getTotalSize = () => {
522
- var _this$getMeasurements;
523
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
524
- };
525
- this._scrollToOffset = (offset, _ref4) => {
526
- let {
527
- adjustments,
528
- behavior,
529
- sync
530
- } = _ref4;
531
- this.options.scrollToFn(offset, {
532
- behavior,
533
- sync,
534
- adjustments
535
- }, this);
525
+ _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options);
526
+ };
527
+ this.scrollToIndex = function (index, _temp2) {
528
+ var _ref5 = _temp2 === void 0 ? {} : _temp2,
529
+ _ref5$align = _ref5.align,
530
+ align = _ref5$align === void 0 ? 'auto' : _ref5$align,
531
+ behavior = _ref5.behavior;
532
+ _this.pendingScrollToIndexCallback = null;
533
+ var offset = _this.scrollOffset;
534
+ var size = _this.getSize();
535
+ var count = _this.options.count;
536
+ var measurements = _this.getMeasurements();
537
+ var measurement = measurements[Math.max(0, Math.min(index, count - 1))];
538
+ if (!measurement) {
539
+ throw new Error("VirtualItem not found for index = " + index);
540
+ }
541
+ if (align === 'auto') {
542
+ if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
543
+ align = 'end';
544
+ } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
545
+ align = 'start';
546
+ } else {
547
+ return;
548
+ }
549
+ }
550
+ var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) {
551
+ var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
552
+ return _this.getOffsetForAlignment(toOffset, align);
536
553
  };
537
- this.measure = () => {
538
- this.itemMeasurementsCache = {};
539
- this.notify();
554
+ var toOffset = getOffsetForIndexAndAlignment(measurement);
555
+ if (toOffset === offset) {
556
+ return;
557
+ }
558
+ var options = {
559
+ adjustments: undefined,
560
+ behavior: behavior,
561
+ sync: false
540
562
  };
541
- this.setOptions(_opts);
542
- this.scrollRect = this.options.initialRect;
543
- this.scrollOffset = this.options.initialOffset;
544
- this.calculateRange();
545
- }
546
- }
547
- const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
563
+ _this._scrollToOffset(toOffset, options);
564
+ var isDynamic = Object.keys(_this.measureElementCache).length > 0;
565
+ if (isDynamic) {
566
+ _this.pendingScrollToIndexCallback = function () {
567
+ _this.scrollToIndex(index, {
568
+ align: align,
569
+ behavior: behavior
570
+ });
571
+ };
572
+ }
573
+ };
574
+ this.scrollBy = function (adjustments, options) {
575
+ _this._scrollToOffset(_this.scrollOffset, {
576
+ adjustments: adjustments,
577
+ behavior: options == null ? void 0 : options.behavior,
578
+ sync: false
579
+ });
580
+ };
581
+ this.getTotalSize = function () {
582
+ var _this$getMeasurements;
583
+ return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) + _this.options.paddingEnd;
584
+ };
585
+ this._scrollToOffset = function (offset, _ref6) {
586
+ var adjustments = _ref6.adjustments,
587
+ behavior = _ref6.behavior,
588
+ sync = _ref6.sync;
589
+ _this.options.scrollToFn(offset, {
590
+ behavior: behavior,
591
+ sync: sync,
592
+ adjustments: adjustments
593
+ }, _this);
594
+ };
595
+ this.measure = function () {
596
+ _this.itemMeasurementsCache = {};
597
+ _this.notify();
598
+ };
599
+ this.setOptions(_opts);
600
+ this.scrollRect = this.options.initialRect;
601
+ this.scrollOffset = this.options.initialOffset;
602
+ this.calculateRange();
603
+ };
604
+ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
548
605
  while (low <= high) {
549
- const middle = (low + high) / 2 | 0;
550
- const currentValue = getCurrentValue(middle);
606
+ var middle = (low + high) / 2 | 0;
607
+ var currentValue = getCurrentValue(middle);
551
608
  if (currentValue < value) {
552
609
  low = middle + 1;
553
610
  } else if (currentValue > value) {
@@ -562,56 +619,57 @@
562
619
  return 0;
563
620
  }
564
621
  };
565
- function calculateRange(_ref5) {
566
- let {
567
- measurements,
568
- outerSize,
569
- scrollOffset
570
- } = _ref5;
571
- const count = measurements.length - 1;
572
- const getOffset = index => measurements[index].start;
573
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
574
- let endIndex = startIndex;
622
+ function calculateRange(_ref7) {
623
+ var measurements = _ref7.measurements,
624
+ outerSize = _ref7.outerSize,
625
+ scrollOffset = _ref7.scrollOffset;
626
+ var count = measurements.length - 1;
627
+ var getOffset = function getOffset(index) {
628
+ return measurements[index].start;
629
+ };
630
+ var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
631
+ var endIndex = startIndex;
575
632
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
576
633
  endIndex++;
577
634
  }
578
635
  return {
579
- startIndex,
580
- endIndex
636
+ startIndex: startIndex,
637
+ endIndex: endIndex
581
638
  };
582
639
  }
583
640
 
584
641
  function createVirtualizerBase(options) {
585
- const virtualizer = new Virtualizer(options);
586
- return store.readable(virtualizer, set => {
587
- const cleanup = virtualizer._didMount();
642
+ var virtualizer = new Virtualizer(options);
643
+ return store.readable(virtualizer, function (set) {
644
+ var cleanup = virtualizer._didMount();
588
645
  virtualizer._willUpdate();
589
- virtualizer.setOptions({
590
- ...options,
591
- onChange: instance => {
646
+ virtualizer.setOptions(_extends$1({}, options, {
647
+ onChange: function onChange(instance) {
592
648
  set(instance);
593
649
  options.onChange == null ? void 0 : options.onChange(instance);
594
650
  }
595
- });
596
- return () => cleanup();
651
+ }));
652
+ return function () {
653
+ return cleanup();
654
+ };
597
655
  });
598
656
  }
599
657
  function createVirtualizer(options) {
600
- return createVirtualizerBase({
658
+ return createVirtualizerBase(_extends$1({
601
659
  observeElementRect: observeElementRect,
602
660
  observeElementOffset: observeElementOffset,
603
- scrollToFn: elementScroll,
604
- ...options
605
- });
661
+ scrollToFn: elementScroll
662
+ }, options));
606
663
  }
607
664
  function createWindowVirtualizer(options) {
608
- return createVirtualizerBase({
609
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
665
+ return createVirtualizerBase(_extends$1({
666
+ getScrollElement: function getScrollElement() {
667
+ return typeof window !== 'undefined' ? window : null;
668
+ },
610
669
  observeElementRect: observeWindowRect,
611
670
  observeElementOffset: observeWindowOffset,
612
- scrollToFn: windowScroll,
613
- ...options
614
- });
671
+ scrollToFn: windowScroll
672
+ }, options));
615
673
  }
616
674
 
617
675
  exports.Virtualizer = Virtualizer;