@tanstack/virtual-core 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.
@@ -8,6 +8,7 @@
8
8
  *
9
9
  * @license MIT
10
10
  */
11
+ import { extends as _extends } from './_virtual/_rollupPluginBabelHelpers.mjs';
11
12
  import { memo } from './utils.mjs';
12
13
  export { memo } from './utils.mjs';
13
14
 
@@ -15,30 +16,32 @@ export { memo } from './utils.mjs';
15
16
 
16
17
  //
17
18
 
18
- const defaultKeyExtractor = index => index;
19
- const defaultRangeExtractor = range => {
20
- const start = Math.max(range.startIndex - range.overscan, 0);
21
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
22
- const arr = [];
23
- for (let i = start; i <= end; i++) {
24
- arr.push(i);
19
+ var defaultKeyExtractor = function defaultKeyExtractor(index) {
20
+ return index;
21
+ };
22
+ var defaultRangeExtractor = function defaultRangeExtractor(range) {
23
+ var start = Math.max(range.startIndex - range.overscan, 0);
24
+ var end = Math.min(range.endIndex + range.overscan, range.count - 1);
25
+ var arr = [];
26
+ for (var _i = start; _i <= end; _i++) {
27
+ arr.push(_i);
25
28
  }
26
29
  return arr;
27
30
  };
28
- const memoRectCallback = (instance, cb) => {
29
- let prev = {
31
+ var memoRectCallback = function memoRectCallback(instance, cb) {
32
+ var prev = {
30
33
  height: -1,
31
34
  width: -1
32
35
  };
33
- return rect => {
36
+ return function (rect) {
34
37
  if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
35
38
  cb(rect);
36
39
  }
37
40
  prev = rect;
38
41
  };
39
42
  };
40
- const observeElementRect = (instance, cb) => {
41
- const observer = new ResizeObserver(entries => {
43
+ var observeElementRect = function observeElementRect(instance, cb) {
44
+ var observer = new ResizeObserver(function (entries) {
42
45
  var _entries$, _entries$2;
43
46
  cb({
44
47
  width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
@@ -50,16 +53,18 @@ const observeElementRect = (instance, cb) => {
50
53
  }
51
54
  cb(instance.scrollElement.getBoundingClientRect());
52
55
  observer.observe(instance.scrollElement);
53
- return () => {
56
+ return function () {
54
57
  observer.unobserve(instance.scrollElement);
55
58
  };
56
59
  };
57
- const observeWindowRect = (instance, cb) => {
58
- const memoizedCallback = memoRectCallback(instance, cb);
59
- const onResize = () => memoizedCallback({
60
- width: instance.scrollElement.innerWidth,
61
- height: instance.scrollElement.innerHeight
62
- });
60
+ var observeWindowRect = function observeWindowRect(instance, cb) {
61
+ var memoizedCallback = memoRectCallback(instance, cb);
62
+ var onResize = function onResize() {
63
+ return memoizedCallback({
64
+ width: instance.scrollElement.innerWidth,
65
+ height: instance.scrollElement.innerHeight
66
+ });
67
+ };
63
68
  if (!instance.scrollElement) {
64
69
  return;
65
70
  }
@@ -68,32 +73,32 @@ const observeWindowRect = (instance, cb) => {
68
73
  capture: false,
69
74
  passive: true
70
75
  });
71
- return () => {
76
+ return function () {
72
77
  instance.scrollElement.removeEventListener('resize', onResize);
73
78
  };
74
79
  };
75
- const scrollProps = {
80
+ var scrollProps = {
76
81
  element: ['scrollLeft', 'scrollTop'],
77
82
  window: ['scrollX', 'scrollY']
78
83
  };
79
- const createOffsetObserver = mode => {
80
- return (instance, cb) => {
84
+ var createOffsetObserver = function createOffsetObserver(mode) {
85
+ return function (instance, cb) {
81
86
  if (!instance.scrollElement) {
82
87
  return;
83
88
  }
84
- const propX = scrollProps[mode][0];
85
- const propY = scrollProps[mode][1];
86
- let prevX = instance.scrollElement[propX];
87
- let prevY = instance.scrollElement[propY];
88
- const scroll = () => {
89
- const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
89
+ var propX = scrollProps[mode][0];
90
+ var propY = scrollProps[mode][1];
91
+ var prevX = instance.scrollElement[propX];
92
+ var prevY = instance.scrollElement[propY];
93
+ var scroll = function scroll() {
94
+ var offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
90
95
  cb(Math.max(0, offset - instance.options.scrollMargin));
91
96
  };
92
97
  scroll();
93
- const onScroll = e => {
94
- const target = e.currentTarget;
95
- const scrollX = target[propX];
96
- const scrollY = target[propY];
98
+ var onScroll = function onScroll(e) {
99
+ var target = e.currentTarget;
100
+ var scrollX = target[propX];
101
+ var scrollY = target[propY];
97
102
  if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
98
103
  scroll();
99
104
  }
@@ -104,388 +109,404 @@ const createOffsetObserver = mode => {
104
109
  capture: false,
105
110
  passive: true
106
111
  });
107
- return () => {
112
+ return function () {
108
113
  instance.scrollElement.removeEventListener('scroll', onScroll);
109
114
  };
110
115
  };
111
116
  };
112
- const observeElementOffset = createOffsetObserver('element');
113
- const observeWindowOffset = createOffsetObserver('window');
114
- const measureElement = (element, instance) => {
117
+ var observeElementOffset = createOffsetObserver('element');
118
+ var observeWindowOffset = createOffsetObserver('window');
119
+ var measureElement = function measureElement(element, instance) {
115
120
  return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
116
121
  };
117
- const windowScroll = (offset, _ref, instance) => {
118
- var _instance$scrollEleme;
119
- let {
120
- adjustments,
121
- behavior,
122
- sync
123
- } = _ref;
124
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
125
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
126
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
127
- behavior
128
- });
122
+ var windowScroll = function windowScroll(offset, _ref, instance) {
123
+ var _instance$scrollEleme, _instance$scrollEleme2;
124
+ var adjustments = _ref.adjustments,
125
+ behavior = _ref.behavior,
126
+ sync = _ref.sync;
127
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
128
+ (_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));
129
129
  };
130
- const elementScroll = (offset, _ref2, instance) => {
131
- var _instance$scrollEleme2;
132
- let {
133
- adjustments,
134
- behavior,
135
- sync
136
- } = _ref2;
137
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
138
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
139
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
140
- behavior
141
- });
130
+ var elementScroll = function elementScroll(offset, _ref2, instance) {
131
+ var _instance$scrollEleme3, _instance$scrollEleme4;
132
+ var adjustments = _ref2.adjustments,
133
+ behavior = _ref2.behavior,
134
+ sync = _ref2.sync;
135
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
136
+ (_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));
142
137
  };
143
- class Virtualizer {
144
- constructor(_opts) {
145
- var _this = this;
146
- this.unsubs = [];
147
- this.scrollElement = null;
148
- this.isScrolling = false;
149
- this.isScrollingTimeoutId = null;
150
- this.measurementsCache = [];
151
- this.itemMeasurementsCache = {};
152
- this.pendingMeasuredCacheIndexes = [];
153
- this.scrollAdjustments = 0;
154
- this.measureElementCache = {};
155
- this.pendingScrollToIndexCallback = null;
156
- this.getResizeObserver = (() => {
157
- let _ro = null;
158
- return () => {
159
- if (_ro) {
160
- return _ro;
161
- } else if (typeof ResizeObserver !== 'undefined') {
162
- return _ro = new ResizeObserver(entries => {
163
- entries.forEach(entry => {
164
- this._measureElement(entry.target, false);
165
- });
138
+ var Virtualizer = function Virtualizer(_opts) {
139
+ var _this = this;
140
+ this.unsubs = [];
141
+ this.scrollElement = null;
142
+ this.isScrolling = false;
143
+ this.isScrollingTimeoutId = null;
144
+ this.measurementsCache = [];
145
+ this.itemMeasurementsCache = {};
146
+ this.pendingMeasuredCacheIndexes = [];
147
+ this.scrollAdjustments = 0;
148
+ this.measureElementCache = {};
149
+ this.pendingScrollToIndexCallback = null;
150
+ this.getResizeObserver = function () {
151
+ var _ro = null;
152
+ return function () {
153
+ if (_ro) {
154
+ return _ro;
155
+ } else if (typeof ResizeObserver !== 'undefined') {
156
+ return _ro = new ResizeObserver(function (entries) {
157
+ entries.forEach(function (entry) {
158
+ _this._measureElement(entry.target, false);
166
159
  });
167
- } else {
168
- return null;
169
- }
170
- };
171
- })();
172
- this.range = {
173
- startIndex: 0,
174
- endIndex: 0
175
- };
176
- this.setOptions = opts => {
177
- Object.entries(opts).forEach(_ref3 => {
178
- let [key, value] = _ref3;
179
- if (typeof value === 'undefined') delete opts[key];
180
- });
181
- this.options = {
182
- debug: false,
183
- initialOffset: 0,
184
- overscan: 1,
185
- paddingStart: 0,
186
- paddingEnd: 0,
187
- scrollPaddingStart: 0,
188
- scrollPaddingEnd: 0,
189
- horizontal: false,
190
- getItemKey: defaultKeyExtractor,
191
- rangeExtractor: defaultRangeExtractor,
192
- onChange: () => {},
193
- measureElement,
194
- initialRect: {
195
- width: 0,
196
- height: 0
197
- },
198
- scrollMargin: 0,
199
- scrollingDelay: 150,
200
- indexAttribute: 'data-index',
201
- ...opts
202
- };
203
- };
204
- this.notify = () => {
205
- var _this$options$onChang, _this$options;
206
- (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
207
- };
208
- this.cleanup = () => {
209
- this.unsubs.filter(Boolean).forEach(d => d());
210
- this.unsubs = [];
211
- this.scrollElement = null;
212
- };
213
- this._didMount = () => {
214
- const ro = this.getResizeObserver();
215
- Object.values(this.measureElementCache).forEach(node => ro == null ? void 0 : ro.observe(node));
216
- return () => {
217
- ro == null ? void 0 : ro.disconnect();
218
- this.cleanup();
219
- };
220
- };
221
- this._willUpdate = () => {
222
- var _this$pendingScrollTo;
223
- (_this$pendingScrollTo = this.pendingScrollToIndexCallback) == null ? void 0 : _this$pendingScrollTo.call(this);
224
- const scrollElement = this.options.getScrollElement();
225
- if (this.scrollElement !== scrollElement) {
226
- this.cleanup();
227
- this.scrollElement = scrollElement;
228
- this._scrollToOffset(this.scrollOffset, {
229
- adjustments: undefined,
230
- behavior: undefined,
231
- sync: true
232
160
  });
233
- this.unsubs.push(this.options.observeElementRect(this, rect => {
234
- this.scrollRect = rect;
235
- this.calculateRange();
236
- }));
237
- this.unsubs.push(this.options.observeElementOffset(this, offset => {
238
- if (this.isScrollingTimeoutId !== null) {
239
- clearTimeout(this.isScrollingTimeoutId);
240
- this.isScrollingTimeoutId = null;
241
- }
242
- if (this.scrollOffset !== offset) {
243
- this.scrollOffset = offset;
244
- this.isScrolling = true;
245
- this.scrollAdjustments = 0;
246
- this.isScrollingTimeoutId = setTimeout(() => {
247
- this.isScrollingTimeoutId = null;
248
- this.isScrolling = false;
249
- this.notify();
250
- }, this.options.scrollingDelay);
251
- } else {
252
- this.isScrolling = false;
253
- this.scrollAdjustments = 0;
254
- }
255
- this.calculateRange();
256
- }));
257
- } else if (!this.isScrolling) {
258
- this.calculateRange();
161
+ } else {
162
+ return null;
259
163
  }
260
164
  };
261
- this.getSize = () => {
262
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
263
- };
264
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
265
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
266
- this.pendingMeasuredCacheIndexes = [];
267
- const measurements = this.measurementsCache.slice(0, min);
268
- for (let i = min; i < count; i++) {
269
- const key = getItemKey(i);
270
- const measuredSize = measurementsCache[key];
271
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
272
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
273
- const end = start + size;
274
- measurements[i] = {
275
- index: i,
276
- start,
277
- size,
278
- end,
279
- key
280
- };
281
- }
282
- this.measurementsCache = measurements;
283
- return measurements;
284
- }, {
285
- key: process.env.NODE_ENV !== 'production' && 'getMeasurements',
286
- debug: () => this.options.debug
165
+ }();
166
+ this.range = {
167
+ startIndex: 0,
168
+ endIndex: 0
169
+ };
170
+ this.setOptions = function (opts) {
171
+ Object.entries(opts).forEach(function (_ref3) {
172
+ var key = _ref3[0],
173
+ value = _ref3[1];
174
+ if (typeof value === 'undefined') delete opts[key];
287
175
  });
288
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
289
- const range = calculateRange({
290
- measurements,
291
- outerSize,
292
- scrollOffset
293
- });
294
- if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
295
- this.range = range;
296
- this.notify();
297
- }
298
- return this.range;
299
- }, {
300
- key: process.env.NODE_ENV !== 'production' && 'calculateRange',
301
- debug: () => this.options.debug
176
+ _this.options = _extends({
177
+ debug: false,
178
+ initialOffset: 0,
179
+ overscan: 1,
180
+ paddingStart: 0,
181
+ paddingEnd: 0,
182
+ scrollPaddingStart: 0,
183
+ scrollPaddingEnd: 0,
184
+ horizontal: false,
185
+ getItemKey: defaultKeyExtractor,
186
+ rangeExtractor: defaultRangeExtractor,
187
+ onChange: function onChange() {},
188
+ measureElement: measureElement,
189
+ initialRect: {
190
+ width: 0,
191
+ height: 0
192
+ },
193
+ scrollMargin: 0,
194
+ scrollingDelay: 150,
195
+ indexAttribute: 'data-index'
196
+ }, opts);
197
+ };
198
+ this.notify = function () {
199
+ _this.options.onChange == null ? void 0 : _this.options.onChange(_this);
200
+ };
201
+ this.cleanup = function () {
202
+ _this.unsubs.filter(Boolean).forEach(function (d) {
203
+ return d();
302
204
  });
303
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
304
- return rangeExtractor({
305
- ...range,
306
- overscan,
307
- count: count
308
- });
309
- }, {
310
- key: process.env.NODE_ENV !== 'production' && 'getIndexes',
311
- debug: () => this.options.debug
205
+ _this.unsubs = [];
206
+ _this.scrollElement = null;
207
+ };
208
+ this._didMount = function () {
209
+ var ro = _this.getResizeObserver();
210
+ Object.values(_this.measureElementCache).forEach(function (node) {
211
+ return ro == null ? void 0 : ro.observe(node);
312
212
  });
313
- this.indexFromElement = node => {
314
- const attributeName = this.options.indexAttribute;
315
- const indexStr = node.getAttribute(attributeName);
316
- if (!indexStr) {
317
- console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
318
- return -1;
319
- }
320
- return parseInt(indexStr, 10);
213
+ return function () {
214
+ ro == null ? void 0 : ro.disconnect();
215
+ _this.cleanup();
321
216
  };
322
- this._measureElement = (node, _sync) => {
323
- const index = this.indexFromElement(node);
324
- const item = this.measurementsCache[index];
325
- if (!item) {
326
- return;
327
- }
328
- const prevNode = this.measureElementCache[item.key];
329
- const ro = this.getResizeObserver();
330
- if (!node.isConnected) {
331
- if (prevNode) {
332
- ro == null ? void 0 : ro.unobserve(prevNode);
333
- delete this.measureElementCache[item.key];
334
- }
335
- return;
336
- }
337
- if (!prevNode || prevNode !== node) {
338
- if (prevNode) {
339
- ro == null ? void 0 : ro.unobserve(prevNode);
217
+ };
218
+ this._willUpdate = function () {
219
+ _this.pendingScrollToIndexCallback == null ? void 0 : _this.pendingScrollToIndexCallback();
220
+ var scrollElement = _this.options.getScrollElement();
221
+ if (_this.scrollElement !== scrollElement) {
222
+ _this.cleanup();
223
+ _this.scrollElement = scrollElement;
224
+ _this._scrollToOffset(_this.scrollOffset, {
225
+ adjustments: undefined,
226
+ behavior: undefined,
227
+ sync: true
228
+ });
229
+ _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
230
+ _this.scrollRect = rect;
231
+ _this.calculateRange();
232
+ }));
233
+ _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
234
+ if (_this.isScrollingTimeoutId !== null) {
235
+ clearTimeout(_this.isScrollingTimeoutId);
236
+ _this.isScrollingTimeoutId = null;
340
237
  }
341
- this.measureElementCache[item.key] = node;
342
- ro == null ? void 0 : ro.observe(node);
343
- }
344
- const measuredItemSize = this.options.measureElement(node, this);
345
- const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
346
- const delta = measuredItemSize - itemSize;
347
- if (delta !== 0) {
348
- if (item.start < this.scrollOffset && this.isScrolling) {
349
- if (process.env.NODE_ENV !== 'production' && this.options.debug) {
350
- console.info('correction', delta);
238
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
239
+ if (_this.isScrolling !== isScrolling) {
240
+ _this.isScrolling = isScrolling;
241
+ _this.notify();
351
242
  }
352
- this._scrollToOffset(this.scrollOffset, {
353
- adjustments: this.scrollAdjustments += delta,
354
- behavior: undefined,
355
- sync: false
356
- });
357
- }
358
- this.pendingMeasuredCacheIndexes.push(index);
359
- this.itemMeasurementsCache = {
360
- ...this.itemMeasurementsCache,
361
- [item.key]: measuredItemSize
362
243
  };
363
- this.notify();
364
- }
365
- };
366
- this.measureElement = node => {
367
- if (!node) {
368
- return;
369
- }
370
- this._measureElement(node, true);
371
- };
372
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
373
- const virtualItems = [];
374
- for (let k = 0, len = indexes.length; k < len; k++) {
375
- const i = indexes[k];
376
- const measurement = measurements[i];
377
- virtualItems.push(measurement);
378
- }
379
- return virtualItems;
380
- }, {
381
- key: process.env.NODE_ENV !== 'production' && 'getIndexes',
382
- debug: () => this.options.debug
383
- });
384
- this.scrollToOffset = function (toOffset, _temp) {
385
- let {
386
- align = 'start',
387
- behavior
388
- } = _temp === void 0 ? {} : _temp;
389
- const offset = _this.scrollOffset;
390
- const size = _this.getSize();
391
- if (align === 'auto') {
392
- if (toOffset <= offset) {
393
- align = 'start';
394
- } else if (toOffset >= offset + size) {
395
- align = 'end';
396
- } else {
397
- align = 'start';
244
+ _this.scrollAdjustments = 0;
245
+ if (_this.scrollOffset !== offset) {
246
+ _this.scrollOffset = offset;
247
+ onIsScrollingChange(true);
398
248
  }
399
- }
400
- const options = {
401
- adjustments: undefined,
402
- behavior,
403
- sync: false
249
+ _this.calculateRange();
250
+ _this.isScrollingTimeoutId = setTimeout(function () {
251
+ _this.isScrollingTimeoutId = null;
252
+ onIsScrollingChange(false);
253
+ }, _this.options.scrollingDelay);
254
+ }));
255
+ } else if (!_this.isScrolling) {
256
+ _this.calculateRange();
257
+ }
258
+ };
259
+ this.getSize = function () {
260
+ return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
261
+ };
262
+ this.getMeasurements = memo(function () {
263
+ return [_this.options.count, _this.options.paddingStart, _this.options.getItemKey, _this.itemMeasurementsCache];
264
+ }, function (count, paddingStart, getItemKey, measurementsCache) {
265
+ var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
266
+ _this.pendingMeasuredCacheIndexes = [];
267
+ var measurements = _this.measurementsCache.slice(0, min);
268
+ for (var _i2 = min; _i2 < count; _i2++) {
269
+ var key = getItemKey(_i2);
270
+ var measuredSize = measurementsCache[key];
271
+ var start = measurements[_i2 - 1] ? measurements[_i2 - 1].end : paddingStart;
272
+ var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
273
+ var end = start + size;
274
+ measurements[_i2] = {
275
+ index: _i2,
276
+ start: start,
277
+ size: size,
278
+ end: end,
279
+ key: key
404
280
  };
405
- if (align === 'start') {
406
- _this._scrollToOffset(toOffset, options);
407
- } else if (align === 'end') {
408
- _this._scrollToOffset(toOffset - size, options);
409
- } else if (align === 'center') {
410
- _this._scrollToOffset(toOffset - size / 2, options);
281
+ }
282
+ _this.measurementsCache = measurements;
283
+ return measurements;
284
+ }, {
285
+ key: process.env.NODE_ENV !== 'production' && 'getMeasurements',
286
+ debug: function debug() {
287
+ return _this.options.debug;
288
+ }
289
+ });
290
+ this.calculateRange = memo(function () {
291
+ return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
292
+ }, function (measurements, outerSize, scrollOffset) {
293
+ var range = calculateRange({
294
+ measurements: measurements,
295
+ outerSize: outerSize,
296
+ scrollOffset: scrollOffset
297
+ });
298
+ if (range.startIndex !== _this.range.startIndex || range.endIndex !== _this.range.endIndex) {
299
+ _this.range = range;
300
+ _this.notify();
301
+ }
302
+ return _this.range;
303
+ }, {
304
+ key: process.env.NODE_ENV !== 'production' && 'calculateRange',
305
+ debug: function debug() {
306
+ return _this.options.debug;
307
+ }
308
+ });
309
+ this.getIndexes = memo(function () {
310
+ return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count];
311
+ }, function (rangeExtractor, range, overscan, count) {
312
+ return rangeExtractor(_extends({}, range, {
313
+ overscan: overscan,
314
+ count: count
315
+ }));
316
+ }, {
317
+ key: process.env.NODE_ENV !== 'production' && 'getIndexes',
318
+ debug: function debug() {
319
+ return _this.options.debug;
320
+ }
321
+ });
322
+ this.indexFromElement = function (node) {
323
+ var attributeName = _this.options.indexAttribute;
324
+ var indexStr = node.getAttribute(attributeName);
325
+ if (!indexStr) {
326
+ console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
327
+ return -1;
328
+ }
329
+ return parseInt(indexStr, 10);
330
+ };
331
+ this._measureElement = function (node, _sync) {
332
+ var _this$itemMeasurement;
333
+ var index = _this.indexFromElement(node);
334
+ var item = _this.measurementsCache[index];
335
+ if (!item) {
336
+ return;
337
+ }
338
+ var prevNode = _this.measureElementCache[item.key];
339
+ var ro = _this.getResizeObserver();
340
+ if (!node.isConnected) {
341
+ if (prevNode) {
342
+ ro == null ? void 0 : ro.unobserve(prevNode);
343
+ delete _this.measureElementCache[item.key];
411
344
  }
412
- };
413
- this.scrollToIndex = function (index, _temp2) {
414
- let {
415
- align = 'auto',
416
- ...rest
417
- } = _temp2 === void 0 ? {} : _temp2;
418
- _this.pendingScrollToIndexCallback = null;
419
- const measurements = _this.getMeasurements();
420
- const offset = _this.scrollOffset;
421
- const size = _this.getSize();
422
- const {
423
- count
424
- } = _this.options;
425
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
426
- if (!measurement) {
427
- return;
345
+ return;
346
+ }
347
+ if (!prevNode || prevNode !== node) {
348
+ if (prevNode) {
349
+ ro == null ? void 0 : ro.unobserve(prevNode);
428
350
  }
429
- if (align === 'auto') {
430
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
431
- align = 'end';
432
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
433
- align = 'start';
434
- } else {
435
- return;
351
+ _this.measureElementCache[item.key] = node;
352
+ ro == null ? void 0 : ro.observe(node);
353
+ }
354
+ var measuredItemSize = _this.options.measureElement(node, _this);
355
+ var itemSize = (_this$itemMeasurement = _this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
356
+ var delta = measuredItemSize - itemSize;
357
+ if (delta !== 0) {
358
+ var _extends2;
359
+ if (item.start < _this.scrollOffset && _this.isScrolling) {
360
+ if (process.env.NODE_ENV !== 'production' && _this.options.debug) {
361
+ console.info('correction', delta);
436
362
  }
363
+ _this._scrollToOffset(_this.scrollOffset, {
364
+ adjustments: _this.scrollAdjustments += delta,
365
+ behavior: undefined,
366
+ sync: false
367
+ });
437
368
  }
438
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
439
- _this.scrollToOffset(toOffset, {
440
- align,
441
- ...rest
442
- });
443
- const isDynamic = Object.keys(_this.measureElementCache).length > 0;
444
- if (isDynamic) {
445
- const didSeen = () => typeof _this.itemMeasurementsCache[_this.options.getItemKey(index)] === 'number';
446
- if (!didSeen()) {
447
- _this.pendingScrollToIndexCallback = () => {
448
- if (didSeen()) {
449
- _this.pendingScrollToIndexCallback = null;
450
- _this.scrollToIndex(index, {
451
- align,
452
- ...rest
453
- });
454
- }
455
- };
456
- }
369
+ _this.pendingMeasuredCacheIndexes.push(index);
370
+ _this.itemMeasurementsCache = _extends({}, _this.itemMeasurementsCache, (_extends2 = {}, _extends2[item.key] = measuredItemSize, _extends2));
371
+ _this.notify();
372
+ }
373
+ };
374
+ this.measureElement = function (node) {
375
+ if (!node) {
376
+ return;
377
+ }
378
+ _this._measureElement(node, true);
379
+ };
380
+ this.getVirtualItems = memo(function () {
381
+ return [_this.getIndexes(), _this.getMeasurements()];
382
+ }, function (indexes, measurements) {
383
+ var virtualItems = [];
384
+ for (var k = 0, len = indexes.length; k < len; k++) {
385
+ var _i3 = indexes[k];
386
+ var measurement = measurements[_i3];
387
+ virtualItems.push(measurement);
388
+ }
389
+ return virtualItems;
390
+ }, {
391
+ key: process.env.NODE_ENV !== 'production' && 'getIndexes',
392
+ debug: function debug() {
393
+ return _this.options.debug;
394
+ }
395
+ });
396
+ this.getOffsetForAlignment = function (toOffset, align) {
397
+ var offset = _this.scrollOffset;
398
+ var size = _this.getSize();
399
+ if (align === 'auto') {
400
+ if (toOffset <= offset) {
401
+ align = 'start';
402
+ } else if (toOffset >= offset + size) {
403
+ align = 'end';
404
+ } else {
405
+ align = 'start';
457
406
  }
407
+ }
408
+ if (align === 'start') {
409
+ return toOffset;
410
+ } else if (align === 'end') {
411
+ return toOffset - size;
412
+ } else if (align === 'center') {
413
+ return toOffset - size / 2;
414
+ }
415
+ return toOffset;
416
+ };
417
+ this.scrollToOffset = function (toOffset, _temp) {
418
+ var _ref4 = _temp === void 0 ? {} : _temp,
419
+ _ref4$align = _ref4.align,
420
+ align = _ref4$align === void 0 ? 'start' : _ref4$align,
421
+ behavior = _ref4.behavior;
422
+ var options = {
423
+ adjustments: undefined,
424
+ behavior: behavior,
425
+ sync: false
458
426
  };
459
- this.getTotalSize = () => {
460
- var _this$getMeasurements;
461
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
462
- };
463
- this._scrollToOffset = (offset, _ref4) => {
464
- let {
465
- adjustments,
466
- behavior,
467
- sync
468
- } = _ref4;
469
- this.options.scrollToFn(offset, {
470
- behavior,
471
- sync,
472
- adjustments
473
- }, this);
427
+ _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options);
428
+ };
429
+ this.scrollToIndex = function (index, _temp2) {
430
+ var _ref5 = _temp2 === void 0 ? {} : _temp2,
431
+ _ref5$align = _ref5.align,
432
+ align = _ref5$align === void 0 ? 'auto' : _ref5$align,
433
+ behavior = _ref5.behavior;
434
+ _this.pendingScrollToIndexCallback = null;
435
+ var offset = _this.scrollOffset;
436
+ var size = _this.getSize();
437
+ var count = _this.options.count;
438
+ var measurements = _this.getMeasurements();
439
+ var measurement = measurements[Math.max(0, Math.min(index, count - 1))];
440
+ if (!measurement) {
441
+ throw new Error("VirtualItem not found for index = " + index);
442
+ }
443
+ if (align === 'auto') {
444
+ if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
445
+ align = 'end';
446
+ } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
447
+ align = 'start';
448
+ } else {
449
+ return;
450
+ }
451
+ }
452
+ var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) {
453
+ var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
454
+ return _this.getOffsetForAlignment(toOffset, align);
474
455
  };
475
- this.measure = () => {
476
- this.itemMeasurementsCache = {};
477
- this.notify();
456
+ var toOffset = getOffsetForIndexAndAlignment(measurement);
457
+ if (toOffset === offset) {
458
+ return;
459
+ }
460
+ var options = {
461
+ adjustments: undefined,
462
+ behavior: behavior,
463
+ sync: false
478
464
  };
479
- this.setOptions(_opts);
480
- this.scrollRect = this.options.initialRect;
481
- this.scrollOffset = this.options.initialOffset;
482
- this.calculateRange();
483
- }
484
- }
485
- const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
465
+ _this._scrollToOffset(toOffset, options);
466
+ var isDynamic = Object.keys(_this.measureElementCache).length > 0;
467
+ if (isDynamic) {
468
+ _this.pendingScrollToIndexCallback = function () {
469
+ _this.scrollToIndex(index, {
470
+ align: align,
471
+ behavior: behavior
472
+ });
473
+ };
474
+ }
475
+ };
476
+ this.scrollBy = function (adjustments, options) {
477
+ _this._scrollToOffset(_this.scrollOffset, {
478
+ adjustments: adjustments,
479
+ behavior: options == null ? void 0 : options.behavior,
480
+ sync: false
481
+ });
482
+ };
483
+ this.getTotalSize = function () {
484
+ var _this$getMeasurements;
485
+ return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) + _this.options.paddingEnd;
486
+ };
487
+ this._scrollToOffset = function (offset, _ref6) {
488
+ var adjustments = _ref6.adjustments,
489
+ behavior = _ref6.behavior,
490
+ sync = _ref6.sync;
491
+ _this.options.scrollToFn(offset, {
492
+ behavior: behavior,
493
+ sync: sync,
494
+ adjustments: adjustments
495
+ }, _this);
496
+ };
497
+ this.measure = function () {
498
+ _this.itemMeasurementsCache = {};
499
+ _this.notify();
500
+ };
501
+ this.setOptions(_opts);
502
+ this.scrollRect = this.options.initialRect;
503
+ this.scrollOffset = this.options.initialOffset;
504
+ this.calculateRange();
505
+ };
506
+ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
486
507
  while (low <= high) {
487
- const middle = (low + high) / 2 | 0;
488
- const currentValue = getCurrentValue(middle);
508
+ var middle = (low + high) / 2 | 0;
509
+ var currentValue = getCurrentValue(middle);
489
510
  if (currentValue < value) {
490
511
  low = middle + 1;
491
512
  } else if (currentValue > value) {
@@ -500,22 +521,22 @@ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
500
521
  return 0;
501
522
  }
502
523
  };
503
- function calculateRange(_ref5) {
504
- let {
505
- measurements,
506
- outerSize,
507
- scrollOffset
508
- } = _ref5;
509
- const count = measurements.length - 1;
510
- const getOffset = index => measurements[index].start;
511
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
512
- let endIndex = startIndex;
524
+ function calculateRange(_ref7) {
525
+ var measurements = _ref7.measurements,
526
+ outerSize = _ref7.outerSize,
527
+ scrollOffset = _ref7.scrollOffset;
528
+ var count = measurements.length - 1;
529
+ var getOffset = function getOffset(index) {
530
+ return measurements[index].start;
531
+ };
532
+ var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
533
+ var endIndex = startIndex;
513
534
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
514
535
  endIndex++;
515
536
  }
516
537
  return {
517
- startIndex,
518
- endIndex
538
+ startIndex: startIndex,
539
+ endIndex: endIndex
519
540
  };
520
541
  }
521
542