@oxog/scrollex 1.0.0
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 +68 -0
- package/LICENSE +21 -0
- package/README.md +328 -0
- package/dist/index.cjs +3494 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1084 -0
- package/dist/index.d.ts +1084 -0
- package/dist/index.js +3475 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/index.cjs +2195 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.cts +1137 -0
- package/dist/plugins/index.d.ts +1137 -0
- package/dist/plugins/index.js +2181 -0
- package/dist/plugins/index.js.map +1 -0
- package/package.json +95 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3494 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
|
|
6
|
+
// src/react/components/virtual-list.tsx
|
|
7
|
+
|
|
8
|
+
// src/kernel/event-bus.ts
|
|
9
|
+
var EventBus = class {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
12
|
+
this.onceHandlers = /* @__PURE__ */ new Map();
|
|
13
|
+
this.emitting = false;
|
|
14
|
+
this.pendingEvents = [];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Subscribe to an event type.
|
|
18
|
+
*
|
|
19
|
+
* @param eventType - Type of event to subscribe to
|
|
20
|
+
* @param handler - Handler function to call when event is emitted
|
|
21
|
+
* @returns Unsubscribe function
|
|
22
|
+
*/
|
|
23
|
+
on(eventType, handler) {
|
|
24
|
+
let handlers = this.handlers.get(eventType);
|
|
25
|
+
if (!handlers) {
|
|
26
|
+
handlers = /* @__PURE__ */ new Set();
|
|
27
|
+
this.handlers.set(eventType, handlers);
|
|
28
|
+
}
|
|
29
|
+
handlers.add(handler);
|
|
30
|
+
return () => {
|
|
31
|
+
this.off(eventType, handler);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Unsubscribe from an event type.
|
|
36
|
+
*
|
|
37
|
+
* @param eventType - Type of event to unsubscribe from
|
|
38
|
+
* @param handler - Handler function to remove
|
|
39
|
+
*/
|
|
40
|
+
off(eventType, handler) {
|
|
41
|
+
const handlers = this.handlers.get(eventType);
|
|
42
|
+
if (handlers) {
|
|
43
|
+
handlers.delete(handler);
|
|
44
|
+
if (handlers.size === 0) {
|
|
45
|
+
this.handlers.delete(eventType);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const onceHandlers = this.onceHandlers.get(eventType);
|
|
49
|
+
if (onceHandlers) {
|
|
50
|
+
onceHandlers.delete(handler);
|
|
51
|
+
if (onceHandlers.size === 0) {
|
|
52
|
+
this.onceHandlers.delete(eventType);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to an event type, but only fire once.
|
|
58
|
+
*
|
|
59
|
+
* @param eventType - Type of event to subscribe to
|
|
60
|
+
* @param handler - Handler function to call when event is emitted
|
|
61
|
+
* @returns Unsubscribe function
|
|
62
|
+
*/
|
|
63
|
+
once(eventType, handler) {
|
|
64
|
+
let onceHandlers = this.onceHandlers.get(eventType);
|
|
65
|
+
if (!onceHandlers) {
|
|
66
|
+
onceHandlers = /* @__PURE__ */ new Set();
|
|
67
|
+
this.onceHandlers.set(eventType, onceHandlers);
|
|
68
|
+
}
|
|
69
|
+
onceHandlers.add(handler);
|
|
70
|
+
return () => {
|
|
71
|
+
this.off(eventType, handler);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Emit an event to all subscribers.
|
|
76
|
+
*
|
|
77
|
+
* @param event - Event to emit
|
|
78
|
+
*/
|
|
79
|
+
emit(event) {
|
|
80
|
+
if (this.emitting) {
|
|
81
|
+
this.pendingEvents.push(event);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
this.emitting = true;
|
|
85
|
+
try {
|
|
86
|
+
this.dispatchEvent(event);
|
|
87
|
+
while (this.pendingEvents.length > 0) {
|
|
88
|
+
const pendingEvent = this.pendingEvents.shift();
|
|
89
|
+
if (pendingEvent) {
|
|
90
|
+
this.dispatchEvent(pendingEvent);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} finally {
|
|
94
|
+
this.emitting = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Dispatch a single event to handlers.
|
|
99
|
+
*/
|
|
100
|
+
dispatchEvent(event) {
|
|
101
|
+
const eventType = event.type;
|
|
102
|
+
const handlers = this.handlers.get(eventType);
|
|
103
|
+
if (handlers) {
|
|
104
|
+
for (const handler of handlers) {
|
|
105
|
+
try {
|
|
106
|
+
handler(event);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.error(`[Scrollex] Error in event handler for "${eventType}":`, error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const onceHandlers = this.onceHandlers.get(eventType);
|
|
113
|
+
if (onceHandlers) {
|
|
114
|
+
const handlersToCall = Array.from(onceHandlers);
|
|
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
|
+
}
|
|
123
|
+
if (onceHandlers.size === 0) {
|
|
124
|
+
this.onceHandlers.delete(eventType);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Check if there are any handlers for an event type.
|
|
130
|
+
*
|
|
131
|
+
* @param eventType - Type of event to check
|
|
132
|
+
* @returns Whether there are handlers
|
|
133
|
+
*/
|
|
134
|
+
hasHandlers(eventType) {
|
|
135
|
+
const handlers = this.handlers.get(eventType);
|
|
136
|
+
const onceHandlers = this.onceHandlers.get(eventType);
|
|
137
|
+
return (handlers?.size ?? 0) > 0 || (onceHandlers?.size ?? 0) > 0;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get the number of handlers for an event type.
|
|
141
|
+
*
|
|
142
|
+
* @param eventType - Type of event to check
|
|
143
|
+
* @returns Number of handlers
|
|
144
|
+
*/
|
|
145
|
+
handlerCount(eventType) {
|
|
146
|
+
const handlers = this.handlers.get(eventType);
|
|
147
|
+
const onceHandlers = this.onceHandlers.get(eventType);
|
|
148
|
+
return (handlers?.size ?? 0) + (onceHandlers?.size ?? 0);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Remove all handlers.
|
|
152
|
+
*/
|
|
153
|
+
clear() {
|
|
154
|
+
this.handlers.clear();
|
|
155
|
+
this.onceHandlers.clear();
|
|
156
|
+
this.pendingEvents = [];
|
|
157
|
+
this.emitting = false;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Remove all handlers for a specific event type.
|
|
161
|
+
*
|
|
162
|
+
* @param eventType - Type of event to clear handlers for
|
|
163
|
+
*/
|
|
164
|
+
clearEvent(eventType) {
|
|
165
|
+
this.handlers.delete(eventType);
|
|
166
|
+
this.onceHandlers.delete(eventType);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/utils/throttle.ts
|
|
171
|
+
function throttleRAF(fn) {
|
|
172
|
+
let rafId = null;
|
|
173
|
+
let lastArgs = null;
|
|
174
|
+
const throttled = function(...args) {
|
|
175
|
+
lastArgs = args;
|
|
176
|
+
if (rafId === null) {
|
|
177
|
+
rafId = requestAnimationFrame(() => {
|
|
178
|
+
rafId = null;
|
|
179
|
+
if (lastArgs) {
|
|
180
|
+
fn.apply(this, lastArgs);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
throttled.cancel = () => {
|
|
186
|
+
if (rafId !== null) {
|
|
187
|
+
cancelAnimationFrame(rafId);
|
|
188
|
+
rafId = null;
|
|
189
|
+
}
|
|
190
|
+
lastArgs = null;
|
|
191
|
+
};
|
|
192
|
+
return throttled;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/utils/scroll.ts
|
|
196
|
+
function getScrollPosition(element) {
|
|
197
|
+
return {
|
|
198
|
+
scrollTop: element.scrollTop,
|
|
199
|
+
scrollLeft: element.scrollLeft
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function setScrollPosition(element, position) {
|
|
203
|
+
if (position.scrollTop !== void 0) {
|
|
204
|
+
element.scrollTop = position.scrollTop;
|
|
205
|
+
}
|
|
206
|
+
if (position.scrollLeft !== void 0) {
|
|
207
|
+
element.scrollLeft = position.scrollLeft;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function getScrollDirection(current, previous) {
|
|
211
|
+
const deltaY = current.scrollTop - previous.scrollTop;
|
|
212
|
+
const deltaX = current.scrollLeft - previous.scrollLeft;
|
|
213
|
+
if (Math.abs(deltaY) > Math.abs(deltaX)) {
|
|
214
|
+
if (deltaY > 0) return "down";
|
|
215
|
+
if (deltaY < 0) return "up";
|
|
216
|
+
} else {
|
|
217
|
+
if (deltaX > 0) return "right";
|
|
218
|
+
if (deltaX < 0) return "left";
|
|
219
|
+
}
|
|
220
|
+
return "none";
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/utils/raf.ts
|
|
224
|
+
function raf(callback) {
|
|
225
|
+
if (typeof requestAnimationFrame !== "undefined") {
|
|
226
|
+
return requestAnimationFrame(callback);
|
|
227
|
+
}
|
|
228
|
+
return setTimeout(() => callback(Date.now()), 16);
|
|
229
|
+
}
|
|
230
|
+
function cancelRaf(id) {
|
|
231
|
+
if (typeof cancelAnimationFrame !== "undefined") {
|
|
232
|
+
cancelAnimationFrame(id);
|
|
233
|
+
} else {
|
|
234
|
+
clearTimeout(id);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/kernel/scroll-engine.ts
|
|
239
|
+
var easings = {
|
|
240
|
+
linear: (t) => t,
|
|
241
|
+
easeIn: (t) => t * t,
|
|
242
|
+
easeOut: (t) => t * (2 - t),
|
|
243
|
+
easeInOut: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
|
|
244
|
+
easeInCubic: (t) => t * t * t,
|
|
245
|
+
easeOutCubic: (t) => 1 - Math.pow(1 - t, 3),
|
|
246
|
+
easeInOutCubic: (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
|
|
247
|
+
};
|
|
248
|
+
var ScrollEngine = class {
|
|
249
|
+
constructor() {
|
|
250
|
+
this.container = null;
|
|
251
|
+
this.scrollPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
252
|
+
this.previousScrollPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
253
|
+
this.scrolling = false;
|
|
254
|
+
this.scrollDirection = "none";
|
|
255
|
+
this.lastScrollTime = 0;
|
|
256
|
+
this.scrollEndTimer = null;
|
|
257
|
+
this.scrollEndDelay = 150;
|
|
258
|
+
// Animation state
|
|
259
|
+
this.animationId = null;
|
|
260
|
+
this.animationStartTime = 0;
|
|
261
|
+
this.animationStartOffset = 0;
|
|
262
|
+
this.animationTargetOffset = 0;
|
|
263
|
+
this.animationDuration = 0;
|
|
264
|
+
this.animationEasing = easings.easeInOut;
|
|
265
|
+
// Callbacks
|
|
266
|
+
this.onScrollCallback = null;
|
|
267
|
+
this.onScrollStartCallback = null;
|
|
268
|
+
this.onScrollEndCallback = null;
|
|
269
|
+
// Throttled handler
|
|
270
|
+
this.throttledHandleScroll = null;
|
|
271
|
+
this.boundHandleScroll = null;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Attach to a container element.
|
|
275
|
+
*
|
|
276
|
+
* @param container - Container element to observe
|
|
277
|
+
*/
|
|
278
|
+
attach(container) {
|
|
279
|
+
this.detach();
|
|
280
|
+
this.container = container;
|
|
281
|
+
this.scrollPosition = getScrollPosition(container);
|
|
282
|
+
this.previousScrollPosition = { ...this.scrollPosition };
|
|
283
|
+
this.throttledHandleScroll = throttleRAF(this.processScroll.bind(this));
|
|
284
|
+
this.boundHandleScroll = () => this.throttledHandleScroll?.();
|
|
285
|
+
container.addEventListener("scroll", this.boundHandleScroll, { passive: true });
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Detach from the current container.
|
|
289
|
+
*/
|
|
290
|
+
detach() {
|
|
291
|
+
if (this.container && this.boundHandleScroll) {
|
|
292
|
+
this.container.removeEventListener("scroll", this.boundHandleScroll);
|
|
293
|
+
}
|
|
294
|
+
if (this.throttledHandleScroll) {
|
|
295
|
+
this.throttledHandleScroll.cancel();
|
|
296
|
+
this.throttledHandleScroll = null;
|
|
297
|
+
}
|
|
298
|
+
if (this.scrollEndTimer) {
|
|
299
|
+
clearTimeout(this.scrollEndTimer);
|
|
300
|
+
this.scrollEndTimer = null;
|
|
301
|
+
}
|
|
302
|
+
if (this.animationId) {
|
|
303
|
+
cancelRaf(this.animationId);
|
|
304
|
+
this.animationId = null;
|
|
305
|
+
}
|
|
306
|
+
this.boundHandleScroll = null;
|
|
307
|
+
this.container = null;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Process scroll event.
|
|
311
|
+
*/
|
|
312
|
+
processScroll() {
|
|
313
|
+
if (!this.container) return;
|
|
314
|
+
const now = performance.now();
|
|
315
|
+
this.previousScrollPosition = { ...this.scrollPosition };
|
|
316
|
+
this.scrollPosition = getScrollPosition(this.container);
|
|
317
|
+
this.scrollDirection = getScrollDirection(this.scrollPosition, this.previousScrollPosition);
|
|
318
|
+
if (!this.scrolling) {
|
|
319
|
+
this.scrolling = true;
|
|
320
|
+
this.onScrollStartCallback?.({
|
|
321
|
+
type: "scroll-start",
|
|
322
|
+
scrollTop: this.scrollPosition.scrollTop,
|
|
323
|
+
scrollLeft: this.scrollPosition.scrollLeft,
|
|
324
|
+
timestamp: now
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
this.onScrollCallback?.({
|
|
328
|
+
type: "scroll",
|
|
329
|
+
scrollTop: this.scrollPosition.scrollTop,
|
|
330
|
+
scrollLeft: this.scrollPosition.scrollLeft,
|
|
331
|
+
deltaY: this.scrollPosition.scrollTop - this.previousScrollPosition.scrollTop,
|
|
332
|
+
deltaX: this.scrollPosition.scrollLeft - this.previousScrollPosition.scrollLeft,
|
|
333
|
+
direction: this.scrollDirection,
|
|
334
|
+
timestamp: now
|
|
335
|
+
});
|
|
336
|
+
this.lastScrollTime = now;
|
|
337
|
+
this.scheduleScrollEnd();
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Schedule scroll end detection.
|
|
341
|
+
*/
|
|
342
|
+
scheduleScrollEnd() {
|
|
343
|
+
if (this.scrollEndTimer) {
|
|
344
|
+
clearTimeout(this.scrollEndTimer);
|
|
345
|
+
}
|
|
346
|
+
this.scrollEndTimer = setTimeout(() => {
|
|
347
|
+
this.scrolling = false;
|
|
348
|
+
this.scrollEndTimer = null;
|
|
349
|
+
this.onScrollEndCallback?.({
|
|
350
|
+
type: "scroll-end",
|
|
351
|
+
scrollTop: this.scrollPosition.scrollTop,
|
|
352
|
+
scrollLeft: this.scrollPosition.scrollLeft,
|
|
353
|
+
timestamp: performance.now()
|
|
354
|
+
});
|
|
355
|
+
}, this.scrollEndDelay);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get the current scroll position.
|
|
359
|
+
*
|
|
360
|
+
* @returns Current scroll position
|
|
361
|
+
*/
|
|
362
|
+
getScrollPosition() {
|
|
363
|
+
return { ...this.scrollPosition };
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Set the scroll position directly.
|
|
367
|
+
*
|
|
368
|
+
* @param position - New scroll position
|
|
369
|
+
*/
|
|
370
|
+
setScrollPosition(position) {
|
|
371
|
+
if (!this.container) return;
|
|
372
|
+
if (this.animationId) {
|
|
373
|
+
cancelRaf(this.animationId);
|
|
374
|
+
this.animationId = null;
|
|
375
|
+
}
|
|
376
|
+
setScrollPosition(this.container, position);
|
|
377
|
+
if (position.scrollTop !== void 0) {
|
|
378
|
+
this.scrollPosition.scrollTop = position.scrollTop;
|
|
379
|
+
}
|
|
380
|
+
if (position.scrollLeft !== void 0) {
|
|
381
|
+
this.scrollPosition.scrollLeft = position.scrollLeft;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Scroll to an offset with optional animation.
|
|
386
|
+
*
|
|
387
|
+
* @param offset - Target scroll offset
|
|
388
|
+
* @param options - Scroll options
|
|
389
|
+
*/
|
|
390
|
+
scrollTo(offset, options = {}) {
|
|
391
|
+
if (!this.container) return;
|
|
392
|
+
const { behavior = "auto", duration = 300, easing = easings.easeInOut } = options;
|
|
393
|
+
if (behavior === "auto" || behavior === "smooth" && duration === 0) {
|
|
394
|
+
this.setScrollPosition({ scrollTop: offset });
|
|
395
|
+
} else {
|
|
396
|
+
this.animateScroll(offset, duration, easing);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Animate scroll to a target offset.
|
|
401
|
+
*/
|
|
402
|
+
animateScroll(targetOffset, duration, easing) {
|
|
403
|
+
if (this.animationId) {
|
|
404
|
+
cancelRaf(this.animationId);
|
|
405
|
+
}
|
|
406
|
+
this.animationStartTime = performance.now();
|
|
407
|
+
this.animationStartOffset = this.scrollPosition.scrollTop;
|
|
408
|
+
this.animationTargetOffset = targetOffset;
|
|
409
|
+
this.animationDuration = duration;
|
|
410
|
+
this.animationEasing = easing;
|
|
411
|
+
const animate = (currentTime) => {
|
|
412
|
+
const elapsed = currentTime - this.animationStartTime;
|
|
413
|
+
const progress = Math.min(elapsed / this.animationDuration, 1);
|
|
414
|
+
const easedProgress = this.animationEasing(progress);
|
|
415
|
+
const currentOffset = this.animationStartOffset + (this.animationTargetOffset - this.animationStartOffset) * easedProgress;
|
|
416
|
+
this.setScrollPosition({ scrollTop: currentOffset });
|
|
417
|
+
if (progress < 1) {
|
|
418
|
+
this.animationId = raf(animate);
|
|
419
|
+
} else {
|
|
420
|
+
this.animationId = null;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
this.animationId = raf(animate);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Check if currently scrolling.
|
|
427
|
+
*
|
|
428
|
+
* @returns Whether scrolling
|
|
429
|
+
*/
|
|
430
|
+
isScrolling() {
|
|
431
|
+
return this.scrolling;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Check if an animation is in progress.
|
|
435
|
+
*
|
|
436
|
+
* @returns Whether animating
|
|
437
|
+
*/
|
|
438
|
+
isAnimating() {
|
|
439
|
+
return this.animationId !== null;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Get the current scroll direction.
|
|
443
|
+
*
|
|
444
|
+
* @returns Scroll direction
|
|
445
|
+
*/
|
|
446
|
+
getScrollDirection() {
|
|
447
|
+
return this.scrollDirection;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Set scroll end delay.
|
|
451
|
+
*
|
|
452
|
+
* @param delay - Delay in ms
|
|
453
|
+
*/
|
|
454
|
+
setScrollEndDelay(delay) {
|
|
455
|
+
this.scrollEndDelay = delay;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Set callback for scroll events.
|
|
459
|
+
*
|
|
460
|
+
* @param callback - Callback function
|
|
461
|
+
*/
|
|
462
|
+
onScroll(callback) {
|
|
463
|
+
this.onScrollCallback = callback;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Set callback for scroll start events.
|
|
467
|
+
*
|
|
468
|
+
* @param callback - Callback function
|
|
469
|
+
*/
|
|
470
|
+
onScrollStart(callback) {
|
|
471
|
+
this.onScrollStartCallback = callback;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Set callback for scroll end events.
|
|
475
|
+
*
|
|
476
|
+
* @param callback - Callback function
|
|
477
|
+
*/
|
|
478
|
+
onScrollEnd(callback) {
|
|
479
|
+
this.onScrollEndCallback = callback;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Get the container element.
|
|
483
|
+
*
|
|
484
|
+
* @returns Container element, or null if not attached
|
|
485
|
+
*/
|
|
486
|
+
getContainer() {
|
|
487
|
+
return this.container;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Check if attached to a container.
|
|
491
|
+
*
|
|
492
|
+
* @returns Whether attached
|
|
493
|
+
*/
|
|
494
|
+
isAttached() {
|
|
495
|
+
return this.container !== null;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Stop any ongoing scroll animation.
|
|
499
|
+
*/
|
|
500
|
+
stopAnimation() {
|
|
501
|
+
if (this.animationId) {
|
|
502
|
+
cancelRaf(this.animationId);
|
|
503
|
+
this.animationId = null;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
// src/utils/rect.ts
|
|
509
|
+
function getViewport(element) {
|
|
510
|
+
return {
|
|
511
|
+
width: element.clientWidth,
|
|
512
|
+
height: element.clientHeight,
|
|
513
|
+
scrollWidth: element.scrollWidth,
|
|
514
|
+
scrollHeight: element.scrollHeight
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/kernel/viewport.ts
|
|
519
|
+
var ViewportManager = class {
|
|
520
|
+
constructor() {
|
|
521
|
+
this.viewport = {
|
|
522
|
+
width: 0,
|
|
523
|
+
height: 0,
|
|
524
|
+
scrollWidth: 0,
|
|
525
|
+
scrollHeight: 0
|
|
526
|
+
};
|
|
527
|
+
this.container = null;
|
|
528
|
+
this.resizeObserver = null;
|
|
529
|
+
this.onChangeCallback = null;
|
|
530
|
+
this.throttledHandleResize = null;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Attach to a container element.
|
|
534
|
+
*
|
|
535
|
+
* @param container - Container element to observe
|
|
536
|
+
*/
|
|
537
|
+
attach(container) {
|
|
538
|
+
this.detach();
|
|
539
|
+
this.container = container;
|
|
540
|
+
this.viewport = getViewport(container);
|
|
541
|
+
this.throttledHandleResize = throttleRAF(this.handleResize.bind(this));
|
|
542
|
+
this.resizeObserver = new ResizeObserver(this.throttledHandleResize);
|
|
543
|
+
this.resizeObserver.observe(container);
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Detach from the current container.
|
|
547
|
+
*/
|
|
548
|
+
detach() {
|
|
549
|
+
if (this.resizeObserver) {
|
|
550
|
+
this.resizeObserver.disconnect();
|
|
551
|
+
this.resizeObserver = null;
|
|
552
|
+
}
|
|
553
|
+
if (this.throttledHandleResize) {
|
|
554
|
+
this.throttledHandleResize.cancel();
|
|
555
|
+
this.throttledHandleResize = null;
|
|
556
|
+
}
|
|
557
|
+
this.container = null;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Handle resize observer callback.
|
|
561
|
+
*/
|
|
562
|
+
handleResize() {
|
|
563
|
+
if (!this.container) return;
|
|
564
|
+
const previousViewport = { ...this.viewport };
|
|
565
|
+
this.viewport = getViewport(this.container);
|
|
566
|
+
if (previousViewport.width === this.viewport.width && previousViewport.height === this.viewport.height && previousViewport.scrollWidth === this.viewport.scrollWidth && previousViewport.scrollHeight === this.viewport.scrollHeight) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
if (this.onChangeCallback) {
|
|
570
|
+
this.onChangeCallback({
|
|
571
|
+
type: "resize",
|
|
572
|
+
viewport: this.viewport,
|
|
573
|
+
previousViewport
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Get the current viewport dimensions.
|
|
579
|
+
*
|
|
580
|
+
* @returns Current viewport
|
|
581
|
+
*/
|
|
582
|
+
getViewport() {
|
|
583
|
+
return { ...this.viewport };
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Force a measurement update.
|
|
587
|
+
*
|
|
588
|
+
* @returns Updated viewport
|
|
589
|
+
*/
|
|
590
|
+
measure() {
|
|
591
|
+
if (this.container) {
|
|
592
|
+
const previousViewport = { ...this.viewport };
|
|
593
|
+
this.viewport = getViewport(this.container);
|
|
594
|
+
if ((previousViewport.width !== this.viewport.width || previousViewport.height !== this.viewport.height || previousViewport.scrollWidth !== this.viewport.scrollWidth || previousViewport.scrollHeight !== this.viewport.scrollHeight) && this.onChangeCallback) {
|
|
595
|
+
this.onChangeCallback({
|
|
596
|
+
type: "resize",
|
|
597
|
+
viewport: this.viewport,
|
|
598
|
+
previousViewport
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return { ...this.viewport };
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Update the scroll dimensions without triggering a resize event.
|
|
606
|
+
* Used when the content size changes but container size stays the same.
|
|
607
|
+
*
|
|
608
|
+
* @param scrollWidth - New scroll width
|
|
609
|
+
* @param scrollHeight - New scroll height
|
|
610
|
+
*/
|
|
611
|
+
updateScrollDimensions(scrollWidth, scrollHeight) {
|
|
612
|
+
this.viewport.scrollWidth = scrollWidth;
|
|
613
|
+
this.viewport.scrollHeight = scrollHeight;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Set callback for viewport changes.
|
|
617
|
+
*
|
|
618
|
+
* @param callback - Callback function
|
|
619
|
+
*/
|
|
620
|
+
onChange(callback) {
|
|
621
|
+
this.onChangeCallback = callback;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Get the container element.
|
|
625
|
+
*
|
|
626
|
+
* @returns Container element, or null if not attached
|
|
627
|
+
*/
|
|
628
|
+
getContainer() {
|
|
629
|
+
return this.container;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Check if attached to a container.
|
|
633
|
+
*
|
|
634
|
+
* @returns Whether attached
|
|
635
|
+
*/
|
|
636
|
+
isAttached() {
|
|
637
|
+
return this.container !== null;
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Get the visible width.
|
|
641
|
+
*
|
|
642
|
+
* @returns Visible width
|
|
643
|
+
*/
|
|
644
|
+
getWidth() {
|
|
645
|
+
return this.viewport.width;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Get the visible height.
|
|
649
|
+
*
|
|
650
|
+
* @returns Visible height
|
|
651
|
+
*/
|
|
652
|
+
getHeight() {
|
|
653
|
+
return this.viewport.height;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Get the total scrollable width.
|
|
657
|
+
*
|
|
658
|
+
* @returns Scroll width
|
|
659
|
+
*/
|
|
660
|
+
getScrollWidth() {
|
|
661
|
+
return this.viewport.scrollWidth;
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Get the total scrollable height.
|
|
665
|
+
*
|
|
666
|
+
* @returns Scroll height
|
|
667
|
+
*/
|
|
668
|
+
getScrollHeight() {
|
|
669
|
+
return this.viewport.scrollHeight;
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// src/utils/binary-search.ts
|
|
674
|
+
function binarySearch(length, predicate) {
|
|
675
|
+
let low = 0;
|
|
676
|
+
let high = length - 1;
|
|
677
|
+
while (low <= high) {
|
|
678
|
+
const mid = Math.floor((low + high) / 2);
|
|
679
|
+
if (predicate(mid)) {
|
|
680
|
+
high = mid - 1;
|
|
681
|
+
} else {
|
|
682
|
+
low = mid + 1;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return low;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// src/utils/array.ts
|
|
689
|
+
function clamp(value, min, max) {
|
|
690
|
+
return Math.min(Math.max(value, min), max);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// src/kernel/measurement-cache.ts
|
|
694
|
+
var MeasurementCache = class {
|
|
695
|
+
/**
|
|
696
|
+
* Create a new measurement cache.
|
|
697
|
+
*
|
|
698
|
+
* @param estimatedHeight - Default estimated height for unmeasured items
|
|
699
|
+
*/
|
|
700
|
+
constructor(estimatedHeight) {
|
|
701
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
702
|
+
this.totalMeasured = 0;
|
|
703
|
+
this.sumOfHeights = 0;
|
|
704
|
+
this.estimatedHeight = estimatedHeight;
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Get the cached height for an index.
|
|
708
|
+
*
|
|
709
|
+
* @param index - Item index
|
|
710
|
+
* @returns Cached height, or undefined if not measured
|
|
711
|
+
*/
|
|
712
|
+
get(index) {
|
|
713
|
+
return this.cache.get(index);
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Set the height for an index.
|
|
717
|
+
*
|
|
718
|
+
* @param index - Item index
|
|
719
|
+
* @param height - Measured height
|
|
720
|
+
* @returns Previous height, or undefined if not previously measured
|
|
721
|
+
*/
|
|
722
|
+
set(index, height) {
|
|
723
|
+
const previousHeight = this.cache.get(index);
|
|
724
|
+
if (previousHeight !== void 0) {
|
|
725
|
+
this.sumOfHeights = this.sumOfHeights - previousHeight + height;
|
|
726
|
+
} else {
|
|
727
|
+
this.totalMeasured++;
|
|
728
|
+
this.sumOfHeights += height;
|
|
729
|
+
}
|
|
730
|
+
this.cache.set(index, height);
|
|
731
|
+
return previousHeight;
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Check if an index has been measured.
|
|
735
|
+
*
|
|
736
|
+
* @param index - Item index
|
|
737
|
+
* @returns Whether the index has a cached measurement
|
|
738
|
+
*/
|
|
739
|
+
has(index) {
|
|
740
|
+
return this.cache.has(index);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Delete a cached measurement.
|
|
744
|
+
*
|
|
745
|
+
* @param index - Item index
|
|
746
|
+
* @returns Whether a measurement was deleted
|
|
747
|
+
*/
|
|
748
|
+
delete(index) {
|
|
749
|
+
const height = this.cache.get(index);
|
|
750
|
+
if (height !== void 0) {
|
|
751
|
+
this.cache.delete(index);
|
|
752
|
+
this.totalMeasured--;
|
|
753
|
+
this.sumOfHeights -= height;
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Clear all cached measurements.
|
|
760
|
+
*/
|
|
761
|
+
clear() {
|
|
762
|
+
this.cache.clear();
|
|
763
|
+
this.totalMeasured = 0;
|
|
764
|
+
this.sumOfHeights = 0;
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* Get the height for an index, using estimated height if not measured.
|
|
768
|
+
*
|
|
769
|
+
* @param index - Item index
|
|
770
|
+
* @returns Height (measured or estimated)
|
|
771
|
+
*/
|
|
772
|
+
getOrEstimate(index) {
|
|
773
|
+
return this.cache.get(index) ?? this.getEstimatedHeight();
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Get the average height of measured items.
|
|
777
|
+
* Falls back to estimated height if no measurements.
|
|
778
|
+
*
|
|
779
|
+
* @returns Average height
|
|
780
|
+
*/
|
|
781
|
+
getAverageHeight() {
|
|
782
|
+
if (this.totalMeasured === 0) {
|
|
783
|
+
return this.estimatedHeight;
|
|
784
|
+
}
|
|
785
|
+
return this.sumOfHeights / this.totalMeasured;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Get the estimated height.
|
|
789
|
+
* Uses average of measured items if available, otherwise initial estimate.
|
|
790
|
+
*
|
|
791
|
+
* @returns Estimated height
|
|
792
|
+
*/
|
|
793
|
+
getEstimatedHeight() {
|
|
794
|
+
if (this.totalMeasured > 0) {
|
|
795
|
+
return this.getAverageHeight();
|
|
796
|
+
}
|
|
797
|
+
return this.estimatedHeight;
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Set the base estimated height.
|
|
801
|
+
*
|
|
802
|
+
* @param height - New estimated height
|
|
803
|
+
*/
|
|
804
|
+
setEstimatedHeight(height) {
|
|
805
|
+
this.estimatedHeight = height;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Get the number of measured items.
|
|
809
|
+
*
|
|
810
|
+
* @returns Number of measured items
|
|
811
|
+
*/
|
|
812
|
+
getMeasuredCount() {
|
|
813
|
+
return this.totalMeasured;
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Get all measured indices.
|
|
817
|
+
*
|
|
818
|
+
* @returns Array of measured indices
|
|
819
|
+
*/
|
|
820
|
+
getMeasuredIndices() {
|
|
821
|
+
return Array.from(this.cache.keys());
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Get the total size (sum of all measured heights).
|
|
825
|
+
*
|
|
826
|
+
* @returns Total measured size
|
|
827
|
+
*/
|
|
828
|
+
getTotalMeasuredSize() {
|
|
829
|
+
return this.sumOfHeights;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Shift all indices by an offset.
|
|
833
|
+
* Used when items are added or removed before the measured items.
|
|
834
|
+
*
|
|
835
|
+
* @param startIndex - Index from which to start shifting
|
|
836
|
+
* @param offset - Amount to shift (positive = shift right, negative = shift left)
|
|
837
|
+
*/
|
|
838
|
+
shiftIndices(startIndex, offset) {
|
|
839
|
+
if (offset === 0) return;
|
|
840
|
+
const newCache = /* @__PURE__ */ new Map();
|
|
841
|
+
for (const [index, height] of this.cache) {
|
|
842
|
+
if (index >= startIndex) {
|
|
843
|
+
const newIndex = index + offset;
|
|
844
|
+
if (newIndex >= 0) {
|
|
845
|
+
newCache.set(newIndex, height);
|
|
846
|
+
} else {
|
|
847
|
+
this.totalMeasured--;
|
|
848
|
+
this.sumOfHeights -= height;
|
|
849
|
+
}
|
|
850
|
+
} else {
|
|
851
|
+
newCache.set(index, height);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
this.cache = newCache;
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Get the internal cache map.
|
|
858
|
+
* Useful for debugging or serialization.
|
|
859
|
+
*
|
|
860
|
+
* @returns The cache map
|
|
861
|
+
*/
|
|
862
|
+
getCache() {
|
|
863
|
+
return new Map(this.cache);
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Restore cache from a map.
|
|
867
|
+
* Useful for deserialization or restoration.
|
|
868
|
+
*
|
|
869
|
+
* @param cache - Cache map to restore
|
|
870
|
+
*/
|
|
871
|
+
restoreCache(cache) {
|
|
872
|
+
this.clear();
|
|
873
|
+
for (const [index, height] of cache) {
|
|
874
|
+
this.set(index, height);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
// src/kernel/range-calculator.ts
|
|
880
|
+
var RangeCalculator = class {
|
|
881
|
+
/**
|
|
882
|
+
* Create a new range calculator.
|
|
883
|
+
*
|
|
884
|
+
* @param options - Configuration options
|
|
885
|
+
*/
|
|
886
|
+
constructor(options) {
|
|
887
|
+
this.offsetCache = [];
|
|
888
|
+
this.dirtyFromIndex = 0;
|
|
889
|
+
this.totalSize = 0;
|
|
890
|
+
this.itemCount = options.itemCount;
|
|
891
|
+
this.overscan = options.overscan;
|
|
892
|
+
this.measurementCache = new MeasurementCache(options.estimatedItemHeight);
|
|
893
|
+
this.rebuildOffsetCache();
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* Get the visible range of items.
|
|
897
|
+
*
|
|
898
|
+
* @param scrollOffset - Current scroll position
|
|
899
|
+
* @param viewportSize - Size of the visible viewport
|
|
900
|
+
* @returns Visible range with overscan
|
|
901
|
+
*/
|
|
902
|
+
getVisibleRange(scrollOffset, viewportSize) {
|
|
903
|
+
if (this.itemCount === 0) {
|
|
904
|
+
return {
|
|
905
|
+
startIndex: 0,
|
|
906
|
+
endIndex: 0,
|
|
907
|
+
overscanStartIndex: 0,
|
|
908
|
+
overscanEndIndex: 0
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
912
|
+
const startIndex = this.findStartIndex(scrollOffset);
|
|
913
|
+
const scrollEnd = scrollOffset + viewportSize;
|
|
914
|
+
const endIndex = this.findEndIndex(scrollEnd, startIndex);
|
|
915
|
+
const overscanStartIndex = Math.max(0, startIndex - this.overscan);
|
|
916
|
+
const overscanEndIndex = Math.min(this.itemCount - 1, endIndex + this.overscan);
|
|
917
|
+
return {
|
|
918
|
+
startIndex,
|
|
919
|
+
endIndex,
|
|
920
|
+
overscanStartIndex,
|
|
921
|
+
overscanEndIndex
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Find the start index for a given scroll offset.
|
|
926
|
+
*/
|
|
927
|
+
findStartIndex(scrollOffset) {
|
|
928
|
+
if (scrollOffset <= 0) return 0;
|
|
929
|
+
const index = binarySearch(this.itemCount, (mid) => {
|
|
930
|
+
const itemEnd = this.getItemEnd(mid);
|
|
931
|
+
return itemEnd > scrollOffset;
|
|
932
|
+
});
|
|
933
|
+
return clamp(index, 0, this.itemCount - 1);
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Find the end index for a given scroll end position.
|
|
937
|
+
*/
|
|
938
|
+
findEndIndex(scrollEnd, startIndex) {
|
|
939
|
+
if (scrollEnd >= this.totalSize) return this.itemCount - 1;
|
|
940
|
+
let index = startIndex;
|
|
941
|
+
while (index < this.itemCount - 1) {
|
|
942
|
+
const itemStart = this.getItemOffset(index);
|
|
943
|
+
if (itemStart >= scrollEnd) {
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
index++;
|
|
947
|
+
}
|
|
948
|
+
return clamp(index, startIndex, this.itemCount - 1);
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Get the offset (start position) of an item.
|
|
952
|
+
*
|
|
953
|
+
* @param index - Item index
|
|
954
|
+
* @returns Offset from start
|
|
955
|
+
*/
|
|
956
|
+
getItemOffset(index) {
|
|
957
|
+
if (index < 0 || this.itemCount === 0) return 0;
|
|
958
|
+
if (index >= this.itemCount) return this.totalSize;
|
|
959
|
+
this.ensureOffsetCacheValid(index);
|
|
960
|
+
return this.offsetCache[index] ?? 0;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Get the end position of an item.
|
|
964
|
+
*
|
|
965
|
+
* @param index - Item index
|
|
966
|
+
* @returns End position (offset + size)
|
|
967
|
+
*/
|
|
968
|
+
getItemEnd(index) {
|
|
969
|
+
return this.getItemOffset(index) + this.getItemSize(index);
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Get the size of an item.
|
|
973
|
+
*
|
|
974
|
+
* @param index - Item index
|
|
975
|
+
* @returns Item size
|
|
976
|
+
*/
|
|
977
|
+
getItemSize(index) {
|
|
978
|
+
return this.measurementCache.getOrEstimate(index);
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Get the total size of all items.
|
|
982
|
+
*
|
|
983
|
+
* @returns Total size
|
|
984
|
+
*/
|
|
985
|
+
getTotalSize() {
|
|
986
|
+
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
987
|
+
return this.totalSize;
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Set the height for an item.
|
|
991
|
+
*
|
|
992
|
+
* @param index - Item index
|
|
993
|
+
* @param height - Item height
|
|
994
|
+
* @returns Previous height, or undefined if not measured
|
|
995
|
+
*/
|
|
996
|
+
setItemHeight(index, height) {
|
|
997
|
+
const previousHeight = this.measurementCache.set(index, height);
|
|
998
|
+
this.invalidateFrom(index);
|
|
999
|
+
return previousHeight;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Get the cached height for an item.
|
|
1003
|
+
*
|
|
1004
|
+
* @param index - Item index
|
|
1005
|
+
* @returns Cached height, or undefined if not measured
|
|
1006
|
+
*/
|
|
1007
|
+
getCachedHeight(index) {
|
|
1008
|
+
return this.measurementCache.get(index);
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Invalidate measurement for an item.
|
|
1012
|
+
*
|
|
1013
|
+
* @param index - Item index
|
|
1014
|
+
*/
|
|
1015
|
+
invalidate(index) {
|
|
1016
|
+
if (this.measurementCache.delete(index)) {
|
|
1017
|
+
this.invalidateFrom(index);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Invalidate all measurements.
|
|
1022
|
+
*/
|
|
1023
|
+
invalidateAll() {
|
|
1024
|
+
this.measurementCache.clear();
|
|
1025
|
+
this.dirtyFromIndex = 0;
|
|
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
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Update item count.
|
|
1063
|
+
*
|
|
1064
|
+
* @param count - New item count
|
|
1065
|
+
*/
|
|
1066
|
+
setItemCount(count) {
|
|
1067
|
+
if (count === this.itemCount) return;
|
|
1068
|
+
const previousCount = this.itemCount;
|
|
1069
|
+
this.itemCount = count;
|
|
1070
|
+
if (count < previousCount) {
|
|
1071
|
+
this.offsetCache.length = count;
|
|
1072
|
+
if (count > 0) {
|
|
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;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Update overscan amount.
|
|
1088
|
+
*
|
|
1089
|
+
* @param overscan - New overscan value
|
|
1090
|
+
*/
|
|
1091
|
+
setOverscan(overscan) {
|
|
1092
|
+
this.overscan = overscan;
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Update estimated item height.
|
|
1096
|
+
*
|
|
1097
|
+
* @param height - New estimated height
|
|
1098
|
+
*/
|
|
1099
|
+
setEstimatedHeight(height) {
|
|
1100
|
+
this.measurementCache.setEstimatedHeight(height);
|
|
1101
|
+
this.dirtyFromIndex = 0;
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Get the estimated item height.
|
|
1105
|
+
*
|
|
1106
|
+
* @returns Estimated height
|
|
1107
|
+
*/
|
|
1108
|
+
getEstimatedHeight() {
|
|
1109
|
+
return this.measurementCache.getEstimatedHeight();
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Get the item index at a given offset.
|
|
1113
|
+
*
|
|
1114
|
+
* @param offset - Offset position
|
|
1115
|
+
* @returns Item index at that offset
|
|
1116
|
+
*/
|
|
1117
|
+
getItemAtOffset(offset) {
|
|
1118
|
+
if (offset <= 0 || this.itemCount === 0) return 0;
|
|
1119
|
+
if (offset >= this.totalSize) return this.itemCount - 1;
|
|
1120
|
+
this.ensureOffsetCacheValid(this.itemCount - 1);
|
|
1121
|
+
return binarySearch(this.itemCount, (mid) => {
|
|
1122
|
+
const itemEnd = this.getItemEnd(mid);
|
|
1123
|
+
return itemEnd > offset;
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Get the measurement cache.
|
|
1128
|
+
*
|
|
1129
|
+
* @returns Measurement cache
|
|
1130
|
+
*/
|
|
1131
|
+
getMeasurementCache() {
|
|
1132
|
+
return this.measurementCache;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Get current item count.
|
|
1136
|
+
*
|
|
1137
|
+
* @returns Item count
|
|
1138
|
+
*/
|
|
1139
|
+
getItemCount() {
|
|
1140
|
+
return this.itemCount;
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
// src/kernel/plugin-registry.ts
|
|
1145
|
+
var PluginRegistry = class {
|
|
1146
|
+
constructor() {
|
|
1147
|
+
this.plugins = /* @__PURE__ */ new Map();
|
|
1148
|
+
this.kernel = null;
|
|
1149
|
+
this.hookSubscriptions = /* @__PURE__ */ new Map();
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Set the kernel instance.
|
|
1153
|
+
*
|
|
1154
|
+
* @param kernel - Kernel instance
|
|
1155
|
+
*/
|
|
1156
|
+
setKernel(kernel) {
|
|
1157
|
+
this.kernel = kernel;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Register a plugin.
|
|
1161
|
+
*
|
|
1162
|
+
* @param plugin - Plugin to register
|
|
1163
|
+
* @throws Error if plugin with same name already registered
|
|
1164
|
+
*/
|
|
1165
|
+
register(plugin) {
|
|
1166
|
+
this.validatePlugin(plugin);
|
|
1167
|
+
if (this.plugins.has(plugin.name)) {
|
|
1168
|
+
throw new Error(
|
|
1169
|
+
`[Scrollex] Plugin "${plugin.name}" is already registered. Unregister the existing plugin first.`
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1172
|
+
this.plugins.set(plugin.name, plugin);
|
|
1173
|
+
if (this.kernel && plugin.install) {
|
|
1174
|
+
plugin.install(this.kernel);
|
|
1175
|
+
this.setupHooks(plugin);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
/**
|
|
1179
|
+
* Unregister a plugin by name.
|
|
1180
|
+
*
|
|
1181
|
+
* @param name - Plugin name to unregister
|
|
1182
|
+
* @returns Whether a plugin was unregistered
|
|
1183
|
+
*/
|
|
1184
|
+
unregister(name) {
|
|
1185
|
+
const plugin = this.plugins.get(name);
|
|
1186
|
+
if (!plugin) {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
this.teardownHooks(plugin);
|
|
1190
|
+
plugin.uninstall?.();
|
|
1191
|
+
this.plugins.delete(name);
|
|
1192
|
+
return true;
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* Get a plugin by name.
|
|
1196
|
+
*
|
|
1197
|
+
* @param name - Plugin name
|
|
1198
|
+
* @returns Plugin instance, or undefined if not found
|
|
1199
|
+
*/
|
|
1200
|
+
get(name) {
|
|
1201
|
+
return this.plugins.get(name);
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Check if a plugin is registered.
|
|
1205
|
+
*
|
|
1206
|
+
* @param name - Plugin name
|
|
1207
|
+
* @returns Whether plugin is registered
|
|
1208
|
+
*/
|
|
1209
|
+
has(name) {
|
|
1210
|
+
return this.plugins.has(name);
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* List all registered plugins.
|
|
1214
|
+
*
|
|
1215
|
+
* @returns Array of plugin info
|
|
1216
|
+
*/
|
|
1217
|
+
list() {
|
|
1218
|
+
return Array.from(this.plugins.values()).map((plugin) => ({
|
|
1219
|
+
name: plugin.name,
|
|
1220
|
+
version: plugin.version,
|
|
1221
|
+
type: plugin.type,
|
|
1222
|
+
enabled: true
|
|
1223
|
+
}));
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Get all registered plugins.
|
|
1227
|
+
*
|
|
1228
|
+
* @returns Array of plugins
|
|
1229
|
+
*/
|
|
1230
|
+
getAll() {
|
|
1231
|
+
return Array.from(this.plugins.values());
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Validate a plugin before registration.
|
|
1235
|
+
*/
|
|
1236
|
+
validatePlugin(plugin) {
|
|
1237
|
+
if (!plugin.name || typeof plugin.name !== "string") {
|
|
1238
|
+
throw new Error("[Scrollex] Plugin must have a name");
|
|
1239
|
+
}
|
|
1240
|
+
if (!plugin.version || typeof plugin.version !== "string") {
|
|
1241
|
+
throw new Error(`[Scrollex] Plugin "${plugin.name}" must have a version`);
|
|
1242
|
+
}
|
|
1243
|
+
if (plugin.type !== "core" && plugin.type !== "optional") {
|
|
1244
|
+
throw new Error(
|
|
1245
|
+
`[Scrollex] Plugin "${plugin.name}" type must be "core" or "optional"`
|
|
1246
|
+
);
|
|
1247
|
+
}
|
|
1248
|
+
if (typeof plugin.install !== "function") {
|
|
1249
|
+
throw new Error(`[Scrollex] Plugin "${plugin.name}" must have an install method`);
|
|
1250
|
+
}
|
|
1251
|
+
if (typeof plugin.uninstall !== "function") {
|
|
1252
|
+
throw new Error(`[Scrollex] Plugin "${plugin.name}" must have an uninstall method`);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Set up hooks for a plugin.
|
|
1257
|
+
*/
|
|
1258
|
+
setupHooks(plugin) {
|
|
1259
|
+
if (!plugin.hooks || !this.kernel) return;
|
|
1260
|
+
const subscriptions = [];
|
|
1261
|
+
const hooks = plugin.hooks;
|
|
1262
|
+
const k = this.kernel;
|
|
1263
|
+
if (hooks.onScroll) {
|
|
1264
|
+
const handler = hooks.onScroll;
|
|
1265
|
+
const unsub = k.on("scroll", ((event) => {
|
|
1266
|
+
if (event.type === "scroll") handler(event);
|
|
1267
|
+
}));
|
|
1268
|
+
subscriptions.push(unsub);
|
|
1269
|
+
}
|
|
1270
|
+
if (hooks.onScrollStart) {
|
|
1271
|
+
const handler = hooks.onScrollStart;
|
|
1272
|
+
const unsub = k.on("scroll-start", ((event) => {
|
|
1273
|
+
if (event.type === "scroll-start") handler(event);
|
|
1274
|
+
}));
|
|
1275
|
+
subscriptions.push(unsub);
|
|
1276
|
+
}
|
|
1277
|
+
if (hooks.onScrollEnd) {
|
|
1278
|
+
const handler = hooks.onScrollEnd;
|
|
1279
|
+
const unsub = k.on("scroll-end", ((event) => {
|
|
1280
|
+
if (event.type === "scroll-end") handler(event);
|
|
1281
|
+
}));
|
|
1282
|
+
subscriptions.push(unsub);
|
|
1283
|
+
}
|
|
1284
|
+
if (hooks.onVisibleRangeChange) {
|
|
1285
|
+
const handler = hooks.onVisibleRangeChange;
|
|
1286
|
+
const unsub = k.on("visible-range-change", ((event) => {
|
|
1287
|
+
if (event.type === "visible-range-change") {
|
|
1288
|
+
const e = event;
|
|
1289
|
+
handler(e.range, e.previousRange);
|
|
1290
|
+
}
|
|
1291
|
+
}));
|
|
1292
|
+
subscriptions.push(unsub);
|
|
1293
|
+
}
|
|
1294
|
+
if (hooks.onItemMeasured) {
|
|
1295
|
+
const handler = hooks.onItemMeasured;
|
|
1296
|
+
const unsub = k.on("item-measured", ((event) => {
|
|
1297
|
+
if (event.type === "item-measured") {
|
|
1298
|
+
const e = event;
|
|
1299
|
+
handler(e.index, e.height);
|
|
1300
|
+
}
|
|
1301
|
+
}));
|
|
1302
|
+
subscriptions.push(unsub);
|
|
1303
|
+
}
|
|
1304
|
+
if (hooks.onResize) {
|
|
1305
|
+
const handler = hooks.onResize;
|
|
1306
|
+
const unsub = k.on("resize", ((event) => {
|
|
1307
|
+
if (event.type === "resize") {
|
|
1308
|
+
const e = event;
|
|
1309
|
+
handler(e.viewport);
|
|
1310
|
+
}
|
|
1311
|
+
}));
|
|
1312
|
+
subscriptions.push(unsub);
|
|
1313
|
+
}
|
|
1314
|
+
if (hooks.onLoadMore) {
|
|
1315
|
+
const handler = hooks.onLoadMore;
|
|
1316
|
+
const unsub = k.on("load-more", ((event) => {
|
|
1317
|
+
if (event.type === "load-more") {
|
|
1318
|
+
const e = event;
|
|
1319
|
+
handler(e.direction);
|
|
1320
|
+
}
|
|
1321
|
+
}));
|
|
1322
|
+
subscriptions.push(unsub);
|
|
1323
|
+
}
|
|
1324
|
+
if (hooks.onItemsChange) {
|
|
1325
|
+
const handler = hooks.onItemsChange;
|
|
1326
|
+
const unsub = k.on("items-change", ((event) => {
|
|
1327
|
+
if (event.type === "items-change") {
|
|
1328
|
+
const e = event;
|
|
1329
|
+
handler(e.count, e.previousCount);
|
|
1330
|
+
}
|
|
1331
|
+
}));
|
|
1332
|
+
subscriptions.push(unsub);
|
|
1333
|
+
}
|
|
1334
|
+
if (subscriptions.length > 0) {
|
|
1335
|
+
this.hookSubscriptions.set(plugin.name, subscriptions);
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
/**
|
|
1339
|
+
* Tear down hooks for a plugin.
|
|
1340
|
+
*/
|
|
1341
|
+
teardownHooks(plugin) {
|
|
1342
|
+
const subscriptions = this.hookSubscriptions.get(plugin.name);
|
|
1343
|
+
if (subscriptions) {
|
|
1344
|
+
for (const unsub of subscriptions) {
|
|
1345
|
+
unsub();
|
|
1346
|
+
}
|
|
1347
|
+
this.hookSubscriptions.delete(plugin.name);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
/**
|
|
1351
|
+
* Dispatch a hook to all plugins that implement it.
|
|
1352
|
+
*
|
|
1353
|
+
* @param hookName - Name of the hook
|
|
1354
|
+
* @param args - Arguments to pass to the hook
|
|
1355
|
+
*/
|
|
1356
|
+
dispatchHook(hookName, ...args) {
|
|
1357
|
+
for (const plugin of this.plugins.values()) {
|
|
1358
|
+
const hook = plugin.hooks?.[hookName];
|
|
1359
|
+
if (hook) {
|
|
1360
|
+
try {
|
|
1361
|
+
;
|
|
1362
|
+
hook(...args);
|
|
1363
|
+
} catch (error) {
|
|
1364
|
+
console.error(
|
|
1365
|
+
`[Scrollex] Error in plugin "${plugin.name}" hook "${String(hookName)}":`,
|
|
1366
|
+
error
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Unregister all plugins.
|
|
1374
|
+
*/
|
|
1375
|
+
clear() {
|
|
1376
|
+
const names = Array.from(this.plugins.keys()).reverse();
|
|
1377
|
+
for (const name of names) {
|
|
1378
|
+
this.unregister(name);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Get the number of registered plugins.
|
|
1383
|
+
*
|
|
1384
|
+
* @returns Number of plugins
|
|
1385
|
+
*/
|
|
1386
|
+
size() {
|
|
1387
|
+
return this.plugins.size;
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
|
|
1391
|
+
// src/kernel/kernel.ts
|
|
1392
|
+
var DEFAULT_OPTIONS = {
|
|
1393
|
+
itemCount: 0,
|
|
1394
|
+
estimatedItemHeight: 50,
|
|
1395
|
+
overscan: 5,
|
|
1396
|
+
direction: "vertical",
|
|
1397
|
+
getItemKey: (index) => index
|
|
1398
|
+
};
|
|
1399
|
+
function createKernel(options = {}) {
|
|
1400
|
+
const config = { ...DEFAULT_OPTIONS, ...options };
|
|
1401
|
+
const eventBus = new EventBus();
|
|
1402
|
+
const scrollEngine = new ScrollEngine();
|
|
1403
|
+
const viewportManager = new ViewportManager();
|
|
1404
|
+
const rangeCalculator = new RangeCalculator({
|
|
1405
|
+
itemCount: config.itemCount,
|
|
1406
|
+
estimatedItemHeight: config.estimatedItemHeight,
|
|
1407
|
+
overscan: config.overscan
|
|
1408
|
+
});
|
|
1409
|
+
const pluginRegistry = new PluginRegistry();
|
|
1410
|
+
let currentRange = {
|
|
1411
|
+
startIndex: 0,
|
|
1412
|
+
endIndex: 0,
|
|
1413
|
+
overscanStartIndex: 0,
|
|
1414
|
+
overscanEndIndex: 0
|
|
1415
|
+
};
|
|
1416
|
+
let attached = false;
|
|
1417
|
+
const kernel = {
|
|
1418
|
+
// Plugin Management
|
|
1419
|
+
register(plugin) {
|
|
1420
|
+
pluginRegistry.register(plugin);
|
|
1421
|
+
},
|
|
1422
|
+
unregister(pluginName) {
|
|
1423
|
+
pluginRegistry.unregister(pluginName);
|
|
1424
|
+
},
|
|
1425
|
+
getPlugin(name) {
|
|
1426
|
+
return pluginRegistry.get(name);
|
|
1427
|
+
},
|
|
1428
|
+
listPlugins() {
|
|
1429
|
+
return pluginRegistry.list();
|
|
1430
|
+
},
|
|
1431
|
+
// Scroll Control
|
|
1432
|
+
getScrollPosition() {
|
|
1433
|
+
return scrollEngine.getScrollPosition();
|
|
1434
|
+
},
|
|
1435
|
+
setScrollPosition(position) {
|
|
1436
|
+
scrollEngine.setScrollPosition(position);
|
|
1437
|
+
},
|
|
1438
|
+
scrollTo(offset, options2) {
|
|
1439
|
+
scrollEngine.scrollTo(offset, options2);
|
|
1440
|
+
},
|
|
1441
|
+
scrollToIndex(index, options2 = {}) {
|
|
1442
|
+
const { align = "auto", offset: additionalOffset = 0, ...scrollOptions } = options2;
|
|
1443
|
+
const itemOffset = rangeCalculator.getItemOffset(index);
|
|
1444
|
+
const itemSize = rangeCalculator.getItemSize(index);
|
|
1445
|
+
const viewport = viewportManager.getViewport();
|
|
1446
|
+
let targetOffset;
|
|
1447
|
+
switch (align) {
|
|
1448
|
+
case "start":
|
|
1449
|
+
targetOffset = itemOffset;
|
|
1450
|
+
break;
|
|
1451
|
+
case "center":
|
|
1452
|
+
targetOffset = itemOffset - viewport.height / 2 + itemSize / 2;
|
|
1453
|
+
break;
|
|
1454
|
+
case "end":
|
|
1455
|
+
targetOffset = itemOffset - viewport.height + itemSize;
|
|
1456
|
+
break;
|
|
1457
|
+
case "auto":
|
|
1458
|
+
default: {
|
|
1459
|
+
const scrollPosition = scrollEngine.getScrollPosition();
|
|
1460
|
+
const visibleStart = scrollPosition.scrollTop;
|
|
1461
|
+
const visibleEnd = visibleStart + viewport.height;
|
|
1462
|
+
const itemEnd = itemOffset + itemSize;
|
|
1463
|
+
if (itemOffset < visibleStart) {
|
|
1464
|
+
targetOffset = itemOffset;
|
|
1465
|
+
} else if (itemEnd > visibleEnd) {
|
|
1466
|
+
targetOffset = itemEnd - viewport.height;
|
|
1467
|
+
} else {
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1470
|
+
break;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
const maxScroll = Math.max(0, rangeCalculator.getTotalSize() - viewport.height);
|
|
1474
|
+
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
1475
|
+
scrollEngine.scrollTo(targetOffset, scrollOptions);
|
|
1476
|
+
},
|
|
1477
|
+
// Viewport
|
|
1478
|
+
getViewport() {
|
|
1479
|
+
return viewportManager.getViewport();
|
|
1480
|
+
},
|
|
1481
|
+
getVisibleRange() {
|
|
1482
|
+
return { ...currentRange };
|
|
1483
|
+
},
|
|
1484
|
+
getRenderRange() {
|
|
1485
|
+
return {
|
|
1486
|
+
startIndex: currentRange.overscanStartIndex,
|
|
1487
|
+
endIndex: currentRange.overscanEndIndex,
|
|
1488
|
+
overscanStartIndex: currentRange.overscanStartIndex,
|
|
1489
|
+
overscanEndIndex: currentRange.overscanEndIndex,
|
|
1490
|
+
start: currentRange.overscanStartIndex,
|
|
1491
|
+
end: currentRange.overscanEndIndex
|
|
1492
|
+
};
|
|
1493
|
+
},
|
|
1494
|
+
// Measurement
|
|
1495
|
+
measureItem(index, size) {
|
|
1496
|
+
const previousHeight = rangeCalculator.setItemHeight(index, size);
|
|
1497
|
+
eventBus.emit({
|
|
1498
|
+
type: "item-measured",
|
|
1499
|
+
index,
|
|
1500
|
+
height: size,
|
|
1501
|
+
previousHeight
|
|
1502
|
+
});
|
|
1503
|
+
updateVisibleRange();
|
|
1504
|
+
},
|
|
1505
|
+
getCachedHeight(index) {
|
|
1506
|
+
return rangeCalculator.getCachedHeight(index);
|
|
1507
|
+
},
|
|
1508
|
+
invalidateMeasurement(index) {
|
|
1509
|
+
rangeCalculator.invalidate(index);
|
|
1510
|
+
updateVisibleRange();
|
|
1511
|
+
},
|
|
1512
|
+
invalidateAllMeasurements() {
|
|
1513
|
+
rangeCalculator.invalidateAll();
|
|
1514
|
+
updateVisibleRange();
|
|
1515
|
+
},
|
|
1516
|
+
getEstimatedHeight() {
|
|
1517
|
+
return rangeCalculator.getEstimatedHeight();
|
|
1518
|
+
},
|
|
1519
|
+
getItemOffset(index) {
|
|
1520
|
+
return rangeCalculator.getItemOffset(index);
|
|
1521
|
+
},
|
|
1522
|
+
// Events
|
|
1523
|
+
emit(event) {
|
|
1524
|
+
eventBus.emit(event);
|
|
1525
|
+
},
|
|
1526
|
+
on(eventType, handler) {
|
|
1527
|
+
return eventBus.on(eventType, handler);
|
|
1528
|
+
},
|
|
1529
|
+
off(eventType, handler) {
|
|
1530
|
+
eventBus.off(eventType, handler);
|
|
1531
|
+
},
|
|
1532
|
+
// Configuration
|
|
1533
|
+
configure(newOptions) {
|
|
1534
|
+
const previousCount = config.itemCount;
|
|
1535
|
+
if (newOptions.itemCount !== void 0) {
|
|
1536
|
+
config.itemCount = newOptions.itemCount;
|
|
1537
|
+
rangeCalculator.setItemCount(newOptions.itemCount);
|
|
1538
|
+
if (newOptions.itemCount !== previousCount) {
|
|
1539
|
+
eventBus.emit({
|
|
1540
|
+
type: "items-change",
|
|
1541
|
+
count: newOptions.itemCount,
|
|
1542
|
+
previousCount
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
if (newOptions.estimatedItemHeight !== void 0) {
|
|
1547
|
+
config.estimatedItemHeight = newOptions.estimatedItemHeight;
|
|
1548
|
+
rangeCalculator.setEstimatedHeight(newOptions.estimatedItemHeight);
|
|
1549
|
+
}
|
|
1550
|
+
if (newOptions.overscan !== void 0) {
|
|
1551
|
+
config.overscan = newOptions.overscan;
|
|
1552
|
+
rangeCalculator.setOverscan(newOptions.overscan);
|
|
1553
|
+
}
|
|
1554
|
+
if (newOptions.direction !== void 0) {
|
|
1555
|
+
config.direction = newOptions.direction;
|
|
1556
|
+
}
|
|
1557
|
+
if (newOptions.getItemKey !== void 0) {
|
|
1558
|
+
config.getItemKey = newOptions.getItemKey;
|
|
1559
|
+
}
|
|
1560
|
+
updateVisibleRange();
|
|
1561
|
+
},
|
|
1562
|
+
getOptions() {
|
|
1563
|
+
return { ...config };
|
|
1564
|
+
},
|
|
1565
|
+
// State
|
|
1566
|
+
isScrolling() {
|
|
1567
|
+
return scrollEngine.isScrolling();
|
|
1568
|
+
},
|
|
1569
|
+
getItemCount() {
|
|
1570
|
+
return config.itemCount;
|
|
1571
|
+
},
|
|
1572
|
+
getTotalSize() {
|
|
1573
|
+
return rangeCalculator.getTotalSize();
|
|
1574
|
+
},
|
|
1575
|
+
// Lifecycle
|
|
1576
|
+
attach(container) {
|
|
1577
|
+
if (attached) {
|
|
1578
|
+
kernel.detach();
|
|
1579
|
+
}
|
|
1580
|
+
scrollEngine.attach(container);
|
|
1581
|
+
viewportManager.attach(container);
|
|
1582
|
+
attached = true;
|
|
1583
|
+
scrollEngine.onScroll((event) => {
|
|
1584
|
+
eventBus.emit(event);
|
|
1585
|
+
updateVisibleRange();
|
|
1586
|
+
});
|
|
1587
|
+
scrollEngine.onScrollStart((event) => {
|
|
1588
|
+
eventBus.emit(event);
|
|
1589
|
+
});
|
|
1590
|
+
scrollEngine.onScrollEnd((event) => {
|
|
1591
|
+
eventBus.emit(event);
|
|
1592
|
+
});
|
|
1593
|
+
viewportManager.onChange((event) => {
|
|
1594
|
+
eventBus.emit(event);
|
|
1595
|
+
updateVisibleRange();
|
|
1596
|
+
});
|
|
1597
|
+
updateVisibleRange();
|
|
1598
|
+
},
|
|
1599
|
+
detach() {
|
|
1600
|
+
scrollEngine.detach();
|
|
1601
|
+
viewportManager.detach();
|
|
1602
|
+
attached = false;
|
|
1603
|
+
},
|
|
1604
|
+
destroy() {
|
|
1605
|
+
kernel.detach();
|
|
1606
|
+
pluginRegistry.clear();
|
|
1607
|
+
eventBus.clear();
|
|
1608
|
+
}
|
|
1609
|
+
};
|
|
1610
|
+
pluginRegistry.setKernel(kernel);
|
|
1611
|
+
function updateVisibleRange() {
|
|
1612
|
+
if (!attached) return;
|
|
1613
|
+
const viewport = viewportManager.getViewport();
|
|
1614
|
+
const scrollPosition = scrollEngine.getScrollPosition();
|
|
1615
|
+
const scrollOffset = config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft;
|
|
1616
|
+
const viewportSize = config.direction === "vertical" ? viewport.height : viewport.width;
|
|
1617
|
+
const previousRange = { ...currentRange };
|
|
1618
|
+
currentRange = rangeCalculator.getVisibleRange(scrollOffset, viewportSize);
|
|
1619
|
+
if (currentRange.startIndex !== previousRange.startIndex || currentRange.endIndex !== previousRange.endIndex || currentRange.overscanStartIndex !== previousRange.overscanStartIndex || currentRange.overscanEndIndex !== previousRange.overscanEndIndex) {
|
|
1620
|
+
eventBus.emit({
|
|
1621
|
+
type: "visible-range-change",
|
|
1622
|
+
range: currentRange,
|
|
1623
|
+
previousRange
|
|
1624
|
+
});
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
return kernel;
|
|
1628
|
+
}
|
|
1629
|
+
function createPlugin(config) {
|
|
1630
|
+
return {
|
|
1631
|
+
name: config.name,
|
|
1632
|
+
version: config.version,
|
|
1633
|
+
type: config.type,
|
|
1634
|
+
hooks: config.hooks,
|
|
1635
|
+
api: config.api,
|
|
1636
|
+
install(kernel) {
|
|
1637
|
+
config.install(kernel);
|
|
1638
|
+
},
|
|
1639
|
+
uninstall() {
|
|
1640
|
+
config.uninstall?.();
|
|
1641
|
+
}
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
// 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
|
+
function listRendererPlugin(options = {}) {
|
|
1673
|
+
const { getItemKey = (index) => index } = options;
|
|
1674
|
+
let kernel = null;
|
|
1675
|
+
let virtualItems = [];
|
|
1676
|
+
let currentRange = {
|
|
1677
|
+
startIndex: 0,
|
|
1678
|
+
endIndex: 0,
|
|
1679
|
+
overscanStartIndex: 0,
|
|
1680
|
+
overscanEndIndex: 0
|
|
1681
|
+
};
|
|
1682
|
+
const itemPool = new VirtualItemPool();
|
|
1683
|
+
function buildVirtualItems(range) {
|
|
1684
|
+
if (!kernel) return [];
|
|
1685
|
+
itemPool.releaseAll(virtualItems);
|
|
1686
|
+
const items = [];
|
|
1687
|
+
range.startIndex;
|
|
1688
|
+
range.endIndex;
|
|
1689
|
+
for (let i = range.overscanStartIndex; i <= range.overscanEndIndex; i++) {
|
|
1690
|
+
const item = itemPool.acquire();
|
|
1691
|
+
const offset = kernel.getItemOffset(i);
|
|
1692
|
+
const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
|
|
1693
|
+
item.index = i;
|
|
1694
|
+
item.key = getItemKey(i);
|
|
1695
|
+
item.start = offset;
|
|
1696
|
+
item.end = offset + size;
|
|
1697
|
+
item.size = size;
|
|
1698
|
+
item.lane = 0;
|
|
1699
|
+
items.push(item);
|
|
1700
|
+
}
|
|
1701
|
+
return items;
|
|
1702
|
+
}
|
|
1703
|
+
function recalculate() {
|
|
1704
|
+
if (!kernel) return;
|
|
1705
|
+
currentRange = kernel.getVisibleRange();
|
|
1706
|
+
virtualItems = buildVirtualItems(currentRange);
|
|
1707
|
+
}
|
|
1708
|
+
const api = {
|
|
1709
|
+
getVirtualItems() {
|
|
1710
|
+
return virtualItems;
|
|
1711
|
+
},
|
|
1712
|
+
getTotalHeight() {
|
|
1713
|
+
return kernel?.getTotalSize() ?? 0;
|
|
1714
|
+
},
|
|
1715
|
+
getStartOffset() {
|
|
1716
|
+
if (virtualItems.length === 0) return 0;
|
|
1717
|
+
return virtualItems[0]?.start ?? 0;
|
|
1718
|
+
},
|
|
1719
|
+
getEndOffset() {
|
|
1720
|
+
if (virtualItems.length === 0) return 0;
|
|
1721
|
+
const lastItem = virtualItems[virtualItems.length - 1];
|
|
1722
|
+
return lastItem?.end ?? 0;
|
|
1723
|
+
},
|
|
1724
|
+
isItemVisible(index) {
|
|
1725
|
+
return index >= currentRange.startIndex && index <= currentRange.endIndex;
|
|
1726
|
+
},
|
|
1727
|
+
recalculate
|
|
1728
|
+
};
|
|
1729
|
+
return {
|
|
1730
|
+
name: "list-renderer",
|
|
1731
|
+
version: "1.0.0",
|
|
1732
|
+
type: "core",
|
|
1733
|
+
install(k) {
|
|
1734
|
+
kernel = k;
|
|
1735
|
+
recalculate();
|
|
1736
|
+
},
|
|
1737
|
+
uninstall() {
|
|
1738
|
+
itemPool.releaseAll(virtualItems);
|
|
1739
|
+
virtualItems = [];
|
|
1740
|
+
kernel = null;
|
|
1741
|
+
},
|
|
1742
|
+
hooks: {
|
|
1743
|
+
onScroll: () => recalculate(),
|
|
1744
|
+
onResize: () => recalculate(),
|
|
1745
|
+
onItemMeasured: () => recalculate(),
|
|
1746
|
+
onVisibleRangeChange: (range) => {
|
|
1747
|
+
currentRange = range;
|
|
1748
|
+
virtualItems = buildVirtualItems(range);
|
|
1749
|
+
},
|
|
1750
|
+
onItemsChange: () => recalculate()
|
|
1751
|
+
},
|
|
1752
|
+
api
|
|
1753
|
+
};
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
// src/react/hooks/use-virtual-list.ts
|
|
1757
|
+
function useVirtualList(options) {
|
|
1758
|
+
const {
|
|
1759
|
+
count,
|
|
1760
|
+
getItemHeight,
|
|
1761
|
+
estimatedItemHeight = 50,
|
|
1762
|
+
overscan = 5,
|
|
1763
|
+
paddingStart = 0,
|
|
1764
|
+
paddingEnd = 0,
|
|
1765
|
+
initialOffset = 0,
|
|
1766
|
+
getItemKey = (index) => index,
|
|
1767
|
+
horizontal = false,
|
|
1768
|
+
containerRef
|
|
1769
|
+
} = options;
|
|
1770
|
+
const kernelRef = react.useRef(
|
|
1771
|
+
createKernel({
|
|
1772
|
+
itemCount: count,
|
|
1773
|
+
estimatedItemHeight,
|
|
1774
|
+
overscan,
|
|
1775
|
+
direction: horizontal ? "horizontal" : "vertical",
|
|
1776
|
+
getItemKey
|
|
1777
|
+
})
|
|
1778
|
+
);
|
|
1779
|
+
const [virtualItems, setVirtualItems] = react.useState([]);
|
|
1780
|
+
const [isScrolling, setIsScrolling] = react.useState(false);
|
|
1781
|
+
const [scrollOffset, setScrollOffset] = react.useState(initialOffset);
|
|
1782
|
+
const [range, setRange] = react.useState({
|
|
1783
|
+
startIndex: 0,
|
|
1784
|
+
endIndex: 0,
|
|
1785
|
+
overscanStartIndex: 0,
|
|
1786
|
+
overscanEndIndex: 0
|
|
1787
|
+
});
|
|
1788
|
+
const listRendererRef = react.useRef(null);
|
|
1789
|
+
const measurementRefs = react.useRef(/* @__PURE__ */ new Map());
|
|
1790
|
+
const resizeObserverRef = react.useRef(null);
|
|
1791
|
+
const kernel = kernelRef.current;
|
|
1792
|
+
react.useEffect(() => {
|
|
1793
|
+
const plugin = listRendererPlugin({ getItemKey });
|
|
1794
|
+
kernel.register(plugin);
|
|
1795
|
+
listRendererRef.current = plugin.api;
|
|
1796
|
+
const unsubScroll = kernel.on("scroll", (event) => {
|
|
1797
|
+
const e = event;
|
|
1798
|
+
setScrollOffset(horizontal ? e.scrollLeft : e.scrollTop);
|
|
1799
|
+
});
|
|
1800
|
+
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
1801
|
+
setIsScrolling(true);
|
|
1802
|
+
});
|
|
1803
|
+
const unsubScrollEnd = kernel.on("scroll-end", () => {
|
|
1804
|
+
setIsScrolling(false);
|
|
1805
|
+
});
|
|
1806
|
+
const unsubRangeChange = kernel.on("visible-range-change", (event) => {
|
|
1807
|
+
const e = event;
|
|
1808
|
+
setRange(e.range);
|
|
1809
|
+
setVirtualItems(listRendererRef.current?.getVirtualItems() ?? []);
|
|
1810
|
+
});
|
|
1811
|
+
return () => {
|
|
1812
|
+
unsubScroll();
|
|
1813
|
+
unsubScrollStart();
|
|
1814
|
+
unsubScrollEnd();
|
|
1815
|
+
unsubRangeChange();
|
|
1816
|
+
kernel.destroy();
|
|
1817
|
+
};
|
|
1818
|
+
}, []);
|
|
1819
|
+
react.useEffect(() => {
|
|
1820
|
+
const container = containerRef.current;
|
|
1821
|
+
if (!container) return;
|
|
1822
|
+
kernel.attach(container);
|
|
1823
|
+
if (initialOffset > 0) {
|
|
1824
|
+
kernel.scrollTo(initialOffset);
|
|
1825
|
+
}
|
|
1826
|
+
return () => {
|
|
1827
|
+
kernel.detach();
|
|
1828
|
+
};
|
|
1829
|
+
}, [containerRef, kernel, initialOffset]);
|
|
1830
|
+
react.useEffect(() => {
|
|
1831
|
+
kernel.configure({ itemCount: count });
|
|
1832
|
+
}, [count, kernel]);
|
|
1833
|
+
react.useEffect(() => {
|
|
1834
|
+
kernel.configure({ overscan });
|
|
1835
|
+
}, [overscan, kernel]);
|
|
1836
|
+
react.useEffect(() => {
|
|
1837
|
+
const observer = new ResizeObserver((entries) => {
|
|
1838
|
+
for (const entry of entries) {
|
|
1839
|
+
const element = entry.target;
|
|
1840
|
+
const index = parseInt(element.dataset["virtualIndex"] ?? "", 10);
|
|
1841
|
+
if (!isNaN(index)) {
|
|
1842
|
+
const rect = entry.contentRect;
|
|
1843
|
+
const size = horizontal ? rect.width : rect.height;
|
|
1844
|
+
if (size > 0) {
|
|
1845
|
+
kernel.measureItem(index, size);
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
});
|
|
1850
|
+
resizeObserverRef.current = observer;
|
|
1851
|
+
return () => {
|
|
1852
|
+
observer.disconnect();
|
|
1853
|
+
};
|
|
1854
|
+
}, [kernel, horizontal]);
|
|
1855
|
+
const measureElement = react.useCallback(
|
|
1856
|
+
(index, element) => {
|
|
1857
|
+
const observer = resizeObserverRef.current;
|
|
1858
|
+
const refs = measurementRefs.current;
|
|
1859
|
+
const oldElement = refs.get(index);
|
|
1860
|
+
if (oldElement && oldElement !== element) {
|
|
1861
|
+
observer?.unobserve(oldElement);
|
|
1862
|
+
refs.delete(index);
|
|
1863
|
+
}
|
|
1864
|
+
if (element) {
|
|
1865
|
+
element.dataset["virtualIndex"] = String(index);
|
|
1866
|
+
refs.set(index, element);
|
|
1867
|
+
observer?.observe(element);
|
|
1868
|
+
if (getItemHeight) {
|
|
1869
|
+
const size = getItemHeight(index);
|
|
1870
|
+
kernel.measureItem(index, size);
|
|
1871
|
+
} else {
|
|
1872
|
+
const rect = element.getBoundingClientRect();
|
|
1873
|
+
const size = horizontal ? rect.width : rect.height;
|
|
1874
|
+
if (size > 0) {
|
|
1875
|
+
kernel.measureItem(index, size);
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
},
|
|
1880
|
+
[kernel, horizontal, getItemHeight]
|
|
1881
|
+
);
|
|
1882
|
+
const scrollTo = react.useCallback(
|
|
1883
|
+
(offset, scrollOptions) => {
|
|
1884
|
+
kernel.scrollTo(offset, scrollOptions);
|
|
1885
|
+
},
|
|
1886
|
+
[kernel]
|
|
1887
|
+
);
|
|
1888
|
+
const scrollToIndex = react.useCallback(
|
|
1889
|
+
(index, scrollOptions) => {
|
|
1890
|
+
kernel.scrollToIndex(index, scrollOptions);
|
|
1891
|
+
},
|
|
1892
|
+
[kernel]
|
|
1893
|
+
);
|
|
1894
|
+
const getMeasurement = react.useCallback(
|
|
1895
|
+
(index) => kernel.getCachedHeight(index),
|
|
1896
|
+
[kernel]
|
|
1897
|
+
);
|
|
1898
|
+
const invalidateMeasurement = react.useCallback(
|
|
1899
|
+
(index) => kernel.invalidateMeasurement(index),
|
|
1900
|
+
[kernel]
|
|
1901
|
+
);
|
|
1902
|
+
const invalidateAllMeasurements = react.useCallback(
|
|
1903
|
+
() => kernel.invalidateAllMeasurements(),
|
|
1904
|
+
[kernel]
|
|
1905
|
+
);
|
|
1906
|
+
const totalSize = react.useMemo(() => {
|
|
1907
|
+
return paddingStart + kernel.getTotalSize() + paddingEnd;
|
|
1908
|
+
}, [kernel, paddingStart, paddingEnd, virtualItems]);
|
|
1909
|
+
return {
|
|
1910
|
+
virtualItems,
|
|
1911
|
+
totalSize,
|
|
1912
|
+
scrollOffset,
|
|
1913
|
+
isScrolling,
|
|
1914
|
+
scrollTo,
|
|
1915
|
+
scrollToIndex,
|
|
1916
|
+
measureElement,
|
|
1917
|
+
range,
|
|
1918
|
+
getMeasurement,
|
|
1919
|
+
invalidateMeasurement,
|
|
1920
|
+
invalidateAllMeasurements
|
|
1921
|
+
};
|
|
1922
|
+
}
|
|
1923
|
+
var ScrollexContext = react.createContext(null);
|
|
1924
|
+
function useScrollex() {
|
|
1925
|
+
const context = react.useContext(ScrollexContext);
|
|
1926
|
+
return context?.kernel ?? null;
|
|
1927
|
+
}
|
|
1928
|
+
function useScrollexRequired() {
|
|
1929
|
+
const context = react.useContext(ScrollexContext);
|
|
1930
|
+
if (!context) {
|
|
1931
|
+
throw new Error(
|
|
1932
|
+
"[Scrollex] useScrollexRequired must be used within a Scrollex component. Make sure you are rendering this component inside VirtualList, VirtualGrid, or VirtualMasonry."
|
|
1933
|
+
);
|
|
1934
|
+
}
|
|
1935
|
+
return context.kernel;
|
|
1936
|
+
}
|
|
1937
|
+
function VirtualListInner(props, ref) {
|
|
1938
|
+
const {
|
|
1939
|
+
data,
|
|
1940
|
+
renderItem,
|
|
1941
|
+
itemHeight,
|
|
1942
|
+
estimatedItemHeight = 50,
|
|
1943
|
+
height = "100%",
|
|
1944
|
+
width = "100%",
|
|
1945
|
+
direction = "vertical",
|
|
1946
|
+
overscan = 5,
|
|
1947
|
+
initialScrollOffset = 0,
|
|
1948
|
+
getItemKey,
|
|
1949
|
+
onLoadMore,
|
|
1950
|
+
hasMore = false,
|
|
1951
|
+
isLoading = false,
|
|
1952
|
+
loadingIndicator,
|
|
1953
|
+
threshold = 200,
|
|
1954
|
+
onScroll,
|
|
1955
|
+
onVisibleRangeChange,
|
|
1956
|
+
onItemsRendered,
|
|
1957
|
+
plugins = [],
|
|
1958
|
+
className,
|
|
1959
|
+
style,
|
|
1960
|
+
innerClassName,
|
|
1961
|
+
innerStyle,
|
|
1962
|
+
itemClassName,
|
|
1963
|
+
itemStyle,
|
|
1964
|
+
role = "listbox",
|
|
1965
|
+
ariaLabel,
|
|
1966
|
+
tabIndex,
|
|
1967
|
+
...restProps
|
|
1968
|
+
} = props;
|
|
1969
|
+
const containerRef = react.useRef(null);
|
|
1970
|
+
const {
|
|
1971
|
+
virtualItems,
|
|
1972
|
+
totalSize,
|
|
1973
|
+
scrollTo,
|
|
1974
|
+
scrollToIndex,
|
|
1975
|
+
measureElement,
|
|
1976
|
+
isScrolling,
|
|
1977
|
+
range
|
|
1978
|
+
} = useVirtualList({
|
|
1979
|
+
count: data.length,
|
|
1980
|
+
getItemHeight: itemHeight === "auto" ? void 0 : () => itemHeight,
|
|
1981
|
+
estimatedItemHeight: itemHeight === "auto" ? estimatedItemHeight : itemHeight,
|
|
1982
|
+
overscan,
|
|
1983
|
+
initialOffset: initialScrollOffset,
|
|
1984
|
+
getItemKey: getItemKey ? (index) => getItemKey(index, data) : void 0,
|
|
1985
|
+
horizontal: direction === "horizontal",
|
|
1986
|
+
containerRef
|
|
1987
|
+
});
|
|
1988
|
+
const kernelRef = react.useRef(createKernel({
|
|
1989
|
+
itemCount: data.length,
|
|
1990
|
+
estimatedItemHeight: itemHeight === "auto" ? estimatedItemHeight : itemHeight,
|
|
1991
|
+
overscan,
|
|
1992
|
+
direction
|
|
1993
|
+
}));
|
|
1994
|
+
react.useImperativeHandle(ref, () => ({
|
|
1995
|
+
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
1996
|
+
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
1997
|
+
scrollToTop: (options) => scrollTo(0, options),
|
|
1998
|
+
scrollToBottom: (options) => scrollTo(totalSize, options),
|
|
1999
|
+
getScrollPosition: () => ({
|
|
2000
|
+
scrollTop: containerRef.current?.scrollTop ?? 0,
|
|
2001
|
+
scrollLeft: containerRef.current?.scrollLeft ?? 0
|
|
2002
|
+
})
|
|
2003
|
+
}), [scrollTo, scrollToIndex, totalSize]);
|
|
2004
|
+
const handleScroll = react.useCallback((event) => {
|
|
2005
|
+
const target = event.currentTarget;
|
|
2006
|
+
onScroll?.(target.scrollTop, target.scrollLeft);
|
|
2007
|
+
}, [onScroll]);
|
|
2008
|
+
const handleScrollForInfinite = react.useCallback((event) => {
|
|
2009
|
+
if (!onLoadMore || !hasMore || isLoading) return;
|
|
2010
|
+
const target = event.currentTarget;
|
|
2011
|
+
const scrollBottom = target.scrollHeight - target.scrollTop - target.clientHeight;
|
|
2012
|
+
if (scrollBottom < threshold) {
|
|
2013
|
+
onLoadMore();
|
|
2014
|
+
}
|
|
2015
|
+
}, [onLoadMore, hasMore, isLoading, threshold]);
|
|
2016
|
+
const containerStyles = react.useMemo(() => ({
|
|
2017
|
+
height,
|
|
2018
|
+
width,
|
|
2019
|
+
overflow: "auto",
|
|
2020
|
+
position: "relative",
|
|
2021
|
+
contain: "strict",
|
|
2022
|
+
...style
|
|
2023
|
+
}), [height, width, style]);
|
|
2024
|
+
const innerContainerStyles = react.useMemo(() => ({
|
|
2025
|
+
height: direction === "vertical" ? totalSize : "100%",
|
|
2026
|
+
width: direction === "horizontal" ? totalSize : "100%",
|
|
2027
|
+
position: "relative",
|
|
2028
|
+
contain: "size layout",
|
|
2029
|
+
...innerStyle
|
|
2030
|
+
}), [totalSize, direction, innerStyle]);
|
|
2031
|
+
const createRenderProps = react.useCallback(
|
|
2032
|
+
(index) => {
|
|
2033
|
+
const item = data[index];
|
|
2034
|
+
if (item === void 0) {
|
|
2035
|
+
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
2036
|
+
}
|
|
2037
|
+
const virtualItem = virtualItems.find((vi) => vi.index === index);
|
|
2038
|
+
const itemOffset = virtualItem?.start ?? 0;
|
|
2039
|
+
const itemSize = virtualItem?.size ?? estimatedItemHeight;
|
|
2040
|
+
const itemStyles = {
|
|
2041
|
+
position: "absolute",
|
|
2042
|
+
top: 0,
|
|
2043
|
+
left: 0,
|
|
2044
|
+
width: direction === "vertical" ? "100%" : itemSize,
|
|
2045
|
+
height: direction === "vertical" ? itemSize : "100%",
|
|
2046
|
+
transform: direction === "vertical" ? `translateY(${itemOffset}px)` : `translateX(${itemOffset}px)`,
|
|
2047
|
+
willChange: "transform",
|
|
2048
|
+
contain: "layout style",
|
|
2049
|
+
...itemStyle
|
|
2050
|
+
};
|
|
2051
|
+
return {
|
|
2052
|
+
item,
|
|
2053
|
+
index,
|
|
2054
|
+
style: itemStyles,
|
|
2055
|
+
measureRef: (el) => measureElement(index, el),
|
|
2056
|
+
isVisible: index >= range.startIndex && index <= range.endIndex,
|
|
2057
|
+
isScrolling
|
|
2058
|
+
};
|
|
2059
|
+
},
|
|
2060
|
+
[data, virtualItems, estimatedItemHeight, direction, itemStyle, measureElement, range, isScrolling]
|
|
2061
|
+
);
|
|
2062
|
+
const contextValue = react.useMemo(() => ({
|
|
2063
|
+
kernel: kernelRef.current
|
|
2064
|
+
}), []);
|
|
2065
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2066
|
+
"div",
|
|
2067
|
+
{
|
|
2068
|
+
ref: containerRef,
|
|
2069
|
+
className,
|
|
2070
|
+
style: containerStyles,
|
|
2071
|
+
onScroll: (e) => {
|
|
2072
|
+
handleScroll(e);
|
|
2073
|
+
handleScrollForInfinite(e);
|
|
2074
|
+
},
|
|
2075
|
+
role,
|
|
2076
|
+
"aria-label": ariaLabel,
|
|
2077
|
+
"aria-rowcount": data.length,
|
|
2078
|
+
tabIndex,
|
|
2079
|
+
...restProps,
|
|
2080
|
+
children: [
|
|
2081
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2082
|
+
const index = virtualItem.index;
|
|
2083
|
+
const item = data[index];
|
|
2084
|
+
if (item === void 0) return null;
|
|
2085
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2086
|
+
"div",
|
|
2087
|
+
{
|
|
2088
|
+
className: itemClassName,
|
|
2089
|
+
"data-index": index,
|
|
2090
|
+
role: "option",
|
|
2091
|
+
"aria-rowindex": index + 1,
|
|
2092
|
+
children: renderItem(createRenderProps(index))
|
|
2093
|
+
},
|
|
2094
|
+
virtualItem.key
|
|
2095
|
+
);
|
|
2096
|
+
}) }),
|
|
2097
|
+
isLoading && loadingIndicator
|
|
2098
|
+
]
|
|
2099
|
+
}
|
|
2100
|
+
) });
|
|
2101
|
+
}
|
|
2102
|
+
var VirtualList = react.forwardRef(VirtualListInner);
|
|
2103
|
+
|
|
2104
|
+
// src/plugins/core/grid-renderer.ts
|
|
2105
|
+
function gridRendererPlugin(options) {
|
|
2106
|
+
const {
|
|
2107
|
+
columns,
|
|
2108
|
+
minColumnWidth = 100,
|
|
2109
|
+
rowHeight,
|
|
2110
|
+
gap = 0,
|
|
2111
|
+
getItemKey = (index) => index
|
|
2112
|
+
} = options;
|
|
2113
|
+
let kernel = null;
|
|
2114
|
+
let virtualItems = [];
|
|
2115
|
+
let columnCount = typeof columns === "number" ? columns : 1;
|
|
2116
|
+
let columnWidth = 0;
|
|
2117
|
+
let gapX = typeof gap === "number" ? gap : gap.x;
|
|
2118
|
+
let gapY = typeof gap === "number" ? gap : gap.y;
|
|
2119
|
+
function calculateColumns(containerWidth) {
|
|
2120
|
+
if (typeof columns === "number") {
|
|
2121
|
+
columnCount = columns;
|
|
2122
|
+
} else {
|
|
2123
|
+
columnCount = Math.max(1, Math.floor((containerWidth + gapX) / (minColumnWidth + gapX)));
|
|
2124
|
+
}
|
|
2125
|
+
columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
|
|
2126
|
+
}
|
|
2127
|
+
function getRowCount() {
|
|
2128
|
+
if (!kernel) return 0;
|
|
2129
|
+
return Math.ceil(kernel.getItemCount() / columnCount);
|
|
2130
|
+
}
|
|
2131
|
+
function buildVirtualItems() {
|
|
2132
|
+
if (!kernel) return [];
|
|
2133
|
+
const viewport = kernel.getViewport();
|
|
2134
|
+
const scrollPosition = kernel.getScrollPosition();
|
|
2135
|
+
const scrollTop = scrollPosition.scrollTop;
|
|
2136
|
+
const effectiveRowHeight = rowHeight + gapY;
|
|
2137
|
+
const startRow = Math.max(0, Math.floor(scrollTop / effectiveRowHeight));
|
|
2138
|
+
const visibleRows = Math.ceil(viewport.height / effectiveRowHeight);
|
|
2139
|
+
const overscan = kernel.getOptions().overscan;
|
|
2140
|
+
const endRow = Math.min(getRowCount() - 1, startRow + visibleRows + overscan);
|
|
2141
|
+
const overscanStartRow = Math.max(0, startRow - overscan);
|
|
2142
|
+
const items = [];
|
|
2143
|
+
const itemCount = kernel.getItemCount();
|
|
2144
|
+
for (let row = overscanStartRow; row <= endRow; row++) {
|
|
2145
|
+
for (let col = 0; col < columnCount; col++) {
|
|
2146
|
+
const index = row * columnCount + col;
|
|
2147
|
+
if (index >= itemCount) break;
|
|
2148
|
+
const top = row * effectiveRowHeight;
|
|
2149
|
+
items.push({
|
|
2150
|
+
index,
|
|
2151
|
+
key: getItemKey(index),
|
|
2152
|
+
start: top,
|
|
2153
|
+
end: top + rowHeight,
|
|
2154
|
+
size: rowHeight,
|
|
2155
|
+
lane: col
|
|
2156
|
+
});
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
return items;
|
|
2160
|
+
}
|
|
2161
|
+
function recalculate() {
|
|
2162
|
+
if (!kernel) return;
|
|
2163
|
+
const viewport = kernel.getViewport();
|
|
2164
|
+
calculateColumns(viewport.width);
|
|
2165
|
+
virtualItems = buildVirtualItems();
|
|
2166
|
+
}
|
|
2167
|
+
const api = {
|
|
2168
|
+
getVirtualItems() {
|
|
2169
|
+
return virtualItems;
|
|
2170
|
+
},
|
|
2171
|
+
getTotalHeight() {
|
|
2172
|
+
return getRowCount() * (rowHeight + gapY) - gapY;
|
|
2173
|
+
},
|
|
2174
|
+
getColumnCount() {
|
|
2175
|
+
return columnCount;
|
|
2176
|
+
},
|
|
2177
|
+
getColumnWidth() {
|
|
2178
|
+
return columnWidth;
|
|
2179
|
+
},
|
|
2180
|
+
getItemsPerRow() {
|
|
2181
|
+
return columnCount;
|
|
2182
|
+
},
|
|
2183
|
+
getRowCount,
|
|
2184
|
+
recalculate
|
|
2185
|
+
};
|
|
2186
|
+
return {
|
|
2187
|
+
name: "grid-renderer",
|
|
2188
|
+
version: "1.0.0",
|
|
2189
|
+
type: "core",
|
|
2190
|
+
install(k) {
|
|
2191
|
+
kernel = k;
|
|
2192
|
+
recalculate();
|
|
2193
|
+
},
|
|
2194
|
+
uninstall() {
|
|
2195
|
+
virtualItems = [];
|
|
2196
|
+
kernel = null;
|
|
2197
|
+
},
|
|
2198
|
+
hooks: {
|
|
2199
|
+
onScroll: () => {
|
|
2200
|
+
virtualItems = buildVirtualItems();
|
|
2201
|
+
},
|
|
2202
|
+
onResize: () => recalculate(),
|
|
2203
|
+
onItemsChange: () => recalculate()
|
|
2204
|
+
},
|
|
2205
|
+
api
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
// src/react/hooks/use-virtual-grid.ts
|
|
2210
|
+
function useVirtualGrid(options) {
|
|
2211
|
+
const {
|
|
2212
|
+
count,
|
|
2213
|
+
columns,
|
|
2214
|
+
rowHeight,
|
|
2215
|
+
gap = 0,
|
|
2216
|
+
overscan = 2,
|
|
2217
|
+
initialOffset = 0,
|
|
2218
|
+
getItemKey = (index) => index,
|
|
2219
|
+
containerRef
|
|
2220
|
+
} = options;
|
|
2221
|
+
const gapX = typeof gap === "number" ? gap : gap.x;
|
|
2222
|
+
const gapY = typeof gap === "number" ? gap : gap.y;
|
|
2223
|
+
const kernelRef = react.useRef(
|
|
2224
|
+
createKernel({
|
|
2225
|
+
itemCount: count,
|
|
2226
|
+
estimatedItemHeight: rowHeight,
|
|
2227
|
+
overscan,
|
|
2228
|
+
direction: "vertical",
|
|
2229
|
+
getItemKey
|
|
2230
|
+
})
|
|
2231
|
+
);
|
|
2232
|
+
const [virtualItems, setVirtualItems] = react.useState([]);
|
|
2233
|
+
const [isScrolling, setIsScrolling] = react.useState(false);
|
|
2234
|
+
const [scrollOffset, setScrollOffset] = react.useState(initialOffset);
|
|
2235
|
+
const [range, setRange] = react.useState({
|
|
2236
|
+
startIndex: 0,
|
|
2237
|
+
endIndex: 0,
|
|
2238
|
+
overscanStartIndex: 0,
|
|
2239
|
+
overscanEndIndex: 0
|
|
2240
|
+
});
|
|
2241
|
+
const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? columns : 1);
|
|
2242
|
+
const [columnWidth, setColumnWidth] = react.useState(0);
|
|
2243
|
+
const gridRendererRef = react.useRef(null);
|
|
2244
|
+
const kernel = kernelRef.current;
|
|
2245
|
+
react.useEffect(() => {
|
|
2246
|
+
const plugin = gridRendererPlugin({
|
|
2247
|
+
columns,
|
|
2248
|
+
rowHeight,
|
|
2249
|
+
gap: { x: gapX, y: gapY },
|
|
2250
|
+
getItemKey
|
|
2251
|
+
});
|
|
2252
|
+
kernel.register(plugin);
|
|
2253
|
+
gridRendererRef.current = plugin.api;
|
|
2254
|
+
const unsubScroll = kernel.on("scroll", (event) => {
|
|
2255
|
+
const e = event;
|
|
2256
|
+
setScrollOffset(e.scrollTop);
|
|
2257
|
+
setVirtualItems(gridRendererRef.current?.getVirtualItems() ?? []);
|
|
2258
|
+
});
|
|
2259
|
+
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
2260
|
+
setIsScrolling(true);
|
|
2261
|
+
});
|
|
2262
|
+
const unsubScrollEnd = kernel.on("scroll-end", () => {
|
|
2263
|
+
setIsScrolling(false);
|
|
2264
|
+
});
|
|
2265
|
+
const unsubResize = kernel.on("resize", () => {
|
|
2266
|
+
const api = gridRendererRef.current;
|
|
2267
|
+
if (api) {
|
|
2268
|
+
api.recalculate();
|
|
2269
|
+
setColumnCount(api.getColumnCount());
|
|
2270
|
+
setColumnWidth(api.getColumnWidth());
|
|
2271
|
+
setVirtualItems(api.getVirtualItems());
|
|
2272
|
+
}
|
|
2273
|
+
});
|
|
2274
|
+
const unsubRangeChange = kernel.on("visible-range-change", (event) => {
|
|
2275
|
+
const e = event;
|
|
2276
|
+
setRange(e.range);
|
|
2277
|
+
});
|
|
2278
|
+
return () => {
|
|
2279
|
+
unsubScroll();
|
|
2280
|
+
unsubScrollStart();
|
|
2281
|
+
unsubScrollEnd();
|
|
2282
|
+
unsubResize();
|
|
2283
|
+
unsubRangeChange();
|
|
2284
|
+
kernel.destroy();
|
|
2285
|
+
};
|
|
2286
|
+
}, []);
|
|
2287
|
+
react.useEffect(() => {
|
|
2288
|
+
const container = containerRef.current;
|
|
2289
|
+
if (!container) return;
|
|
2290
|
+
kernel.attach(container);
|
|
2291
|
+
const api = gridRendererRef.current;
|
|
2292
|
+
if (api) {
|
|
2293
|
+
api.recalculate();
|
|
2294
|
+
setColumnCount(api.getColumnCount());
|
|
2295
|
+
setColumnWidth(api.getColumnWidth());
|
|
2296
|
+
setVirtualItems(api.getVirtualItems());
|
|
2297
|
+
}
|
|
2298
|
+
if (initialOffset > 0) {
|
|
2299
|
+
kernel.scrollTo(initialOffset);
|
|
2300
|
+
}
|
|
2301
|
+
return () => {
|
|
2302
|
+
kernel.detach();
|
|
2303
|
+
};
|
|
2304
|
+
}, [containerRef, kernel, initialOffset]);
|
|
2305
|
+
react.useEffect(() => {
|
|
2306
|
+
kernel.configure({ itemCount: count });
|
|
2307
|
+
const api = gridRendererRef.current;
|
|
2308
|
+
if (api) {
|
|
2309
|
+
api.recalculate();
|
|
2310
|
+
setVirtualItems(api.getVirtualItems());
|
|
2311
|
+
}
|
|
2312
|
+
}, [count, kernel]);
|
|
2313
|
+
const scrollTo = react.useCallback(
|
|
2314
|
+
(offset, scrollOptions) => {
|
|
2315
|
+
kernel.scrollTo(offset, scrollOptions);
|
|
2316
|
+
},
|
|
2317
|
+
[kernel]
|
|
2318
|
+
);
|
|
2319
|
+
const scrollToIndex = react.useCallback(
|
|
2320
|
+
(index, scrollOptions) => {
|
|
2321
|
+
const row = Math.floor(index / columnCount);
|
|
2322
|
+
const rowOffset = row * (rowHeight + gapY);
|
|
2323
|
+
kernel.scrollTo(rowOffset, scrollOptions);
|
|
2324
|
+
},
|
|
2325
|
+
[kernel, columnCount, rowHeight, gapY]
|
|
2326
|
+
);
|
|
2327
|
+
const measureElement = react.useCallback(
|
|
2328
|
+
(_index, _element) => {
|
|
2329
|
+
},
|
|
2330
|
+
[]
|
|
2331
|
+
);
|
|
2332
|
+
const getMeasurement = react.useCallback(
|
|
2333
|
+
(_index) => rowHeight,
|
|
2334
|
+
[rowHeight]
|
|
2335
|
+
);
|
|
2336
|
+
const invalidateMeasurement = react.useCallback(
|
|
2337
|
+
(_index) => {
|
|
2338
|
+
},
|
|
2339
|
+
[]
|
|
2340
|
+
);
|
|
2341
|
+
const invalidateAllMeasurements = react.useCallback(() => {
|
|
2342
|
+
}, []);
|
|
2343
|
+
const totalSize = react.useMemo(() => {
|
|
2344
|
+
return gridRendererRef.current?.getTotalHeight() ?? 0;
|
|
2345
|
+
}, [virtualItems]);
|
|
2346
|
+
const rowCount = react.useMemo(() => {
|
|
2347
|
+
return gridRendererRef.current?.getRowCount() ?? 0;
|
|
2348
|
+
}, [count, columnCount]);
|
|
2349
|
+
return {
|
|
2350
|
+
virtualItems,
|
|
2351
|
+
totalSize,
|
|
2352
|
+
scrollOffset,
|
|
2353
|
+
isScrolling,
|
|
2354
|
+
scrollTo,
|
|
2355
|
+
scrollToIndex,
|
|
2356
|
+
measureElement,
|
|
2357
|
+
range,
|
|
2358
|
+
getMeasurement,
|
|
2359
|
+
invalidateMeasurement,
|
|
2360
|
+
invalidateAllMeasurements,
|
|
2361
|
+
columnCount,
|
|
2362
|
+
columnWidth,
|
|
2363
|
+
rowCount
|
|
2364
|
+
};
|
|
2365
|
+
}
|
|
2366
|
+
function VirtualGridInner(props, ref) {
|
|
2367
|
+
const {
|
|
2368
|
+
data,
|
|
2369
|
+
renderItem,
|
|
2370
|
+
itemHeight,
|
|
2371
|
+
columns,
|
|
2372
|
+
minColumnWidth = 100,
|
|
2373
|
+
gap = 0,
|
|
2374
|
+
height = "100%",
|
|
2375
|
+
width = "100%",
|
|
2376
|
+
overscan = 2,
|
|
2377
|
+
initialScrollOffset = 0,
|
|
2378
|
+
getItemKey,
|
|
2379
|
+
onLoadMore,
|
|
2380
|
+
hasMore = false,
|
|
2381
|
+
isLoading = false,
|
|
2382
|
+
loadingIndicator,
|
|
2383
|
+
threshold = 200,
|
|
2384
|
+
onScroll,
|
|
2385
|
+
onVisibleRangeChange,
|
|
2386
|
+
onItemsRendered,
|
|
2387
|
+
plugins = [],
|
|
2388
|
+
className,
|
|
2389
|
+
style,
|
|
2390
|
+
innerClassName,
|
|
2391
|
+
innerStyle,
|
|
2392
|
+
itemClassName,
|
|
2393
|
+
itemStyle,
|
|
2394
|
+
role = "grid",
|
|
2395
|
+
ariaLabel,
|
|
2396
|
+
tabIndex,
|
|
2397
|
+
...restProps
|
|
2398
|
+
} = props;
|
|
2399
|
+
const containerRef = react.useRef(null);
|
|
2400
|
+
const gapX = typeof gap === "number" ? gap : gap.x;
|
|
2401
|
+
const gapY = typeof gap === "number" ? gap : gap.y;
|
|
2402
|
+
const {
|
|
2403
|
+
virtualItems,
|
|
2404
|
+
totalSize,
|
|
2405
|
+
scrollTo,
|
|
2406
|
+
scrollToIndex,
|
|
2407
|
+
isScrolling,
|
|
2408
|
+
range,
|
|
2409
|
+
columnCount,
|
|
2410
|
+
columnWidth,
|
|
2411
|
+
rowCount
|
|
2412
|
+
} = useVirtualGrid({
|
|
2413
|
+
count: data.length,
|
|
2414
|
+
columns: typeof columns === "number" ? columns : 1,
|
|
2415
|
+
// TODO: handle auto
|
|
2416
|
+
rowHeight: itemHeight,
|
|
2417
|
+
gap: { x: gapX, y: gapY },
|
|
2418
|
+
overscan,
|
|
2419
|
+
initialOffset: initialScrollOffset,
|
|
2420
|
+
getItemKey: getItemKey ? (index) => getItemKey(index, data) : void 0,
|
|
2421
|
+
containerRef
|
|
2422
|
+
});
|
|
2423
|
+
const kernelRef = react.useRef(createKernel({
|
|
2424
|
+
itemCount: data.length,
|
|
2425
|
+
estimatedItemHeight: itemHeight,
|
|
2426
|
+
overscan,
|
|
2427
|
+
direction: "vertical"
|
|
2428
|
+
}));
|
|
2429
|
+
react.useImperativeHandle(ref, () => ({
|
|
2430
|
+
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
2431
|
+
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
2432
|
+
scrollToTop: (options) => scrollTo(0, options),
|
|
2433
|
+
scrollToBottom: (options) => scrollTo(totalSize, options),
|
|
2434
|
+
getScrollPosition: () => ({
|
|
2435
|
+
scrollTop: containerRef.current?.scrollTop ?? 0,
|
|
2436
|
+
scrollLeft: containerRef.current?.scrollLeft ?? 0
|
|
2437
|
+
})
|
|
2438
|
+
}), [scrollTo, scrollToIndex, totalSize]);
|
|
2439
|
+
const handleScroll = react.useCallback((event) => {
|
|
2440
|
+
const target = event.currentTarget;
|
|
2441
|
+
onScroll?.(target.scrollTop, target.scrollLeft);
|
|
2442
|
+
}, [onScroll]);
|
|
2443
|
+
const handleScrollForInfinite = react.useCallback((event) => {
|
|
2444
|
+
if (!onLoadMore || !hasMore || isLoading) return;
|
|
2445
|
+
const target = event.currentTarget;
|
|
2446
|
+
const scrollBottom = target.scrollHeight - target.scrollTop - target.clientHeight;
|
|
2447
|
+
if (scrollBottom < threshold) {
|
|
2448
|
+
onLoadMore();
|
|
2449
|
+
}
|
|
2450
|
+
}, [onLoadMore, hasMore, isLoading, threshold]);
|
|
2451
|
+
const containerStyles = react.useMemo(() => ({
|
|
2452
|
+
height,
|
|
2453
|
+
width,
|
|
2454
|
+
overflow: "auto",
|
|
2455
|
+
position: "relative",
|
|
2456
|
+
contain: "strict",
|
|
2457
|
+
...style
|
|
2458
|
+
}), [height, width, style]);
|
|
2459
|
+
const innerContainerStyles = react.useMemo(() => ({
|
|
2460
|
+
height: totalSize,
|
|
2461
|
+
width: "100%",
|
|
2462
|
+
position: "relative",
|
|
2463
|
+
contain: "size layout",
|
|
2464
|
+
...innerStyle
|
|
2465
|
+
}), [totalSize, innerStyle]);
|
|
2466
|
+
const createRenderProps = react.useCallback(
|
|
2467
|
+
(index, virtualItem) => {
|
|
2468
|
+
const item = data[index];
|
|
2469
|
+
if (item === void 0) {
|
|
2470
|
+
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
2471
|
+
}
|
|
2472
|
+
const columnIndex = virtualItem.lane;
|
|
2473
|
+
const rowIndex = Math.floor(index / columnCount);
|
|
2474
|
+
const left = columnIndex * (columnWidth + gapX);
|
|
2475
|
+
const itemStyles = {
|
|
2476
|
+
position: "absolute",
|
|
2477
|
+
top: 0,
|
|
2478
|
+
left: 0,
|
|
2479
|
+
width: columnWidth,
|
|
2480
|
+
height: virtualItem.size,
|
|
2481
|
+
transform: `translate(${left}px, ${virtualItem.start}px)`,
|
|
2482
|
+
willChange: "transform",
|
|
2483
|
+
contain: "layout style",
|
|
2484
|
+
...itemStyle
|
|
2485
|
+
};
|
|
2486
|
+
return {
|
|
2487
|
+
item,
|
|
2488
|
+
index,
|
|
2489
|
+
style: itemStyles,
|
|
2490
|
+
measureRef: () => {
|
|
2491
|
+
},
|
|
2492
|
+
// Grid uses fixed heights
|
|
2493
|
+
isVisible: index >= range.startIndex && index <= range.endIndex,
|
|
2494
|
+
isScrolling,
|
|
2495
|
+
columnIndex,
|
|
2496
|
+
rowIndex,
|
|
2497
|
+
columnWidth
|
|
2498
|
+
};
|
|
2499
|
+
},
|
|
2500
|
+
[data, columnCount, columnWidth, gapX, itemStyle, range, isScrolling]
|
|
2501
|
+
);
|
|
2502
|
+
const contextValue = react.useMemo(() => ({
|
|
2503
|
+
kernel: kernelRef.current
|
|
2504
|
+
}), []);
|
|
2505
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2506
|
+
"div",
|
|
2507
|
+
{
|
|
2508
|
+
ref: containerRef,
|
|
2509
|
+
className,
|
|
2510
|
+
style: containerStyles,
|
|
2511
|
+
onScroll: (e) => {
|
|
2512
|
+
handleScroll(e);
|
|
2513
|
+
handleScrollForInfinite(e);
|
|
2514
|
+
},
|
|
2515
|
+
role,
|
|
2516
|
+
"aria-label": ariaLabel,
|
|
2517
|
+
"aria-rowcount": rowCount,
|
|
2518
|
+
"aria-colcount": columnCount,
|
|
2519
|
+
tabIndex,
|
|
2520
|
+
...restProps,
|
|
2521
|
+
children: [
|
|
2522
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2523
|
+
const index = virtualItem.index;
|
|
2524
|
+
const item = data[index];
|
|
2525
|
+
if (item === void 0) return null;
|
|
2526
|
+
const rowIndex = Math.floor(index / columnCount);
|
|
2527
|
+
const columnIndex = virtualItem.lane;
|
|
2528
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2529
|
+
"div",
|
|
2530
|
+
{
|
|
2531
|
+
className: itemClassName,
|
|
2532
|
+
"data-index": index,
|
|
2533
|
+
role: "gridcell",
|
|
2534
|
+
"aria-rowindex": rowIndex + 1,
|
|
2535
|
+
"aria-colindex": columnIndex + 1,
|
|
2536
|
+
children: renderItem(createRenderProps(index, virtualItem))
|
|
2537
|
+
},
|
|
2538
|
+
virtualItem.key
|
|
2539
|
+
);
|
|
2540
|
+
}) }),
|
|
2541
|
+
isLoading && loadingIndicator
|
|
2542
|
+
]
|
|
2543
|
+
}
|
|
2544
|
+
) });
|
|
2545
|
+
}
|
|
2546
|
+
var VirtualGrid = react.forwardRef(VirtualGridInner);
|
|
2547
|
+
function useVirtualMasonry(options) {
|
|
2548
|
+
const {
|
|
2549
|
+
count,
|
|
2550
|
+
columns,
|
|
2551
|
+
getItemHeight,
|
|
2552
|
+
gap = 0,
|
|
2553
|
+
overscan = 5,
|
|
2554
|
+
containerRef
|
|
2555
|
+
} = options;
|
|
2556
|
+
const kernelRef = react.useRef(
|
|
2557
|
+
createKernel({
|
|
2558
|
+
itemCount: count,
|
|
2559
|
+
estimatedItemHeight: 200,
|
|
2560
|
+
overscan,
|
|
2561
|
+
direction: "vertical"
|
|
2562
|
+
})
|
|
2563
|
+
);
|
|
2564
|
+
const [virtualItems, setVirtualItems] = react.useState([]);
|
|
2565
|
+
const [isScrolling, setIsScrolling] = react.useState(false);
|
|
2566
|
+
const [scrollOffset, setScrollOffset] = react.useState(0);
|
|
2567
|
+
const [range, setRange] = react.useState({
|
|
2568
|
+
startIndex: 0,
|
|
2569
|
+
endIndex: 0,
|
|
2570
|
+
overscanStartIndex: 0,
|
|
2571
|
+
overscanEndIndex: 0
|
|
2572
|
+
});
|
|
2573
|
+
const [columnWidth, setColumnWidth] = react.useState(0);
|
|
2574
|
+
const [columnHeights, setColumnHeights] = react.useState([]);
|
|
2575
|
+
const [totalSize, setTotalSize] = react.useState(0);
|
|
2576
|
+
const layoutCacheRef = react.useRef(/* @__PURE__ */ new Map());
|
|
2577
|
+
const kernel = kernelRef.current;
|
|
2578
|
+
const calculateLayout = react.useCallback((containerWidth) => {
|
|
2579
|
+
if (containerWidth === 0 || count === 0) {
|
|
2580
|
+
setVirtualItems([]);
|
|
2581
|
+
setColumnHeights([]);
|
|
2582
|
+
setTotalSize(0);
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
const newColumnWidth = (containerWidth - (columns - 1) * gap) / columns;
|
|
2586
|
+
setColumnWidth(newColumnWidth);
|
|
2587
|
+
const heights = new Array(columns).fill(0);
|
|
2588
|
+
const cache = /* @__PURE__ */ new Map();
|
|
2589
|
+
for (let i = 0; i < count; i++) {
|
|
2590
|
+
let shortestCol = 0;
|
|
2591
|
+
for (let j = 1; j < columns; j++) {
|
|
2592
|
+
if ((heights[j] ?? 0) < (heights[shortestCol] ?? 0)) {
|
|
2593
|
+
shortestCol = j;
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
const itemHeight = getItemHeight(i, newColumnWidth);
|
|
2597
|
+
const top = heights[shortestCol] ?? 0;
|
|
2598
|
+
cache.set(i, { top, lane: shortestCol, height: itemHeight });
|
|
2599
|
+
heights[shortestCol] = top + itemHeight + gap;
|
|
2600
|
+
}
|
|
2601
|
+
layoutCacheRef.current = cache;
|
|
2602
|
+
setColumnHeights(heights);
|
|
2603
|
+
setTotalSize(Math.max(...heights) - gap);
|
|
2604
|
+
}, [count, columns, gap, getItemHeight]);
|
|
2605
|
+
const getVisibleItems = react.useCallback((scrollTop, viewportHeight) => {
|
|
2606
|
+
const cache = layoutCacheRef.current;
|
|
2607
|
+
const items = [];
|
|
2608
|
+
const overscanAmount = overscan * 100;
|
|
2609
|
+
const viewStart = Math.max(0, scrollTop - overscanAmount);
|
|
2610
|
+
const viewEnd = scrollTop + viewportHeight + overscanAmount;
|
|
2611
|
+
for (let i = 0; i < count; i++) {
|
|
2612
|
+
const layout = cache.get(i);
|
|
2613
|
+
if (!layout) continue;
|
|
2614
|
+
const itemEnd = layout.top + layout.height;
|
|
2615
|
+
if (itemEnd >= viewStart && layout.top <= viewEnd) {
|
|
2616
|
+
items.push({
|
|
2617
|
+
index: i,
|
|
2618
|
+
key: i,
|
|
2619
|
+
start: layout.top,
|
|
2620
|
+
end: itemEnd,
|
|
2621
|
+
size: layout.height,
|
|
2622
|
+
lane: layout.lane
|
|
2623
|
+
});
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
return items;
|
|
2627
|
+
}, [count, overscan]);
|
|
2628
|
+
react.useEffect(() => {
|
|
2629
|
+
const unsubScrollStart = kernel.on("scroll-start", () => {
|
|
2630
|
+
setIsScrolling(true);
|
|
2631
|
+
});
|
|
2632
|
+
const unsubScrollEnd = kernel.on("scroll-end", () => {
|
|
2633
|
+
setIsScrolling(false);
|
|
2634
|
+
});
|
|
2635
|
+
return () => {
|
|
2636
|
+
unsubScrollStart();
|
|
2637
|
+
unsubScrollEnd();
|
|
2638
|
+
kernel.destroy();
|
|
2639
|
+
};
|
|
2640
|
+
}, [kernel]);
|
|
2641
|
+
react.useEffect(() => {
|
|
2642
|
+
const container = containerRef.current;
|
|
2643
|
+
if (!container) return;
|
|
2644
|
+
kernel.attach(container);
|
|
2645
|
+
calculateLayout(container.clientWidth);
|
|
2646
|
+
const handleScroll = () => {
|
|
2647
|
+
const scrollTop = container.scrollTop;
|
|
2648
|
+
setScrollOffset(scrollTop);
|
|
2649
|
+
setVirtualItems(getVisibleItems(scrollTop, container.clientHeight));
|
|
2650
|
+
};
|
|
2651
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
2652
|
+
for (const entry of entries) {
|
|
2653
|
+
calculateLayout(entry.contentRect.width);
|
|
2654
|
+
setVirtualItems(getVisibleItems(container.scrollTop, entry.contentRect.height));
|
|
2655
|
+
}
|
|
2656
|
+
});
|
|
2657
|
+
container.addEventListener("scroll", handleScroll, { passive: true });
|
|
2658
|
+
resizeObserver.observe(container);
|
|
2659
|
+
handleScroll();
|
|
2660
|
+
return () => {
|
|
2661
|
+
container.removeEventListener("scroll", handleScroll);
|
|
2662
|
+
resizeObserver.disconnect();
|
|
2663
|
+
kernel.detach();
|
|
2664
|
+
};
|
|
2665
|
+
}, [containerRef, kernel, calculateLayout, getVisibleItems]);
|
|
2666
|
+
react.useEffect(() => {
|
|
2667
|
+
const container = containerRef.current;
|
|
2668
|
+
if (!container) return;
|
|
2669
|
+
calculateLayout(container.clientWidth);
|
|
2670
|
+
setVirtualItems(getVisibleItems(container.scrollTop, container.clientHeight));
|
|
2671
|
+
}, [count, containerRef, calculateLayout, getVisibleItems]);
|
|
2672
|
+
const scrollTo = react.useCallback(
|
|
2673
|
+
(offset, scrollOptions) => {
|
|
2674
|
+
kernel.scrollTo(offset, scrollOptions);
|
|
2675
|
+
},
|
|
2676
|
+
[kernel]
|
|
2677
|
+
);
|
|
2678
|
+
const scrollToIndex = react.useCallback(
|
|
2679
|
+
(index, _scrollOptions) => {
|
|
2680
|
+
const layout = layoutCacheRef.current.get(index);
|
|
2681
|
+
if (layout) {
|
|
2682
|
+
kernel.scrollTo(layout.top);
|
|
2683
|
+
}
|
|
2684
|
+
},
|
|
2685
|
+
[kernel]
|
|
2686
|
+
);
|
|
2687
|
+
const measureElement = react.useCallback(
|
|
2688
|
+
(_index, _element) => {
|
|
2689
|
+
},
|
|
2690
|
+
[]
|
|
2691
|
+
);
|
|
2692
|
+
const getMeasurement = react.useCallback(
|
|
2693
|
+
(index) => {
|
|
2694
|
+
return layoutCacheRef.current.get(index)?.height;
|
|
2695
|
+
},
|
|
2696
|
+
[]
|
|
2697
|
+
);
|
|
2698
|
+
const invalidateMeasurement = react.useCallback((_index) => {
|
|
2699
|
+
const container = containerRef.current;
|
|
2700
|
+
if (container) {
|
|
2701
|
+
calculateLayout(container.clientWidth);
|
|
2702
|
+
}
|
|
2703
|
+
}, [containerRef, calculateLayout]);
|
|
2704
|
+
const invalidateAllMeasurements = react.useCallback(() => {
|
|
2705
|
+
const container = containerRef.current;
|
|
2706
|
+
if (container) {
|
|
2707
|
+
calculateLayout(container.clientWidth);
|
|
2708
|
+
}
|
|
2709
|
+
}, [containerRef, calculateLayout]);
|
|
2710
|
+
return {
|
|
2711
|
+
virtualItems,
|
|
2712
|
+
totalSize,
|
|
2713
|
+
scrollOffset,
|
|
2714
|
+
isScrolling,
|
|
2715
|
+
scrollTo,
|
|
2716
|
+
scrollToIndex,
|
|
2717
|
+
measureElement,
|
|
2718
|
+
range,
|
|
2719
|
+
getMeasurement,
|
|
2720
|
+
invalidateMeasurement,
|
|
2721
|
+
invalidateAllMeasurements,
|
|
2722
|
+
columnCount: columns,
|
|
2723
|
+
columnWidth,
|
|
2724
|
+
columnHeights
|
|
2725
|
+
};
|
|
2726
|
+
}
|
|
2727
|
+
function VirtualMasonryInner(props, ref) {
|
|
2728
|
+
const {
|
|
2729
|
+
data,
|
|
2730
|
+
renderItem,
|
|
2731
|
+
getItemHeight,
|
|
2732
|
+
columns,
|
|
2733
|
+
minColumnWidth = 200,
|
|
2734
|
+
gap = 0,
|
|
2735
|
+
height = "100%",
|
|
2736
|
+
width = "100%",
|
|
2737
|
+
overscan = 5,
|
|
2738
|
+
initialScrollOffset = 0,
|
|
2739
|
+
getItemKey,
|
|
2740
|
+
onLoadMore,
|
|
2741
|
+
hasMore = false,
|
|
2742
|
+
isLoading = false,
|
|
2743
|
+
loadingIndicator,
|
|
2744
|
+
threshold = 200,
|
|
2745
|
+
onScroll,
|
|
2746
|
+
onVisibleRangeChange,
|
|
2747
|
+
onItemsRendered,
|
|
2748
|
+
plugins = [],
|
|
2749
|
+
className,
|
|
2750
|
+
style,
|
|
2751
|
+
innerClassName,
|
|
2752
|
+
innerStyle,
|
|
2753
|
+
itemClassName,
|
|
2754
|
+
itemStyle,
|
|
2755
|
+
role = "grid",
|
|
2756
|
+
ariaLabel,
|
|
2757
|
+
tabIndex,
|
|
2758
|
+
...restProps
|
|
2759
|
+
} = props;
|
|
2760
|
+
const containerRef = react.useRef(null);
|
|
2761
|
+
const gapValue = typeof gap === "number" ? gap : gap.x;
|
|
2762
|
+
const {
|
|
2763
|
+
virtualItems,
|
|
2764
|
+
totalSize,
|
|
2765
|
+
scrollTo,
|
|
2766
|
+
scrollToIndex,
|
|
2767
|
+
isScrolling,
|
|
2768
|
+
range,
|
|
2769
|
+
columnCount,
|
|
2770
|
+
columnWidth,
|
|
2771
|
+
columnHeights
|
|
2772
|
+
} = useVirtualMasonry({
|
|
2773
|
+
count: data.length,
|
|
2774
|
+
columns: typeof columns === "number" ? columns : 3,
|
|
2775
|
+
// TODO: handle auto
|
|
2776
|
+
getItemHeight: (index, colWidth) => {
|
|
2777
|
+
const item = data[index];
|
|
2778
|
+
if (item === void 0) return 100;
|
|
2779
|
+
return getItemHeight(item, colWidth);
|
|
2780
|
+
},
|
|
2781
|
+
gap: gapValue,
|
|
2782
|
+
overscan,
|
|
2783
|
+
containerRef
|
|
2784
|
+
});
|
|
2785
|
+
const kernelRef = react.useRef(createKernel({
|
|
2786
|
+
itemCount: data.length,
|
|
2787
|
+
estimatedItemHeight: 200,
|
|
2788
|
+
overscan,
|
|
2789
|
+
direction: "vertical"
|
|
2790
|
+
}));
|
|
2791
|
+
react.useImperativeHandle(ref, () => ({
|
|
2792
|
+
scrollTo: (offset, options) => scrollTo(offset, options),
|
|
2793
|
+
scrollToIndex: (index, options) => scrollToIndex(index, options),
|
|
2794
|
+
scrollToTop: (options) => scrollTo(0, options),
|
|
2795
|
+
scrollToBottom: (options) => scrollTo(totalSize, options),
|
|
2796
|
+
getScrollPosition: () => ({
|
|
2797
|
+
scrollTop: containerRef.current?.scrollTop ?? 0,
|
|
2798
|
+
scrollLeft: containerRef.current?.scrollLeft ?? 0
|
|
2799
|
+
})
|
|
2800
|
+
}), [scrollTo, scrollToIndex, totalSize]);
|
|
2801
|
+
const handleScroll = react.useCallback((event) => {
|
|
2802
|
+
const target = event.currentTarget;
|
|
2803
|
+
onScroll?.(target.scrollTop, target.scrollLeft);
|
|
2804
|
+
}, [onScroll]);
|
|
2805
|
+
const handleScrollForInfinite = react.useCallback((event) => {
|
|
2806
|
+
if (!onLoadMore || !hasMore || isLoading) return;
|
|
2807
|
+
const target = event.currentTarget;
|
|
2808
|
+
const scrollBottom = target.scrollHeight - target.scrollTop - target.clientHeight;
|
|
2809
|
+
if (scrollBottom < threshold) {
|
|
2810
|
+
onLoadMore();
|
|
2811
|
+
}
|
|
2812
|
+
}, [onLoadMore, hasMore, isLoading, threshold]);
|
|
2813
|
+
const containerStyles = react.useMemo(() => ({
|
|
2814
|
+
height,
|
|
2815
|
+
width,
|
|
2816
|
+
overflow: "auto",
|
|
2817
|
+
position: "relative",
|
|
2818
|
+
contain: "strict",
|
|
2819
|
+
...style
|
|
2820
|
+
}), [height, width, style]);
|
|
2821
|
+
const innerContainerStyles = react.useMemo(() => ({
|
|
2822
|
+
height: totalSize,
|
|
2823
|
+
width: "100%",
|
|
2824
|
+
position: "relative",
|
|
2825
|
+
contain: "size layout",
|
|
2826
|
+
...innerStyle
|
|
2827
|
+
}), [totalSize, innerStyle]);
|
|
2828
|
+
const createRenderProps = react.useCallback(
|
|
2829
|
+
(index, virtualItem) => {
|
|
2830
|
+
const item = data[index];
|
|
2831
|
+
if (item === void 0) {
|
|
2832
|
+
throw new Error(`[Scrollex] Item at index ${index} is undefined`);
|
|
2833
|
+
}
|
|
2834
|
+
const columnIndex = virtualItem.lane;
|
|
2835
|
+
const left = columnIndex * (columnWidth + gapValue);
|
|
2836
|
+
const itemStyles = {
|
|
2837
|
+
position: "absolute",
|
|
2838
|
+
top: 0,
|
|
2839
|
+
left: 0,
|
|
2840
|
+
width: columnWidth,
|
|
2841
|
+
height: virtualItem.size,
|
|
2842
|
+
transform: `translate(${left}px, ${virtualItem.start}px)`,
|
|
2843
|
+
willChange: "transform",
|
|
2844
|
+
contain: "layout style",
|
|
2845
|
+
...itemStyle
|
|
2846
|
+
};
|
|
2847
|
+
return {
|
|
2848
|
+
item,
|
|
2849
|
+
index,
|
|
2850
|
+
style: itemStyles,
|
|
2851
|
+
measureRef: () => {
|
|
2852
|
+
},
|
|
2853
|
+
// Masonry uses calculated heights
|
|
2854
|
+
isVisible: true,
|
|
2855
|
+
// All virtual items are considered visible
|
|
2856
|
+
isScrolling,
|
|
2857
|
+
columnIndex,
|
|
2858
|
+
rowIndex: 0,
|
|
2859
|
+
// Not meaningful for masonry
|
|
2860
|
+
columnWidth,
|
|
2861
|
+
width: columnWidth
|
|
2862
|
+
};
|
|
2863
|
+
},
|
|
2864
|
+
[data, columnWidth, gapValue, itemStyle, isScrolling]
|
|
2865
|
+
);
|
|
2866
|
+
const contextValue = react.useMemo(() => ({
|
|
2867
|
+
kernel: kernelRef.current
|
|
2868
|
+
}), []);
|
|
2869
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ScrollexContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2870
|
+
"div",
|
|
2871
|
+
{
|
|
2872
|
+
ref: containerRef,
|
|
2873
|
+
className,
|
|
2874
|
+
style: containerStyles,
|
|
2875
|
+
onScroll: (e) => {
|
|
2876
|
+
handleScroll(e);
|
|
2877
|
+
handleScrollForInfinite(e);
|
|
2878
|
+
},
|
|
2879
|
+
role,
|
|
2880
|
+
"aria-label": ariaLabel,
|
|
2881
|
+
"aria-colcount": columnCount,
|
|
2882
|
+
tabIndex,
|
|
2883
|
+
...restProps,
|
|
2884
|
+
children: [
|
|
2885
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: innerClassName, style: innerContainerStyles, children: virtualItems.map((virtualItem) => {
|
|
2886
|
+
const index = virtualItem.index;
|
|
2887
|
+
const item = data[index];
|
|
2888
|
+
if (item === void 0) return null;
|
|
2889
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2890
|
+
"div",
|
|
2891
|
+
{
|
|
2892
|
+
className: itemClassName,
|
|
2893
|
+
"data-index": index,
|
|
2894
|
+
role: "gridcell",
|
|
2895
|
+
"aria-colindex": virtualItem.lane + 1,
|
|
2896
|
+
children: renderItem(createRenderProps(index, virtualItem))
|
|
2897
|
+
},
|
|
2898
|
+
virtualItem.key
|
|
2899
|
+
);
|
|
2900
|
+
}) }),
|
|
2901
|
+
isLoading && loadingIndicator
|
|
2902
|
+
]
|
|
2903
|
+
}
|
|
2904
|
+
) });
|
|
2905
|
+
}
|
|
2906
|
+
var VirtualMasonry = react.forwardRef(VirtualMasonryInner);
|
|
2907
|
+
function useMeasure(options = {}) {
|
|
2908
|
+
const { onResize, debounce: debounce2 = 0 } = options;
|
|
2909
|
+
const elementRef = react.useRef(null);
|
|
2910
|
+
const observerRef = react.useRef(null);
|
|
2911
|
+
const dimensionsRef = react.useRef({ width: 0, height: 0 });
|
|
2912
|
+
const debounceTimerRef = react.useRef(null);
|
|
2913
|
+
const handleResize = react.useCallback(
|
|
2914
|
+
(width, height) => {
|
|
2915
|
+
dimensionsRef.current = { width, height };
|
|
2916
|
+
if (onResize) {
|
|
2917
|
+
if (debounce2 > 0) {
|
|
2918
|
+
if (debounceTimerRef.current) {
|
|
2919
|
+
clearTimeout(debounceTimerRef.current);
|
|
2920
|
+
}
|
|
2921
|
+
debounceTimerRef.current = setTimeout(() => {
|
|
2922
|
+
onResize(width, height);
|
|
2923
|
+
}, debounce2);
|
|
2924
|
+
} else {
|
|
2925
|
+
onResize(width, height);
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
},
|
|
2929
|
+
[onResize, debounce2]
|
|
2930
|
+
);
|
|
2931
|
+
react.useEffect(() => {
|
|
2932
|
+
observerRef.current = new ResizeObserver((entries) => {
|
|
2933
|
+
for (const entry of entries) {
|
|
2934
|
+
const { width, height } = entry.contentRect;
|
|
2935
|
+
handleResize(width, height);
|
|
2936
|
+
}
|
|
2937
|
+
});
|
|
2938
|
+
if (elementRef.current) {
|
|
2939
|
+
observerRef.current.observe(elementRef.current);
|
|
2940
|
+
}
|
|
2941
|
+
return () => {
|
|
2942
|
+
observerRef.current?.disconnect();
|
|
2943
|
+
if (debounceTimerRef.current) {
|
|
2944
|
+
clearTimeout(debounceTimerRef.current);
|
|
2945
|
+
}
|
|
2946
|
+
};
|
|
2947
|
+
}, [handleResize]);
|
|
2948
|
+
const measureRef = react.useCallback((element) => {
|
|
2949
|
+
if (elementRef.current && observerRef.current) {
|
|
2950
|
+
observerRef.current.unobserve(elementRef.current);
|
|
2951
|
+
}
|
|
2952
|
+
elementRef.current = element;
|
|
2953
|
+
if (element && observerRef.current) {
|
|
2954
|
+
observerRef.current.observe(element);
|
|
2955
|
+
const rect = element.getBoundingClientRect();
|
|
2956
|
+
handleResize(rect.width, rect.height);
|
|
2957
|
+
}
|
|
2958
|
+
}, [handleResize]);
|
|
2959
|
+
return {
|
|
2960
|
+
measureRef,
|
|
2961
|
+
width: dimensionsRef.current.width,
|
|
2962
|
+
height: dimensionsRef.current.height
|
|
2963
|
+
};
|
|
2964
|
+
}
|
|
2965
|
+
function useMeasureMany() {
|
|
2966
|
+
const elementsRef = react.useRef(/* @__PURE__ */ new Map());
|
|
2967
|
+
const observerRef = react.useRef(null);
|
|
2968
|
+
const measurementsRef = react.useRef(/* @__PURE__ */ new Map());
|
|
2969
|
+
const callbackRef = react.useRef(null);
|
|
2970
|
+
react.useEffect(() => {
|
|
2971
|
+
observerRef.current = new ResizeObserver((entries) => {
|
|
2972
|
+
for (const entry of entries) {
|
|
2973
|
+
const element = entry.target;
|
|
2974
|
+
const key = element.dataset["measureKey"];
|
|
2975
|
+
if (key !== void 0) {
|
|
2976
|
+
const { width, height } = entry.contentRect;
|
|
2977
|
+
measurementsRef.current.set(key, { width, height });
|
|
2978
|
+
callbackRef.current?.(key, width, height);
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
});
|
|
2982
|
+
return () => {
|
|
2983
|
+
observerRef.current?.disconnect();
|
|
2984
|
+
};
|
|
2985
|
+
}, []);
|
|
2986
|
+
const createMeasureRef = react.useCallback(
|
|
2987
|
+
(key) => (element) => {
|
|
2988
|
+
const elements = elementsRef.current;
|
|
2989
|
+
const observer = observerRef.current;
|
|
2990
|
+
const oldElement = elements.get(key);
|
|
2991
|
+
if (oldElement && oldElement !== element) {
|
|
2992
|
+
observer?.unobserve(oldElement);
|
|
2993
|
+
elements.delete(key);
|
|
2994
|
+
}
|
|
2995
|
+
if (element) {
|
|
2996
|
+
element.dataset["measureKey"] = String(key);
|
|
2997
|
+
elements.set(key, element);
|
|
2998
|
+
observer?.observe(element);
|
|
2999
|
+
const rect = element.getBoundingClientRect();
|
|
3000
|
+
measurementsRef.current.set(key, { width: rect.width, height: rect.height });
|
|
3001
|
+
}
|
|
3002
|
+
},
|
|
3003
|
+
[]
|
|
3004
|
+
);
|
|
3005
|
+
const getMeasurement = react.useCallback((key) => {
|
|
3006
|
+
return measurementsRef.current.get(key);
|
|
3007
|
+
}, []);
|
|
3008
|
+
const onMeasure = react.useCallback(
|
|
3009
|
+
(callback) => {
|
|
3010
|
+
callbackRef.current = callback;
|
|
3011
|
+
},
|
|
3012
|
+
[]
|
|
3013
|
+
);
|
|
3014
|
+
const clear = react.useCallback(() => {
|
|
3015
|
+
const observer = observerRef.current;
|
|
3016
|
+
for (const element of elementsRef.current.values()) {
|
|
3017
|
+
observer?.unobserve(element);
|
|
3018
|
+
}
|
|
3019
|
+
elementsRef.current.clear();
|
|
3020
|
+
measurementsRef.current.clear();
|
|
3021
|
+
}, []);
|
|
3022
|
+
return {
|
|
3023
|
+
createMeasureRef,
|
|
3024
|
+
getMeasurement,
|
|
3025
|
+
onMeasure,
|
|
3026
|
+
clear
|
|
3027
|
+
};
|
|
3028
|
+
}
|
|
3029
|
+
|
|
3030
|
+
// src/utils/debounce.ts
|
|
3031
|
+
function debounce(fn, wait) {
|
|
3032
|
+
let timeoutId = null;
|
|
3033
|
+
let lastArgs = null;
|
|
3034
|
+
let lastThis = null;
|
|
3035
|
+
const debounced = function(...args) {
|
|
3036
|
+
lastArgs = args;
|
|
3037
|
+
lastThis = this;
|
|
3038
|
+
if (timeoutId) {
|
|
3039
|
+
clearTimeout(timeoutId);
|
|
3040
|
+
}
|
|
3041
|
+
timeoutId = setTimeout(() => {
|
|
3042
|
+
timeoutId = null;
|
|
3043
|
+
if (lastArgs) {
|
|
3044
|
+
fn.apply(lastThis, lastArgs);
|
|
3045
|
+
lastArgs = null;
|
|
3046
|
+
lastThis = null;
|
|
3047
|
+
}
|
|
3048
|
+
}, wait);
|
|
3049
|
+
};
|
|
3050
|
+
debounced.cancel = () => {
|
|
3051
|
+
if (timeoutId) {
|
|
3052
|
+
clearTimeout(timeoutId);
|
|
3053
|
+
timeoutId = null;
|
|
3054
|
+
}
|
|
3055
|
+
lastArgs = null;
|
|
3056
|
+
lastThis = null;
|
|
3057
|
+
};
|
|
3058
|
+
debounced.flush = () => {
|
|
3059
|
+
if (timeoutId) {
|
|
3060
|
+
clearTimeout(timeoutId);
|
|
3061
|
+
timeoutId = null;
|
|
3062
|
+
if (lastArgs) {
|
|
3063
|
+
fn.apply(lastThis, lastArgs);
|
|
3064
|
+
lastArgs = null;
|
|
3065
|
+
lastThis = null;
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
};
|
|
3069
|
+
return debounced;
|
|
3070
|
+
}
|
|
3071
|
+
|
|
3072
|
+
// src/plugins/core/auto-measurer.ts
|
|
3073
|
+
function autoMeasurerPlugin(options = {}) {
|
|
3074
|
+
const {
|
|
3075
|
+
estimatedItemHeight = 50,
|
|
3076
|
+
measureOnResize = true,
|
|
3077
|
+
debounceMs = 100
|
|
3078
|
+
} = options;
|
|
3079
|
+
let kernel = null;
|
|
3080
|
+
let resizeObserver = null;
|
|
3081
|
+
const observedElements = /* @__PURE__ */ new WeakMap();
|
|
3082
|
+
const elementsByIndex = /* @__PURE__ */ new Map();
|
|
3083
|
+
let measureQueue = /* @__PURE__ */ new Set();
|
|
3084
|
+
let flushScheduled = false;
|
|
3085
|
+
const debouncedFlush = debounce(() => {
|
|
3086
|
+
flush();
|
|
3087
|
+
}, debounceMs);
|
|
3088
|
+
function flush() {
|
|
3089
|
+
if (!kernel || measureQueue.size === 0) {
|
|
3090
|
+
flushScheduled = false;
|
|
3091
|
+
return;
|
|
3092
|
+
}
|
|
3093
|
+
const indicesToMeasure = Array.from(measureQueue);
|
|
3094
|
+
measureQueue = /* @__PURE__ */ new Set();
|
|
3095
|
+
flushScheduled = false;
|
|
3096
|
+
const measurements = [];
|
|
3097
|
+
for (const index of indicesToMeasure) {
|
|
3098
|
+
const ref = elementsByIndex.get(index);
|
|
3099
|
+
const element = ref?.deref();
|
|
3100
|
+
if (element) {
|
|
3101
|
+
const rect = element.getBoundingClientRect();
|
|
3102
|
+
measurements.push({ index, height: rect.height });
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
3105
|
+
for (const { index, height } of measurements) {
|
|
3106
|
+
if (height > 0) {
|
|
3107
|
+
kernel.measureItem(index, height);
|
|
3108
|
+
}
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
function scheduleFlush() {
|
|
3112
|
+
if (!flushScheduled) {
|
|
3113
|
+
flushScheduled = true;
|
|
3114
|
+
requestAnimationFrame(() => {
|
|
3115
|
+
flush();
|
|
3116
|
+
});
|
|
3117
|
+
}
|
|
3118
|
+
}
|
|
3119
|
+
function handleResize(entries) {
|
|
3120
|
+
for (const entry of entries) {
|
|
3121
|
+
const index = observedElements.get(entry.target);
|
|
3122
|
+
if (index !== void 0) {
|
|
3123
|
+
measureQueue.add(index);
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
if (measureOnResize && measureQueue.size > 0) {
|
|
3127
|
+
debouncedFlush();
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
const api = {
|
|
3131
|
+
measureElement(index, element) {
|
|
3132
|
+
const rect = element.getBoundingClientRect();
|
|
3133
|
+
const height = rect.height;
|
|
3134
|
+
if (height > 0 && kernel) {
|
|
3135
|
+
kernel.measureItem(index, height);
|
|
3136
|
+
}
|
|
3137
|
+
return height;
|
|
3138
|
+
},
|
|
3139
|
+
getCachedSize(index) {
|
|
3140
|
+
return kernel?.getCachedHeight(index);
|
|
3141
|
+
},
|
|
3142
|
+
invalidate(index) {
|
|
3143
|
+
kernel?.invalidateMeasurement(index);
|
|
3144
|
+
},
|
|
3145
|
+
invalidateAll() {
|
|
3146
|
+
kernel?.invalidateAllMeasurements();
|
|
3147
|
+
},
|
|
3148
|
+
getMeasurementCache() {
|
|
3149
|
+
const cache = /* @__PURE__ */ new Map();
|
|
3150
|
+
if (!kernel) return cache;
|
|
3151
|
+
const itemCount = kernel.getItemCount();
|
|
3152
|
+
for (let i = 0; i < itemCount; i++) {
|
|
3153
|
+
const height = kernel.getCachedHeight(i);
|
|
3154
|
+
if (height !== void 0) {
|
|
3155
|
+
cache.set(i, height);
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
return cache;
|
|
3159
|
+
},
|
|
3160
|
+
observe(index, element) {
|
|
3161
|
+
if (!resizeObserver) return;
|
|
3162
|
+
const existingRef = elementsByIndex.get(index);
|
|
3163
|
+
const existingElement = existingRef?.deref();
|
|
3164
|
+
if (existingElement && existingElement !== element) {
|
|
3165
|
+
resizeObserver.unobserve(existingElement);
|
|
3166
|
+
observedElements.delete(existingElement);
|
|
3167
|
+
}
|
|
3168
|
+
observedElements.set(element, index);
|
|
3169
|
+
elementsByIndex.set(index, new WeakRef(element));
|
|
3170
|
+
resizeObserver.observe(element);
|
|
3171
|
+
measureQueue.add(index);
|
|
3172
|
+
scheduleFlush();
|
|
3173
|
+
},
|
|
3174
|
+
unobserve(element) {
|
|
3175
|
+
if (!resizeObserver) return;
|
|
3176
|
+
const index = observedElements.get(element);
|
|
3177
|
+
if (index !== void 0) {
|
|
3178
|
+
resizeObserver.unobserve(element);
|
|
3179
|
+
observedElements.delete(element);
|
|
3180
|
+
elementsByIndex.delete(index);
|
|
3181
|
+
measureQueue.delete(index);
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
};
|
|
3185
|
+
return {
|
|
3186
|
+
name: "auto-measurer",
|
|
3187
|
+
version: "1.0.0",
|
|
3188
|
+
type: "core",
|
|
3189
|
+
install(k) {
|
|
3190
|
+
kernel = k;
|
|
3191
|
+
kernel.configure({ estimatedItemHeight });
|
|
3192
|
+
if (measureOnResize && typeof ResizeObserver !== "undefined") {
|
|
3193
|
+
resizeObserver = new ResizeObserver(handleResize);
|
|
3194
|
+
}
|
|
3195
|
+
},
|
|
3196
|
+
uninstall() {
|
|
3197
|
+
if (resizeObserver) {
|
|
3198
|
+
resizeObserver.disconnect();
|
|
3199
|
+
resizeObserver = null;
|
|
3200
|
+
}
|
|
3201
|
+
debouncedFlush.cancel();
|
|
3202
|
+
measureQueue.clear();
|
|
3203
|
+
elementsByIndex.clear();
|
|
3204
|
+
flushScheduled = false;
|
|
3205
|
+
kernel = null;
|
|
3206
|
+
},
|
|
3207
|
+
api
|
|
3208
|
+
};
|
|
3209
|
+
}
|
|
3210
|
+
|
|
3211
|
+
// src/plugins/core/infinite-loader.ts
|
|
3212
|
+
function infiniteLoaderPlugin(options) {
|
|
3213
|
+
const {
|
|
3214
|
+
threshold = 200,
|
|
3215
|
+
thresholdItems = 5,
|
|
3216
|
+
direction = "forward",
|
|
3217
|
+
onLoadMore
|
|
3218
|
+
} = options;
|
|
3219
|
+
let kernel = null;
|
|
3220
|
+
let loading = false;
|
|
3221
|
+
let hasMoreForward = true;
|
|
3222
|
+
let hasMoreBackward = direction === "backward" || direction === "both";
|
|
3223
|
+
let lastCheckTime = 0;
|
|
3224
|
+
const checkDebounceMs = 100;
|
|
3225
|
+
function checkLoadMore(event) {
|
|
3226
|
+
if (loading || !kernel) return;
|
|
3227
|
+
const now = performance.now();
|
|
3228
|
+
if (now - lastCheckTime < checkDebounceMs) return;
|
|
3229
|
+
lastCheckTime = now;
|
|
3230
|
+
const viewport = kernel.getViewport();
|
|
3231
|
+
const totalSize = kernel.getTotalSize();
|
|
3232
|
+
const { scrollTop } = event;
|
|
3233
|
+
if ((direction === "forward" || direction === "both") && hasMoreForward) {
|
|
3234
|
+
const distanceFromBottom = totalSize - scrollTop - viewport.height;
|
|
3235
|
+
if (distanceFromBottom < threshold) {
|
|
3236
|
+
triggerLoad("forward");
|
|
3237
|
+
return;
|
|
3238
|
+
}
|
|
3239
|
+
}
|
|
3240
|
+
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
3241
|
+
if (scrollTop < threshold) {
|
|
3242
|
+
triggerLoad("backward");
|
|
3243
|
+
return;
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
}
|
|
3247
|
+
function checkLoadMoreByRange(range) {
|
|
3248
|
+
if (loading || !kernel) return;
|
|
3249
|
+
const itemCount = kernel.getItemCount();
|
|
3250
|
+
if ((direction === "forward" || direction === "both") && hasMoreForward) {
|
|
3251
|
+
const itemsFromEnd = itemCount - 1 - range.endIndex;
|
|
3252
|
+
if (itemsFromEnd <= thresholdItems) {
|
|
3253
|
+
triggerLoad("forward");
|
|
3254
|
+
return;
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
3258
|
+
if (range.startIndex <= thresholdItems) {
|
|
3259
|
+
triggerLoad("backward");
|
|
3260
|
+
return;
|
|
3261
|
+
}
|
|
3262
|
+
}
|
|
3263
|
+
}
|
|
3264
|
+
async function triggerLoad(loadDirection) {
|
|
3265
|
+
if (loading || !kernel) return;
|
|
3266
|
+
if (loadDirection === "forward" && !hasMoreForward) return;
|
|
3267
|
+
if (loadDirection === "backward" && !hasMoreBackward) return;
|
|
3268
|
+
loading = true;
|
|
3269
|
+
kernel.emit({
|
|
3270
|
+
type: "load-more",
|
|
3271
|
+
direction: loadDirection
|
|
3272
|
+
});
|
|
3273
|
+
try {
|
|
3274
|
+
await onLoadMore(loadDirection);
|
|
3275
|
+
} catch (error) {
|
|
3276
|
+
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
3277
|
+
} finally {
|
|
3278
|
+
loading = false;
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
const api = {
|
|
3282
|
+
isLoading() {
|
|
3283
|
+
return loading;
|
|
3284
|
+
},
|
|
3285
|
+
setLoading(l) {
|
|
3286
|
+
loading = l;
|
|
3287
|
+
},
|
|
3288
|
+
hasMore() {
|
|
3289
|
+
return hasMoreForward || hasMoreBackward;
|
|
3290
|
+
},
|
|
3291
|
+
setHasMore(hasMore) {
|
|
3292
|
+
hasMoreForward = hasMore;
|
|
3293
|
+
hasMoreBackward = hasMore;
|
|
3294
|
+
},
|
|
3295
|
+
hasMoreForward() {
|
|
3296
|
+
return hasMoreForward;
|
|
3297
|
+
},
|
|
3298
|
+
setHasMoreForward(hasMore) {
|
|
3299
|
+
hasMoreForward = hasMore;
|
|
3300
|
+
},
|
|
3301
|
+
hasMoreBackward() {
|
|
3302
|
+
return hasMoreBackward;
|
|
3303
|
+
},
|
|
3304
|
+
setHasMoreBackward(hasMore) {
|
|
3305
|
+
hasMoreBackward = hasMore;
|
|
3306
|
+
},
|
|
3307
|
+
reset() {
|
|
3308
|
+
loading = false;
|
|
3309
|
+
hasMoreForward = true;
|
|
3310
|
+
hasMoreBackward = direction === "backward" || direction === "both";
|
|
3311
|
+
},
|
|
3312
|
+
triggerLoad(loadDirection) {
|
|
3313
|
+
triggerLoad(loadDirection);
|
|
3314
|
+
}
|
|
3315
|
+
};
|
|
3316
|
+
return {
|
|
3317
|
+
name: "infinite-loader",
|
|
3318
|
+
version: "1.0.0",
|
|
3319
|
+
type: "core",
|
|
3320
|
+
install(k) {
|
|
3321
|
+
kernel = k;
|
|
3322
|
+
},
|
|
3323
|
+
uninstall() {
|
|
3324
|
+
kernel = null;
|
|
3325
|
+
loading = false;
|
|
3326
|
+
},
|
|
3327
|
+
hooks: {
|
|
3328
|
+
onScroll: checkLoadMore,
|
|
3329
|
+
onVisibleRangeChange: (range) => checkLoadMoreByRange(range)
|
|
3330
|
+
},
|
|
3331
|
+
api
|
|
3332
|
+
};
|
|
3333
|
+
}
|
|
3334
|
+
|
|
3335
|
+
// src/plugins/core/scroll-controller.ts
|
|
3336
|
+
var easings2 = {
|
|
3337
|
+
easeInOut: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t};
|
|
3338
|
+
function scrollControllerPlugin(options = {}) {
|
|
3339
|
+
const {
|
|
3340
|
+
defaultDuration = 300,
|
|
3341
|
+
defaultEasing = easings2.easeInOut
|
|
3342
|
+
} = options;
|
|
3343
|
+
let kernel = null;
|
|
3344
|
+
let animationId = null;
|
|
3345
|
+
let animating = false;
|
|
3346
|
+
function animateScroll(targetOffset, duration, easing) {
|
|
3347
|
+
if (!kernel) return;
|
|
3348
|
+
stopAnimation();
|
|
3349
|
+
const startTime = performance.now();
|
|
3350
|
+
const startOffset = kernel.getScrollPosition().scrollTop;
|
|
3351
|
+
const distance = targetOffset - startOffset;
|
|
3352
|
+
if (distance === 0 || duration === 0) {
|
|
3353
|
+
kernel.setScrollPosition({ scrollTop: targetOffset, scrollLeft: 0 });
|
|
3354
|
+
return;
|
|
3355
|
+
}
|
|
3356
|
+
animating = true;
|
|
3357
|
+
function animate(currentTime) {
|
|
3358
|
+
if (!kernel) {
|
|
3359
|
+
animating = false;
|
|
3360
|
+
return;
|
|
3361
|
+
}
|
|
3362
|
+
const elapsed = currentTime - startTime;
|
|
3363
|
+
const progress = Math.min(elapsed / duration, 1);
|
|
3364
|
+
const easedProgress = easing(progress);
|
|
3365
|
+
const currentOffset = startOffset + distance * easedProgress;
|
|
3366
|
+
kernel.setScrollPosition({ scrollTop: currentOffset, scrollLeft: 0 });
|
|
3367
|
+
if (progress < 1) {
|
|
3368
|
+
animationId = raf(animate);
|
|
3369
|
+
} else {
|
|
3370
|
+
animationId = null;
|
|
3371
|
+
animating = false;
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
animationId = raf(animate);
|
|
3375
|
+
}
|
|
3376
|
+
function stopAnimation() {
|
|
3377
|
+
if (animationId !== null) {
|
|
3378
|
+
cancelRaf(animationId);
|
|
3379
|
+
animationId = null;
|
|
3380
|
+
}
|
|
3381
|
+
animating = false;
|
|
3382
|
+
}
|
|
3383
|
+
const api = {
|
|
3384
|
+
scrollTo(offset, options2 = {}) {
|
|
3385
|
+
if (!kernel) return;
|
|
3386
|
+
const {
|
|
3387
|
+
behavior = "auto",
|
|
3388
|
+
duration = defaultDuration,
|
|
3389
|
+
easing = defaultEasing
|
|
3390
|
+
} = options2;
|
|
3391
|
+
if (behavior === "auto") {
|
|
3392
|
+
stopAnimation();
|
|
3393
|
+
kernel.setScrollPosition({ scrollTop: offset, scrollLeft: 0 });
|
|
3394
|
+
} else {
|
|
3395
|
+
animateScroll(offset, duration, easing);
|
|
3396
|
+
}
|
|
3397
|
+
},
|
|
3398
|
+
scrollToIndex(index, options2 = {}) {
|
|
3399
|
+
if (!kernel) return;
|
|
3400
|
+
const {
|
|
3401
|
+
align = "auto",
|
|
3402
|
+
offset: additionalOffset = 0,
|
|
3403
|
+
...scrollOptions
|
|
3404
|
+
} = options2;
|
|
3405
|
+
const itemOffset = kernel.getItemOffset(index);
|
|
3406
|
+
const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
|
|
3407
|
+
const viewport = kernel.getViewport();
|
|
3408
|
+
let targetOffset;
|
|
3409
|
+
switch (align) {
|
|
3410
|
+
case "start":
|
|
3411
|
+
targetOffset = itemOffset;
|
|
3412
|
+
break;
|
|
3413
|
+
case "center":
|
|
3414
|
+
targetOffset = itemOffset - viewport.height / 2 + itemSize / 2;
|
|
3415
|
+
break;
|
|
3416
|
+
case "end":
|
|
3417
|
+
targetOffset = itemOffset - viewport.height + itemSize;
|
|
3418
|
+
break;
|
|
3419
|
+
case "auto":
|
|
3420
|
+
default: {
|
|
3421
|
+
const scrollPosition = kernel.getScrollPosition();
|
|
3422
|
+
const visibleStart = scrollPosition.scrollTop;
|
|
3423
|
+
const visibleEnd = visibleStart + viewport.height;
|
|
3424
|
+
const itemEnd = itemOffset + itemSize;
|
|
3425
|
+
if (itemOffset < visibleStart) {
|
|
3426
|
+
targetOffset = itemOffset;
|
|
3427
|
+
} else if (itemEnd > visibleEnd) {
|
|
3428
|
+
targetOffset = itemEnd - viewport.height;
|
|
3429
|
+
} else {
|
|
3430
|
+
return;
|
|
3431
|
+
}
|
|
3432
|
+
break;
|
|
3433
|
+
}
|
|
3434
|
+
}
|
|
3435
|
+
const maxScroll = Math.max(0, kernel.getTotalSize() - viewport.height);
|
|
3436
|
+
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
3437
|
+
api.scrollTo(targetOffset, scrollOptions);
|
|
3438
|
+
},
|
|
3439
|
+
scrollToTop(options2 = {}) {
|
|
3440
|
+
api.scrollTo(0, options2);
|
|
3441
|
+
},
|
|
3442
|
+
scrollToBottom(options2 = {}) {
|
|
3443
|
+
if (!kernel) return;
|
|
3444
|
+
const viewport = kernel.getViewport();
|
|
3445
|
+
const totalSize = kernel.getTotalSize();
|
|
3446
|
+
const maxScroll = Math.max(0, totalSize - viewport.height);
|
|
3447
|
+
api.scrollTo(maxScroll, options2);
|
|
3448
|
+
},
|
|
3449
|
+
getScrollPosition() {
|
|
3450
|
+
return kernel?.getScrollPosition() ?? { scrollTop: 0, scrollLeft: 0 };
|
|
3451
|
+
},
|
|
3452
|
+
isScrolling() {
|
|
3453
|
+
return kernel?.isScrolling() ?? false;
|
|
3454
|
+
},
|
|
3455
|
+
isAnimating() {
|
|
3456
|
+
return animating;
|
|
3457
|
+
},
|
|
3458
|
+
stopAnimation
|
|
3459
|
+
};
|
|
3460
|
+
return {
|
|
3461
|
+
name: "scroll-controller",
|
|
3462
|
+
version: "1.0.0",
|
|
3463
|
+
type: "core",
|
|
3464
|
+
install(k) {
|
|
3465
|
+
kernel = k;
|
|
3466
|
+
},
|
|
3467
|
+
uninstall() {
|
|
3468
|
+
stopAnimation();
|
|
3469
|
+
kernel = null;
|
|
3470
|
+
},
|
|
3471
|
+
api
|
|
3472
|
+
};
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
exports.VirtualGrid = VirtualGrid;
|
|
3476
|
+
exports.VirtualList = VirtualList;
|
|
3477
|
+
exports.VirtualMasonry = VirtualMasonry;
|
|
3478
|
+
exports.autoMeasurerPlugin = autoMeasurerPlugin;
|
|
3479
|
+
exports.createKernel = createKernel;
|
|
3480
|
+
exports.createPlugin = createPlugin;
|
|
3481
|
+
exports.easings = easings;
|
|
3482
|
+
exports.gridRendererPlugin = gridRendererPlugin;
|
|
3483
|
+
exports.infiniteLoaderPlugin = infiniteLoaderPlugin;
|
|
3484
|
+
exports.listRendererPlugin = listRendererPlugin;
|
|
3485
|
+
exports.scrollControllerPlugin = scrollControllerPlugin;
|
|
3486
|
+
exports.useMeasure = useMeasure;
|
|
3487
|
+
exports.useMeasureMany = useMeasureMany;
|
|
3488
|
+
exports.useScrollex = useScrollex;
|
|
3489
|
+
exports.useScrollexRequired = useScrollexRequired;
|
|
3490
|
+
exports.useVirtualGrid = useVirtualGrid;
|
|
3491
|
+
exports.useVirtualList = useVirtualList;
|
|
3492
|
+
exports.useVirtualMasonry = useVirtualMasonry;
|
|
3493
|
+
//# sourceMappingURL=index.cjs.map
|
|
3494
|
+
//# sourceMappingURL=index.cjs.map
|