@tanstack/virtual-core 3.0.4 → 3.1.1
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.
- package/dist/cjs/index.cjs +649 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +126 -0
- package/dist/cjs/utils.cjs +59 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/{build/lib → dist/esm}/index.d.ts +1 -1
- package/dist/esm/index.js +649 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/utils.d.ts +10 -0
- package/dist/esm/utils.js +59 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +33 -23
- package/src/index.ts +22 -21
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +0 -27
- package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -31
- package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs +0 -27
- package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -653
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.js +0 -668
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.mjs +0 -653
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/utils.esm.js +0 -58
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.js +0 -64
- package/build/lib/utils.js.map +0 -1
- package/build/lib/utils.mjs +0 -58
- package/build/lib/utils.mjs.map +0 -1
- package/build/umd/index.development.js +0 -732
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -12
- package/build/umd/index.production.js.map +0 -1
- /package/{build/lib/utils.d.ts → dist/cjs/utils.d.cts} +0 -0
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const utils = require("./utils.cjs");
|
|
4
|
+
const defaultKeyExtractor = (index) => index;
|
|
5
|
+
const defaultRangeExtractor = (range) => {
|
|
6
|
+
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
7
|
+
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
8
|
+
const arr = [];
|
|
9
|
+
for (let i = start; i <= end; i++) {
|
|
10
|
+
arr.push(i);
|
|
11
|
+
}
|
|
12
|
+
return arr;
|
|
13
|
+
};
|
|
14
|
+
const observeElementRect = (instance, cb) => {
|
|
15
|
+
const element = instance.scrollElement;
|
|
16
|
+
if (!element) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const handler = (rect) => {
|
|
20
|
+
const { width, height } = rect;
|
|
21
|
+
cb({ width: Math.round(width), height: Math.round(height) });
|
|
22
|
+
};
|
|
23
|
+
handler(element.getBoundingClientRect());
|
|
24
|
+
if (typeof ResizeObserver === "undefined") {
|
|
25
|
+
return () => {
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const observer = new ResizeObserver((entries) => {
|
|
29
|
+
const entry = entries[0];
|
|
30
|
+
if (entry == null ? void 0 : entry.borderBoxSize) {
|
|
31
|
+
const box = entry.borderBoxSize[0];
|
|
32
|
+
if (box) {
|
|
33
|
+
handler({ width: box.inlineSize, height: box.blockSize });
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
handler(element.getBoundingClientRect());
|
|
38
|
+
});
|
|
39
|
+
observer.observe(element, { box: "border-box" });
|
|
40
|
+
return () => {
|
|
41
|
+
observer.unobserve(element);
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const observeWindowRect = (instance, cb) => {
|
|
45
|
+
const element = instance.scrollElement;
|
|
46
|
+
if (!element) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const handler = () => {
|
|
50
|
+
cb({ width: element.innerWidth, height: element.innerHeight });
|
|
51
|
+
};
|
|
52
|
+
handler();
|
|
53
|
+
element.addEventListener("resize", handler, {
|
|
54
|
+
passive: true
|
|
55
|
+
});
|
|
56
|
+
return () => {
|
|
57
|
+
element.removeEventListener("resize", handler);
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const observeElementOffset = (instance, cb) => {
|
|
61
|
+
const element = instance.scrollElement;
|
|
62
|
+
if (!element) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const handler = () => {
|
|
66
|
+
cb(element[instance.options.horizontal ? "scrollLeft" : "scrollTop"]);
|
|
67
|
+
};
|
|
68
|
+
handler();
|
|
69
|
+
element.addEventListener("scroll", handler, {
|
|
70
|
+
passive: true
|
|
71
|
+
});
|
|
72
|
+
return () => {
|
|
73
|
+
element.removeEventListener("scroll", handler);
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const observeWindowOffset = (instance, cb) => {
|
|
77
|
+
const element = instance.scrollElement;
|
|
78
|
+
if (!element) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const handler = () => {
|
|
82
|
+
cb(element[instance.options.horizontal ? "scrollX" : "scrollY"]);
|
|
83
|
+
};
|
|
84
|
+
handler();
|
|
85
|
+
element.addEventListener("scroll", handler, {
|
|
86
|
+
passive: true
|
|
87
|
+
});
|
|
88
|
+
return () => {
|
|
89
|
+
element.removeEventListener("scroll", handler);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const measureElement = (element, entry, instance) => {
|
|
93
|
+
if (entry == null ? void 0 : entry.borderBoxSize) {
|
|
94
|
+
const box = entry.borderBoxSize[0];
|
|
95
|
+
if (box) {
|
|
96
|
+
const size = Math.round(
|
|
97
|
+
box[instance.options.horizontal ? "inlineSize" : "blockSize"]
|
|
98
|
+
);
|
|
99
|
+
return size;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return Math.round(
|
|
103
|
+
element.getBoundingClientRect()[instance.options.horizontal ? "width" : "height"]
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
const windowScroll = (offset, {
|
|
107
|
+
adjustments = 0,
|
|
108
|
+
behavior
|
|
109
|
+
}, instance) => {
|
|
110
|
+
var _a, _b;
|
|
111
|
+
const toOffset = offset + adjustments;
|
|
112
|
+
(_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
|
|
113
|
+
[instance.options.horizontal ? "left" : "top"]: toOffset,
|
|
114
|
+
behavior
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
const elementScroll = (offset, {
|
|
118
|
+
adjustments = 0,
|
|
119
|
+
behavior
|
|
120
|
+
}, instance) => {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
const toOffset = offset + adjustments;
|
|
123
|
+
(_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
|
|
124
|
+
[instance.options.horizontal ? "left" : "top"]: toOffset,
|
|
125
|
+
behavior
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
class Virtualizer {
|
|
129
|
+
constructor(opts) {
|
|
130
|
+
this.unsubs = [];
|
|
131
|
+
this.scrollElement = null;
|
|
132
|
+
this.isScrolling = false;
|
|
133
|
+
this.isScrollingTimeoutId = null;
|
|
134
|
+
this.scrollToIndexTimeoutId = null;
|
|
135
|
+
this.measurementsCache = [];
|
|
136
|
+
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
137
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
138
|
+
this.scrollDirection = null;
|
|
139
|
+
this.scrollAdjustments = 0;
|
|
140
|
+
this.measureElementCache = /* @__PURE__ */ new Map();
|
|
141
|
+
this.observer = /* @__PURE__ */ (() => {
|
|
142
|
+
let _ro = null;
|
|
143
|
+
const get = () => {
|
|
144
|
+
if (_ro) {
|
|
145
|
+
return _ro;
|
|
146
|
+
} else if (typeof ResizeObserver !== "undefined") {
|
|
147
|
+
return _ro = new ResizeObserver((entries) => {
|
|
148
|
+
entries.forEach((entry) => {
|
|
149
|
+
this._measureElement(entry.target, entry);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
disconnect: () => {
|
|
158
|
+
var _a;
|
|
159
|
+
return (_a = get()) == null ? void 0 : _a.disconnect();
|
|
160
|
+
},
|
|
161
|
+
observe: (target) => {
|
|
162
|
+
var _a;
|
|
163
|
+
return (_a = get()) == null ? void 0 : _a.observe(target, { box: "border-box" });
|
|
164
|
+
},
|
|
165
|
+
unobserve: (target) => {
|
|
166
|
+
var _a;
|
|
167
|
+
return (_a = get()) == null ? void 0 : _a.unobserve(target);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
})();
|
|
171
|
+
this.range = null;
|
|
172
|
+
this.setOptions = (opts2) => {
|
|
173
|
+
Object.entries(opts2).forEach(([key, value]) => {
|
|
174
|
+
if (typeof value === "undefined")
|
|
175
|
+
delete opts2[key];
|
|
176
|
+
});
|
|
177
|
+
this.options = {
|
|
178
|
+
debug: false,
|
|
179
|
+
initialOffset: 0,
|
|
180
|
+
overscan: 1,
|
|
181
|
+
paddingStart: 0,
|
|
182
|
+
paddingEnd: 0,
|
|
183
|
+
scrollPaddingStart: 0,
|
|
184
|
+
scrollPaddingEnd: 0,
|
|
185
|
+
horizontal: false,
|
|
186
|
+
getItemKey: defaultKeyExtractor,
|
|
187
|
+
rangeExtractor: defaultRangeExtractor,
|
|
188
|
+
onChange: () => {
|
|
189
|
+
},
|
|
190
|
+
measureElement,
|
|
191
|
+
initialRect: { width: 0, height: 0 },
|
|
192
|
+
scrollMargin: 0,
|
|
193
|
+
scrollingDelay: 150,
|
|
194
|
+
indexAttribute: "data-index",
|
|
195
|
+
initialMeasurementsCache: [],
|
|
196
|
+
lanes: 1,
|
|
197
|
+
...opts2
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
this.notify = (sync) => {
|
|
201
|
+
var _a, _b;
|
|
202
|
+
(_b = (_a = this.options).onChange) == null ? void 0 : _b.call(_a, this, sync);
|
|
203
|
+
};
|
|
204
|
+
this.maybeNotify = utils.memo(
|
|
205
|
+
() => {
|
|
206
|
+
this.calculateRange();
|
|
207
|
+
return [
|
|
208
|
+
this.isScrolling,
|
|
209
|
+
this.range ? this.range.startIndex : null,
|
|
210
|
+
this.range ? this.range.endIndex : null
|
|
211
|
+
];
|
|
212
|
+
},
|
|
213
|
+
(isScrolling) => {
|
|
214
|
+
this.notify(isScrolling);
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
key: process.env.NODE_ENV !== "production" && "maybeNotify",
|
|
218
|
+
debug: () => this.options.debug,
|
|
219
|
+
initialDeps: [
|
|
220
|
+
this.isScrolling,
|
|
221
|
+
this.range ? this.range.startIndex : null,
|
|
222
|
+
this.range ? this.range.endIndex : null
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
this.cleanup = () => {
|
|
227
|
+
this.unsubs.filter(Boolean).forEach((d) => d());
|
|
228
|
+
this.unsubs = [];
|
|
229
|
+
this.scrollElement = null;
|
|
230
|
+
};
|
|
231
|
+
this._didMount = () => {
|
|
232
|
+
this.measureElementCache.forEach(this.observer.observe);
|
|
233
|
+
return () => {
|
|
234
|
+
this.observer.disconnect();
|
|
235
|
+
this.cleanup();
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
this._willUpdate = () => {
|
|
239
|
+
const scrollElement = this.options.getScrollElement();
|
|
240
|
+
if (this.scrollElement !== scrollElement) {
|
|
241
|
+
this.cleanup();
|
|
242
|
+
this.scrollElement = scrollElement;
|
|
243
|
+
this._scrollToOffset(this.scrollOffset, {
|
|
244
|
+
adjustments: void 0,
|
|
245
|
+
behavior: void 0
|
|
246
|
+
});
|
|
247
|
+
this.unsubs.push(
|
|
248
|
+
this.options.observeElementRect(this, (rect) => {
|
|
249
|
+
this.scrollRect = rect;
|
|
250
|
+
this.maybeNotify();
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
this.unsubs.push(
|
|
254
|
+
this.options.observeElementOffset(this, (offset) => {
|
|
255
|
+
this.scrollAdjustments = 0;
|
|
256
|
+
if (this.scrollOffset === offset) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (this.isScrollingTimeoutId !== null) {
|
|
260
|
+
clearTimeout(this.isScrollingTimeoutId);
|
|
261
|
+
this.isScrollingTimeoutId = null;
|
|
262
|
+
}
|
|
263
|
+
this.isScrolling = true;
|
|
264
|
+
this.scrollDirection = this.scrollOffset < offset ? "forward" : "backward";
|
|
265
|
+
this.scrollOffset = offset;
|
|
266
|
+
this.maybeNotify();
|
|
267
|
+
this.isScrollingTimeoutId = setTimeout(() => {
|
|
268
|
+
this.isScrollingTimeoutId = null;
|
|
269
|
+
this.isScrolling = false;
|
|
270
|
+
this.scrollDirection = null;
|
|
271
|
+
this.maybeNotify();
|
|
272
|
+
}, this.options.scrollingDelay);
|
|
273
|
+
})
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
this.getSize = () => {
|
|
278
|
+
return this.scrollRect[this.options.horizontal ? "width" : "height"];
|
|
279
|
+
};
|
|
280
|
+
this.memoOptions = utils.memo(
|
|
281
|
+
() => [
|
|
282
|
+
this.options.count,
|
|
283
|
+
this.options.paddingStart,
|
|
284
|
+
this.options.scrollMargin,
|
|
285
|
+
this.options.getItemKey
|
|
286
|
+
],
|
|
287
|
+
(count, paddingStart, scrollMargin, getItemKey) => {
|
|
288
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
289
|
+
return {
|
|
290
|
+
count,
|
|
291
|
+
paddingStart,
|
|
292
|
+
scrollMargin,
|
|
293
|
+
getItemKey
|
|
294
|
+
};
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
key: false
|
|
298
|
+
}
|
|
299
|
+
);
|
|
300
|
+
this.getFurthestMeasurement = (measurements, index) => {
|
|
301
|
+
const furthestMeasurementsFound = /* @__PURE__ */ new Map();
|
|
302
|
+
const furthestMeasurements = /* @__PURE__ */ new Map();
|
|
303
|
+
for (let m = index - 1; m >= 0; m--) {
|
|
304
|
+
const measurement = measurements[m];
|
|
305
|
+
if (furthestMeasurementsFound.has(measurement.lane)) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const previousFurthestMeasurement = furthestMeasurements.get(
|
|
309
|
+
measurement.lane
|
|
310
|
+
);
|
|
311
|
+
if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
|
|
312
|
+
furthestMeasurements.set(measurement.lane, measurement);
|
|
313
|
+
} else if (measurement.end < previousFurthestMeasurement.end) {
|
|
314
|
+
furthestMeasurementsFound.set(measurement.lane, true);
|
|
315
|
+
}
|
|
316
|
+
if (furthestMeasurementsFound.size === this.options.lanes) {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return furthestMeasurements.size === this.options.lanes ? Array.from(furthestMeasurements.values()).sort((a, b) => {
|
|
321
|
+
if (a.end === b.end) {
|
|
322
|
+
return a.index - b.index;
|
|
323
|
+
}
|
|
324
|
+
return a.end - b.end;
|
|
325
|
+
})[0] : void 0;
|
|
326
|
+
};
|
|
327
|
+
this.getMeasurements = utils.memo(
|
|
328
|
+
() => [this.memoOptions(), this.itemSizeCache],
|
|
329
|
+
({ count, paddingStart, scrollMargin, getItemKey }, itemSizeCache) => {
|
|
330
|
+
const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
|
|
331
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
332
|
+
const measurements = this.measurementsCache.slice(0, min);
|
|
333
|
+
for (let i = min; i < count; i++) {
|
|
334
|
+
const key = getItemKey(i);
|
|
335
|
+
const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
|
|
336
|
+
const start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
|
|
337
|
+
const measuredSize = itemSizeCache.get(key);
|
|
338
|
+
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
339
|
+
const end = start + size;
|
|
340
|
+
const lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
|
|
341
|
+
measurements[i] = {
|
|
342
|
+
index: i,
|
|
343
|
+
start,
|
|
344
|
+
size,
|
|
345
|
+
end,
|
|
346
|
+
key,
|
|
347
|
+
lane
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
this.measurementsCache = measurements;
|
|
351
|
+
return measurements;
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
key: process.env.NODE_ENV !== "production" && "getMeasurements",
|
|
355
|
+
debug: () => this.options.debug
|
|
356
|
+
}
|
|
357
|
+
);
|
|
358
|
+
this.calculateRange = utils.memo(
|
|
359
|
+
() => [this.getMeasurements(), this.getSize(), this.scrollOffset],
|
|
360
|
+
(measurements, outerSize, scrollOffset) => {
|
|
361
|
+
return this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
|
|
362
|
+
measurements,
|
|
363
|
+
outerSize,
|
|
364
|
+
scrollOffset
|
|
365
|
+
}) : null;
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
key: process.env.NODE_ENV !== "production" && "calculateRange",
|
|
369
|
+
debug: () => this.options.debug
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
this.getIndexes = utils.memo(
|
|
373
|
+
() => [
|
|
374
|
+
this.options.rangeExtractor,
|
|
375
|
+
this.calculateRange(),
|
|
376
|
+
this.options.overscan,
|
|
377
|
+
this.options.count
|
|
378
|
+
],
|
|
379
|
+
(rangeExtractor, range, overscan, count) => {
|
|
380
|
+
return range === null ? [] : rangeExtractor({
|
|
381
|
+
...range,
|
|
382
|
+
overscan,
|
|
383
|
+
count
|
|
384
|
+
});
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
key: process.env.NODE_ENV !== "production" && "getIndexes",
|
|
388
|
+
debug: () => this.options.debug
|
|
389
|
+
}
|
|
390
|
+
);
|
|
391
|
+
this.indexFromElement = (node) => {
|
|
392
|
+
const attributeName = this.options.indexAttribute;
|
|
393
|
+
const indexStr = node.getAttribute(attributeName);
|
|
394
|
+
if (!indexStr) {
|
|
395
|
+
console.warn(
|
|
396
|
+
`Missing attribute name '${attributeName}={index}' on measured element.`
|
|
397
|
+
);
|
|
398
|
+
return -1;
|
|
399
|
+
}
|
|
400
|
+
return parseInt(indexStr, 10);
|
|
401
|
+
};
|
|
402
|
+
this._measureElement = (node, entry) => {
|
|
403
|
+
const item = this.measurementsCache[this.indexFromElement(node)];
|
|
404
|
+
if (!item || !node.isConnected) {
|
|
405
|
+
this.measureElementCache.forEach((cached, key) => {
|
|
406
|
+
if (cached === node) {
|
|
407
|
+
this.observer.unobserve(node);
|
|
408
|
+
this.measureElementCache.delete(key);
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const prevNode = this.measureElementCache.get(item.key);
|
|
414
|
+
if (prevNode !== node) {
|
|
415
|
+
if (prevNode) {
|
|
416
|
+
this.observer.unobserve(prevNode);
|
|
417
|
+
}
|
|
418
|
+
this.observer.observe(node);
|
|
419
|
+
this.measureElementCache.set(item.key, node);
|
|
420
|
+
}
|
|
421
|
+
const measuredItemSize = this.options.measureElement(node, entry, this);
|
|
422
|
+
this.resizeItem(item, measuredItemSize);
|
|
423
|
+
};
|
|
424
|
+
this.resizeItem = (item, size) => {
|
|
425
|
+
const itemSize = this.itemSizeCache.get(item.key) ?? item.size;
|
|
426
|
+
const delta = size - itemSize;
|
|
427
|
+
if (delta !== 0) {
|
|
428
|
+
if (item.start < this.scrollOffset + this.scrollAdjustments) {
|
|
429
|
+
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
430
|
+
console.info("correction", delta);
|
|
431
|
+
}
|
|
432
|
+
this._scrollToOffset(this.scrollOffset, {
|
|
433
|
+
adjustments: this.scrollAdjustments += delta,
|
|
434
|
+
behavior: void 0
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
this.pendingMeasuredCacheIndexes.push(item.index);
|
|
438
|
+
this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
|
|
439
|
+
this.notify(false);
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
this.measureElement = (node) => {
|
|
443
|
+
if (!node) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
this._measureElement(node, void 0);
|
|
447
|
+
};
|
|
448
|
+
this.getVirtualItems = utils.memo(
|
|
449
|
+
() => [this.getIndexes(), this.getMeasurements()],
|
|
450
|
+
(indexes, measurements) => {
|
|
451
|
+
const virtualItems = [];
|
|
452
|
+
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
453
|
+
const i = indexes[k];
|
|
454
|
+
const measurement = measurements[i];
|
|
455
|
+
virtualItems.push(measurement);
|
|
456
|
+
}
|
|
457
|
+
return virtualItems;
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
key: process.env.NODE_ENV !== "production" && "getIndexes",
|
|
461
|
+
debug: () => this.options.debug
|
|
462
|
+
}
|
|
463
|
+
);
|
|
464
|
+
this.getVirtualItemForOffset = (offset) => {
|
|
465
|
+
const measurements = this.getMeasurements();
|
|
466
|
+
return utils.notUndefined(
|
|
467
|
+
measurements[findNearestBinarySearch(
|
|
468
|
+
0,
|
|
469
|
+
measurements.length - 1,
|
|
470
|
+
(index) => utils.notUndefined(measurements[index]).start,
|
|
471
|
+
offset
|
|
472
|
+
)]
|
|
473
|
+
);
|
|
474
|
+
};
|
|
475
|
+
this.getOffsetForAlignment = (toOffset, align) => {
|
|
476
|
+
const size = this.getSize();
|
|
477
|
+
if (align === "auto") {
|
|
478
|
+
if (toOffset <= this.scrollOffset) {
|
|
479
|
+
align = "start";
|
|
480
|
+
} else if (toOffset >= this.scrollOffset + size) {
|
|
481
|
+
align = "end";
|
|
482
|
+
} else {
|
|
483
|
+
align = "start";
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
if (align === "start") {
|
|
487
|
+
toOffset = toOffset;
|
|
488
|
+
} else if (align === "end") {
|
|
489
|
+
toOffset = toOffset - size;
|
|
490
|
+
} else if (align === "center") {
|
|
491
|
+
toOffset = toOffset - size / 2;
|
|
492
|
+
}
|
|
493
|
+
const scrollSizeProp = this.options.horizontal ? "scrollWidth" : "scrollHeight";
|
|
494
|
+
const scrollSize = this.scrollElement ? "document" in this.scrollElement ? this.scrollElement.document.documentElement[scrollSizeProp] : this.scrollElement[scrollSizeProp] : 0;
|
|
495
|
+
const maxOffset = scrollSize - this.getSize();
|
|
496
|
+
return Math.max(Math.min(maxOffset, toOffset), 0);
|
|
497
|
+
};
|
|
498
|
+
this.getOffsetForIndex = (index, align = "auto") => {
|
|
499
|
+
index = Math.max(0, Math.min(index, this.options.count - 1));
|
|
500
|
+
const measurement = utils.notUndefined(this.getMeasurements()[index]);
|
|
501
|
+
if (align === "auto") {
|
|
502
|
+
if (measurement.end >= this.scrollOffset + this.getSize() - this.options.scrollPaddingEnd) {
|
|
503
|
+
align = "end";
|
|
504
|
+
} else if (measurement.start <= this.scrollOffset + this.options.scrollPaddingStart) {
|
|
505
|
+
align = "start";
|
|
506
|
+
} else {
|
|
507
|
+
return [this.scrollOffset, align];
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const toOffset = align === "end" ? measurement.end + this.options.scrollPaddingEnd : measurement.start - this.options.scrollPaddingStart;
|
|
511
|
+
return [this.getOffsetForAlignment(toOffset, align), align];
|
|
512
|
+
};
|
|
513
|
+
this.isDynamicMode = () => this.measureElementCache.size > 0;
|
|
514
|
+
this.cancelScrollToIndex = () => {
|
|
515
|
+
if (this.scrollToIndexTimeoutId !== null) {
|
|
516
|
+
clearTimeout(this.scrollToIndexTimeoutId);
|
|
517
|
+
this.scrollToIndexTimeoutId = null;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
this.scrollToOffset = (toOffset, { align = "start", behavior } = {}) => {
|
|
521
|
+
this.cancelScrollToIndex();
|
|
522
|
+
if (behavior === "smooth" && this.isDynamicMode()) {
|
|
523
|
+
console.warn(
|
|
524
|
+
"The `smooth` scroll behavior is not fully supported with dynamic size."
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
this._scrollToOffset(this.getOffsetForAlignment(toOffset, align), {
|
|
528
|
+
adjustments: void 0,
|
|
529
|
+
behavior
|
|
530
|
+
});
|
|
531
|
+
};
|
|
532
|
+
this.scrollToIndex = (index, { align: initialAlign = "auto", behavior } = {}) => {
|
|
533
|
+
index = Math.max(0, Math.min(index, this.options.count - 1));
|
|
534
|
+
this.cancelScrollToIndex();
|
|
535
|
+
if (behavior === "smooth" && this.isDynamicMode()) {
|
|
536
|
+
console.warn(
|
|
537
|
+
"The `smooth` scroll behavior is not fully supported with dynamic size."
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
const [toOffset, align] = this.getOffsetForIndex(index, initialAlign);
|
|
541
|
+
this._scrollToOffset(toOffset, { adjustments: void 0, behavior });
|
|
542
|
+
if (behavior !== "smooth" && this.isDynamicMode()) {
|
|
543
|
+
this.scrollToIndexTimeoutId = setTimeout(() => {
|
|
544
|
+
this.scrollToIndexTimeoutId = null;
|
|
545
|
+
const elementInDOM = this.measureElementCache.has(
|
|
546
|
+
this.options.getItemKey(index)
|
|
547
|
+
);
|
|
548
|
+
if (elementInDOM) {
|
|
549
|
+
const [toOffset2] = this.getOffsetForIndex(index, align);
|
|
550
|
+
if (!utils.approxEqual(toOffset2, this.scrollOffset)) {
|
|
551
|
+
this.scrollToIndex(index, { align, behavior });
|
|
552
|
+
}
|
|
553
|
+
} else {
|
|
554
|
+
this.scrollToIndex(index, { align, behavior });
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
this.scrollBy = (delta, { behavior } = {}) => {
|
|
560
|
+
this.cancelScrollToIndex();
|
|
561
|
+
if (behavior === "smooth" && this.isDynamicMode()) {
|
|
562
|
+
console.warn(
|
|
563
|
+
"The `smooth` scroll behavior is not fully supported with dynamic size."
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
this._scrollToOffset(this.scrollOffset + delta, {
|
|
567
|
+
adjustments: void 0,
|
|
568
|
+
behavior
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
this.getTotalSize = () => {
|
|
572
|
+
var _a;
|
|
573
|
+
const measurements = this.getMeasurements();
|
|
574
|
+
let end;
|
|
575
|
+
if (measurements.length === 0) {
|
|
576
|
+
end = this.options.paddingStart;
|
|
577
|
+
} else {
|
|
578
|
+
end = this.options.lanes === 1 ? ((_a = measurements[measurements.length - 1]) == null ? void 0 : _a.end) ?? 0 : Math.max(
|
|
579
|
+
...measurements.slice(-this.options.lanes).map((m) => m.end)
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
return end - this.options.scrollMargin + this.options.paddingEnd;
|
|
583
|
+
};
|
|
584
|
+
this._scrollToOffset = (offset, {
|
|
585
|
+
adjustments,
|
|
586
|
+
behavior
|
|
587
|
+
}) => {
|
|
588
|
+
this.options.scrollToFn(offset, { behavior, adjustments }, this);
|
|
589
|
+
};
|
|
590
|
+
this.measure = () => {
|
|
591
|
+
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
592
|
+
this.notify(false);
|
|
593
|
+
};
|
|
594
|
+
this.setOptions(opts);
|
|
595
|
+
this.scrollRect = this.options.initialRect;
|
|
596
|
+
this.scrollOffset = this.options.initialOffset;
|
|
597
|
+
this.measurementsCache = this.options.initialMeasurementsCache;
|
|
598
|
+
this.measurementsCache.forEach((item) => {
|
|
599
|
+
this.itemSizeCache.set(item.key, item.size);
|
|
600
|
+
});
|
|
601
|
+
this.maybeNotify();
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
605
|
+
while (low <= high) {
|
|
606
|
+
const middle = (low + high) / 2 | 0;
|
|
607
|
+
const currentValue = getCurrentValue(middle);
|
|
608
|
+
if (currentValue < value) {
|
|
609
|
+
low = middle + 1;
|
|
610
|
+
} else if (currentValue > value) {
|
|
611
|
+
high = middle - 1;
|
|
612
|
+
} else {
|
|
613
|
+
return middle;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (low > 0) {
|
|
617
|
+
return low - 1;
|
|
618
|
+
} else {
|
|
619
|
+
return 0;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
function calculateRange({
|
|
623
|
+
measurements,
|
|
624
|
+
outerSize,
|
|
625
|
+
scrollOffset
|
|
626
|
+
}) {
|
|
627
|
+
const count = measurements.length - 1;
|
|
628
|
+
const getOffset = (index) => measurements[index].start;
|
|
629
|
+
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
630
|
+
let endIndex = startIndex;
|
|
631
|
+
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
632
|
+
endIndex++;
|
|
633
|
+
}
|
|
634
|
+
return { startIndex, endIndex };
|
|
635
|
+
}
|
|
636
|
+
exports.approxEqual = utils.approxEqual;
|
|
637
|
+
exports.memo = utils.memo;
|
|
638
|
+
exports.notUndefined = utils.notUndefined;
|
|
639
|
+
exports.Virtualizer = Virtualizer;
|
|
640
|
+
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
641
|
+
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
642
|
+
exports.elementScroll = elementScroll;
|
|
643
|
+
exports.measureElement = measureElement;
|
|
644
|
+
exports.observeElementOffset = observeElementOffset;
|
|
645
|
+
exports.observeElementRect = observeElementRect;
|
|
646
|
+
exports.observeWindowOffset = observeWindowOffset;
|
|
647
|
+
exports.observeWindowRect = observeWindowRect;
|
|
648
|
+
exports.windowScroll = windowScroll;
|
|
649
|
+
//# sourceMappingURL=index.cjs.map
|