@tanstack/svelte-virtual 3.0.0-beta.29 → 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,388 +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
- if (this.scrollOffset !== offset) {
303
- this.scrollOffset = offset;
304
- this.isScrolling = true;
305
- this.scrollAdjustments = 0;
306
- this.isScrollingTimeoutId = setTimeout(() => {
307
- this.isScrollingTimeoutId = null;
308
- this.isScrolling = false;
309
- this.notify();
310
- }, this.options.scrollingDelay);
311
- } else {
312
- this.isScrolling = false;
313
- this.scrollAdjustments = 0;
314
- }
315
- this.calculateRange();
316
- }));
317
- } else if (!this.isScrolling) {
318
- this.calculateRange();
259
+ } else {
260
+ return null;
319
261
  }
320
262
  };
321
- this.getSize = () => {
322
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
323
- };
324
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
325
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
326
- this.pendingMeasuredCacheIndexes = [];
327
- const measurements = this.measurementsCache.slice(0, min);
328
- for (let i = min; i < count; i++) {
329
- const key = getItemKey(i);
330
- const measuredSize = measurementsCache[key];
331
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
332
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
333
- const end = start + size;
334
- measurements[i] = {
335
- index: i,
336
- start,
337
- size,
338
- end,
339
- key
340
- };
341
- }
342
- this.measurementsCache = measurements;
343
- return measurements;
344
- }, {
345
- key: 'getMeasurements',
346
- 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];
347
273
  });
348
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
349
- const range = calculateRange({
350
- measurements,
351
- outerSize,
352
- scrollOffset
353
- });
354
- if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
355
- this.range = range;
356
- this.notify();
357
- }
358
- return this.range;
359
- }, {
360
- key: 'calculateRange',
361
- 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();
362
302
  });
363
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
364
- return rangeExtractor({
365
- ...range,
366
- overscan,
367
- count: count
368
- });
369
- }, {
370
- key: 'getIndexes',
371
- 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);
372
310
  });
373
- this.indexFromElement = node => {
374
- const attributeName = this.options.indexAttribute;
375
- const indexStr = node.getAttribute(attributeName);
376
- if (!indexStr) {
377
- console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
378
- return -1;
379
- }
380
- return parseInt(indexStr, 10);
311
+ return function () {
312
+ ro == null ? void 0 : ro.disconnect();
313
+ _this.cleanup();
381
314
  };
382
- this._measureElement = (node, _sync) => {
383
- const index = this.indexFromElement(node);
384
- const item = this.measurementsCache[index];
385
- if (!item) {
386
- return;
387
- }
388
- const prevNode = this.measureElementCache[item.key];
389
- const ro = this.getResizeObserver();
390
- if (!node.isConnected) {
391
- if (prevNode) {
392
- ro == null ? void 0 : ro.unobserve(prevNode);
393
- delete this.measureElementCache[item.key];
394
- }
395
- return;
396
- }
397
- if (!prevNode || prevNode !== node) {
398
- if (prevNode) {
399
- 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;
400
335
  }
401
- this.measureElementCache[item.key] = node;
402
- ro == null ? void 0 : ro.observe(node);
403
- }
404
- const measuredItemSize = this.options.measureElement(node, this);
405
- const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
406
- const delta = measuredItemSize - itemSize;
407
- if (delta !== 0) {
408
- if (item.start < this.scrollOffset && this.isScrolling) {
409
- if (this.options.debug) {
410
- console.info('correction', delta);
336
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
337
+ if (_this.isScrolling !== isScrolling) {
338
+ _this.isScrolling = isScrolling;
339
+ _this.notify();
411
340
  }
412
- this._scrollToOffset(this.scrollOffset, {
413
- adjustments: this.scrollAdjustments += delta,
414
- behavior: undefined,
415
- sync: false
416
- });
417
- }
418
- this.pendingMeasuredCacheIndexes.push(index);
419
- this.itemMeasurementsCache = {
420
- ...this.itemMeasurementsCache,
421
- [item.key]: measuredItemSize
422
341
  };
423
- this.notify();
424
- }
425
- };
426
- this.measureElement = node => {
427
- if (!node) {
428
- return;
429
- }
430
- this._measureElement(node, true);
431
- };
432
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
433
- const virtualItems = [];
434
- for (let k = 0, len = indexes.length; k < len; k++) {
435
- const i = indexes[k];
436
- const measurement = measurements[i];
437
- virtualItems.push(measurement);
438
- }
439
- return virtualItems;
440
- }, {
441
- key: 'getIndexes',
442
- debug: () => this.options.debug
443
- });
444
- this.scrollToOffset = function (toOffset, _temp) {
445
- let {
446
- align = 'start',
447
- behavior
448
- } = _temp === void 0 ? {} : _temp;
449
- const offset = _this.scrollOffset;
450
- const size = _this.getSize();
451
- if (align === 'auto') {
452
- if (toOffset <= offset) {
453
- align = 'start';
454
- } else if (toOffset >= offset + size) {
455
- align = 'end';
456
- } else {
457
- align = 'start';
342
+ _this.scrollAdjustments = 0;
343
+ if (_this.scrollOffset !== offset) {
344
+ _this.scrollOffset = offset;
345
+ onIsScrollingChange(true);
458
346
  }
459
- }
460
- const options = {
461
- adjustments: undefined,
462
- behavior,
463
- 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
464
378
  };
465
- if (align === 'start') {
466
- _this._scrollToOffset(toOffset, options);
467
- } else if (align === 'end') {
468
- _this._scrollToOffset(toOffset - size, options);
469
- } else if (align === 'center') {
470
- _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];
471
442
  }
472
- };
473
- this.scrollToIndex = function (index, _temp2) {
474
- let {
475
- align = 'auto',
476
- ...rest
477
- } = _temp2 === void 0 ? {} : _temp2;
478
- _this.pendingScrollToIndexCallback = null;
479
- const measurements = _this.getMeasurements();
480
- const offset = _this.scrollOffset;
481
- const size = _this.getSize();
482
- const {
483
- count
484
- } = _this.options;
485
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
486
- if (!measurement) {
487
- return;
443
+ return;
444
+ }
445
+ if (!prevNode || prevNode !== node) {
446
+ if (prevNode) {
447
+ ro == null ? void 0 : ro.unobserve(prevNode);
488
448
  }
489
- if (align === 'auto') {
490
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
491
- align = 'end';
492
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
493
- align = 'start';
494
- } else {
495
- 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);
496
460
  }
461
+ _this._scrollToOffset(_this.scrollOffset, {
462
+ adjustments: _this.scrollAdjustments += delta,
463
+ behavior: undefined,
464
+ sync: false
465
+ });
497
466
  }
498
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
499
- _this.scrollToOffset(toOffset, {
500
- align,
501
- ...rest
502
- });
503
- const isDynamic = Object.keys(_this.measureElementCache).length > 0;
504
- if (isDynamic) {
505
- const didSeen = () => typeof _this.itemMeasurementsCache[_this.options.getItemKey(index)] === 'number';
506
- if (!didSeen()) {
507
- _this.pendingScrollToIndexCallback = () => {
508
- if (didSeen()) {
509
- _this.pendingScrollToIndexCallback = null;
510
- _this.scrollToIndex(index, {
511
- align,
512
- ...rest
513
- });
514
- }
515
- };
516
- }
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';
517
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
518
524
  };
519
- this.getTotalSize = () => {
520
- var _this$getMeasurements;
521
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
522
- };
523
- this._scrollToOffset = (offset, _ref4) => {
524
- let {
525
- adjustments,
526
- behavior,
527
- sync
528
- } = _ref4;
529
- this.options.scrollToFn(offset, {
530
- behavior,
531
- sync,
532
- adjustments
533
- }, 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);
534
553
  };
535
- this.measure = () => {
536
- this.itemMeasurementsCache = {};
537
- 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
538
562
  };
539
- this.setOptions(_opts);
540
- this.scrollRect = this.options.initialRect;
541
- this.scrollOffset = this.options.initialOffset;
542
- this.calculateRange();
543
- }
544
- }
545
- 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) {
546
605
  while (low <= high) {
547
- const middle = (low + high) / 2 | 0;
548
- const currentValue = getCurrentValue(middle);
606
+ var middle = (low + high) / 2 | 0;
607
+ var currentValue = getCurrentValue(middle);
549
608
  if (currentValue < value) {
550
609
  low = middle + 1;
551
610
  } else if (currentValue > value) {
@@ -560,56 +619,57 @@
560
619
  return 0;
561
620
  }
562
621
  };
563
- function calculateRange(_ref5) {
564
- let {
565
- measurements,
566
- outerSize,
567
- scrollOffset
568
- } = _ref5;
569
- const count = measurements.length - 1;
570
- const getOffset = index => measurements[index].start;
571
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
572
- 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;
573
632
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
574
633
  endIndex++;
575
634
  }
576
635
  return {
577
- startIndex,
578
- endIndex
636
+ startIndex: startIndex,
637
+ endIndex: endIndex
579
638
  };
580
639
  }
581
640
 
582
641
  function createVirtualizerBase(options) {
583
- const virtualizer = new Virtualizer(options);
584
- return store.readable(virtualizer, set => {
585
- const cleanup = virtualizer._didMount();
642
+ var virtualizer = new Virtualizer(options);
643
+ return store.readable(virtualizer, function (set) {
644
+ var cleanup = virtualizer._didMount();
586
645
  virtualizer._willUpdate();
587
- virtualizer.setOptions({
588
- ...options,
589
- onChange: instance => {
646
+ virtualizer.setOptions(_extends$1({}, options, {
647
+ onChange: function onChange(instance) {
590
648
  set(instance);
591
649
  options.onChange == null ? void 0 : options.onChange(instance);
592
650
  }
593
- });
594
- return () => cleanup();
651
+ }));
652
+ return function () {
653
+ return cleanup();
654
+ };
595
655
  });
596
656
  }
597
657
  function createVirtualizer(options) {
598
- return createVirtualizerBase({
658
+ return createVirtualizerBase(_extends$1({
599
659
  observeElementRect: observeElementRect,
600
660
  observeElementOffset: observeElementOffset,
601
- scrollToFn: elementScroll,
602
- ...options
603
- });
661
+ scrollToFn: elementScroll
662
+ }, options));
604
663
  }
605
664
  function createWindowVirtualizer(options) {
606
- return createVirtualizerBase({
607
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
665
+ return createVirtualizerBase(_extends$1({
666
+ getScrollElement: function getScrollElement() {
667
+ return typeof window !== 'undefined' ? window : null;
668
+ },
608
669
  observeElementRect: observeWindowRect,
609
670
  observeElementOffset: observeWindowOffset,
610
- scrollToFn: windowScroll,
611
- ...options
612
- });
671
+ scrollToFn: windowScroll
672
+ }, options));
613
673
  }
614
674
 
615
675
  exports.Virtualizer = Virtualizer;