@oxog/scrollex 1.0.0 → 1.0.3
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/CHANGELOG.md +66 -5
- package/README.md +30 -22
- package/dist/index.cjs +796 -363
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -15
- package/dist/index.d.ts +39 -15
- package/dist/index.js +797 -364
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +381 -180
- package/dist/plugins/index.cjs.map +1 -1
- package/dist/plugins/index.d.cts +74 -49
- package/dist/plugins/index.d.ts +74 -49
- package/dist/plugins/index.js +381 -180
- package/dist/plugins/index.js.map +1 -1
- package/package.json +13 -10
package/dist/index.cjs
CHANGED
|
@@ -99,9 +99,11 @@ var EventBus = class {
|
|
|
99
99
|
*/
|
|
100
100
|
dispatchEvent(event) {
|
|
101
101
|
const eventType = event.type;
|
|
102
|
+
const onceSnapshot = Array.from(this.onceHandlers.get(eventType) ?? []);
|
|
102
103
|
const handlers = this.handlers.get(eventType);
|
|
103
104
|
if (handlers) {
|
|
104
|
-
for (const handler of handlers) {
|
|
105
|
+
for (const handler of Array.from(handlers)) {
|
|
106
|
+
if (!handlers.has(handler)) continue;
|
|
105
107
|
try {
|
|
106
108
|
handler(event);
|
|
107
109
|
} catch (error) {
|
|
@@ -109,20 +111,17 @@ var EventBus = class {
|
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
onceHandlers.clear();
|
|
116
|
-
for (const handler of handlersToCall) {
|
|
117
|
-
try {
|
|
118
|
-
handler(event);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
console.error(`[Scrollex] Error in once handler for "${eventType}":`, error);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
114
|
+
for (const handler of onceSnapshot) {
|
|
115
|
+
const onceHandlers = this.onceHandlers.get(eventType);
|
|
116
|
+
if (!onceHandlers || !onceHandlers.delete(handler)) continue;
|
|
123
117
|
if (onceHandlers.size === 0) {
|
|
124
118
|
this.onceHandlers.delete(eventType);
|
|
125
119
|
}
|
|
120
|
+
try {
|
|
121
|
+
handler(event);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error(`[Scrollex] Error in once handler for "${eventType}":`, error);
|
|
124
|
+
}
|
|
126
125
|
}
|
|
127
126
|
}
|
|
128
127
|
/**
|
|
@@ -210,7 +209,7 @@ function setScrollPosition(element, position) {
|
|
|
210
209
|
function getScrollDirection(current, previous) {
|
|
211
210
|
const deltaY = current.scrollTop - previous.scrollTop;
|
|
212
211
|
const deltaX = current.scrollLeft - previous.scrollLeft;
|
|
213
|
-
if (Math.abs(deltaY)
|
|
212
|
+
if (Math.abs(deltaY) >= Math.abs(deltaX)) {
|
|
214
213
|
if (deltaY > 0) return "down";
|
|
215
214
|
if (deltaY < 0) return "up";
|
|
216
215
|
} else {
|
|
@@ -225,7 +224,7 @@ function raf(callback) {
|
|
|
225
224
|
if (typeof requestAnimationFrame !== "undefined") {
|
|
226
225
|
return requestAnimationFrame(callback);
|
|
227
226
|
}
|
|
228
|
-
return setTimeout(() => callback(
|
|
227
|
+
return setTimeout(() => callback(performance.now()), 16);
|
|
229
228
|
}
|
|
230
229
|
function cancelRaf(id) {
|
|
231
230
|
if (typeof cancelAnimationFrame !== "undefined") {
|
|
@@ -250,11 +249,15 @@ var ScrollEngine = class {
|
|
|
250
249
|
this.container = null;
|
|
251
250
|
this.scrollPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
252
251
|
this.previousScrollPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
252
|
+
// The position the last scroll event reported (scrollPosition is also updated by programmatic scrolls, ahead of the
|
|
253
|
+
// event the browser fires for them, and a delta against it would always be 0)
|
|
254
|
+
this.reportedPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
253
255
|
this.scrolling = false;
|
|
254
256
|
this.scrollDirection = "none";
|
|
255
257
|
this.lastScrollTime = 0;
|
|
256
258
|
this.scrollEndTimer = null;
|
|
257
259
|
this.scrollEndDelay = 150;
|
|
260
|
+
this.horizontal = false;
|
|
258
261
|
// Animation state
|
|
259
262
|
this.animationId = null;
|
|
260
263
|
this.animationStartTime = 0;
|
|
@@ -280,6 +283,7 @@ var ScrollEngine = class {
|
|
|
280
283
|
this.container = container;
|
|
281
284
|
this.scrollPosition = getScrollPosition(container);
|
|
282
285
|
this.previousScrollPosition = { ...this.scrollPosition };
|
|
286
|
+
this.reportedPosition = { ...this.scrollPosition };
|
|
283
287
|
this.throttledHandleScroll = throttleRAF(this.processScroll.bind(this));
|
|
284
288
|
this.boundHandleScroll = () => this.throttledHandleScroll?.();
|
|
285
289
|
container.addEventListener("scroll", this.boundHandleScroll, { passive: true });
|
|
@@ -288,6 +292,15 @@ var ScrollEngine = class {
|
|
|
288
292
|
* Detach from the current container.
|
|
289
293
|
*/
|
|
290
294
|
detach() {
|
|
295
|
+
if (this.scrolling) {
|
|
296
|
+
this.scrolling = false;
|
|
297
|
+
this.onScrollEndCallback?.({
|
|
298
|
+
type: "scroll-end",
|
|
299
|
+
scrollTop: this.scrollPosition.scrollTop,
|
|
300
|
+
scrollLeft: this.scrollPosition.scrollLeft,
|
|
301
|
+
timestamp: performance.now()
|
|
302
|
+
});
|
|
303
|
+
}
|
|
291
304
|
if (this.container && this.boundHandleScroll) {
|
|
292
305
|
this.container.removeEventListener("scroll", this.boundHandleScroll);
|
|
293
306
|
}
|
|
@@ -312,8 +325,9 @@ var ScrollEngine = class {
|
|
|
312
325
|
processScroll() {
|
|
313
326
|
if (!this.container) return;
|
|
314
327
|
const now = performance.now();
|
|
315
|
-
this.previousScrollPosition = { ...this.
|
|
328
|
+
this.previousScrollPosition = { ...this.reportedPosition };
|
|
316
329
|
this.scrollPosition = getScrollPosition(this.container);
|
|
330
|
+
this.reportedPosition = { ...this.scrollPosition };
|
|
317
331
|
this.scrollDirection = getScrollDirection(this.scrollPosition, this.previousScrollPosition);
|
|
318
332
|
if (!this.scrolling) {
|
|
319
333
|
this.scrolling = true;
|
|
@@ -374,11 +388,12 @@ var ScrollEngine = class {
|
|
|
374
388
|
this.animationId = null;
|
|
375
389
|
}
|
|
376
390
|
setScrollPosition(this.container, position);
|
|
391
|
+
const actual = getScrollPosition(this.container);
|
|
377
392
|
if (position.scrollTop !== void 0) {
|
|
378
|
-
this.scrollPosition.scrollTop =
|
|
393
|
+
this.scrollPosition.scrollTop = actual.scrollTop;
|
|
379
394
|
}
|
|
380
395
|
if (position.scrollLeft !== void 0) {
|
|
381
|
-
this.scrollPosition.scrollLeft =
|
|
396
|
+
this.scrollPosition.scrollLeft = actual.scrollLeft;
|
|
382
397
|
}
|
|
383
398
|
}
|
|
384
399
|
/**
|
|
@@ -390,8 +405,8 @@ var ScrollEngine = class {
|
|
|
390
405
|
scrollTo(offset, options = {}) {
|
|
391
406
|
if (!this.container) return;
|
|
392
407
|
const { behavior = "auto", duration = 300, easing = easings.easeInOut } = options;
|
|
393
|
-
if (behavior === "auto" ||
|
|
394
|
-
this.setScrollPosition({ scrollTop: offset });
|
|
408
|
+
if (behavior === "auto" || !(duration > 0)) {
|
|
409
|
+
this.setScrollPosition(this.horizontal ? { scrollLeft: offset } : { scrollTop: offset });
|
|
395
410
|
} else {
|
|
396
411
|
this.animateScroll(offset, duration, easing);
|
|
397
412
|
}
|
|
@@ -404,16 +419,16 @@ var ScrollEngine = class {
|
|
|
404
419
|
cancelRaf(this.animationId);
|
|
405
420
|
}
|
|
406
421
|
this.animationStartTime = performance.now();
|
|
407
|
-
this.animationStartOffset = this.scrollPosition.scrollTop;
|
|
422
|
+
this.animationStartOffset = this.horizontal ? this.scrollPosition.scrollLeft : this.scrollPosition.scrollTop;
|
|
408
423
|
this.animationTargetOffset = targetOffset;
|
|
409
424
|
this.animationDuration = duration;
|
|
410
425
|
this.animationEasing = easing;
|
|
411
426
|
const animate = (currentTime) => {
|
|
412
427
|
const elapsed = currentTime - this.animationStartTime;
|
|
413
|
-
const progress = Math.min(elapsed / this.animationDuration, 1);
|
|
428
|
+
const progress = Math.max(0, Math.min(elapsed / this.animationDuration, 1));
|
|
414
429
|
const easedProgress = this.animationEasing(progress);
|
|
415
430
|
const currentOffset = this.animationStartOffset + (this.animationTargetOffset - this.animationStartOffset) * easedProgress;
|
|
416
|
-
this.setScrollPosition({ scrollTop: currentOffset });
|
|
431
|
+
this.setScrollPosition(this.horizontal ? { scrollLeft: currentOffset } : { scrollTop: currentOffset });
|
|
417
432
|
if (progress < 1) {
|
|
418
433
|
this.animationId = raf(animate);
|
|
419
434
|
} else {
|
|
@@ -451,6 +466,14 @@ var ScrollEngine = class {
|
|
|
451
466
|
*
|
|
452
467
|
* @param delay - Delay in ms
|
|
453
468
|
*/
|
|
469
|
+
/**
|
|
470
|
+
* Set the axis that scrollTo() and its animation drive.
|
|
471
|
+
*
|
|
472
|
+
* @param direction - Scroll direction
|
|
473
|
+
*/
|
|
474
|
+
setDirection(direction) {
|
|
475
|
+
this.horizontal = direction === "horizontal";
|
|
476
|
+
}
|
|
454
477
|
setScrollEndDelay(delay) {
|
|
455
478
|
this.scrollEndDelay = delay;
|
|
456
479
|
}
|
|
@@ -514,6 +537,11 @@ function getViewport(element) {
|
|
|
514
537
|
scrollHeight: element.scrollHeight
|
|
515
538
|
};
|
|
516
539
|
}
|
|
540
|
+
function getContentWidth(element) {
|
|
541
|
+
const style = typeof getComputedStyle === "function" ? getComputedStyle(element) : null;
|
|
542
|
+
const padding = style ? (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0) : 0;
|
|
543
|
+
return Math.max(0, element.clientWidth - padding);
|
|
544
|
+
}
|
|
517
545
|
|
|
518
546
|
// src/kernel/viewport.ts
|
|
519
547
|
var ViewportManager = class {
|
|
@@ -689,6 +717,16 @@ function binarySearch(length, predicate) {
|
|
|
689
717
|
function clamp(value, min, max) {
|
|
690
718
|
return Math.min(Math.max(value, min), max);
|
|
691
719
|
}
|
|
720
|
+
function normalizeColumnCount(columns) {
|
|
721
|
+
return Number.isFinite(columns) ? Math.max(1, Math.floor(columns)) : 1;
|
|
722
|
+
}
|
|
723
|
+
function autoColumnCount(containerWidth, minColumnWidth, gap) {
|
|
724
|
+
const slot = minColumnWidth + gap;
|
|
725
|
+
return slot > 0 ? normalizeColumnCount(Math.floor((containerWidth + gap) / slot)) : 1;
|
|
726
|
+
}
|
|
727
|
+
function normalizeOverscan(overscan) {
|
|
728
|
+
return Number.isNaN(overscan) ? 0 : Math.max(0, Math.floor(overscan));
|
|
729
|
+
}
|
|
692
730
|
|
|
693
731
|
// src/kernel/measurement-cache.ts
|
|
694
732
|
var MeasurementCache = class {
|
|
@@ -839,19 +877,17 @@ var MeasurementCache = class {
|
|
|
839
877
|
if (offset === 0) return;
|
|
840
878
|
const newCache = /* @__PURE__ */ new Map();
|
|
841
879
|
for (const [index, height] of this.cache) {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
newCache.set(newIndex, height);
|
|
846
|
-
} else {
|
|
847
|
-
this.totalMeasured--;
|
|
848
|
-
this.sumOfHeights -= height;
|
|
849
|
-
}
|
|
850
|
-
} else {
|
|
851
|
-
newCache.set(index, height);
|
|
880
|
+
const newIndex = index >= startIndex ? index + offset : index;
|
|
881
|
+
if (newIndex >= 0) {
|
|
882
|
+
newCache.set(newIndex, height);
|
|
852
883
|
}
|
|
853
884
|
}
|
|
854
885
|
this.cache = newCache;
|
|
886
|
+
this.totalMeasured = newCache.size;
|
|
887
|
+
this.sumOfHeights = 0;
|
|
888
|
+
for (const height of newCache.values()) {
|
|
889
|
+
this.sumOfHeights += height;
|
|
890
|
+
}
|
|
855
891
|
}
|
|
856
892
|
/**
|
|
857
893
|
* Get the internal cache map.
|
|
@@ -884,13 +920,13 @@ var RangeCalculator = class {
|
|
|
884
920
|
* @param options - Configuration options
|
|
885
921
|
*/
|
|
886
922
|
constructor(options) {
|
|
887
|
-
|
|
888
|
-
this.
|
|
889
|
-
this.
|
|
923
|
+
// Fenwick trees (1-based) over the measured items with index < itemCount
|
|
924
|
+
this.countTree = new Float64Array(1);
|
|
925
|
+
this.sumTree = new Float64Array(1);
|
|
926
|
+
this.treesValid = false;
|
|
890
927
|
this.itemCount = options.itemCount;
|
|
891
928
|
this.overscan = options.overscan;
|
|
892
929
|
this.measurementCache = new MeasurementCache(options.estimatedItemHeight);
|
|
893
|
-
this.rebuildOffsetCache();
|
|
894
930
|
}
|
|
895
931
|
/**
|
|
896
932
|
* Get the visible range of items.
|
|
@@ -908,7 +944,6 @@ var RangeCalculator = class {
|
|
|
908
944
|
overscanEndIndex: 0
|
|
909
945
|
};
|
|
910
946
|
}
|
|
911
|
-
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
912
947
|
const startIndex = this.findStartIndex(scrollOffset);
|
|
913
948
|
const scrollEnd = scrollOffset + viewportSize;
|
|
914
949
|
const endIndex = this.findEndIndex(scrollEnd, startIndex);
|
|
@@ -936,13 +971,9 @@ var RangeCalculator = class {
|
|
|
936
971
|
* Find the end index for a given scroll end position.
|
|
937
972
|
*/
|
|
938
973
|
findEndIndex(scrollEnd, startIndex) {
|
|
939
|
-
if (scrollEnd >= this.
|
|
974
|
+
if (scrollEnd >= this.getTotalSize()) return this.itemCount - 1;
|
|
940
975
|
let index = startIndex;
|
|
941
|
-
while (index < this.itemCount - 1) {
|
|
942
|
-
const itemStart = this.getItemOffset(index);
|
|
943
|
-
if (itemStart >= scrollEnd) {
|
|
944
|
-
break;
|
|
945
|
-
}
|
|
976
|
+
while (index < this.itemCount - 1 && this.getItemOffset(index + 1) < scrollEnd) {
|
|
946
977
|
index++;
|
|
947
978
|
}
|
|
948
979
|
return clamp(index, startIndex, this.itemCount - 1);
|
|
@@ -955,9 +986,8 @@ var RangeCalculator = class {
|
|
|
955
986
|
*/
|
|
956
987
|
getItemOffset(index) {
|
|
957
988
|
if (index < 0 || this.itemCount === 0) return 0;
|
|
958
|
-
if (index >= this.itemCount) return this.
|
|
959
|
-
this.
|
|
960
|
-
return this.offsetCache[index] ?? 0;
|
|
989
|
+
if (index >= this.itemCount) return this.getTotalSize();
|
|
990
|
+
return this.offsetBefore(Math.floor(index));
|
|
961
991
|
}
|
|
962
992
|
/**
|
|
963
993
|
* Get the end position of an item.
|
|
@@ -983,8 +1013,7 @@ var RangeCalculator = class {
|
|
|
983
1013
|
* @returns Total size
|
|
984
1014
|
*/
|
|
985
1015
|
getTotalSize() {
|
|
986
|
-
this.
|
|
987
|
-
return this.totalSize;
|
|
1016
|
+
return this.offsetBefore(this.itemCount);
|
|
988
1017
|
}
|
|
989
1018
|
/**
|
|
990
1019
|
* Set the height for an item.
|
|
@@ -995,7 +1024,7 @@ var RangeCalculator = class {
|
|
|
995
1024
|
*/
|
|
996
1025
|
setItemHeight(index, height) {
|
|
997
1026
|
const previousHeight = this.measurementCache.set(index, height);
|
|
998
|
-
this.
|
|
1027
|
+
this.track(index, previousHeight === void 0 ? 1 : 0, height - (previousHeight ?? 0));
|
|
999
1028
|
return previousHeight;
|
|
1000
1029
|
}
|
|
1001
1030
|
/**
|
|
@@ -1013,8 +1042,9 @@ var RangeCalculator = class {
|
|
|
1013
1042
|
* @param index - Item index
|
|
1014
1043
|
*/
|
|
1015
1044
|
invalidate(index) {
|
|
1045
|
+
const previousHeight = this.measurementCache.get(index);
|
|
1016
1046
|
if (this.measurementCache.delete(index)) {
|
|
1017
|
-
this.
|
|
1047
|
+
this.track(index, -1, -(previousHeight ?? 0));
|
|
1018
1048
|
}
|
|
1019
1049
|
}
|
|
1020
1050
|
/**
|
|
@@ -1022,41 +1052,7 @@ var RangeCalculator = class {
|
|
|
1022
1052
|
*/
|
|
1023
1053
|
invalidateAll() {
|
|
1024
1054
|
this.measurementCache.clear();
|
|
1025
|
-
this.
|
|
1026
|
-
this.rebuildOffsetCache();
|
|
1027
|
-
}
|
|
1028
|
-
/**
|
|
1029
|
-
* Mark offset cache as dirty from a specific index.
|
|
1030
|
-
*/
|
|
1031
|
-
invalidateFrom(index) {
|
|
1032
|
-
this.dirtyFromIndex = Math.min(this.dirtyFromIndex, index);
|
|
1033
|
-
}
|
|
1034
|
-
/**
|
|
1035
|
-
* Ensure offset cache is valid up to the given index.
|
|
1036
|
-
*/
|
|
1037
|
-
ensureOffsetCacheValid(upToIndex) {
|
|
1038
|
-
if (this.dirtyFromIndex > upToIndex) return;
|
|
1039
|
-
let offset = this.dirtyFromIndex === 0 ? 0 : (this.offsetCache[this.dirtyFromIndex - 1] ?? 0) + this.getItemSize(this.dirtyFromIndex - 1);
|
|
1040
|
-
for (let i = this.dirtyFromIndex; i <= upToIndex && i < this.itemCount; i++) {
|
|
1041
|
-
this.offsetCache[i] = offset;
|
|
1042
|
-
offset += this.getItemSize(i);
|
|
1043
|
-
}
|
|
1044
|
-
if (upToIndex >= this.itemCount - 1) {
|
|
1045
|
-
this.totalSize = offset;
|
|
1046
|
-
}
|
|
1047
|
-
this.dirtyFromIndex = upToIndex + 1;
|
|
1048
|
-
}
|
|
1049
|
-
/**
|
|
1050
|
-
* Rebuild the entire offset cache.
|
|
1051
|
-
*/
|
|
1052
|
-
rebuildOffsetCache() {
|
|
1053
|
-
this.offsetCache = new Array(this.itemCount);
|
|
1054
|
-
this.dirtyFromIndex = 0;
|
|
1055
|
-
if (this.itemCount > 0) {
|
|
1056
|
-
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
1057
|
-
} else {
|
|
1058
|
-
this.totalSize = 0;
|
|
1059
|
-
}
|
|
1055
|
+
this.treesValid = false;
|
|
1060
1056
|
}
|
|
1061
1057
|
/**
|
|
1062
1058
|
* Update item count.
|
|
@@ -1068,20 +1064,11 @@ var RangeCalculator = class {
|
|
|
1068
1064
|
const previousCount = this.itemCount;
|
|
1069
1065
|
this.itemCount = count;
|
|
1070
1066
|
if (count < previousCount) {
|
|
1071
|
-
this.
|
|
1072
|
-
|
|
1073
|
-
const lastIndex = count - 1;
|
|
1074
|
-
this.dirtyFromIndex = Math.min(this.dirtyFromIndex, lastIndex);
|
|
1075
|
-
this.ensureOffsetCacheValid(lastIndex);
|
|
1076
|
-
} else {
|
|
1077
|
-
this.totalSize = 0;
|
|
1078
|
-
}
|
|
1079
|
-
} else {
|
|
1080
|
-
this.offsetCache.length = count;
|
|
1081
|
-
if (this.dirtyFromIndex > previousCount) {
|
|
1082
|
-
this.dirtyFromIndex = previousCount;
|
|
1067
|
+
for (const measured of this.measurementCache.getMeasuredIndices()) {
|
|
1068
|
+
if (measured >= count) this.measurementCache.delete(measured);
|
|
1083
1069
|
}
|
|
1084
1070
|
}
|
|
1071
|
+
this.treesValid = false;
|
|
1085
1072
|
}
|
|
1086
1073
|
/**
|
|
1087
1074
|
* Update overscan amount.
|
|
@@ -1098,7 +1085,6 @@ var RangeCalculator = class {
|
|
|
1098
1085
|
*/
|
|
1099
1086
|
setEstimatedHeight(height) {
|
|
1100
1087
|
this.measurementCache.setEstimatedHeight(height);
|
|
1101
|
-
this.dirtyFromIndex = 0;
|
|
1102
1088
|
}
|
|
1103
1089
|
/**
|
|
1104
1090
|
* Get the estimated item height.
|
|
@@ -1116,8 +1102,7 @@ var RangeCalculator = class {
|
|
|
1116
1102
|
*/
|
|
1117
1103
|
getItemAtOffset(offset) {
|
|
1118
1104
|
if (offset <= 0 || this.itemCount === 0) return 0;
|
|
1119
|
-
if (offset >= this.
|
|
1120
|
-
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
1105
|
+
if (offset >= this.getTotalSize()) return this.itemCount - 1;
|
|
1121
1106
|
return binarySearch(this.itemCount, (mid) => {
|
|
1122
1107
|
const itemEnd = this.getItemEnd(mid);
|
|
1123
1108
|
return itemEnd > offset;
|
|
@@ -1139,6 +1124,51 @@ var RangeCalculator = class {
|
|
|
1139
1124
|
getItemCount() {
|
|
1140
1125
|
return this.itemCount;
|
|
1141
1126
|
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Position where item `index` starts (index === itemCount gives the total size).
|
|
1129
|
+
*/
|
|
1130
|
+
offsetBefore(index) {
|
|
1131
|
+
this.ensureTrees();
|
|
1132
|
+
let measuredCount = 0;
|
|
1133
|
+
let measuredSum = 0;
|
|
1134
|
+
for (let i = index; i > 0; i -= i & -i) {
|
|
1135
|
+
measuredCount += this.countTree[i];
|
|
1136
|
+
measuredSum += this.sumTree[i];
|
|
1137
|
+
}
|
|
1138
|
+
return measuredSum + (index - measuredCount) * this.measurementCache.getEstimatedHeight();
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Record that the measured set changed at `index` (a no-op until the trees are built).
|
|
1142
|
+
*/
|
|
1143
|
+
track(index, countDelta, sumDelta) {
|
|
1144
|
+
if (!this.treesValid || index < 0 || index >= this.itemCount || !Number.isInteger(index)) return;
|
|
1145
|
+
for (let i = index + 1; i <= this.itemCount; i += i & -i) {
|
|
1146
|
+
this.countTree[i] += countDelta;
|
|
1147
|
+
this.sumTree[i] += sumDelta;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Build the Fenwick trees from the measurement cache (linear time).
|
|
1152
|
+
*/
|
|
1153
|
+
ensureTrees() {
|
|
1154
|
+
if (this.treesValid) return;
|
|
1155
|
+
const size = this.itemCount;
|
|
1156
|
+
this.countTree = new Float64Array(size + 1);
|
|
1157
|
+
this.sumTree = new Float64Array(size + 1);
|
|
1158
|
+
for (const index of this.measurementCache.getMeasuredIndices()) {
|
|
1159
|
+
if (index < 0 || index >= size || !Number.isInteger(index)) continue;
|
|
1160
|
+
this.countTree[index + 1] = 1;
|
|
1161
|
+
this.sumTree[index + 1] = this.measurementCache.get(index) ?? 0;
|
|
1162
|
+
}
|
|
1163
|
+
for (let i = 1; i <= size; i++) {
|
|
1164
|
+
const parent = i + (i & -i);
|
|
1165
|
+
if (parent <= size) {
|
|
1166
|
+
this.countTree[parent] += this.countTree[i];
|
|
1167
|
+
this.sumTree[parent] += this.sumTree[i];
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
this.treesValid = true;
|
|
1171
|
+
}
|
|
1142
1172
|
};
|
|
1143
1173
|
|
|
1144
1174
|
// src/kernel/plugin-registry.ts
|
|
@@ -1171,7 +1201,12 @@ var PluginRegistry = class {
|
|
|
1171
1201
|
}
|
|
1172
1202
|
this.plugins.set(plugin.name, plugin);
|
|
1173
1203
|
if (this.kernel && plugin.install) {
|
|
1174
|
-
|
|
1204
|
+
try {
|
|
1205
|
+
plugin.install(this.kernel);
|
|
1206
|
+
} catch (error) {
|
|
1207
|
+
this.plugins.delete(plugin.name);
|
|
1208
|
+
throw error;
|
|
1209
|
+
}
|
|
1175
1210
|
this.setupHooks(plugin);
|
|
1176
1211
|
}
|
|
1177
1212
|
}
|
|
@@ -1187,8 +1222,11 @@ var PluginRegistry = class {
|
|
|
1187
1222
|
return false;
|
|
1188
1223
|
}
|
|
1189
1224
|
this.teardownHooks(plugin);
|
|
1190
|
-
|
|
1191
|
-
|
|
1225
|
+
try {
|
|
1226
|
+
plugin.uninstall?.();
|
|
1227
|
+
} finally {
|
|
1228
|
+
this.plugins.delete(name);
|
|
1229
|
+
}
|
|
1192
1230
|
return true;
|
|
1193
1231
|
}
|
|
1194
1232
|
/**
|
|
@@ -1375,7 +1413,11 @@ var PluginRegistry = class {
|
|
|
1375
1413
|
clear() {
|
|
1376
1414
|
const names = Array.from(this.plugins.keys()).reverse();
|
|
1377
1415
|
for (const name of names) {
|
|
1378
|
-
|
|
1416
|
+
try {
|
|
1417
|
+
this.unregister(name);
|
|
1418
|
+
} catch (error) {
|
|
1419
|
+
console.error(`[Scrollex] Error uninstalling plugin "${name}":`, error);
|
|
1420
|
+
}
|
|
1379
1421
|
}
|
|
1380
1422
|
}
|
|
1381
1423
|
/**
|
|
@@ -1398,8 +1440,10 @@ var DEFAULT_OPTIONS = {
|
|
|
1398
1440
|
};
|
|
1399
1441
|
function createKernel(options = {}) {
|
|
1400
1442
|
const config = { ...DEFAULT_OPTIONS, ...options };
|
|
1443
|
+
config.overscan = normalizeOverscan(config.overscan);
|
|
1401
1444
|
const eventBus = new EventBus();
|
|
1402
1445
|
const scrollEngine = new ScrollEngine();
|
|
1446
|
+
scrollEngine.setDirection(config.direction);
|
|
1403
1447
|
const viewportManager = new ViewportManager();
|
|
1404
1448
|
const rangeCalculator = new RangeCalculator({
|
|
1405
1449
|
itemCount: config.itemCount,
|
|
@@ -1407,6 +1451,8 @@ function createKernel(options = {}) {
|
|
|
1407
1451
|
overscan: config.overscan
|
|
1408
1452
|
});
|
|
1409
1453
|
const pluginRegistry = new PluginRegistry();
|
|
1454
|
+
const startPadding = () => config.paddingStart ?? 0;
|
|
1455
|
+
let rangeAnnounced = false;
|
|
1410
1456
|
let currentRange = {
|
|
1411
1457
|
startIndex: 0,
|
|
1412
1458
|
endIndex: 0,
|
|
@@ -1430,7 +1476,13 @@ function createKernel(options = {}) {
|
|
|
1430
1476
|
},
|
|
1431
1477
|
// Scroll Control
|
|
1432
1478
|
getScrollPosition() {
|
|
1433
|
-
|
|
1479
|
+
const position = scrollEngine.getScrollPosition();
|
|
1480
|
+
const horizontal = config.direction === "horizontal";
|
|
1481
|
+
const offset = horizontal ? position.scrollLeft : position.scrollTop;
|
|
1482
|
+
const viewport = viewportManager.getViewport();
|
|
1483
|
+
const maxScroll = rangeCalculator.getTotalSize() + startPadding() - (horizontal ? viewport.width : viewport.height);
|
|
1484
|
+
const percentage = maxScroll > 0 ? Math.min(1, Math.max(0, offset / maxScroll)) : 0;
|
|
1485
|
+
return { ...position, offset, percentage };
|
|
1434
1486
|
},
|
|
1435
1487
|
setScrollPosition(position) {
|
|
1436
1488
|
scrollEngine.setScrollPosition(position);
|
|
@@ -1440,37 +1492,39 @@ function createKernel(options = {}) {
|
|
|
1440
1492
|
},
|
|
1441
1493
|
scrollToIndex(index, options2 = {}) {
|
|
1442
1494
|
const { align = "auto", offset: additionalOffset = 0, ...scrollOptions } = options2;
|
|
1443
|
-
const itemOffset = rangeCalculator.getItemOffset(index);
|
|
1495
|
+
const itemOffset = rangeCalculator.getItemOffset(index) + startPadding();
|
|
1444
1496
|
const itemSize = rangeCalculator.getItemSize(index);
|
|
1445
1497
|
const viewport = viewportManager.getViewport();
|
|
1498
|
+
const horizontal = config.direction === "horizontal";
|
|
1499
|
+
const viewportSize = horizontal ? viewport.width : viewport.height;
|
|
1446
1500
|
let targetOffset;
|
|
1447
1501
|
switch (align) {
|
|
1448
1502
|
case "start":
|
|
1449
1503
|
targetOffset = itemOffset;
|
|
1450
1504
|
break;
|
|
1451
1505
|
case "center":
|
|
1452
|
-
targetOffset = itemOffset -
|
|
1506
|
+
targetOffset = itemOffset - viewportSize / 2 + itemSize / 2;
|
|
1453
1507
|
break;
|
|
1454
1508
|
case "end":
|
|
1455
|
-
targetOffset = itemOffset -
|
|
1509
|
+
targetOffset = itemOffset - viewportSize + itemSize;
|
|
1456
1510
|
break;
|
|
1457
1511
|
case "auto":
|
|
1458
1512
|
default: {
|
|
1459
1513
|
const scrollPosition = scrollEngine.getScrollPosition();
|
|
1460
|
-
const visibleStart = scrollPosition.scrollTop;
|
|
1461
|
-
const visibleEnd = visibleStart +
|
|
1514
|
+
const visibleStart = horizontal ? scrollPosition.scrollLeft : scrollPosition.scrollTop;
|
|
1515
|
+
const visibleEnd = visibleStart + viewportSize;
|
|
1462
1516
|
const itemEnd = itemOffset + itemSize;
|
|
1463
1517
|
if (itemOffset < visibleStart) {
|
|
1464
1518
|
targetOffset = itemOffset;
|
|
1465
1519
|
} else if (itemEnd > visibleEnd) {
|
|
1466
|
-
targetOffset = itemEnd -
|
|
1520
|
+
targetOffset = itemEnd - viewportSize;
|
|
1467
1521
|
} else {
|
|
1468
1522
|
return;
|
|
1469
1523
|
}
|
|
1470
1524
|
break;
|
|
1471
1525
|
}
|
|
1472
1526
|
}
|
|
1473
|
-
const maxScroll = Math.max(0, rangeCalculator.getTotalSize() -
|
|
1527
|
+
const maxScroll = Math.max(0, rangeCalculator.getTotalSize() + startPadding() - viewportSize);
|
|
1474
1528
|
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
1475
1529
|
scrollEngine.scrollTo(targetOffset, scrollOptions);
|
|
1476
1530
|
},
|
|
@@ -1493,6 +1547,8 @@ function createKernel(options = {}) {
|
|
|
1493
1547
|
},
|
|
1494
1548
|
// Measurement
|
|
1495
1549
|
measureItem(index, size) {
|
|
1550
|
+
if (!Number.isFinite(size) || size < 0) return;
|
|
1551
|
+
if (!Number.isInteger(index) || index < 0 || index >= rangeCalculator.getItemCount()) return;
|
|
1496
1552
|
const previousHeight = rangeCalculator.setItemHeight(index, size);
|
|
1497
1553
|
eventBus.emit({
|
|
1498
1554
|
type: "item-measured",
|
|
@@ -1506,18 +1562,19 @@ function createKernel(options = {}) {
|
|
|
1506
1562
|
return rangeCalculator.getCachedHeight(index);
|
|
1507
1563
|
},
|
|
1508
1564
|
invalidateMeasurement(index) {
|
|
1565
|
+
const wasMeasured = rangeCalculator.getCachedHeight(index) !== void 0;
|
|
1509
1566
|
rangeCalculator.invalidate(index);
|
|
1510
|
-
updateVisibleRange();
|
|
1567
|
+
updateVisibleRange(wasMeasured);
|
|
1511
1568
|
},
|
|
1512
1569
|
invalidateAllMeasurements() {
|
|
1513
1570
|
rangeCalculator.invalidateAll();
|
|
1514
|
-
updateVisibleRange();
|
|
1571
|
+
updateVisibleRange(true);
|
|
1515
1572
|
},
|
|
1516
1573
|
getEstimatedHeight() {
|
|
1517
1574
|
return rangeCalculator.getEstimatedHeight();
|
|
1518
1575
|
},
|
|
1519
1576
|
getItemOffset(index) {
|
|
1520
|
-
return rangeCalculator.getItemOffset(index);
|
|
1577
|
+
return rangeCalculator.getItemOffset(index) + startPadding();
|
|
1521
1578
|
},
|
|
1522
1579
|
// Events
|
|
1523
1580
|
emit(event) {
|
|
@@ -1543,21 +1600,28 @@ function createKernel(options = {}) {
|
|
|
1543
1600
|
});
|
|
1544
1601
|
}
|
|
1545
1602
|
}
|
|
1603
|
+
let layoutChanged = false;
|
|
1604
|
+
if (newOptions.paddingStart !== void 0) {
|
|
1605
|
+
layoutChanged = newOptions.paddingStart !== startPadding();
|
|
1606
|
+
config.paddingStart = newOptions.paddingStart;
|
|
1607
|
+
}
|
|
1546
1608
|
if (newOptions.estimatedItemHeight !== void 0) {
|
|
1609
|
+
layoutChanged = layoutChanged || newOptions.estimatedItemHeight !== config.estimatedItemHeight;
|
|
1547
1610
|
config.estimatedItemHeight = newOptions.estimatedItemHeight;
|
|
1548
1611
|
rangeCalculator.setEstimatedHeight(newOptions.estimatedItemHeight);
|
|
1549
1612
|
}
|
|
1550
1613
|
if (newOptions.overscan !== void 0) {
|
|
1551
|
-
config.overscan = newOptions.overscan;
|
|
1552
|
-
rangeCalculator.setOverscan(
|
|
1614
|
+
config.overscan = normalizeOverscan(newOptions.overscan);
|
|
1615
|
+
rangeCalculator.setOverscan(config.overscan);
|
|
1553
1616
|
}
|
|
1554
1617
|
if (newOptions.direction !== void 0) {
|
|
1555
1618
|
config.direction = newOptions.direction;
|
|
1619
|
+
scrollEngine.setDirection(newOptions.direction);
|
|
1556
1620
|
}
|
|
1557
1621
|
if (newOptions.getItemKey !== void 0) {
|
|
1558
1622
|
config.getItemKey = newOptions.getItemKey;
|
|
1559
1623
|
}
|
|
1560
|
-
updateVisibleRange();
|
|
1624
|
+
updateVisibleRange(layoutChanged);
|
|
1561
1625
|
},
|
|
1562
1626
|
getOptions() {
|
|
1563
1627
|
return { ...config };
|
|
@@ -1570,13 +1634,14 @@ function createKernel(options = {}) {
|
|
|
1570
1634
|
return config.itemCount;
|
|
1571
1635
|
},
|
|
1572
1636
|
getTotalSize() {
|
|
1573
|
-
return rangeCalculator.getTotalSize();
|
|
1637
|
+
return rangeCalculator.getTotalSize() + startPadding();
|
|
1574
1638
|
},
|
|
1575
1639
|
// Lifecycle
|
|
1576
1640
|
attach(container) {
|
|
1577
1641
|
if (attached) {
|
|
1578
1642
|
kernel.detach();
|
|
1579
1643
|
}
|
|
1644
|
+
const previousViewport = viewportManager.getViewport();
|
|
1580
1645
|
scrollEngine.attach(container);
|
|
1581
1646
|
viewportManager.attach(container);
|
|
1582
1647
|
attached = true;
|
|
@@ -1594,6 +1659,10 @@ function createKernel(options = {}) {
|
|
|
1594
1659
|
eventBus.emit(event);
|
|
1595
1660
|
updateVisibleRange();
|
|
1596
1661
|
});
|
|
1662
|
+
const viewport = viewportManager.getViewport();
|
|
1663
|
+
if (viewport.width !== previousViewport.width || viewport.height !== previousViewport.height || viewport.scrollWidth !== previousViewport.scrollWidth || viewport.scrollHeight !== previousViewport.scrollHeight) {
|
|
1664
|
+
eventBus.emit({ type: "resize", viewport, previousViewport });
|
|
1665
|
+
}
|
|
1597
1666
|
updateVisibleRange();
|
|
1598
1667
|
},
|
|
1599
1668
|
detach() {
|
|
@@ -1608,15 +1677,16 @@ function createKernel(options = {}) {
|
|
|
1608
1677
|
}
|
|
1609
1678
|
};
|
|
1610
1679
|
pluginRegistry.setKernel(kernel);
|
|
1611
|
-
function updateVisibleRange() {
|
|
1680
|
+
function updateVisibleRange(layoutChanged = false) {
|
|
1612
1681
|
if (!attached) return;
|
|
1613
1682
|
const viewport = viewportManager.getViewport();
|
|
1614
1683
|
const scrollPosition = scrollEngine.getScrollPosition();
|
|
1615
|
-
const scrollOffset = config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft;
|
|
1684
|
+
const scrollOffset = (config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft) - startPadding();
|
|
1616
1685
|
const viewportSize = config.direction === "vertical" ? viewport.height : viewport.width;
|
|
1617
1686
|
const previousRange = { ...currentRange };
|
|
1618
1687
|
currentRange = rangeCalculator.getVisibleRange(scrollOffset, viewportSize);
|
|
1619
|
-
if (currentRange.startIndex !== previousRange.startIndex || currentRange.endIndex !== previousRange.endIndex || currentRange.overscanStartIndex !== previousRange.overscanStartIndex || currentRange.overscanEndIndex !== previousRange.overscanEndIndex) {
|
|
1688
|
+
if (layoutChanged || !rangeAnnounced || currentRange.startIndex !== previousRange.startIndex || currentRange.endIndex !== previousRange.endIndex || currentRange.overscanStartIndex !== previousRange.overscanStartIndex || currentRange.overscanEndIndex !== previousRange.overscanEndIndex) {
|
|
1689
|
+
rangeAnnounced = true;
|
|
1620
1690
|
eventBus.emit({
|
|
1621
1691
|
type: "visible-range-change",
|
|
1622
1692
|
range: currentRange,
|
|
@@ -1643,32 +1713,6 @@ function createPlugin(config) {
|
|
|
1643
1713
|
}
|
|
1644
1714
|
|
|
1645
1715
|
// src/plugins/core/list-renderer.ts
|
|
1646
|
-
var VirtualItemPool = class {
|
|
1647
|
-
constructor() {
|
|
1648
|
-
this.pool = [];
|
|
1649
|
-
}
|
|
1650
|
-
acquire() {
|
|
1651
|
-
const item = this.pool.pop();
|
|
1652
|
-
if (item) {
|
|
1653
|
-
return item;
|
|
1654
|
-
}
|
|
1655
|
-
return { index: 0, key: 0, start: 0, end: 0, size: 0, lane: 0 };
|
|
1656
|
-
}
|
|
1657
|
-
release(item) {
|
|
1658
|
-
item.index = 0;
|
|
1659
|
-
item.key = 0;
|
|
1660
|
-
item.start = 0;
|
|
1661
|
-
item.end = 0;
|
|
1662
|
-
item.size = 0;
|
|
1663
|
-
item.lane = 0;
|
|
1664
|
-
this.pool.push(item);
|
|
1665
|
-
}
|
|
1666
|
-
releaseAll(items) {
|
|
1667
|
-
for (const item of items) {
|
|
1668
|
-
this.release(item);
|
|
1669
|
-
}
|
|
1670
|
-
}
|
|
1671
|
-
};
|
|
1672
1716
|
function listRendererPlugin(options = {}) {
|
|
1673
1717
|
const { getItemKey = (index) => index } = options;
|
|
1674
1718
|
let kernel = null;
|
|
@@ -1679,24 +1723,24 @@ function listRendererPlugin(options = {}) {
|
|
|
1679
1723
|
overscanStartIndex: 0,
|
|
1680
1724
|
overscanEndIndex: 0
|
|
1681
1725
|
};
|
|
1682
|
-
const itemPool = new VirtualItemPool();
|
|
1683
1726
|
function buildVirtualItems(range) {
|
|
1684
1727
|
if (!kernel) return [];
|
|
1685
|
-
itemPool.releaseAll(virtualItems);
|
|
1686
1728
|
const items = [];
|
|
1687
1729
|
range.startIndex;
|
|
1688
1730
|
range.endIndex;
|
|
1689
|
-
|
|
1690
|
-
|
|
1731
|
+
const lastIndex = Math.min(range.overscanEndIndex, kernel.getItemCount() - 1);
|
|
1732
|
+
for (let i = range.overscanStartIndex; i <= lastIndex; i++) {
|
|
1691
1733
|
const offset = kernel.getItemOffset(i);
|
|
1692
1734
|
const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1735
|
+
items.push({
|
|
1736
|
+
index: i,
|
|
1737
|
+
key: getItemKey(i),
|
|
1738
|
+
start: offset,
|
|
1739
|
+
end: offset + size,
|
|
1740
|
+
size,
|
|
1741
|
+
lane: 0
|
|
1742
|
+
// For list, always lane 0
|
|
1743
|
+
});
|
|
1700
1744
|
}
|
|
1701
1745
|
return items;
|
|
1702
1746
|
}
|
|
@@ -1722,6 +1766,7 @@ function listRendererPlugin(options = {}) {
|
|
|
1722
1766
|
return lastItem?.end ?? 0;
|
|
1723
1767
|
},
|
|
1724
1768
|
isItemVisible(index) {
|
|
1769
|
+
if (!kernel || index >= kernel.getItemCount()) return false;
|
|
1725
1770
|
return index >= currentRange.startIndex && index <= currentRange.endIndex;
|
|
1726
1771
|
},
|
|
1727
1772
|
recalculate
|
|
@@ -1735,7 +1780,6 @@ function listRendererPlugin(options = {}) {
|
|
|
1735
1780
|
recalculate();
|
|
1736
1781
|
},
|
|
1737
1782
|
uninstall() {
|
|
1738
|
-
itemPool.releaseAll(virtualItems);
|
|
1739
1783
|
virtualItems = [];
|
|
1740
1784
|
kernel = null;
|
|
1741
1785
|
},
|
|
@@ -1753,7 +1797,56 @@ function listRendererPlugin(options = {}) {
|
|
|
1753
1797
|
};
|
|
1754
1798
|
}
|
|
1755
1799
|
|
|
1800
|
+
// src/react/hooks/items-range.ts
|
|
1801
|
+
function getItemsRange(items, scrollTop, viewportHeight) {
|
|
1802
|
+
if (items.length === 0) {
|
|
1803
|
+
return { startIndex: 0, endIndex: 0, overscanStartIndex: 0, overscanEndIndex: 0 };
|
|
1804
|
+
}
|
|
1805
|
+
const overscanStartIndex = items[0].index;
|
|
1806
|
+
const overscanEndIndex = items[items.length - 1].index;
|
|
1807
|
+
const viewEnd = scrollTop + viewportHeight;
|
|
1808
|
+
let startIndex = -1;
|
|
1809
|
+
let endIndex = -1;
|
|
1810
|
+
for (const item of items) {
|
|
1811
|
+
if (item.end > scrollTop && item.start < viewEnd) {
|
|
1812
|
+
if (startIndex === -1) startIndex = item.index;
|
|
1813
|
+
endIndex = item.index;
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
return startIndex === -1 ? { startIndex: overscanStartIndex, endIndex: overscanEndIndex, overscanStartIndex, overscanEndIndex } : { startIndex, endIndex, overscanStartIndex, overscanEndIndex };
|
|
1817
|
+
}
|
|
1818
|
+
function rekeyItems(items, count, getItemKey) {
|
|
1819
|
+
let changed = false;
|
|
1820
|
+
const next = [];
|
|
1821
|
+
for (const item of items) {
|
|
1822
|
+
if (item.index >= count) {
|
|
1823
|
+
changed = true;
|
|
1824
|
+
continue;
|
|
1825
|
+
}
|
|
1826
|
+
const key = getItemKey(item.index);
|
|
1827
|
+
if (key === item.key) {
|
|
1828
|
+
next.push(item);
|
|
1829
|
+
} else {
|
|
1830
|
+
changed = true;
|
|
1831
|
+
next.push({ ...item, key });
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
return changed ? next : items;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1756
1837
|
// src/react/hooks/use-virtual-list.ts
|
|
1838
|
+
function sameItems(a, b) {
|
|
1839
|
+
if (a === b) return true;
|
|
1840
|
+
if (a.length !== b.length) return false;
|
|
1841
|
+
for (let i = 0; i < a.length; i++) {
|
|
1842
|
+
const x = a[i];
|
|
1843
|
+
const y = b[i];
|
|
1844
|
+
if (x.index !== y.index || x.key !== y.key || x.start !== y.start || x.size !== y.size || x.lane !== y.lane) {
|
|
1845
|
+
return false;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
return true;
|
|
1849
|
+
}
|
|
1757
1850
|
function useVirtualList(options) {
|
|
1758
1851
|
const {
|
|
1759
1852
|
count,
|
|
@@ -1767,17 +1860,21 @@ function useVirtualList(options) {
|
|
|
1767
1860
|
horizontal = false,
|
|
1768
1861
|
containerRef
|
|
1769
1862
|
} = options;
|
|
1863
|
+
const getItemKeyRef = react.useRef(getItemKey);
|
|
1864
|
+
getItemKeyRef.current = getItemKey;
|
|
1770
1865
|
const kernelRef = react.useRef(
|
|
1771
1866
|
createKernel({
|
|
1772
1867
|
itemCount: count,
|
|
1773
1868
|
estimatedItemHeight,
|
|
1774
1869
|
overscan,
|
|
1775
1870
|
direction: horizontal ? "horizontal" : "vertical",
|
|
1776
|
-
getItemKey
|
|
1871
|
+
getItemKey,
|
|
1872
|
+
paddingStart
|
|
1777
1873
|
})
|
|
1778
1874
|
);
|
|
1779
1875
|
const [virtualItems, setVirtualItems] = react.useState([]);
|
|
1780
1876
|
const [isScrolling, setIsScrolling] = react.useState(false);
|
|
1877
|
+
const [kernelTotalSize, setKernelTotalSize] = react.useState(() => kernelRef.current.getTotalSize());
|
|
1781
1878
|
const [scrollOffset, setScrollOffset] = react.useState(initialOffset);
|
|
1782
1879
|
const [range, setRange] = react.useState({
|
|
1783
1880
|
startIndex: 0,
|
|
@@ -1790,12 +1887,12 @@ function useVirtualList(options) {
|
|
|
1790
1887
|
const resizeObserverRef = react.useRef(null);
|
|
1791
1888
|
const kernel = kernelRef.current;
|
|
1792
1889
|
react.useEffect(() => {
|
|
1793
|
-
const plugin = listRendererPlugin({ getItemKey });
|
|
1890
|
+
const plugin = listRendererPlugin({ getItemKey: (index) => getItemKeyRef.current(index) });
|
|
1794
1891
|
kernel.register(plugin);
|
|
1795
1892
|
listRendererRef.current = plugin.api;
|
|
1796
1893
|
const unsubScroll = kernel.on("scroll", (event) => {
|
|
1797
1894
|
const e = event;
|
|
1798
|
-
setScrollOffset(horizontal ? e.scrollLeft : e.scrollTop);
|
|
1895
|
+
setScrollOffset(kernel.getOptions().direction === "horizontal" ? e.scrollLeft : e.scrollTop);
|
|
1799
1896
|
});
|
|
1800
1897
|
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
1801
1898
|
setIsScrolling(true);
|
|
@@ -1807,40 +1904,70 @@ function useVirtualList(options) {
|
|
|
1807
1904
|
const e = event;
|
|
1808
1905
|
setRange(e.range);
|
|
1809
1906
|
setVirtualItems(listRendererRef.current?.getVirtualItems() ?? []);
|
|
1907
|
+
setKernelTotalSize(kernel.getTotalSize());
|
|
1810
1908
|
});
|
|
1909
|
+
const syncLayout = () => {
|
|
1910
|
+
const next = listRendererRef.current?.getVirtualItems() ?? [];
|
|
1911
|
+
setVirtualItems((previous) => sameItems(previous, next) ? previous : next);
|
|
1912
|
+
setKernelTotalSize(kernel.getTotalSize());
|
|
1913
|
+
};
|
|
1914
|
+
const unsubItemsChange = kernel.on("items-change", syncLayout);
|
|
1915
|
+
const unsubItemMeasured = kernel.on("item-measured", syncLayout);
|
|
1811
1916
|
return () => {
|
|
1812
1917
|
unsubScroll();
|
|
1813
1918
|
unsubScrollStart();
|
|
1814
1919
|
unsubScrollEnd();
|
|
1815
1920
|
unsubRangeChange();
|
|
1921
|
+
unsubItemsChange();
|
|
1922
|
+
unsubItemMeasured();
|
|
1816
1923
|
kernel.destroy();
|
|
1817
1924
|
};
|
|
1818
1925
|
}, []);
|
|
1926
|
+
const initialOffsetAppliedRef = react.useRef(false);
|
|
1819
1927
|
react.useEffect(() => {
|
|
1820
1928
|
const container = containerRef.current;
|
|
1821
1929
|
if (!container) return;
|
|
1822
1930
|
kernel.attach(container);
|
|
1823
|
-
if (initialOffset > 0) {
|
|
1824
|
-
kernel.scrollTo(initialOffset);
|
|
1825
|
-
}
|
|
1826
1931
|
return () => {
|
|
1827
1932
|
kernel.detach();
|
|
1828
1933
|
};
|
|
1829
|
-
}, [containerRef, kernel
|
|
1934
|
+
}, [containerRef, kernel]);
|
|
1935
|
+
const hasRows = virtualItems.length > 0;
|
|
1936
|
+
react.useEffect(() => {
|
|
1937
|
+
if (initialOffset > 0 && hasRows && !initialOffsetAppliedRef.current && containerRef.current) {
|
|
1938
|
+
initialOffsetAppliedRef.current = true;
|
|
1939
|
+
kernel.scrollTo(initialOffset);
|
|
1940
|
+
}
|
|
1941
|
+
}, [initialOffset, hasRows, kernel, containerRef]);
|
|
1830
1942
|
react.useEffect(() => {
|
|
1831
1943
|
kernel.configure({ itemCount: count });
|
|
1832
1944
|
}, [count, kernel]);
|
|
1833
1945
|
react.useEffect(() => {
|
|
1834
1946
|
kernel.configure({ overscan });
|
|
1835
1947
|
}, [overscan, kernel]);
|
|
1948
|
+
react.useEffect(() => {
|
|
1949
|
+
kernel.configure({ estimatedItemHeight });
|
|
1950
|
+
}, [estimatedItemHeight, kernel]);
|
|
1951
|
+
react.useEffect(() => {
|
|
1952
|
+
kernel.configure({ paddingStart });
|
|
1953
|
+
}, [paddingStart, kernel]);
|
|
1954
|
+
react.useEffect(() => {
|
|
1955
|
+
kernel.configure({ direction: horizontal ? "horizontal" : "vertical" });
|
|
1956
|
+
}, [horizontal, kernel]);
|
|
1836
1957
|
react.useEffect(() => {
|
|
1837
1958
|
const observer = new ResizeObserver((entries) => {
|
|
1838
1959
|
for (const entry of entries) {
|
|
1839
1960
|
const element = entry.target;
|
|
1840
1961
|
const index = parseInt(element.dataset["virtualIndex"] ?? "", 10);
|
|
1841
1962
|
if (!isNaN(index)) {
|
|
1842
|
-
const
|
|
1843
|
-
const
|
|
1963
|
+
const horizontalList = kernel.getOptions().direction === "horizontal";
|
|
1964
|
+
const borderBox = entry.borderBoxSize?.[0];
|
|
1965
|
+
let size = borderBox ? horizontalList ? borderBox.inlineSize : borderBox.blockSize : 0;
|
|
1966
|
+
if (!(size > 0)) {
|
|
1967
|
+
const box = element.getBoundingClientRect();
|
|
1968
|
+
size = horizontalList ? box.width : box.height;
|
|
1969
|
+
}
|
|
1970
|
+
if (!(size > 0)) size = horizontalList ? entry.contentRect.width : entry.contentRect.height;
|
|
1844
1971
|
if (size > 0) {
|
|
1845
1972
|
kernel.measureItem(index, size);
|
|
1846
1973
|
}
|
|
@@ -1851,7 +1978,7 @@ function useVirtualList(options) {
|
|
|
1851
1978
|
return () => {
|
|
1852
1979
|
observer.disconnect();
|
|
1853
1980
|
};
|
|
1854
|
-
}, [kernel
|
|
1981
|
+
}, [kernel]);
|
|
1855
1982
|
const measureElement = react.useCallback(
|
|
1856
1983
|
(index, element) => {
|
|
1857
1984
|
const observer = resizeObserverRef.current;
|
|
@@ -1903,11 +2030,13 @@ function useVirtualList(options) {
|
|
|
1903
2030
|
() => kernel.invalidateAllMeasurements(),
|
|
1904
2031
|
[kernel]
|
|
1905
2032
|
);
|
|
1906
|
-
const totalSize =
|
|
1907
|
-
|
|
1908
|
-
|
|
2033
|
+
const totalSize = kernelTotalSize + paddingEnd;
|
|
2034
|
+
const keyedItems = react.useMemo(
|
|
2035
|
+
() => rekeyItems(virtualItems, count, getItemKey),
|
|
2036
|
+
[virtualItems, count, getItemKey]
|
|
2037
|
+
);
|
|
1909
2038
|
return {
|
|
1910
|
-
virtualItems,
|
|
2039
|
+
virtualItems: keyedItems,
|
|
1911
2040
|
totalSize,
|
|
1912
2041
|
scrollOffset,
|
|
1913
2042
|
isScrolling,
|
|
@@ -1917,9 +2046,57 @@ function useVirtualList(options) {
|
|
|
1917
2046
|
range,
|
|
1918
2047
|
getMeasurement,
|
|
1919
2048
|
invalidateMeasurement,
|
|
1920
|
-
invalidateAllMeasurements
|
|
2049
|
+
invalidateAllMeasurements,
|
|
2050
|
+
kernel
|
|
1921
2051
|
};
|
|
1922
2052
|
}
|
|
2053
|
+
function useKernelPlugins(kernel, plugins) {
|
|
2054
|
+
const registeredRef = react.useRef([]);
|
|
2055
|
+
react.useEffect(() => {
|
|
2056
|
+
const previous = registeredRef.current;
|
|
2057
|
+
if (previous.length === plugins.length && previous.every((plugin, i) => plugin === plugins[i])) {
|
|
2058
|
+
return;
|
|
2059
|
+
}
|
|
2060
|
+
for (const plugin of previous) {
|
|
2061
|
+
kernel.unregister(plugin.name);
|
|
2062
|
+
}
|
|
2063
|
+
for (const plugin of plugins) {
|
|
2064
|
+
kernel.register(plugin);
|
|
2065
|
+
}
|
|
2066
|
+
registeredRef.current = plugins;
|
|
2067
|
+
});
|
|
2068
|
+
react.useEffect(() => () => {
|
|
2069
|
+
for (const plugin of registeredRef.current) {
|
|
2070
|
+
kernel.unregister(plugin.name);
|
|
2071
|
+
}
|
|
2072
|
+
registeredRef.current = [];
|
|
2073
|
+
}, [kernel]);
|
|
2074
|
+
}
|
|
2075
|
+
function useLoadMoreWhenShort(containerRef, { count, totalSize, hasMore, isLoading, threshold, horizontal = false, onLoadMore }) {
|
|
2076
|
+
const requestedCount = react.useRef(-1);
|
|
2077
|
+
react.useEffect(() => {
|
|
2078
|
+
if (!hasMore) {
|
|
2079
|
+
requestedCount.current = -1;
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
2082
|
+
const element = containerRef.current;
|
|
2083
|
+
if (!element || !onLoadMore || isLoading || requestedCount.current === count) return;
|
|
2084
|
+
const distanceToEnd = horizontal ? element.scrollWidth - element.scrollLeft - element.clientWidth : element.scrollHeight - element.scrollTop - element.clientHeight;
|
|
2085
|
+
if (distanceToEnd < threshold) {
|
|
2086
|
+
requestedCount.current = count;
|
|
2087
|
+
try {
|
|
2088
|
+
const pending = onLoadMore();
|
|
2089
|
+
if (pending && typeof pending.then === "function") {
|
|
2090
|
+
Promise.resolve(pending).catch((error) => {
|
|
2091
|
+
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
} catch (error) {
|
|
2095
|
+
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
}, [count, totalSize, hasMore, isLoading, threshold, horizontal, !!onLoadMore]);
|
|
2099
|
+
}
|
|
1923
2100
|
var ScrollexContext = react.createContext(null);
|
|
1924
2101
|
function useScrollex() {
|
|
1925
2102
|
const context = react.useContext(ScrollexContext);
|
|
@@ -1974,7 +2151,8 @@ function VirtualListInner(props, ref) {
|
|
|
1974
2151
|
scrollToIndex,
|
|
1975
2152
|
measureElement,
|
|
1976
2153
|
isScrolling,
|
|
1977
|
-
range
|
|
2154
|
+
range,
|
|
2155
|
+
kernel
|
|
1978
2156
|
} = useVirtualList({
|
|
1979
2157
|
count: data.length,
|
|
1980
2158
|
getItemHeight: itemHeight === "auto" ? void 0 : () => itemHeight,
|
|
@@ -1985,12 +2163,16 @@ function VirtualListInner(props, ref) {
|
|
|
1985
2163
|
horizontal: direction === "horizontal",
|
|
1986
2164
|
containerRef
|
|
1987
2165
|
});
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
2166
|
+
react.useEffect(() => {
|
|
2167
|
+
if (virtualItems.length === 0) return;
|
|
2168
|
+
onVisibleRangeChange?.(range);
|
|
2169
|
+
onItemsRendered?.({
|
|
2170
|
+
overscanStartIndex: range.overscanStartIndex,
|
|
2171
|
+
overscanEndIndex: range.overscanEndIndex,
|
|
2172
|
+
visibleStartIndex: range.startIndex,
|
|
2173
|
+
visibleEndIndex: range.endIndex
|
|
2174
|
+
});
|
|
2175
|
+
}, [range.startIndex, range.endIndex, range.overscanStartIndex, range.overscanEndIndex]);
|
|
1994
2176
|
react.useImperativeHandle(ref, () => ({
|
|
1995
2177
|
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
1996
2178
|
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
@@ -2008,11 +2190,11 @@ function VirtualListInner(props, ref) {
|
|
|
2008
2190
|
const handleScrollForInfinite = react.useCallback((event) => {
|
|
2009
2191
|
if (!onLoadMore || !hasMore || isLoading) return;
|
|
2010
2192
|
const target = event.currentTarget;
|
|
2011
|
-
const
|
|
2012
|
-
if (
|
|
2193
|
+
const distanceToEnd = direction === "horizontal" ? target.scrollWidth - target.scrollLeft - target.clientWidth : target.scrollHeight - target.scrollTop - target.clientHeight;
|
|
2194
|
+
if (distanceToEnd < threshold) {
|
|
2013
2195
|
onLoadMore();
|
|
2014
2196
|
}
|
|
2015
|
-
}, [onLoadMore, hasMore, isLoading, threshold]);
|
|
2197
|
+
}, [onLoadMore, hasMore, isLoading, threshold, direction]);
|
|
2016
2198
|
const containerStyles = react.useMemo(() => ({
|
|
2017
2199
|
height,
|
|
2018
2200
|
width,
|
|
@@ -2030,10 +2212,10 @@ function VirtualListInner(props, ref) {
|
|
|
2030
2212
|
}), [totalSize, direction, innerStyle]);
|
|
2031
2213
|
const createRenderProps = react.useCallback(
|
|
2032
2214
|
(index) => {
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
2215
|
+
if (index >= data.length) {
|
|
2216
|
+
throw new Error(`[Scrollex] Item at index ${index} is out of range`);
|
|
2036
2217
|
}
|
|
2218
|
+
const item = data[index];
|
|
2037
2219
|
const virtualItem = virtualItems.find((vi) => vi.index === index);
|
|
2038
2220
|
const itemOffset = virtualItem?.start ?? 0;
|
|
2039
2221
|
const itemSize = virtualItem?.size ?? estimatedItemHeight;
|
|
@@ -2041,8 +2223,10 @@ function VirtualListInner(props, ref) {
|
|
|
2041
2223
|
position: "absolute",
|
|
2042
2224
|
top: 0,
|
|
2043
2225
|
left: 0,
|
|
2044
|
-
|
|
2045
|
-
|
|
2226
|
+
// Rows that are measured take the size of their content: a fixed size on the measured element would only
|
|
2227
|
+
// measure itself (the estimate until it is measured) and never grow
|
|
2228
|
+
width: direction === "vertical" ? "100%" : itemHeight === "auto" ? "auto" : itemSize,
|
|
2229
|
+
height: direction === "vertical" ? itemHeight === "auto" ? "auto" : itemSize : "100%",
|
|
2046
2230
|
transform: direction === "vertical" ? `translateY(${itemOffset}px)` : `translateX(${itemOffset}px)`,
|
|
2047
2231
|
willChange: "transform",
|
|
2048
2232
|
contain: "layout style",
|
|
@@ -2057,11 +2241,13 @@ function VirtualListInner(props, ref) {
|
|
|
2057
2241
|
isScrolling
|
|
2058
2242
|
};
|
|
2059
2243
|
},
|
|
2060
|
-
[data, virtualItems, estimatedItemHeight, direction, itemStyle, measureElement, range, isScrolling]
|
|
2244
|
+
[data, virtualItems, estimatedItemHeight, itemHeight, direction, itemStyle, measureElement, range, isScrolling]
|
|
2061
2245
|
);
|
|
2246
|
+
useKernelPlugins(kernel, plugins);
|
|
2247
|
+
useLoadMoreWhenShort(containerRef, { count: data.length, totalSize, hasMore, isLoading, threshold, horizontal: direction === "horizontal", onLoadMore });
|
|
2062
2248
|
const contextValue = react.useMemo(() => ({
|
|
2063
|
-
kernel
|
|
2064
|
-
}), []);
|
|
2249
|
+
kernel
|
|
2250
|
+
}), [kernel]);
|
|
2065
2251
|
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2066
2252
|
"div",
|
|
2067
2253
|
{
|
|
@@ -2074,21 +2260,21 @@ function VirtualListInner(props, ref) {
|
|
|
2074
2260
|
},
|
|
2075
2261
|
role,
|
|
2076
2262
|
"aria-label": ariaLabel,
|
|
2077
|
-
"aria-
|
|
2263
|
+
"aria-orientation": direction,
|
|
2078
2264
|
tabIndex,
|
|
2079
2265
|
...restProps,
|
|
2080
2266
|
children: [
|
|
2081
2267
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2082
2268
|
const index = virtualItem.index;
|
|
2083
|
-
|
|
2084
|
-
if (item === void 0) return null;
|
|
2269
|
+
if (index >= data.length) return null;
|
|
2085
2270
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2086
2271
|
"div",
|
|
2087
2272
|
{
|
|
2088
2273
|
className: itemClassName,
|
|
2089
2274
|
"data-index": index,
|
|
2090
2275
|
role: "option",
|
|
2091
|
-
"aria-
|
|
2276
|
+
"aria-setsize": data.length,
|
|
2277
|
+
"aria-posinset": index + 1,
|
|
2092
2278
|
children: renderItem(createRenderProps(index))
|
|
2093
2279
|
},
|
|
2094
2280
|
virtualItem.key
|
|
@@ -2108,19 +2294,20 @@ function gridRendererPlugin(options) {
|
|
|
2108
2294
|
minColumnWidth = 100,
|
|
2109
2295
|
rowHeight,
|
|
2110
2296
|
gap = 0,
|
|
2111
|
-
getItemKey = (index) => index
|
|
2297
|
+
getItemKey = (index) => index,
|
|
2298
|
+
getContainerWidth
|
|
2112
2299
|
} = options;
|
|
2113
2300
|
let kernel = null;
|
|
2114
2301
|
let virtualItems = [];
|
|
2115
|
-
let columnCount = typeof columns === "number" ? columns : 1;
|
|
2302
|
+
let columnCount = typeof columns === "number" ? normalizeColumnCount(columns) : 1;
|
|
2116
2303
|
let columnWidth = 0;
|
|
2117
2304
|
let gapX = typeof gap === "number" ? gap : gap.x;
|
|
2118
2305
|
let gapY = typeof gap === "number" ? gap : gap.y;
|
|
2119
2306
|
function calculateColumns(containerWidth) {
|
|
2120
2307
|
if (typeof columns === "number") {
|
|
2121
|
-
columnCount = columns;
|
|
2308
|
+
columnCount = normalizeColumnCount(columns);
|
|
2122
2309
|
} else {
|
|
2123
|
-
columnCount =
|
|
2310
|
+
columnCount = autoColumnCount(containerWidth, minColumnWidth, gapX);
|
|
2124
2311
|
}
|
|
2125
2312
|
columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
|
|
2126
2313
|
}
|
|
@@ -2161,7 +2348,7 @@ function gridRendererPlugin(options) {
|
|
|
2161
2348
|
function recalculate() {
|
|
2162
2349
|
if (!kernel) return;
|
|
2163
2350
|
const viewport = kernel.getViewport();
|
|
2164
|
-
calculateColumns(viewport.width);
|
|
2351
|
+
calculateColumns(getContainerWidth?.() ?? viewport.width);
|
|
2165
2352
|
virtualItems = buildVirtualItems();
|
|
2166
2353
|
}
|
|
2167
2354
|
const api = {
|
|
@@ -2169,7 +2356,8 @@ function gridRendererPlugin(options) {
|
|
|
2169
2356
|
return virtualItems;
|
|
2170
2357
|
},
|
|
2171
2358
|
getTotalHeight() {
|
|
2172
|
-
|
|
2359
|
+
const rowCount = getRowCount();
|
|
2360
|
+
return rowCount === 0 ? 0 : rowCount * (rowHeight + gapY) - gapY;
|
|
2173
2361
|
},
|
|
2174
2362
|
getColumnCount() {
|
|
2175
2363
|
return columnCount;
|
|
@@ -2211,6 +2399,7 @@ function useVirtualGrid(options) {
|
|
|
2211
2399
|
const {
|
|
2212
2400
|
count,
|
|
2213
2401
|
columns,
|
|
2402
|
+
minColumnWidth,
|
|
2214
2403
|
rowHeight,
|
|
2215
2404
|
gap = 0,
|
|
2216
2405
|
overscan = 2,
|
|
@@ -2220,6 +2409,8 @@ function useVirtualGrid(options) {
|
|
|
2220
2409
|
} = options;
|
|
2221
2410
|
const gapX = typeof gap === "number" ? gap : gap.x;
|
|
2222
2411
|
const gapY = typeof gap === "number" ? gap : gap.y;
|
|
2412
|
+
const getItemKeyRef = react.useRef(getItemKey);
|
|
2413
|
+
getItemKeyRef.current = getItemKey;
|
|
2223
2414
|
const kernelRef = react.useRef(
|
|
2224
2415
|
createKernel({
|
|
2225
2416
|
itemCount: count,
|
|
@@ -2238,23 +2429,49 @@ function useVirtualGrid(options) {
|
|
|
2238
2429
|
overscanStartIndex: 0,
|
|
2239
2430
|
overscanEndIndex: 0
|
|
2240
2431
|
});
|
|
2241
|
-
const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? columns : 1);
|
|
2432
|
+
const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? normalizeColumnCount(columns) : 1);
|
|
2242
2433
|
const [columnWidth, setColumnWidth] = react.useState(0);
|
|
2243
2434
|
const gridRendererRef = react.useRef(null);
|
|
2244
2435
|
const kernel = kernelRef.current;
|
|
2436
|
+
const publishItems = react.useCallback((items) => {
|
|
2437
|
+
setVirtualItems(items);
|
|
2438
|
+
setRange(getItemsRange(items, kernel.getScrollPosition().scrollTop, kernel.getViewport().height));
|
|
2439
|
+
const api = gridRendererRef.current;
|
|
2440
|
+
const itemCount = kernel.getItemCount();
|
|
2441
|
+
if (api && itemCount > 0) kernel.configure({ estimatedItemHeight: api.getTotalHeight() / itemCount });
|
|
2442
|
+
}, [kernel]);
|
|
2245
2443
|
react.useEffect(() => {
|
|
2246
2444
|
const plugin = gridRendererPlugin({
|
|
2247
2445
|
columns,
|
|
2446
|
+
minColumnWidth,
|
|
2248
2447
|
rowHeight,
|
|
2249
2448
|
gap: { x: gapX, y: gapY },
|
|
2250
|
-
getItemKey
|
|
2449
|
+
getItemKey: (index) => getItemKeyRef.current(index),
|
|
2450
|
+
getContainerWidth: () => containerRef.current ? getContentWidth(containerRef.current) : void 0
|
|
2251
2451
|
});
|
|
2252
2452
|
kernel.register(plugin);
|
|
2253
|
-
|
|
2453
|
+
const api = plugin.api;
|
|
2454
|
+
gridRendererRef.current = api;
|
|
2455
|
+
if (containerRef.current) {
|
|
2456
|
+
api.recalculate();
|
|
2457
|
+
setColumnCount(api.getColumnCount());
|
|
2458
|
+
setColumnWidth(api.getColumnWidth());
|
|
2459
|
+
publishItems(api.getVirtualItems());
|
|
2460
|
+
}
|
|
2461
|
+
return () => {
|
|
2462
|
+
kernel.unregister(plugin.name);
|
|
2463
|
+
gridRendererRef.current = null;
|
|
2464
|
+
};
|
|
2465
|
+
}, [kernel, columns, minColumnWidth, rowHeight, gapX, gapY]);
|
|
2466
|
+
react.useEffect(() => {
|
|
2254
2467
|
const unsubScroll = kernel.on("scroll", (event) => {
|
|
2255
2468
|
const e = event;
|
|
2256
2469
|
setScrollOffset(e.scrollTop);
|
|
2257
|
-
|
|
2470
|
+
const api = gridRendererRef.current;
|
|
2471
|
+
if (api) {
|
|
2472
|
+
api.recalculate();
|
|
2473
|
+
publishItems(api.getVirtualItems());
|
|
2474
|
+
}
|
|
2258
2475
|
});
|
|
2259
2476
|
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
2260
2477
|
setIsScrolling(true);
|
|
@@ -2268,22 +2485,18 @@ function useVirtualGrid(options) {
|
|
|
2268
2485
|
api.recalculate();
|
|
2269
2486
|
setColumnCount(api.getColumnCount());
|
|
2270
2487
|
setColumnWidth(api.getColumnWidth());
|
|
2271
|
-
|
|
2488
|
+
publishItems(api.getVirtualItems());
|
|
2272
2489
|
}
|
|
2273
2490
|
});
|
|
2274
|
-
const unsubRangeChange = kernel.on("visible-range-change", (event) => {
|
|
2275
|
-
const e = event;
|
|
2276
|
-
setRange(e.range);
|
|
2277
|
-
});
|
|
2278
2491
|
return () => {
|
|
2279
2492
|
unsubScroll();
|
|
2280
2493
|
unsubScrollStart();
|
|
2281
2494
|
unsubScrollEnd();
|
|
2282
2495
|
unsubResize();
|
|
2283
|
-
unsubRangeChange();
|
|
2284
2496
|
kernel.destroy();
|
|
2285
2497
|
};
|
|
2286
2498
|
}, []);
|
|
2499
|
+
const initialOffsetAppliedRef = react.useRef(false);
|
|
2287
2500
|
react.useEffect(() => {
|
|
2288
2501
|
const container = containerRef.current;
|
|
2289
2502
|
if (!container) return;
|
|
@@ -2293,23 +2506,28 @@ function useVirtualGrid(options) {
|
|
|
2293
2506
|
api.recalculate();
|
|
2294
2507
|
setColumnCount(api.getColumnCount());
|
|
2295
2508
|
setColumnWidth(api.getColumnWidth());
|
|
2296
|
-
|
|
2297
|
-
}
|
|
2298
|
-
if (initialOffset > 0) {
|
|
2299
|
-
kernel.scrollTo(initialOffset);
|
|
2509
|
+
publishItems(api.getVirtualItems());
|
|
2300
2510
|
}
|
|
2301
2511
|
return () => {
|
|
2302
2512
|
kernel.detach();
|
|
2303
2513
|
};
|
|
2304
|
-
}, [containerRef, kernel
|
|
2514
|
+
}, [containerRef, kernel]);
|
|
2305
2515
|
react.useEffect(() => {
|
|
2306
2516
|
kernel.configure({ itemCount: count });
|
|
2307
2517
|
const api = gridRendererRef.current;
|
|
2308
2518
|
if (api) {
|
|
2309
2519
|
api.recalculate();
|
|
2310
|
-
|
|
2520
|
+
publishItems(api.getVirtualItems());
|
|
2311
2521
|
}
|
|
2312
|
-
}, [count, kernel]);
|
|
2522
|
+
}, [count, kernel, publishItems]);
|
|
2523
|
+
react.useEffect(() => {
|
|
2524
|
+
kernel.configure({ overscan });
|
|
2525
|
+
const api = gridRendererRef.current;
|
|
2526
|
+
if (api && containerRef.current) {
|
|
2527
|
+
api.recalculate();
|
|
2528
|
+
publishItems(api.getVirtualItems());
|
|
2529
|
+
}
|
|
2530
|
+
}, [overscan, kernel, publishItems]);
|
|
2313
2531
|
const scrollTo = react.useCallback(
|
|
2314
2532
|
(offset, scrollOptions) => {
|
|
2315
2533
|
kernel.scrollTo(offset, scrollOptions);
|
|
@@ -2317,12 +2535,42 @@ function useVirtualGrid(options) {
|
|
|
2317
2535
|
[kernel]
|
|
2318
2536
|
);
|
|
2319
2537
|
const scrollToIndex = react.useCallback(
|
|
2320
|
-
(index, scrollOptions) => {
|
|
2538
|
+
(index, scrollOptions = {}) => {
|
|
2321
2539
|
const row = Math.floor(index / columnCount);
|
|
2322
2540
|
const rowOffset = row * (rowHeight + gapY);
|
|
2323
|
-
|
|
2541
|
+
const container = containerRef.current;
|
|
2542
|
+
if (!container) return;
|
|
2543
|
+
const { align = "auto", offset: additionalOffset = 0, ...options2 } = scrollOptions;
|
|
2544
|
+
const viewportHeight = container.clientHeight;
|
|
2545
|
+
const rowEnd = rowOffset + rowHeight;
|
|
2546
|
+
let targetOffset;
|
|
2547
|
+
switch (align) {
|
|
2548
|
+
case "start":
|
|
2549
|
+
targetOffset = rowOffset;
|
|
2550
|
+
break;
|
|
2551
|
+
case "center":
|
|
2552
|
+
targetOffset = rowOffset - viewportHeight / 2 + rowHeight / 2;
|
|
2553
|
+
break;
|
|
2554
|
+
case "end":
|
|
2555
|
+
targetOffset = rowEnd - viewportHeight;
|
|
2556
|
+
break;
|
|
2557
|
+
case "auto":
|
|
2558
|
+
default: {
|
|
2559
|
+
const visibleStart = container.scrollTop;
|
|
2560
|
+
if (rowOffset < visibleStart) {
|
|
2561
|
+
targetOffset = rowOffset;
|
|
2562
|
+
} else if (rowEnd > visibleStart + viewportHeight) {
|
|
2563
|
+
targetOffset = rowEnd - viewportHeight;
|
|
2564
|
+
} else {
|
|
2565
|
+
return;
|
|
2566
|
+
}
|
|
2567
|
+
break;
|
|
2568
|
+
}
|
|
2569
|
+
}
|
|
2570
|
+
const maxScroll = Math.max(0, container.scrollHeight - viewportHeight);
|
|
2571
|
+
kernel.scrollTo(Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll)), options2);
|
|
2324
2572
|
},
|
|
2325
|
-
[kernel, columnCount, rowHeight, gapY]
|
|
2573
|
+
[kernel, containerRef, columnCount, rowHeight, gapY]
|
|
2326
2574
|
);
|
|
2327
2575
|
const measureElement = react.useCallback(
|
|
2328
2576
|
(_index, _element) => {
|
|
@@ -2343,11 +2591,19 @@ function useVirtualGrid(options) {
|
|
|
2343
2591
|
const totalSize = react.useMemo(() => {
|
|
2344
2592
|
return gridRendererRef.current?.getTotalHeight() ?? 0;
|
|
2345
2593
|
}, [virtualItems]);
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2594
|
+
react.useEffect(() => {
|
|
2595
|
+
if (initialOffset > 0 && totalSize > 0 && !initialOffsetAppliedRef.current && containerRef.current) {
|
|
2596
|
+
initialOffsetAppliedRef.current = true;
|
|
2597
|
+
kernel.scrollTo(initialOffset);
|
|
2598
|
+
}
|
|
2599
|
+
}, [initialOffset, totalSize, kernel, containerRef]);
|
|
2600
|
+
const rowCount = react.useMemo(() => Math.ceil(count / Math.max(1, columnCount)), [count, columnCount]);
|
|
2601
|
+
const keyedItems = react.useMemo(
|
|
2602
|
+
() => rekeyItems(virtualItems, count, getItemKey),
|
|
2603
|
+
[virtualItems, count, getItemKey]
|
|
2604
|
+
);
|
|
2349
2605
|
return {
|
|
2350
|
-
virtualItems,
|
|
2606
|
+
virtualItems: keyedItems,
|
|
2351
2607
|
totalSize,
|
|
2352
2608
|
scrollOffset,
|
|
2353
2609
|
isScrolling,
|
|
@@ -2358,6 +2614,7 @@ function useVirtualGrid(options) {
|
|
|
2358
2614
|
getMeasurement,
|
|
2359
2615
|
invalidateMeasurement,
|
|
2360
2616
|
invalidateAllMeasurements,
|
|
2617
|
+
kernel,
|
|
2361
2618
|
columnCount,
|
|
2362
2619
|
columnWidth,
|
|
2363
2620
|
rowCount
|
|
@@ -2408,11 +2665,12 @@ function VirtualGridInner(props, ref) {
|
|
|
2408
2665
|
range,
|
|
2409
2666
|
columnCount,
|
|
2410
2667
|
columnWidth,
|
|
2411
|
-
rowCount
|
|
2668
|
+
rowCount,
|
|
2669
|
+
kernel
|
|
2412
2670
|
} = useVirtualGrid({
|
|
2413
2671
|
count: data.length,
|
|
2414
|
-
columns
|
|
2415
|
-
|
|
2672
|
+
columns,
|
|
2673
|
+
minColumnWidth,
|
|
2416
2674
|
rowHeight: itemHeight,
|
|
2417
2675
|
gap: { x: gapX, y: gapY },
|
|
2418
2676
|
overscan,
|
|
@@ -2420,12 +2678,16 @@ function VirtualGridInner(props, ref) {
|
|
|
2420
2678
|
getItemKey: getItemKey ? (index) => getItemKey(index, data) : void 0,
|
|
2421
2679
|
containerRef
|
|
2422
2680
|
});
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2681
|
+
react.useEffect(() => {
|
|
2682
|
+
if (virtualItems.length === 0) return;
|
|
2683
|
+
onVisibleRangeChange?.(range);
|
|
2684
|
+
onItemsRendered?.({
|
|
2685
|
+
overscanStartIndex: range.overscanStartIndex,
|
|
2686
|
+
overscanEndIndex: range.overscanEndIndex,
|
|
2687
|
+
visibleStartIndex: range.startIndex,
|
|
2688
|
+
visibleEndIndex: range.endIndex
|
|
2689
|
+
});
|
|
2690
|
+
}, [range.startIndex, range.endIndex, range.overscanStartIndex, range.overscanEndIndex]);
|
|
2429
2691
|
react.useImperativeHandle(ref, () => ({
|
|
2430
2692
|
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
2431
2693
|
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
@@ -2465,10 +2727,10 @@ function VirtualGridInner(props, ref) {
|
|
|
2465
2727
|
}), [totalSize, innerStyle]);
|
|
2466
2728
|
const createRenderProps = react.useCallback(
|
|
2467
2729
|
(index, virtualItem) => {
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
2730
|
+
if (index >= data.length) {
|
|
2731
|
+
throw new Error(`[Scrollex] Item at index ${index} is out of range`);
|
|
2471
2732
|
}
|
|
2733
|
+
const item = data[index];
|
|
2472
2734
|
const columnIndex = virtualItem.lane;
|
|
2473
2735
|
const rowIndex = Math.floor(index / columnCount);
|
|
2474
2736
|
const left = columnIndex * (columnWidth + gapX);
|
|
@@ -2499,9 +2761,11 @@ function VirtualGridInner(props, ref) {
|
|
|
2499
2761
|
},
|
|
2500
2762
|
[data, columnCount, columnWidth, gapX, itemStyle, range, isScrolling]
|
|
2501
2763
|
);
|
|
2764
|
+
useKernelPlugins(kernel, plugins);
|
|
2765
|
+
useLoadMoreWhenShort(containerRef, { count: data.length, totalSize, hasMore, isLoading, threshold, onLoadMore });
|
|
2502
2766
|
const contextValue = react.useMemo(() => ({
|
|
2503
|
-
kernel
|
|
2504
|
-
}), []);
|
|
2767
|
+
kernel
|
|
2768
|
+
}), [kernel]);
|
|
2505
2769
|
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2506
2770
|
"div",
|
|
2507
2771
|
{
|
|
@@ -2521,8 +2785,7 @@ function VirtualGridInner(props, ref) {
|
|
|
2521
2785
|
children: [
|
|
2522
2786
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2523
2787
|
const index = virtualItem.index;
|
|
2524
|
-
|
|
2525
|
-
if (item === void 0) return null;
|
|
2788
|
+
if (index >= data.length) return null;
|
|
2526
2789
|
const rowIndex = Math.floor(index / columnCount);
|
|
2527
2790
|
const columnIndex = virtualItem.lane;
|
|
2528
2791
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -2548,11 +2811,16 @@ function useVirtualMasonry(options) {
|
|
|
2548
2811
|
const {
|
|
2549
2812
|
count,
|
|
2550
2813
|
columns,
|
|
2814
|
+
minColumnWidth = 200,
|
|
2551
2815
|
getItemHeight,
|
|
2552
2816
|
gap = 0,
|
|
2553
2817
|
overscan = 5,
|
|
2818
|
+
initialOffset = 0,
|
|
2819
|
+
getItemKey = (index) => index,
|
|
2554
2820
|
containerRef
|
|
2555
2821
|
} = options;
|
|
2822
|
+
const gapX = typeof gap === "number" ? gap : gap.x;
|
|
2823
|
+
const gapY = typeof gap === "number" ? gap : gap.y;
|
|
2556
2824
|
const kernelRef = react.useRef(
|
|
2557
2825
|
createKernel({
|
|
2558
2826
|
itemCount: count,
|
|
@@ -2570,42 +2838,57 @@ function useVirtualMasonry(options) {
|
|
|
2570
2838
|
overscanStartIndex: 0,
|
|
2571
2839
|
overscanEndIndex: 0
|
|
2572
2840
|
});
|
|
2841
|
+
const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? normalizeColumnCount(columns) : 1);
|
|
2573
2842
|
const [columnWidth, setColumnWidth] = react.useState(0);
|
|
2574
2843
|
const [columnHeights, setColumnHeights] = react.useState([]);
|
|
2575
2844
|
const [totalSize, setTotalSize] = react.useState(0);
|
|
2576
2845
|
const layoutCacheRef = react.useRef(/* @__PURE__ */ new Map());
|
|
2846
|
+
const getItemHeightRef = react.useRef(getItemHeight);
|
|
2847
|
+
getItemHeightRef.current = getItemHeight;
|
|
2848
|
+
const getItemKeyRef = react.useRef(getItemKey);
|
|
2849
|
+
getItemKeyRef.current = getItemKey;
|
|
2850
|
+
const initialOffsetAppliedRef = react.useRef(false);
|
|
2577
2851
|
const kernel = kernelRef.current;
|
|
2578
2852
|
const calculateLayout = react.useCallback((containerWidth) => {
|
|
2853
|
+
kernel.configure({ itemCount: count });
|
|
2579
2854
|
if (containerWidth === 0 || count === 0) {
|
|
2580
2855
|
setVirtualItems([]);
|
|
2581
2856
|
setColumnHeights([]);
|
|
2582
2857
|
setTotalSize(0);
|
|
2583
2858
|
return;
|
|
2584
2859
|
}
|
|
2585
|
-
const
|
|
2860
|
+
const columnTotal = columns === "auto" ? autoColumnCount(containerWidth, minColumnWidth, gapX) : normalizeColumnCount(columns);
|
|
2861
|
+
const newColumnWidth = (containerWidth - (columnTotal - 1) * gapX) / columnTotal;
|
|
2862
|
+
setColumnCount(columnTotal);
|
|
2586
2863
|
setColumnWidth(newColumnWidth);
|
|
2587
|
-
const heights = new Array(
|
|
2864
|
+
const heights = new Array(columnTotal).fill(0);
|
|
2865
|
+
const itemsInColumn = new Array(columnTotal).fill(0);
|
|
2588
2866
|
const cache = /* @__PURE__ */ new Map();
|
|
2589
2867
|
for (let i = 0; i < count; i++) {
|
|
2590
2868
|
let shortestCol = 0;
|
|
2591
|
-
for (let j = 1; j <
|
|
2869
|
+
for (let j = 1; j < columnTotal; j++) {
|
|
2592
2870
|
if ((heights[j] ?? 0) < (heights[shortestCol] ?? 0)) {
|
|
2593
2871
|
shortestCol = j;
|
|
2594
2872
|
}
|
|
2595
2873
|
}
|
|
2596
|
-
const
|
|
2874
|
+
const measured = getItemHeightRef.current(i, newColumnWidth);
|
|
2875
|
+
const itemHeight = Number.isFinite(measured) && measured > 0 ? measured : 0;
|
|
2597
2876
|
const top = heights[shortestCol] ?? 0;
|
|
2598
2877
|
cache.set(i, { top, lane: shortestCol, height: itemHeight });
|
|
2599
|
-
heights[shortestCol] = top + itemHeight +
|
|
2878
|
+
heights[shortestCol] = top + itemHeight + gapY;
|
|
2879
|
+
itemsInColumn[shortestCol] = (itemsInColumn[shortestCol] ?? 0) + 1;
|
|
2600
2880
|
}
|
|
2601
2881
|
layoutCacheRef.current = cache;
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2882
|
+
const extents = heights.map((height, column) => (itemsInColumn[column] ?? 0) > 0 ? height - gapY : 0);
|
|
2883
|
+
setColumnHeights(extents);
|
|
2884
|
+
const total = Math.max(...extents);
|
|
2885
|
+
setTotalSize(total);
|
|
2886
|
+
kernel.configure({ estimatedItemHeight: total / count });
|
|
2887
|
+
}, [kernel, count, columns, minColumnWidth, gapX, gapY]);
|
|
2605
2888
|
const getVisibleItems = react.useCallback((scrollTop, viewportHeight) => {
|
|
2606
2889
|
const cache = layoutCacheRef.current;
|
|
2607
2890
|
const items = [];
|
|
2608
|
-
const overscanAmount = overscan * 100;
|
|
2891
|
+
const overscanAmount = normalizeOverscan(overscan) * 100;
|
|
2609
2892
|
const viewStart = Math.max(0, scrollTop - overscanAmount);
|
|
2610
2893
|
const viewEnd = scrollTop + viewportHeight + overscanAmount;
|
|
2611
2894
|
for (let i = 0; i < count; i++) {
|
|
@@ -2615,7 +2898,7 @@ function useVirtualMasonry(options) {
|
|
|
2615
2898
|
if (itemEnd >= viewStart && layout.top <= viewEnd) {
|
|
2616
2899
|
items.push({
|
|
2617
2900
|
index: i,
|
|
2618
|
-
key: i,
|
|
2901
|
+
key: getItemKeyRef.current(i),
|
|
2619
2902
|
start: layout.top,
|
|
2620
2903
|
end: itemEnd,
|
|
2621
2904
|
size: layout.height,
|
|
@@ -2625,6 +2908,11 @@ function useVirtualMasonry(options) {
|
|
|
2625
2908
|
}
|
|
2626
2909
|
return items;
|
|
2627
2910
|
}, [count, overscan]);
|
|
2911
|
+
const updateVisibleItems = react.useCallback((scrollTop, viewportHeight) => {
|
|
2912
|
+
const items = getVisibleItems(scrollTop, viewportHeight);
|
|
2913
|
+
setVirtualItems(items);
|
|
2914
|
+
setRange(getItemsRange(items, scrollTop, viewportHeight));
|
|
2915
|
+
}, [getVisibleItems]);
|
|
2628
2916
|
react.useEffect(() => {
|
|
2629
2917
|
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
2630
2918
|
setIsScrolling(true);
|
|
@@ -2642,16 +2930,23 @@ function useVirtualMasonry(options) {
|
|
|
2642
2930
|
const container = containerRef.current;
|
|
2643
2931
|
if (!container) return;
|
|
2644
2932
|
kernel.attach(container);
|
|
2645
|
-
|
|
2933
|
+
return () => {
|
|
2934
|
+
kernel.detach();
|
|
2935
|
+
};
|
|
2936
|
+
}, [containerRef, kernel]);
|
|
2937
|
+
react.useEffect(() => {
|
|
2938
|
+
const container = containerRef.current;
|
|
2939
|
+
if (!container) return;
|
|
2940
|
+
calculateLayout(getContentWidth(container));
|
|
2646
2941
|
const handleScroll = () => {
|
|
2647
2942
|
const scrollTop = container.scrollTop;
|
|
2648
2943
|
setScrollOffset(scrollTop);
|
|
2649
|
-
|
|
2944
|
+
updateVisibleItems(scrollTop, container.clientHeight);
|
|
2650
2945
|
};
|
|
2651
2946
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
2652
2947
|
for (const entry of entries) {
|
|
2653
2948
|
calculateLayout(entry.contentRect.width);
|
|
2654
|
-
|
|
2949
|
+
updateVisibleItems(container.scrollTop, entry.contentRect.height);
|
|
2655
2950
|
}
|
|
2656
2951
|
});
|
|
2657
2952
|
container.addEventListener("scroll", handleScroll, { passive: true });
|
|
@@ -2660,15 +2955,20 @@ function useVirtualMasonry(options) {
|
|
|
2660
2955
|
return () => {
|
|
2661
2956
|
container.removeEventListener("scroll", handleScroll);
|
|
2662
2957
|
resizeObserver.disconnect();
|
|
2663
|
-
kernel.detach();
|
|
2664
2958
|
};
|
|
2665
|
-
}, [containerRef, kernel, calculateLayout,
|
|
2959
|
+
}, [containerRef, kernel, calculateLayout, updateVisibleItems]);
|
|
2960
|
+
react.useEffect(() => {
|
|
2961
|
+
if (initialOffset > 0 && totalSize > 0 && !initialOffsetAppliedRef.current && containerRef.current) {
|
|
2962
|
+
initialOffsetAppliedRef.current = true;
|
|
2963
|
+
kernel.scrollTo(initialOffset);
|
|
2964
|
+
}
|
|
2965
|
+
}, [initialOffset, totalSize, kernel, containerRef]);
|
|
2666
2966
|
react.useEffect(() => {
|
|
2667
2967
|
const container = containerRef.current;
|
|
2668
2968
|
if (!container) return;
|
|
2669
|
-
calculateLayout(container
|
|
2670
|
-
|
|
2671
|
-
}, [count, containerRef, calculateLayout,
|
|
2969
|
+
calculateLayout(getContentWidth(container));
|
|
2970
|
+
updateVisibleItems(container.scrollTop, container.clientHeight);
|
|
2971
|
+
}, [count, containerRef, calculateLayout, updateVisibleItems]);
|
|
2672
2972
|
const scrollTo = react.useCallback(
|
|
2673
2973
|
(offset, scrollOptions) => {
|
|
2674
2974
|
kernel.scrollTo(offset, scrollOptions);
|
|
@@ -2676,13 +2976,41 @@ function useVirtualMasonry(options) {
|
|
|
2676
2976
|
[kernel]
|
|
2677
2977
|
);
|
|
2678
2978
|
const scrollToIndex = react.useCallback(
|
|
2679
|
-
(index,
|
|
2979
|
+
(index, scrollOptions = {}) => {
|
|
2680
2980
|
const layout = layoutCacheRef.current.get(index);
|
|
2681
|
-
|
|
2682
|
-
|
|
2981
|
+
const container = containerRef.current;
|
|
2982
|
+
if (!layout || !container) return;
|
|
2983
|
+
const { align = "auto", offset: additionalOffset = 0, ...options2 } = scrollOptions;
|
|
2984
|
+
const viewportHeight = container.clientHeight;
|
|
2985
|
+
const itemEnd = layout.top + layout.height;
|
|
2986
|
+
let targetOffset;
|
|
2987
|
+
switch (align) {
|
|
2988
|
+
case "start":
|
|
2989
|
+
targetOffset = layout.top;
|
|
2990
|
+
break;
|
|
2991
|
+
case "center":
|
|
2992
|
+
targetOffset = layout.top - viewportHeight / 2 + layout.height / 2;
|
|
2993
|
+
break;
|
|
2994
|
+
case "end":
|
|
2995
|
+
targetOffset = itemEnd - viewportHeight;
|
|
2996
|
+
break;
|
|
2997
|
+
case "auto":
|
|
2998
|
+
default: {
|
|
2999
|
+
const visibleStart = container.scrollTop;
|
|
3000
|
+
if (layout.top < visibleStart) {
|
|
3001
|
+
targetOffset = layout.top;
|
|
3002
|
+
} else if (itemEnd > visibleStart + viewportHeight) {
|
|
3003
|
+
targetOffset = itemEnd - viewportHeight;
|
|
3004
|
+
} else {
|
|
3005
|
+
return;
|
|
3006
|
+
}
|
|
3007
|
+
break;
|
|
3008
|
+
}
|
|
2683
3009
|
}
|
|
3010
|
+
const maxScroll = Math.max(0, container.scrollHeight - viewportHeight);
|
|
3011
|
+
kernel.scrollTo(Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll)), options2);
|
|
2684
3012
|
},
|
|
2685
|
-
[kernel]
|
|
3013
|
+
[kernel, containerRef]
|
|
2686
3014
|
);
|
|
2687
3015
|
const measureElement = react.useCallback(
|
|
2688
3016
|
(_index, _element) => {
|
|
@@ -2698,17 +3026,23 @@ function useVirtualMasonry(options) {
|
|
|
2698
3026
|
const invalidateMeasurement = react.useCallback((_index) => {
|
|
2699
3027
|
const container = containerRef.current;
|
|
2700
3028
|
if (container) {
|
|
2701
|
-
calculateLayout(container
|
|
3029
|
+
calculateLayout(getContentWidth(container));
|
|
3030
|
+
updateVisibleItems(container.scrollTop, container.clientHeight);
|
|
2702
3031
|
}
|
|
2703
|
-
}, [containerRef, calculateLayout]);
|
|
3032
|
+
}, [containerRef, calculateLayout, updateVisibleItems]);
|
|
2704
3033
|
const invalidateAllMeasurements = react.useCallback(() => {
|
|
2705
3034
|
const container = containerRef.current;
|
|
2706
3035
|
if (container) {
|
|
2707
|
-
calculateLayout(container
|
|
3036
|
+
calculateLayout(getContentWidth(container));
|
|
3037
|
+
updateVisibleItems(container.scrollTop, container.clientHeight);
|
|
2708
3038
|
}
|
|
2709
|
-
}, [containerRef, calculateLayout]);
|
|
3039
|
+
}, [containerRef, calculateLayout, updateVisibleItems]);
|
|
3040
|
+
const currentItems = react.useMemo(
|
|
3041
|
+
() => virtualItems.some((item) => item.index >= count) ? virtualItems.filter((item) => item.index < count) : virtualItems,
|
|
3042
|
+
[virtualItems, count]
|
|
3043
|
+
);
|
|
2710
3044
|
return {
|
|
2711
|
-
virtualItems,
|
|
3045
|
+
virtualItems: currentItems,
|
|
2712
3046
|
totalSize,
|
|
2713
3047
|
scrollOffset,
|
|
2714
3048
|
isScrolling,
|
|
@@ -2719,7 +3053,8 @@ function useVirtualMasonry(options) {
|
|
|
2719
3053
|
getMeasurement,
|
|
2720
3054
|
invalidateMeasurement,
|
|
2721
3055
|
invalidateAllMeasurements,
|
|
2722
|
-
|
|
3056
|
+
kernel,
|
|
3057
|
+
columnCount,
|
|
2723
3058
|
columnWidth,
|
|
2724
3059
|
columnHeights
|
|
2725
3060
|
};
|
|
@@ -2765,29 +3100,40 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2765
3100
|
scrollTo,
|
|
2766
3101
|
scrollToIndex,
|
|
2767
3102
|
isScrolling,
|
|
3103
|
+
scrollOffset,
|
|
2768
3104
|
range,
|
|
2769
3105
|
columnCount,
|
|
2770
3106
|
columnWidth,
|
|
2771
|
-
columnHeights
|
|
3107
|
+
columnHeights,
|
|
3108
|
+
invalidateAllMeasurements,
|
|
3109
|
+
kernel
|
|
2772
3110
|
} = useVirtualMasonry({
|
|
2773
3111
|
count: data.length,
|
|
2774
|
-
columns
|
|
2775
|
-
|
|
3112
|
+
columns,
|
|
3113
|
+
minColumnWidth,
|
|
2776
3114
|
getItemHeight: (index, colWidth) => {
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
return getItemHeight(item, colWidth);
|
|
3115
|
+
if (index >= data.length) return 100;
|
|
3116
|
+
return getItemHeight(data[index], colWidth);
|
|
2780
3117
|
},
|
|
2781
|
-
gap
|
|
3118
|
+
gap,
|
|
2782
3119
|
overscan,
|
|
3120
|
+
initialOffset: initialScrollOffset,
|
|
3121
|
+
getItemKey: getItemKey ? (index) => getItemKey(index, data) : void 0,
|
|
2783
3122
|
containerRef
|
|
2784
3123
|
});
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
3124
|
+
react.useEffect(() => {
|
|
3125
|
+
invalidateAllMeasurements();
|
|
3126
|
+
}, [data, invalidateAllMeasurements]);
|
|
3127
|
+
react.useEffect(() => {
|
|
3128
|
+
if (virtualItems.length === 0) return;
|
|
3129
|
+
onVisibleRangeChange?.(range);
|
|
3130
|
+
onItemsRendered?.({
|
|
3131
|
+
overscanStartIndex: range.overscanStartIndex,
|
|
3132
|
+
overscanEndIndex: range.overscanEndIndex,
|
|
3133
|
+
visibleStartIndex: range.startIndex,
|
|
3134
|
+
visibleEndIndex: range.endIndex
|
|
3135
|
+
});
|
|
3136
|
+
}, [range.startIndex, range.endIndex, range.overscanStartIndex, range.overscanEndIndex]);
|
|
2791
3137
|
react.useImperativeHandle(ref, () => ({
|
|
2792
3138
|
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
2793
3139
|
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
@@ -2827,12 +3173,13 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2827
3173
|
}), [totalSize, innerStyle]);
|
|
2828
3174
|
const createRenderProps = react.useCallback(
|
|
2829
3175
|
(index, virtualItem) => {
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
3176
|
+
if (index >= data.length) {
|
|
3177
|
+
throw new Error(`[Scrollex] Item at index ${index} is out of range`);
|
|
2833
3178
|
}
|
|
3179
|
+
const item = data[index];
|
|
2834
3180
|
const columnIndex = virtualItem.lane;
|
|
2835
3181
|
const left = columnIndex * (columnWidth + gapValue);
|
|
3182
|
+
const viewportHeight = kernel.getViewport().height;
|
|
2836
3183
|
const itemStyles = {
|
|
2837
3184
|
position: "absolute",
|
|
2838
3185
|
top: 0,
|
|
@@ -2851,8 +3198,7 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2851
3198
|
measureRef: () => {
|
|
2852
3199
|
},
|
|
2853
3200
|
// Masonry uses calculated heights
|
|
2854
|
-
isVisible:
|
|
2855
|
-
// All virtual items are considered visible
|
|
3201
|
+
isVisible: virtualItem.end > scrollOffset && virtualItem.start < scrollOffset + viewportHeight,
|
|
2856
3202
|
isScrolling,
|
|
2857
3203
|
columnIndex,
|
|
2858
3204
|
rowIndex: 0,
|
|
@@ -2861,11 +3207,13 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2861
3207
|
width: columnWidth
|
|
2862
3208
|
};
|
|
2863
3209
|
},
|
|
2864
|
-
[data, columnWidth, gapValue, itemStyle, isScrolling]
|
|
3210
|
+
[data, columnWidth, gapValue, itemStyle, isScrolling, scrollOffset, kernel]
|
|
2865
3211
|
);
|
|
3212
|
+
useKernelPlugins(kernel, plugins);
|
|
3213
|
+
useLoadMoreWhenShort(containerRef, { count: data.length, totalSize, hasMore, isLoading, threshold, onLoadMore });
|
|
2866
3214
|
const contextValue = react.useMemo(() => ({
|
|
2867
|
-
kernel
|
|
2868
|
-
}), []);
|
|
3215
|
+
kernel
|
|
3216
|
+
}), [kernel]);
|
|
2869
3217
|
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2870
3218
|
"div",
|
|
2871
3219
|
{
|
|
@@ -2884,8 +3232,7 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2884
3232
|
children: [
|
|
2885
3233
|
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2886
3234
|
const index = virtualItem.index;
|
|
2887
|
-
|
|
2888
|
-
if (item === void 0) return null;
|
|
3235
|
+
if (index >= data.length) return null;
|
|
2889
3236
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2890
3237
|
"div",
|
|
2891
3238
|
{
|
|
@@ -2904,38 +3251,52 @@ function VirtualMasonryInner(props, ref) {
|
|
|
2904
3251
|
) });
|
|
2905
3252
|
}
|
|
2906
3253
|
var VirtualMasonry = react.forwardRef(VirtualMasonryInner);
|
|
3254
|
+
function boxOf(entry) {
|
|
3255
|
+
const borderBox = entry.borderBoxSize?.[0];
|
|
3256
|
+
if (borderBox && (borderBox.inlineSize > 0 || borderBox.blockSize > 0)) {
|
|
3257
|
+
return { width: borderBox.inlineSize, height: borderBox.blockSize };
|
|
3258
|
+
}
|
|
3259
|
+
return { width: entry.contentRect.width, height: entry.contentRect.height };
|
|
3260
|
+
}
|
|
2907
3261
|
function useMeasure(options = {}) {
|
|
2908
3262
|
const { onResize, debounce: debounce2 = 0 } = options;
|
|
2909
3263
|
const elementRef = react.useRef(null);
|
|
2910
3264
|
const observerRef = react.useRef(null);
|
|
2911
|
-
const
|
|
3265
|
+
const [dimensions, setDimensions] = react.useState({ width: 0, height: 0 });
|
|
2912
3266
|
const debounceTimerRef = react.useRef(null);
|
|
3267
|
+
const onResizeRef = react.useRef(onResize);
|
|
3268
|
+
onResizeRef.current = onResize;
|
|
2913
3269
|
const handleResize = react.useCallback(
|
|
2914
3270
|
(width, height) => {
|
|
2915
|
-
|
|
2916
|
-
|
|
3271
|
+
setDimensions(
|
|
3272
|
+
(previous) => previous.width === width && previous.height === height ? previous : { width, height }
|
|
3273
|
+
);
|
|
3274
|
+
const onResize2 = onResizeRef.current;
|
|
3275
|
+
if (onResize2) {
|
|
2917
3276
|
if (debounce2 > 0) {
|
|
2918
3277
|
if (debounceTimerRef.current) {
|
|
2919
3278
|
clearTimeout(debounceTimerRef.current);
|
|
2920
3279
|
}
|
|
2921
3280
|
debounceTimerRef.current = setTimeout(() => {
|
|
2922
|
-
|
|
3281
|
+
onResize2(width, height);
|
|
2923
3282
|
}, debounce2);
|
|
2924
3283
|
} else {
|
|
2925
|
-
|
|
3284
|
+
onResize2(width, height);
|
|
2926
3285
|
}
|
|
2927
3286
|
}
|
|
2928
3287
|
},
|
|
2929
|
-
[
|
|
3288
|
+
[debounce2]
|
|
2930
3289
|
);
|
|
2931
3290
|
react.useEffect(() => {
|
|
2932
3291
|
observerRef.current = new ResizeObserver((entries) => {
|
|
2933
3292
|
for (const entry of entries) {
|
|
2934
|
-
const { width, height } = entry
|
|
3293
|
+
const { width, height } = boxOf(entry);
|
|
2935
3294
|
handleResize(width, height);
|
|
2936
3295
|
}
|
|
2937
3296
|
});
|
|
2938
3297
|
if (elementRef.current) {
|
|
3298
|
+
const rect = elementRef.current.getBoundingClientRect();
|
|
3299
|
+
handleResize(rect.width, rect.height);
|
|
2939
3300
|
observerRef.current.observe(elementRef.current);
|
|
2940
3301
|
}
|
|
2941
3302
|
return () => {
|
|
@@ -2958,12 +3319,13 @@ function useMeasure(options = {}) {
|
|
|
2958
3319
|
}, [handleResize]);
|
|
2959
3320
|
return {
|
|
2960
3321
|
measureRef,
|
|
2961
|
-
width:
|
|
2962
|
-
height:
|
|
3322
|
+
width: dimensions.width,
|
|
3323
|
+
height: dimensions.height
|
|
2963
3324
|
};
|
|
2964
3325
|
}
|
|
2965
3326
|
function useMeasureMany() {
|
|
2966
3327
|
const elementsRef = react.useRef(/* @__PURE__ */ new Map());
|
|
3328
|
+
const keysRef = react.useRef(/* @__PURE__ */ new WeakMap());
|
|
2967
3329
|
const observerRef = react.useRef(null);
|
|
2968
3330
|
const measurementsRef = react.useRef(/* @__PURE__ */ new Map());
|
|
2969
3331
|
const callbackRef = react.useRef(null);
|
|
@@ -2971,14 +3333,17 @@ function useMeasureMany() {
|
|
|
2971
3333
|
observerRef.current = new ResizeObserver((entries) => {
|
|
2972
3334
|
for (const entry of entries) {
|
|
2973
3335
|
const element = entry.target;
|
|
2974
|
-
const key = element.dataset["measureKey"];
|
|
3336
|
+
const key = element.dataset["measureKey"] !== void 0 ? keysRef.current.get(element) : void 0;
|
|
2975
3337
|
if (key !== void 0) {
|
|
2976
|
-
const { width, height } = entry
|
|
3338
|
+
const { width, height } = boxOf(entry);
|
|
2977
3339
|
measurementsRef.current.set(key, { width, height });
|
|
2978
3340
|
callbackRef.current?.(key, width, height);
|
|
2979
3341
|
}
|
|
2980
3342
|
}
|
|
2981
3343
|
});
|
|
3344
|
+
for (const element of elementsRef.current.values()) {
|
|
3345
|
+
observerRef.current.observe(element);
|
|
3346
|
+
}
|
|
2982
3347
|
return () => {
|
|
2983
3348
|
observerRef.current?.disconnect();
|
|
2984
3349
|
};
|
|
@@ -2991,9 +3356,11 @@ function useMeasureMany() {
|
|
|
2991
3356
|
if (oldElement && oldElement !== element) {
|
|
2992
3357
|
observer?.unobserve(oldElement);
|
|
2993
3358
|
elements.delete(key);
|
|
3359
|
+
keysRef.current.delete(oldElement);
|
|
2994
3360
|
}
|
|
2995
3361
|
if (element) {
|
|
2996
3362
|
element.dataset["measureKey"] = String(key);
|
|
3363
|
+
keysRef.current.set(element, key);
|
|
2997
3364
|
elements.set(key, element);
|
|
2998
3365
|
observer?.observe(element);
|
|
2999
3366
|
const rect = element.getBoundingClientRect();
|
|
@@ -3072,7 +3439,7 @@ function debounce(fn, wait) {
|
|
|
3072
3439
|
// src/plugins/core/auto-measurer.ts
|
|
3073
3440
|
function autoMeasurerPlugin(options = {}) {
|
|
3074
3441
|
const {
|
|
3075
|
-
estimatedItemHeight
|
|
3442
|
+
estimatedItemHeight,
|
|
3076
3443
|
measureOnResize = true,
|
|
3077
3444
|
debounceMs = 100
|
|
3078
3445
|
} = options;
|
|
@@ -3082,6 +3449,9 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3082
3449
|
const elementsByIndex = /* @__PURE__ */ new Map();
|
|
3083
3450
|
let measureQueue = /* @__PURE__ */ new Set();
|
|
3084
3451
|
let flushScheduled = false;
|
|
3452
|
+
function sizeAlongAxis(rect) {
|
|
3453
|
+
return kernel?.getOptions().direction === "horizontal" ? rect.width : rect.height;
|
|
3454
|
+
}
|
|
3085
3455
|
const debouncedFlush = debounce(() => {
|
|
3086
3456
|
flush();
|
|
3087
3457
|
}, debounceMs);
|
|
@@ -3099,7 +3469,7 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3099
3469
|
const element = ref?.deref();
|
|
3100
3470
|
if (element) {
|
|
3101
3471
|
const rect = element.getBoundingClientRect();
|
|
3102
|
-
measurements.push({ index, height: rect
|
|
3472
|
+
measurements.push({ index, height: sizeAlongAxis(rect) });
|
|
3103
3473
|
}
|
|
3104
3474
|
}
|
|
3105
3475
|
for (const { index, height } of measurements) {
|
|
@@ -3130,7 +3500,7 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3130
3500
|
const api = {
|
|
3131
3501
|
measureElement(index, element) {
|
|
3132
3502
|
const rect = element.getBoundingClientRect();
|
|
3133
|
-
const height = rect
|
|
3503
|
+
const height = sizeAlongAxis(rect);
|
|
3134
3504
|
if (height > 0 && kernel) {
|
|
3135
3505
|
kernel.measureItem(index, height);
|
|
3136
3506
|
}
|
|
@@ -3158,24 +3528,27 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3158
3528
|
return cache;
|
|
3159
3529
|
},
|
|
3160
3530
|
observe(index, element) {
|
|
3161
|
-
if (!resizeObserver) return;
|
|
3162
3531
|
const existingRef = elementsByIndex.get(index);
|
|
3163
3532
|
const existingElement = existingRef?.deref();
|
|
3164
3533
|
if (existingElement && existingElement !== element) {
|
|
3165
|
-
resizeObserver
|
|
3534
|
+
resizeObserver?.unobserve(existingElement);
|
|
3166
3535
|
observedElements.delete(existingElement);
|
|
3167
3536
|
}
|
|
3537
|
+
const previousIndex = observedElements.get(element);
|
|
3538
|
+
if (previousIndex !== void 0 && previousIndex !== index) {
|
|
3539
|
+
elementsByIndex.delete(previousIndex);
|
|
3540
|
+
measureQueue.delete(previousIndex);
|
|
3541
|
+
}
|
|
3168
3542
|
observedElements.set(element, index);
|
|
3169
|
-
elementsByIndex.set(index, new WeakRef(element));
|
|
3170
|
-
resizeObserver
|
|
3543
|
+
elementsByIndex.set(index, typeof WeakRef === "function" ? new WeakRef(element) : { deref: () => element });
|
|
3544
|
+
resizeObserver?.observe(element);
|
|
3171
3545
|
measureQueue.add(index);
|
|
3172
3546
|
scheduleFlush();
|
|
3173
3547
|
},
|
|
3174
3548
|
unobserve(element) {
|
|
3175
|
-
if (!resizeObserver) return;
|
|
3176
3549
|
const index = observedElements.get(element);
|
|
3177
3550
|
if (index !== void 0) {
|
|
3178
|
-
resizeObserver
|
|
3551
|
+
resizeObserver?.unobserve(element);
|
|
3179
3552
|
observedElements.delete(element);
|
|
3180
3553
|
elementsByIndex.delete(index);
|
|
3181
3554
|
measureQueue.delete(index);
|
|
@@ -3188,7 +3561,9 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3188
3561
|
type: "core",
|
|
3189
3562
|
install(k) {
|
|
3190
3563
|
kernel = k;
|
|
3191
|
-
|
|
3564
|
+
if (estimatedItemHeight !== void 0) {
|
|
3565
|
+
kernel.configure({ estimatedItemHeight });
|
|
3566
|
+
}
|
|
3192
3567
|
if (measureOnResize && typeof ResizeObserver !== "undefined") {
|
|
3193
3568
|
resizeObserver = new ResizeObserver(handleResize);
|
|
3194
3569
|
}
|
|
@@ -3209,6 +3584,10 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
3209
3584
|
}
|
|
3210
3585
|
|
|
3211
3586
|
// src/plugins/core/infinite-loader.ts
|
|
3587
|
+
function initialLoaderState(direction) {
|
|
3588
|
+
return { loading: false, loadGeneration: 0, hasMoreForward: true, hasMoreBackward: direction === "backward" || direction === "both" };
|
|
3589
|
+
}
|
|
3590
|
+
var sharedLoaderStates = /* @__PURE__ */ new WeakMap();
|
|
3212
3591
|
function infiniteLoaderPlugin(options) {
|
|
3213
3592
|
const {
|
|
3214
3593
|
threshold = 200,
|
|
@@ -3217,44 +3596,61 @@ function infiniteLoaderPlugin(options) {
|
|
|
3217
3596
|
onLoadMore
|
|
3218
3597
|
} = options;
|
|
3219
3598
|
let kernel = null;
|
|
3220
|
-
let
|
|
3221
|
-
let hasMoreForward = true;
|
|
3222
|
-
let hasMoreBackward = direction === "backward" || direction === "both";
|
|
3599
|
+
let state = initialLoaderState(direction);
|
|
3223
3600
|
let lastCheckTime = 0;
|
|
3224
3601
|
const checkDebounceMs = 100;
|
|
3602
|
+
let pendingEvent = null;
|
|
3603
|
+
let trailingTimer = null;
|
|
3225
3604
|
function checkLoadMore(event) {
|
|
3226
|
-
if (loading || !kernel) return;
|
|
3605
|
+
if (state.loading || !kernel) return;
|
|
3227
3606
|
const now = performance.now();
|
|
3228
|
-
if (now - lastCheckTime < checkDebounceMs)
|
|
3229
|
-
|
|
3607
|
+
if (now - lastCheckTime < checkDebounceMs) {
|
|
3608
|
+
pendingEvent = event;
|
|
3609
|
+
if (trailingTimer === null) {
|
|
3610
|
+
trailingTimer = setTimeout(() => {
|
|
3611
|
+
trailingTimer = null;
|
|
3612
|
+
const latest = pendingEvent;
|
|
3613
|
+
pendingEvent = null;
|
|
3614
|
+
if (latest) runCheck(latest);
|
|
3615
|
+
}, Math.max(0, checkDebounceMs - (now - lastCheckTime)));
|
|
3616
|
+
}
|
|
3617
|
+
return;
|
|
3618
|
+
}
|
|
3619
|
+
runCheck(event);
|
|
3620
|
+
}
|
|
3621
|
+
function runCheck(event) {
|
|
3622
|
+
if (state.loading || !kernel) return;
|
|
3623
|
+
lastCheckTime = performance.now();
|
|
3230
3624
|
const viewport = kernel.getViewport();
|
|
3231
3625
|
const totalSize = kernel.getTotalSize();
|
|
3232
|
-
const
|
|
3233
|
-
|
|
3234
|
-
|
|
3626
|
+
const horizontal = kernel.getOptions().direction === "horizontal";
|
|
3627
|
+
const scrollTop = horizontal ? event.scrollLeft : event.scrollTop;
|
|
3628
|
+
const viewportSize = horizontal ? viewport.width : viewport.height;
|
|
3629
|
+
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
3630
|
+
const distanceFromBottom = totalSize - scrollTop - viewportSize;
|
|
3235
3631
|
if (distanceFromBottom < threshold) {
|
|
3236
3632
|
triggerLoad("forward");
|
|
3237
3633
|
return;
|
|
3238
3634
|
}
|
|
3239
3635
|
}
|
|
3240
|
-
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
3636
|
+
if ((direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
3241
3637
|
if (scrollTop < threshold) {
|
|
3242
3638
|
triggerLoad("backward");
|
|
3243
3639
|
return;
|
|
3244
3640
|
}
|
|
3245
3641
|
}
|
|
3246
3642
|
}
|
|
3247
|
-
function checkLoadMoreByRange(range) {
|
|
3248
|
-
if (loading || !kernel) return;
|
|
3643
|
+
function checkLoadMoreByRange(range, forwardOnly = false) {
|
|
3644
|
+
if (state.loading || !kernel) return;
|
|
3249
3645
|
const itemCount = kernel.getItemCount();
|
|
3250
|
-
if ((direction === "forward" || direction === "both") && hasMoreForward) {
|
|
3646
|
+
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
3251
3647
|
const itemsFromEnd = itemCount - 1 - range.endIndex;
|
|
3252
3648
|
if (itemsFromEnd <= thresholdItems) {
|
|
3253
3649
|
triggerLoad("forward");
|
|
3254
3650
|
return;
|
|
3255
3651
|
}
|
|
3256
3652
|
}
|
|
3257
|
-
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
3653
|
+
if (!forwardOnly && (direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
3258
3654
|
if (range.startIndex <= thresholdItems) {
|
|
3259
3655
|
triggerLoad("backward");
|
|
3260
3656
|
return;
|
|
@@ -3262,10 +3658,11 @@ function infiniteLoaderPlugin(options) {
|
|
|
3262
3658
|
}
|
|
3263
3659
|
}
|
|
3264
3660
|
async function triggerLoad(loadDirection) {
|
|
3265
|
-
if (loading || !kernel) return;
|
|
3266
|
-
if (loadDirection === "forward" && !hasMoreForward) return;
|
|
3267
|
-
if (loadDirection === "backward" && !hasMoreBackward) return;
|
|
3268
|
-
loading = true;
|
|
3661
|
+
if (state.loading || !kernel) return;
|
|
3662
|
+
if (loadDirection === "forward" && !state.hasMoreForward) return;
|
|
3663
|
+
if (loadDirection === "backward" && !state.hasMoreBackward) return;
|
|
3664
|
+
state.loading = true;
|
|
3665
|
+
const generation = ++state.loadGeneration;
|
|
3269
3666
|
kernel.emit({
|
|
3270
3667
|
type: "load-more",
|
|
3271
3668
|
direction: loadDirection
|
|
@@ -3275,39 +3672,47 @@ function infiniteLoaderPlugin(options) {
|
|
|
3275
3672
|
} catch (error) {
|
|
3276
3673
|
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
3277
3674
|
} finally {
|
|
3278
|
-
|
|
3675
|
+
if (generation === state.loadGeneration) {
|
|
3676
|
+
state.loading = false;
|
|
3677
|
+
}
|
|
3279
3678
|
}
|
|
3280
3679
|
}
|
|
3281
3680
|
const api = {
|
|
3282
3681
|
isLoading() {
|
|
3283
|
-
return loading;
|
|
3682
|
+
return state.loading;
|
|
3284
3683
|
},
|
|
3285
3684
|
setLoading(l) {
|
|
3286
|
-
loading = l;
|
|
3685
|
+
state.loading = l;
|
|
3287
3686
|
},
|
|
3288
3687
|
hasMore() {
|
|
3289
|
-
return hasMoreForward || hasMoreBackward;
|
|
3688
|
+
return state.hasMoreForward || state.hasMoreBackward;
|
|
3290
3689
|
},
|
|
3291
3690
|
setHasMore(hasMore) {
|
|
3292
|
-
hasMoreForward = hasMore;
|
|
3293
|
-
hasMoreBackward = hasMore;
|
|
3691
|
+
state.hasMoreForward = hasMore;
|
|
3692
|
+
state.hasMoreBackward = hasMore;
|
|
3294
3693
|
},
|
|
3295
3694
|
hasMoreForward() {
|
|
3296
|
-
return hasMoreForward;
|
|
3695
|
+
return state.hasMoreForward;
|
|
3297
3696
|
},
|
|
3298
3697
|
setHasMoreForward(hasMore) {
|
|
3299
|
-
hasMoreForward = hasMore;
|
|
3698
|
+
state.hasMoreForward = hasMore;
|
|
3300
3699
|
},
|
|
3301
3700
|
hasMoreBackward() {
|
|
3302
|
-
return hasMoreBackward;
|
|
3701
|
+
return state.hasMoreBackward;
|
|
3303
3702
|
},
|
|
3304
3703
|
setHasMoreBackward(hasMore) {
|
|
3305
|
-
hasMoreBackward = hasMore;
|
|
3704
|
+
state.hasMoreBackward = hasMore;
|
|
3306
3705
|
},
|
|
3307
3706
|
reset() {
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3707
|
+
state.loadGeneration++;
|
|
3708
|
+
state.loading = false;
|
|
3709
|
+
state.hasMoreForward = true;
|
|
3710
|
+
state.hasMoreBackward = direction === "backward" || direction === "both";
|
|
3711
|
+
if (trailingTimer !== null) {
|
|
3712
|
+
clearTimeout(trailingTimer);
|
|
3713
|
+
trailingTimer = null;
|
|
3714
|
+
}
|
|
3715
|
+
pendingEvent = null;
|
|
3311
3716
|
},
|
|
3312
3717
|
triggerLoad(loadDirection) {
|
|
3313
3718
|
triggerLoad(loadDirection);
|
|
@@ -3319,10 +3724,21 @@ function infiniteLoaderPlugin(options) {
|
|
|
3319
3724
|
type: "core",
|
|
3320
3725
|
install(k) {
|
|
3321
3726
|
kernel = k;
|
|
3727
|
+
const shared = sharedLoaderStates.get(k);
|
|
3728
|
+
if (shared) {
|
|
3729
|
+
state = shared;
|
|
3730
|
+
} else {
|
|
3731
|
+
sharedLoaderStates.set(k, state);
|
|
3732
|
+
checkLoadMoreByRange(k.getVisibleRange(), true);
|
|
3733
|
+
}
|
|
3322
3734
|
},
|
|
3323
3735
|
uninstall() {
|
|
3736
|
+
if (trailingTimer !== null) {
|
|
3737
|
+
clearTimeout(trailingTimer);
|
|
3738
|
+
trailingTimer = null;
|
|
3739
|
+
}
|
|
3740
|
+
pendingEvent = null;
|
|
3324
3741
|
kernel = null;
|
|
3325
|
-
loading = false;
|
|
3326
3742
|
},
|
|
3327
3743
|
hooks: {
|
|
3328
3744
|
onScroll: checkLoadMore,
|
|
@@ -3343,14 +3759,30 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3343
3759
|
let kernel = null;
|
|
3344
3760
|
let animationId = null;
|
|
3345
3761
|
let animating = false;
|
|
3762
|
+
let animationGeneration = 0;
|
|
3763
|
+
function isHorizontal(k) {
|
|
3764
|
+
return k.getOptions().direction === "horizontal";
|
|
3765
|
+
}
|
|
3766
|
+
function getOffset(k) {
|
|
3767
|
+
const position = k.getScrollPosition();
|
|
3768
|
+
return isHorizontal(k) ? position.scrollLeft : position.scrollTop;
|
|
3769
|
+
}
|
|
3770
|
+
function setOffset(k, offset) {
|
|
3771
|
+
k.setScrollPosition(isHorizontal(k) ? { scrollLeft: offset } : { scrollTop: offset });
|
|
3772
|
+
}
|
|
3773
|
+
function getViewportSize(k) {
|
|
3774
|
+
const viewport = k.getViewport();
|
|
3775
|
+
return isHorizontal(k) ? viewport.width : viewport.height;
|
|
3776
|
+
}
|
|
3346
3777
|
function animateScroll(targetOffset, duration, easing) {
|
|
3347
3778
|
if (!kernel) return;
|
|
3348
3779
|
stopAnimation();
|
|
3780
|
+
const generation = animationGeneration;
|
|
3349
3781
|
const startTime = performance.now();
|
|
3350
|
-
const startOffset = kernel
|
|
3782
|
+
const startOffset = getOffset(kernel);
|
|
3351
3783
|
const distance = targetOffset - startOffset;
|
|
3352
|
-
if (distance === 0 || duration
|
|
3353
|
-
kernel
|
|
3784
|
+
if (distance === 0 || !(duration > 0)) {
|
|
3785
|
+
setOffset(kernel, targetOffset);
|
|
3354
3786
|
return;
|
|
3355
3787
|
}
|
|
3356
3788
|
animating = true;
|
|
@@ -3359,11 +3791,13 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3359
3791
|
animating = false;
|
|
3360
3792
|
return;
|
|
3361
3793
|
}
|
|
3794
|
+
if (generation !== animationGeneration) return;
|
|
3362
3795
|
const elapsed = currentTime - startTime;
|
|
3363
|
-
const progress = Math.min(elapsed / duration, 1);
|
|
3796
|
+
const progress = Math.max(0, Math.min(elapsed / duration, 1));
|
|
3364
3797
|
const easedProgress = easing(progress);
|
|
3365
3798
|
const currentOffset = startOffset + distance * easedProgress;
|
|
3366
|
-
kernel
|
|
3799
|
+
setOffset(kernel, currentOffset);
|
|
3800
|
+
if (generation !== animationGeneration) return;
|
|
3367
3801
|
if (progress < 1) {
|
|
3368
3802
|
animationId = raf(animate);
|
|
3369
3803
|
} else {
|
|
@@ -3374,6 +3808,7 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3374
3808
|
animationId = raf(animate);
|
|
3375
3809
|
}
|
|
3376
3810
|
function stopAnimation() {
|
|
3811
|
+
animationGeneration++;
|
|
3377
3812
|
if (animationId !== null) {
|
|
3378
3813
|
cancelRaf(animationId);
|
|
3379
3814
|
animationId = null;
|
|
@@ -3390,7 +3825,7 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3390
3825
|
} = options2;
|
|
3391
3826
|
if (behavior === "auto") {
|
|
3392
3827
|
stopAnimation();
|
|
3393
|
-
kernel
|
|
3828
|
+
setOffset(kernel, offset);
|
|
3394
3829
|
} else {
|
|
3395
3830
|
animateScroll(offset, duration, easing);
|
|
3396
3831
|
}
|
|
@@ -3404,35 +3839,34 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3404
3839
|
} = options2;
|
|
3405
3840
|
const itemOffset = kernel.getItemOffset(index);
|
|
3406
3841
|
const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
|
|
3407
|
-
const
|
|
3842
|
+
const viewportSize = getViewportSize(kernel);
|
|
3408
3843
|
let targetOffset;
|
|
3409
3844
|
switch (align) {
|
|
3410
3845
|
case "start":
|
|
3411
3846
|
targetOffset = itemOffset;
|
|
3412
3847
|
break;
|
|
3413
3848
|
case "center":
|
|
3414
|
-
targetOffset = itemOffset -
|
|
3849
|
+
targetOffset = itemOffset - viewportSize / 2 + itemSize / 2;
|
|
3415
3850
|
break;
|
|
3416
3851
|
case "end":
|
|
3417
|
-
targetOffset = itemOffset -
|
|
3852
|
+
targetOffset = itemOffset - viewportSize + itemSize;
|
|
3418
3853
|
break;
|
|
3419
3854
|
case "auto":
|
|
3420
3855
|
default: {
|
|
3421
|
-
const
|
|
3422
|
-
const
|
|
3423
|
-
const visibleEnd = visibleStart + viewport.height;
|
|
3856
|
+
const visibleStart = getOffset(kernel);
|
|
3857
|
+
const visibleEnd = visibleStart + viewportSize;
|
|
3424
3858
|
const itemEnd = itemOffset + itemSize;
|
|
3425
3859
|
if (itemOffset < visibleStart) {
|
|
3426
3860
|
targetOffset = itemOffset;
|
|
3427
3861
|
} else if (itemEnd > visibleEnd) {
|
|
3428
|
-
targetOffset = itemEnd -
|
|
3862
|
+
targetOffset = itemEnd - viewportSize;
|
|
3429
3863
|
} else {
|
|
3430
3864
|
return;
|
|
3431
3865
|
}
|
|
3432
3866
|
break;
|
|
3433
3867
|
}
|
|
3434
3868
|
}
|
|
3435
|
-
const maxScroll = Math.max(0, kernel.getTotalSize() -
|
|
3869
|
+
const maxScroll = Math.max(0, kernel.getTotalSize() - viewportSize);
|
|
3436
3870
|
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
3437
3871
|
api.scrollTo(targetOffset, scrollOptions);
|
|
3438
3872
|
},
|
|
@@ -3441,9 +3875,8 @@ function scrollControllerPlugin(options = {}) {
|
|
|
3441
3875
|
},
|
|
3442
3876
|
scrollToBottom(options2 = {}) {
|
|
3443
3877
|
if (!kernel) return;
|
|
3444
|
-
const viewport = kernel.getViewport();
|
|
3445
3878
|
const totalSize = kernel.getTotalSize();
|
|
3446
|
-
const maxScroll = Math.max(0, totalSize -
|
|
3879
|
+
const maxScroll = Math.max(0, totalSize - getViewportSize(kernel));
|
|
3447
3880
|
api.scrollTo(maxScroll, options2);
|
|
3448
3881
|
},
|
|
3449
3882
|
getScrollPosition() {
|