@oxog/scrollex 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -5
- package/README.md +30 -22
- package/dist/index.cjs +796 -363
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -15
- package/dist/index.d.ts +39 -15
- package/dist/index.js +797 -364
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +381 -180
- package/dist/plugins/index.cjs.map +1 -1
- package/dist/plugins/index.d.cts +74 -49
- package/dist/plugins/index.d.ts +74 -49
- package/dist/plugins/index.js +381 -180
- package/dist/plugins/index.js.map +1 -1
- package/package.json +13 -10
package/dist/plugins/index.js
CHANGED
|
@@ -2,32 +2,6 @@ import { useMemo, useCallback, useState, useEffect } from 'react';
|
|
|
2
2
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
// src/plugins/core/list-renderer.ts
|
|
5
|
-
var VirtualItemPool = class {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.pool = [];
|
|
8
|
-
}
|
|
9
|
-
acquire() {
|
|
10
|
-
const item = this.pool.pop();
|
|
11
|
-
if (item) {
|
|
12
|
-
return item;
|
|
13
|
-
}
|
|
14
|
-
return { index: 0, key: 0, start: 0, end: 0, size: 0, lane: 0 };
|
|
15
|
-
}
|
|
16
|
-
release(item) {
|
|
17
|
-
item.index = 0;
|
|
18
|
-
item.key = 0;
|
|
19
|
-
item.start = 0;
|
|
20
|
-
item.end = 0;
|
|
21
|
-
item.size = 0;
|
|
22
|
-
item.lane = 0;
|
|
23
|
-
this.pool.push(item);
|
|
24
|
-
}
|
|
25
|
-
releaseAll(items) {
|
|
26
|
-
for (const item of items) {
|
|
27
|
-
this.release(item);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
5
|
function listRendererPlugin(options = {}) {
|
|
32
6
|
const { getItemKey = (index) => index } = options;
|
|
33
7
|
let kernel = null;
|
|
@@ -38,24 +12,24 @@ function listRendererPlugin(options = {}) {
|
|
|
38
12
|
overscanStartIndex: 0,
|
|
39
13
|
overscanEndIndex: 0
|
|
40
14
|
};
|
|
41
|
-
const itemPool = new VirtualItemPool();
|
|
42
15
|
function buildVirtualItems(range) {
|
|
43
16
|
if (!kernel) return [];
|
|
44
|
-
itemPool.releaseAll(virtualItems);
|
|
45
17
|
const items = [];
|
|
46
18
|
range.startIndex;
|
|
47
19
|
range.endIndex;
|
|
48
|
-
|
|
49
|
-
|
|
20
|
+
const lastIndex = Math.min(range.overscanEndIndex, kernel.getItemCount() - 1);
|
|
21
|
+
for (let i = range.overscanStartIndex; i <= lastIndex; i++) {
|
|
50
22
|
const offset = kernel.getItemOffset(i);
|
|
51
23
|
const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
24
|
+
items.push({
|
|
25
|
+
index: i,
|
|
26
|
+
key: getItemKey(i),
|
|
27
|
+
start: offset,
|
|
28
|
+
end: offset + size,
|
|
29
|
+
size,
|
|
30
|
+
lane: 0
|
|
31
|
+
// For list, always lane 0
|
|
32
|
+
});
|
|
59
33
|
}
|
|
60
34
|
return items;
|
|
61
35
|
}
|
|
@@ -81,6 +55,7 @@ function listRendererPlugin(options = {}) {
|
|
|
81
55
|
return lastItem?.end ?? 0;
|
|
82
56
|
},
|
|
83
57
|
isItemVisible(index) {
|
|
58
|
+
if (!kernel || index >= kernel.getItemCount()) return false;
|
|
84
59
|
return index >= currentRange.startIndex && index <= currentRange.endIndex;
|
|
85
60
|
},
|
|
86
61
|
recalculate
|
|
@@ -94,7 +69,6 @@ function listRendererPlugin(options = {}) {
|
|
|
94
69
|
recalculate();
|
|
95
70
|
},
|
|
96
71
|
uninstall() {
|
|
97
|
-
itemPool.releaseAll(virtualItems);
|
|
98
72
|
virtualItems = [];
|
|
99
73
|
kernel = null;
|
|
100
74
|
},
|
|
@@ -112,6 +86,15 @@ function listRendererPlugin(options = {}) {
|
|
|
112
86
|
};
|
|
113
87
|
}
|
|
114
88
|
|
|
89
|
+
// src/utils/array.ts
|
|
90
|
+
function normalizeColumnCount(columns) {
|
|
91
|
+
return Number.isFinite(columns) ? Math.max(1, Math.floor(columns)) : 1;
|
|
92
|
+
}
|
|
93
|
+
function autoColumnCount(containerWidth, minColumnWidth, gap) {
|
|
94
|
+
const slot = minColumnWidth + gap;
|
|
95
|
+
return slot > 0 ? normalizeColumnCount(Math.floor((containerWidth + gap) / slot)) : 1;
|
|
96
|
+
}
|
|
97
|
+
|
|
115
98
|
// src/plugins/core/grid-renderer.ts
|
|
116
99
|
function gridRendererPlugin(options) {
|
|
117
100
|
const {
|
|
@@ -119,19 +102,20 @@ function gridRendererPlugin(options) {
|
|
|
119
102
|
minColumnWidth = 100,
|
|
120
103
|
rowHeight,
|
|
121
104
|
gap = 0,
|
|
122
|
-
getItemKey = (index) => index
|
|
105
|
+
getItemKey = (index) => index,
|
|
106
|
+
getContainerWidth
|
|
123
107
|
} = options;
|
|
124
108
|
let kernel = null;
|
|
125
109
|
let virtualItems = [];
|
|
126
|
-
let columnCount = typeof columns === "number" ? columns : 1;
|
|
110
|
+
let columnCount = typeof columns === "number" ? normalizeColumnCount(columns) : 1;
|
|
127
111
|
let columnWidth = 0;
|
|
128
112
|
let gapX = typeof gap === "number" ? gap : gap.x;
|
|
129
113
|
let gapY = typeof gap === "number" ? gap : gap.y;
|
|
130
114
|
function calculateColumns(containerWidth) {
|
|
131
115
|
if (typeof columns === "number") {
|
|
132
|
-
columnCount = columns;
|
|
116
|
+
columnCount = normalizeColumnCount(columns);
|
|
133
117
|
} else {
|
|
134
|
-
columnCount =
|
|
118
|
+
columnCount = autoColumnCount(containerWidth, minColumnWidth, gapX);
|
|
135
119
|
}
|
|
136
120
|
columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
|
|
137
121
|
}
|
|
@@ -172,7 +156,7 @@ function gridRendererPlugin(options) {
|
|
|
172
156
|
function recalculate() {
|
|
173
157
|
if (!kernel) return;
|
|
174
158
|
const viewport = kernel.getViewport();
|
|
175
|
-
calculateColumns(viewport.width);
|
|
159
|
+
calculateColumns(getContainerWidth?.() ?? viewport.width);
|
|
176
160
|
virtualItems = buildVirtualItems();
|
|
177
161
|
}
|
|
178
162
|
const api = {
|
|
@@ -180,7 +164,8 @@ function gridRendererPlugin(options) {
|
|
|
180
164
|
return virtualItems;
|
|
181
165
|
},
|
|
182
166
|
getTotalHeight() {
|
|
183
|
-
|
|
167
|
+
const rowCount = getRowCount();
|
|
168
|
+
return rowCount === 0 ? 0 : rowCount * (rowHeight + gapY) - gapY;
|
|
184
169
|
},
|
|
185
170
|
getColumnCount() {
|
|
186
171
|
return columnCount;
|
|
@@ -262,7 +247,7 @@ function debounce(fn, wait) {
|
|
|
262
247
|
// src/plugins/core/auto-measurer.ts
|
|
263
248
|
function autoMeasurerPlugin(options = {}) {
|
|
264
249
|
const {
|
|
265
|
-
estimatedItemHeight
|
|
250
|
+
estimatedItemHeight,
|
|
266
251
|
measureOnResize = true,
|
|
267
252
|
debounceMs = 100
|
|
268
253
|
} = options;
|
|
@@ -272,6 +257,9 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
272
257
|
const elementsByIndex = /* @__PURE__ */ new Map();
|
|
273
258
|
let measureQueue = /* @__PURE__ */ new Set();
|
|
274
259
|
let flushScheduled = false;
|
|
260
|
+
function sizeAlongAxis(rect) {
|
|
261
|
+
return kernel?.getOptions().direction === "horizontal" ? rect.width : rect.height;
|
|
262
|
+
}
|
|
275
263
|
const debouncedFlush = debounce(() => {
|
|
276
264
|
flush();
|
|
277
265
|
}, debounceMs);
|
|
@@ -289,7 +277,7 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
289
277
|
const element = ref?.deref();
|
|
290
278
|
if (element) {
|
|
291
279
|
const rect = element.getBoundingClientRect();
|
|
292
|
-
measurements.push({ index, height: rect
|
|
280
|
+
measurements.push({ index, height: sizeAlongAxis(rect) });
|
|
293
281
|
}
|
|
294
282
|
}
|
|
295
283
|
for (const { index, height } of measurements) {
|
|
@@ -320,7 +308,7 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
320
308
|
const api = {
|
|
321
309
|
measureElement(index, element) {
|
|
322
310
|
const rect = element.getBoundingClientRect();
|
|
323
|
-
const height = rect
|
|
311
|
+
const height = sizeAlongAxis(rect);
|
|
324
312
|
if (height > 0 && kernel) {
|
|
325
313
|
kernel.measureItem(index, height);
|
|
326
314
|
}
|
|
@@ -348,24 +336,27 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
348
336
|
return cache;
|
|
349
337
|
},
|
|
350
338
|
observe(index, element) {
|
|
351
|
-
if (!resizeObserver) return;
|
|
352
339
|
const existingRef = elementsByIndex.get(index);
|
|
353
340
|
const existingElement = existingRef?.deref();
|
|
354
341
|
if (existingElement && existingElement !== element) {
|
|
355
|
-
resizeObserver
|
|
342
|
+
resizeObserver?.unobserve(existingElement);
|
|
356
343
|
observedElements.delete(existingElement);
|
|
357
344
|
}
|
|
345
|
+
const previousIndex = observedElements.get(element);
|
|
346
|
+
if (previousIndex !== void 0 && previousIndex !== index) {
|
|
347
|
+
elementsByIndex.delete(previousIndex);
|
|
348
|
+
measureQueue.delete(previousIndex);
|
|
349
|
+
}
|
|
358
350
|
observedElements.set(element, index);
|
|
359
|
-
elementsByIndex.set(index, new WeakRef(element));
|
|
360
|
-
resizeObserver
|
|
351
|
+
elementsByIndex.set(index, typeof WeakRef === "function" ? new WeakRef(element) : { deref: () => element });
|
|
352
|
+
resizeObserver?.observe(element);
|
|
361
353
|
measureQueue.add(index);
|
|
362
354
|
scheduleFlush();
|
|
363
355
|
},
|
|
364
356
|
unobserve(element) {
|
|
365
|
-
if (!resizeObserver) return;
|
|
366
357
|
const index = observedElements.get(element);
|
|
367
358
|
if (index !== void 0) {
|
|
368
|
-
resizeObserver
|
|
359
|
+
resizeObserver?.unobserve(element);
|
|
369
360
|
observedElements.delete(element);
|
|
370
361
|
elementsByIndex.delete(index);
|
|
371
362
|
measureQueue.delete(index);
|
|
@@ -378,7 +369,9 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
378
369
|
type: "core",
|
|
379
370
|
install(k) {
|
|
380
371
|
kernel = k;
|
|
381
|
-
|
|
372
|
+
if (estimatedItemHeight !== void 0) {
|
|
373
|
+
kernel.configure({ estimatedItemHeight });
|
|
374
|
+
}
|
|
382
375
|
if (measureOnResize && typeof ResizeObserver !== "undefined") {
|
|
383
376
|
resizeObserver = new ResizeObserver(handleResize);
|
|
384
377
|
}
|
|
@@ -399,6 +392,10 @@ function autoMeasurerPlugin(options = {}) {
|
|
|
399
392
|
}
|
|
400
393
|
|
|
401
394
|
// src/plugins/core/infinite-loader.ts
|
|
395
|
+
function initialLoaderState(direction) {
|
|
396
|
+
return { loading: false, loadGeneration: 0, hasMoreForward: true, hasMoreBackward: direction === "backward" || direction === "both" };
|
|
397
|
+
}
|
|
398
|
+
var sharedLoaderStates = /* @__PURE__ */ new WeakMap();
|
|
402
399
|
function infiniteLoaderPlugin(options) {
|
|
403
400
|
const {
|
|
404
401
|
threshold = 200,
|
|
@@ -407,44 +404,61 @@ function infiniteLoaderPlugin(options) {
|
|
|
407
404
|
onLoadMore
|
|
408
405
|
} = options;
|
|
409
406
|
let kernel = null;
|
|
410
|
-
let
|
|
411
|
-
let hasMoreForward = true;
|
|
412
|
-
let hasMoreBackward = direction === "backward" || direction === "both";
|
|
407
|
+
let state = initialLoaderState(direction);
|
|
413
408
|
let lastCheckTime = 0;
|
|
414
409
|
const checkDebounceMs = 100;
|
|
410
|
+
let pendingEvent = null;
|
|
411
|
+
let trailingTimer = null;
|
|
415
412
|
function checkLoadMore(event) {
|
|
416
|
-
if (loading || !kernel) return;
|
|
413
|
+
if (state.loading || !kernel) return;
|
|
417
414
|
const now = performance.now();
|
|
418
|
-
if (now - lastCheckTime < checkDebounceMs)
|
|
419
|
-
|
|
415
|
+
if (now - lastCheckTime < checkDebounceMs) {
|
|
416
|
+
pendingEvent = event;
|
|
417
|
+
if (trailingTimer === null) {
|
|
418
|
+
trailingTimer = setTimeout(() => {
|
|
419
|
+
trailingTimer = null;
|
|
420
|
+
const latest = pendingEvent;
|
|
421
|
+
pendingEvent = null;
|
|
422
|
+
if (latest) runCheck(latest);
|
|
423
|
+
}, Math.max(0, checkDebounceMs - (now - lastCheckTime)));
|
|
424
|
+
}
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
runCheck(event);
|
|
428
|
+
}
|
|
429
|
+
function runCheck(event) {
|
|
430
|
+
if (state.loading || !kernel) return;
|
|
431
|
+
lastCheckTime = performance.now();
|
|
420
432
|
const viewport = kernel.getViewport();
|
|
421
433
|
const totalSize = kernel.getTotalSize();
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
|
|
434
|
+
const horizontal = kernel.getOptions().direction === "horizontal";
|
|
435
|
+
const scrollTop = horizontal ? event.scrollLeft : event.scrollTop;
|
|
436
|
+
const viewportSize = horizontal ? viewport.width : viewport.height;
|
|
437
|
+
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
438
|
+
const distanceFromBottom = totalSize - scrollTop - viewportSize;
|
|
425
439
|
if (distanceFromBottom < threshold) {
|
|
426
440
|
triggerLoad("forward");
|
|
427
441
|
return;
|
|
428
442
|
}
|
|
429
443
|
}
|
|
430
|
-
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
444
|
+
if ((direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
431
445
|
if (scrollTop < threshold) {
|
|
432
446
|
triggerLoad("backward");
|
|
433
447
|
return;
|
|
434
448
|
}
|
|
435
449
|
}
|
|
436
450
|
}
|
|
437
|
-
function checkLoadMoreByRange(range) {
|
|
438
|
-
if (loading || !kernel) return;
|
|
451
|
+
function checkLoadMoreByRange(range, forwardOnly = false) {
|
|
452
|
+
if (state.loading || !kernel) return;
|
|
439
453
|
const itemCount = kernel.getItemCount();
|
|
440
|
-
if ((direction === "forward" || direction === "both") && hasMoreForward) {
|
|
454
|
+
if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
|
|
441
455
|
const itemsFromEnd = itemCount - 1 - range.endIndex;
|
|
442
456
|
if (itemsFromEnd <= thresholdItems) {
|
|
443
457
|
triggerLoad("forward");
|
|
444
458
|
return;
|
|
445
459
|
}
|
|
446
460
|
}
|
|
447
|
-
if ((direction === "backward" || direction === "both") && hasMoreBackward) {
|
|
461
|
+
if (!forwardOnly && (direction === "backward" || direction === "both") && state.hasMoreBackward) {
|
|
448
462
|
if (range.startIndex <= thresholdItems) {
|
|
449
463
|
triggerLoad("backward");
|
|
450
464
|
return;
|
|
@@ -452,10 +466,11 @@ function infiniteLoaderPlugin(options) {
|
|
|
452
466
|
}
|
|
453
467
|
}
|
|
454
468
|
async function triggerLoad(loadDirection) {
|
|
455
|
-
if (loading || !kernel) return;
|
|
456
|
-
if (loadDirection === "forward" && !hasMoreForward) return;
|
|
457
|
-
if (loadDirection === "backward" && !hasMoreBackward) return;
|
|
458
|
-
loading = true;
|
|
469
|
+
if (state.loading || !kernel) return;
|
|
470
|
+
if (loadDirection === "forward" && !state.hasMoreForward) return;
|
|
471
|
+
if (loadDirection === "backward" && !state.hasMoreBackward) return;
|
|
472
|
+
state.loading = true;
|
|
473
|
+
const generation = ++state.loadGeneration;
|
|
459
474
|
kernel.emit({
|
|
460
475
|
type: "load-more",
|
|
461
476
|
direction: loadDirection
|
|
@@ -465,39 +480,47 @@ function infiniteLoaderPlugin(options) {
|
|
|
465
480
|
} catch (error) {
|
|
466
481
|
console.error("[Scrollex] Error in onLoadMore:", error);
|
|
467
482
|
} finally {
|
|
468
|
-
|
|
483
|
+
if (generation === state.loadGeneration) {
|
|
484
|
+
state.loading = false;
|
|
485
|
+
}
|
|
469
486
|
}
|
|
470
487
|
}
|
|
471
488
|
const api = {
|
|
472
489
|
isLoading() {
|
|
473
|
-
return loading;
|
|
490
|
+
return state.loading;
|
|
474
491
|
},
|
|
475
492
|
setLoading(l) {
|
|
476
|
-
loading = l;
|
|
493
|
+
state.loading = l;
|
|
477
494
|
},
|
|
478
495
|
hasMore() {
|
|
479
|
-
return hasMoreForward || hasMoreBackward;
|
|
496
|
+
return state.hasMoreForward || state.hasMoreBackward;
|
|
480
497
|
},
|
|
481
498
|
setHasMore(hasMore) {
|
|
482
|
-
hasMoreForward = hasMore;
|
|
483
|
-
hasMoreBackward = hasMore;
|
|
499
|
+
state.hasMoreForward = hasMore;
|
|
500
|
+
state.hasMoreBackward = hasMore;
|
|
484
501
|
},
|
|
485
502
|
hasMoreForward() {
|
|
486
|
-
return hasMoreForward;
|
|
503
|
+
return state.hasMoreForward;
|
|
487
504
|
},
|
|
488
505
|
setHasMoreForward(hasMore) {
|
|
489
|
-
hasMoreForward = hasMore;
|
|
506
|
+
state.hasMoreForward = hasMore;
|
|
490
507
|
},
|
|
491
508
|
hasMoreBackward() {
|
|
492
|
-
return hasMoreBackward;
|
|
509
|
+
return state.hasMoreBackward;
|
|
493
510
|
},
|
|
494
511
|
setHasMoreBackward(hasMore) {
|
|
495
|
-
hasMoreBackward = hasMore;
|
|
512
|
+
state.hasMoreBackward = hasMore;
|
|
496
513
|
},
|
|
497
514
|
reset() {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
515
|
+
state.loadGeneration++;
|
|
516
|
+
state.loading = false;
|
|
517
|
+
state.hasMoreForward = true;
|
|
518
|
+
state.hasMoreBackward = direction === "backward" || direction === "both";
|
|
519
|
+
if (trailingTimer !== null) {
|
|
520
|
+
clearTimeout(trailingTimer);
|
|
521
|
+
trailingTimer = null;
|
|
522
|
+
}
|
|
523
|
+
pendingEvent = null;
|
|
501
524
|
},
|
|
502
525
|
triggerLoad(loadDirection) {
|
|
503
526
|
triggerLoad(loadDirection);
|
|
@@ -509,10 +532,21 @@ function infiniteLoaderPlugin(options) {
|
|
|
509
532
|
type: "core",
|
|
510
533
|
install(k) {
|
|
511
534
|
kernel = k;
|
|
535
|
+
const shared = sharedLoaderStates.get(k);
|
|
536
|
+
if (shared) {
|
|
537
|
+
state = shared;
|
|
538
|
+
} else {
|
|
539
|
+
sharedLoaderStates.set(k, state);
|
|
540
|
+
checkLoadMoreByRange(k.getVisibleRange(), true);
|
|
541
|
+
}
|
|
512
542
|
},
|
|
513
543
|
uninstall() {
|
|
544
|
+
if (trailingTimer !== null) {
|
|
545
|
+
clearTimeout(trailingTimer);
|
|
546
|
+
trailingTimer = null;
|
|
547
|
+
}
|
|
548
|
+
pendingEvent = null;
|
|
514
549
|
kernel = null;
|
|
515
|
-
loading = false;
|
|
516
550
|
},
|
|
517
551
|
hooks: {
|
|
518
552
|
onScroll: checkLoadMore,
|
|
@@ -527,7 +561,7 @@ function raf(callback) {
|
|
|
527
561
|
if (typeof requestAnimationFrame !== "undefined") {
|
|
528
562
|
return requestAnimationFrame(callback);
|
|
529
563
|
}
|
|
530
|
-
return setTimeout(() => callback(
|
|
564
|
+
return setTimeout(() => callback(performance.now()), 16);
|
|
531
565
|
}
|
|
532
566
|
function cancelRaf(id) {
|
|
533
567
|
if (typeof cancelAnimationFrame !== "undefined") {
|
|
@@ -548,14 +582,30 @@ function scrollControllerPlugin(options = {}) {
|
|
|
548
582
|
let kernel = null;
|
|
549
583
|
let animationId = null;
|
|
550
584
|
let animating = false;
|
|
585
|
+
let animationGeneration = 0;
|
|
586
|
+
function isHorizontal(k) {
|
|
587
|
+
return k.getOptions().direction === "horizontal";
|
|
588
|
+
}
|
|
589
|
+
function getOffset(k) {
|
|
590
|
+
const position = k.getScrollPosition();
|
|
591
|
+
return isHorizontal(k) ? position.scrollLeft : position.scrollTop;
|
|
592
|
+
}
|
|
593
|
+
function setOffset(k, offset) {
|
|
594
|
+
k.setScrollPosition(isHorizontal(k) ? { scrollLeft: offset } : { scrollTop: offset });
|
|
595
|
+
}
|
|
596
|
+
function getViewportSize(k) {
|
|
597
|
+
const viewport = k.getViewport();
|
|
598
|
+
return isHorizontal(k) ? viewport.width : viewport.height;
|
|
599
|
+
}
|
|
551
600
|
function animateScroll(targetOffset, duration, easing) {
|
|
552
601
|
if (!kernel) return;
|
|
553
602
|
stopAnimation();
|
|
603
|
+
const generation = animationGeneration;
|
|
554
604
|
const startTime = performance.now();
|
|
555
|
-
const startOffset = kernel
|
|
605
|
+
const startOffset = getOffset(kernel);
|
|
556
606
|
const distance = targetOffset - startOffset;
|
|
557
|
-
if (distance === 0 || duration
|
|
558
|
-
kernel
|
|
607
|
+
if (distance === 0 || !(duration > 0)) {
|
|
608
|
+
setOffset(kernel, targetOffset);
|
|
559
609
|
return;
|
|
560
610
|
}
|
|
561
611
|
animating = true;
|
|
@@ -564,11 +614,13 @@ function scrollControllerPlugin(options = {}) {
|
|
|
564
614
|
animating = false;
|
|
565
615
|
return;
|
|
566
616
|
}
|
|
617
|
+
if (generation !== animationGeneration) return;
|
|
567
618
|
const elapsed = currentTime - startTime;
|
|
568
|
-
const progress = Math.min(elapsed / duration, 1);
|
|
619
|
+
const progress = Math.max(0, Math.min(elapsed / duration, 1));
|
|
569
620
|
const easedProgress = easing(progress);
|
|
570
621
|
const currentOffset = startOffset + distance * easedProgress;
|
|
571
|
-
kernel
|
|
622
|
+
setOffset(kernel, currentOffset);
|
|
623
|
+
if (generation !== animationGeneration) return;
|
|
572
624
|
if (progress < 1) {
|
|
573
625
|
animationId = raf(animate);
|
|
574
626
|
} else {
|
|
@@ -579,6 +631,7 @@ function scrollControllerPlugin(options = {}) {
|
|
|
579
631
|
animationId = raf(animate);
|
|
580
632
|
}
|
|
581
633
|
function stopAnimation() {
|
|
634
|
+
animationGeneration++;
|
|
582
635
|
if (animationId !== null) {
|
|
583
636
|
cancelRaf(animationId);
|
|
584
637
|
animationId = null;
|
|
@@ -595,7 +648,7 @@ function scrollControllerPlugin(options = {}) {
|
|
|
595
648
|
} = options2;
|
|
596
649
|
if (behavior === "auto") {
|
|
597
650
|
stopAnimation();
|
|
598
|
-
kernel
|
|
651
|
+
setOffset(kernel, offset);
|
|
599
652
|
} else {
|
|
600
653
|
animateScroll(offset, duration, easing);
|
|
601
654
|
}
|
|
@@ -609,35 +662,34 @@ function scrollControllerPlugin(options = {}) {
|
|
|
609
662
|
} = options2;
|
|
610
663
|
const itemOffset = kernel.getItemOffset(index);
|
|
611
664
|
const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
|
|
612
|
-
const
|
|
665
|
+
const viewportSize = getViewportSize(kernel);
|
|
613
666
|
let targetOffset;
|
|
614
667
|
switch (align) {
|
|
615
668
|
case "start":
|
|
616
669
|
targetOffset = itemOffset;
|
|
617
670
|
break;
|
|
618
671
|
case "center":
|
|
619
|
-
targetOffset = itemOffset -
|
|
672
|
+
targetOffset = itemOffset - viewportSize / 2 + itemSize / 2;
|
|
620
673
|
break;
|
|
621
674
|
case "end":
|
|
622
|
-
targetOffset = itemOffset -
|
|
675
|
+
targetOffset = itemOffset - viewportSize + itemSize;
|
|
623
676
|
break;
|
|
624
677
|
case "auto":
|
|
625
678
|
default: {
|
|
626
|
-
const
|
|
627
|
-
const
|
|
628
|
-
const visibleEnd = visibleStart + viewport.height;
|
|
679
|
+
const visibleStart = getOffset(kernel);
|
|
680
|
+
const visibleEnd = visibleStart + viewportSize;
|
|
629
681
|
const itemEnd = itemOffset + itemSize;
|
|
630
682
|
if (itemOffset < visibleStart) {
|
|
631
683
|
targetOffset = itemOffset;
|
|
632
684
|
} else if (itemEnd > visibleEnd) {
|
|
633
|
-
targetOffset = itemEnd -
|
|
685
|
+
targetOffset = itemEnd - viewportSize;
|
|
634
686
|
} else {
|
|
635
687
|
return;
|
|
636
688
|
}
|
|
637
689
|
break;
|
|
638
690
|
}
|
|
639
691
|
}
|
|
640
|
-
const maxScroll = Math.max(0, kernel.getTotalSize() -
|
|
692
|
+
const maxScroll = Math.max(0, kernel.getTotalSize() - viewportSize);
|
|
641
693
|
targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
|
|
642
694
|
api.scrollTo(targetOffset, scrollOptions);
|
|
643
695
|
},
|
|
@@ -646,9 +698,8 @@ function scrollControllerPlugin(options = {}) {
|
|
|
646
698
|
},
|
|
647
699
|
scrollToBottom(options2 = {}) {
|
|
648
700
|
if (!kernel) return;
|
|
649
|
-
const viewport = kernel.getViewport();
|
|
650
701
|
const totalSize = kernel.getTotalSize();
|
|
651
|
-
const maxScroll = Math.max(0, totalSize -
|
|
702
|
+
const maxScroll = Math.max(0, totalSize - getViewportSize(kernel));
|
|
652
703
|
api.scrollTo(maxScroll, options2);
|
|
653
704
|
},
|
|
654
705
|
getScrollPosition() {
|
|
@@ -688,16 +739,16 @@ function masonryRenderer(options) {
|
|
|
688
739
|
} = options;
|
|
689
740
|
let kernel = null;
|
|
690
741
|
let virtualItems = [];
|
|
691
|
-
let columnCount = typeof columns === "number" ? columns : 1;
|
|
742
|
+
let columnCount = typeof columns === "number" ? normalizeColumnCount(columns) : 1;
|
|
692
743
|
let columnWidth = 0;
|
|
693
744
|
let columnHeights = [];
|
|
694
745
|
let totalHeight = 0;
|
|
695
746
|
const layoutCache = /* @__PURE__ */ new Map();
|
|
696
747
|
function calculateColumns(containerWidth) {
|
|
697
748
|
if (typeof columns === "number") {
|
|
698
|
-
columnCount = columns;
|
|
749
|
+
columnCount = normalizeColumnCount(columns);
|
|
699
750
|
} else {
|
|
700
|
-
columnCount =
|
|
751
|
+
columnCount = autoColumnCount(containerWidth, minColumnWidth, gap);
|
|
701
752
|
}
|
|
702
753
|
columnWidth = (containerWidth - (columnCount - 1) * gap) / columnCount;
|
|
703
754
|
}
|
|
@@ -705,6 +756,7 @@ function masonryRenderer(options) {
|
|
|
705
756
|
if (!kernel) return;
|
|
706
757
|
const itemCount = kernel.getItemCount();
|
|
707
758
|
columnHeights = new Array(columnCount).fill(0);
|
|
759
|
+
const itemsInColumn = new Array(columnCount).fill(0);
|
|
708
760
|
layoutCache.clear();
|
|
709
761
|
for (let i = 0; i < itemCount; i++) {
|
|
710
762
|
let shortestCol = 0;
|
|
@@ -713,15 +765,15 @@ function masonryRenderer(options) {
|
|
|
713
765
|
shortestCol = j;
|
|
714
766
|
}
|
|
715
767
|
}
|
|
716
|
-
const
|
|
768
|
+
const measured = getItemHeight(i, columnWidth);
|
|
769
|
+
const itemHeight = Number.isFinite(measured) && measured > 0 ? measured : 0;
|
|
717
770
|
const top = columnHeights[shortestCol] ?? 0;
|
|
718
771
|
layoutCache.set(i, { top, lane: shortestCol, height: itemHeight });
|
|
719
772
|
columnHeights[shortestCol] = top + itemHeight + gap;
|
|
773
|
+
itemsInColumn[shortestCol] = (itemsInColumn[shortestCol] ?? 0) + 1;
|
|
720
774
|
}
|
|
775
|
+
columnHeights = columnHeights.map((height, column) => (itemsInColumn[column] ?? 0) > 0 ? height - gap : 0);
|
|
721
776
|
totalHeight = Math.max(...columnHeights, 0);
|
|
722
|
-
if (totalHeight > 0) {
|
|
723
|
-
totalHeight -= gap;
|
|
724
|
-
}
|
|
725
777
|
}
|
|
726
778
|
function buildVirtualItems() {
|
|
727
779
|
if (!kernel) return [];
|
|
@@ -808,6 +860,8 @@ function masonryRenderer(options) {
|
|
|
808
860
|
uninstall() {
|
|
809
861
|
virtualItems = [];
|
|
810
862
|
layoutCache.clear();
|
|
863
|
+
columnHeights = [];
|
|
864
|
+
totalHeight = 0;
|
|
811
865
|
kernel = null;
|
|
812
866
|
},
|
|
813
867
|
hooks: {
|
|
@@ -852,12 +906,15 @@ function stickyHeaders(options) {
|
|
|
852
906
|
}
|
|
853
907
|
}
|
|
854
908
|
}
|
|
855
|
-
function
|
|
909
|
+
function offsetOf(position) {
|
|
910
|
+
return kernel?.getOptions().direction === "horizontal" ? position.scrollLeft : position.scrollTop;
|
|
911
|
+
}
|
|
912
|
+
function updateStickyHeader(scrollOffset) {
|
|
856
913
|
if (!kernel || headerIndices.length === 0) {
|
|
857
914
|
currentStickyHeader = null;
|
|
858
915
|
return;
|
|
859
916
|
}
|
|
860
|
-
const scrollTop =
|
|
917
|
+
const scrollTop = scrollOffset + stickyOffset;
|
|
861
918
|
let newStickyHeader = null;
|
|
862
919
|
for (const headerIndex of headerIndices) {
|
|
863
920
|
const headerOffset = kernel.getItemOffset(headerIndex);
|
|
@@ -869,6 +926,10 @@ function stickyHeaders(options) {
|
|
|
869
926
|
}
|
|
870
927
|
currentStickyHeader = newStickyHeader;
|
|
871
928
|
}
|
|
929
|
+
function refreshStickyHeader() {
|
|
930
|
+
if (!kernel) return;
|
|
931
|
+
updateStickyHeader(offsetOf(kernel.getScrollPosition()));
|
|
932
|
+
}
|
|
872
933
|
function findHeaderForGroup(groupId) {
|
|
873
934
|
const items = groupIdMap.get(groupId);
|
|
874
935
|
if (!items || items.length === 0) return null;
|
|
@@ -909,6 +970,7 @@ function stickyHeaders(options) {
|
|
|
909
970
|
install(k) {
|
|
910
971
|
kernel = k;
|
|
911
972
|
buildHeaderCache();
|
|
973
|
+
refreshStickyHeader();
|
|
912
974
|
},
|
|
913
975
|
uninstall() {
|
|
914
976
|
headerIndices = [];
|
|
@@ -917,14 +979,24 @@ function stickyHeaders(options) {
|
|
|
917
979
|
kernel = null;
|
|
918
980
|
},
|
|
919
981
|
hooks: {
|
|
920
|
-
onScroll: updateStickyHeader,
|
|
921
|
-
onItemsChange: () =>
|
|
982
|
+
onScroll: (event) => updateStickyHeader(offsetOf(event)),
|
|
983
|
+
onItemsChange: () => {
|
|
984
|
+
buildHeaderCache();
|
|
985
|
+
refreshStickyHeader();
|
|
986
|
+
}
|
|
922
987
|
},
|
|
923
988
|
api
|
|
924
989
|
};
|
|
925
990
|
}
|
|
926
991
|
|
|
927
992
|
// src/plugins/optional/keyboard-nav.ts
|
|
993
|
+
var focusByKernel = /* @__PURE__ */ new WeakMap();
|
|
994
|
+
function isEditableTarget(target) {
|
|
995
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
996
|
+
if (target.isContentEditable) return true;
|
|
997
|
+
const tag = target.tagName;
|
|
998
|
+
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT";
|
|
999
|
+
}
|
|
928
1000
|
function keyboardNav(options = {}) {
|
|
929
1001
|
const {
|
|
930
1002
|
enabled: initialEnabled = true,
|
|
@@ -944,6 +1016,9 @@ function keyboardNav(options = {}) {
|
|
|
944
1016
|
let focusedIndex = null;
|
|
945
1017
|
let typeaheadBuffer = "";
|
|
946
1018
|
let typeaheadTimer = null;
|
|
1019
|
+
function rememberFocus() {
|
|
1020
|
+
if (kernel) focusByKernel.set(kernel, focusedIndex);
|
|
1021
|
+
}
|
|
947
1022
|
function getItemCount() {
|
|
948
1023
|
return kernel?.getItemCount() ?? 0;
|
|
949
1024
|
}
|
|
@@ -952,18 +1027,39 @@ function keyboardNav(options = {}) {
|
|
|
952
1027
|
if (count === 0) return 0;
|
|
953
1028
|
return Math.max(0, Math.min(index, count - 1));
|
|
954
1029
|
}
|
|
955
|
-
function
|
|
1030
|
+
function revealIndex(index) {
|
|
1031
|
+
if (!kernel) return;
|
|
1032
|
+
if (orientation !== "grid" || columns <= 1) {
|
|
1033
|
+
kernel.scrollToIndex(index, { align: "auto" });
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
const rows = Math.ceil(getItemCount() / columns);
|
|
1037
|
+
if (rows === 0) return;
|
|
1038
|
+
const pitch = kernel.getTotalSize() / rows;
|
|
1039
|
+
const top = Math.floor(index / columns) * pitch;
|
|
1040
|
+
const bottom = top + pitch;
|
|
1041
|
+
const viewportHeight = kernel.getViewport().height;
|
|
1042
|
+
const scrollTop = kernel.getScrollPosition().scrollTop;
|
|
1043
|
+
if (top < scrollTop) {
|
|
1044
|
+
kernel.scrollTo(top);
|
|
1045
|
+
} else if (bottom > scrollTop + viewportHeight) {
|
|
1046
|
+
kernel.scrollTo(bottom - viewportHeight);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
function setFocus(index, wrap = true) {
|
|
956
1050
|
if (index === null) {
|
|
957
1051
|
focusedIndex = null;
|
|
1052
|
+
rememberFocus();
|
|
958
1053
|
return;
|
|
959
1054
|
}
|
|
960
1055
|
const count = getItemCount();
|
|
961
1056
|
if (count === 0) {
|
|
962
1057
|
focusedIndex = null;
|
|
1058
|
+
rememberFocus();
|
|
963
1059
|
return;
|
|
964
1060
|
}
|
|
965
1061
|
let newIndex = index;
|
|
966
|
-
if (loop) {
|
|
1062
|
+
if (loop && wrap) {
|
|
967
1063
|
if (newIndex < 0) {
|
|
968
1064
|
newIndex = count - 1;
|
|
969
1065
|
} else if (newIndex >= count) {
|
|
@@ -974,12 +1070,14 @@ function keyboardNav(options = {}) {
|
|
|
974
1070
|
}
|
|
975
1071
|
if (newIndex !== focusedIndex) {
|
|
976
1072
|
focusedIndex = newIndex;
|
|
1073
|
+
rememberFocus();
|
|
977
1074
|
onFocus?.(newIndex);
|
|
978
|
-
|
|
1075
|
+
revealIndex(newIndex);
|
|
979
1076
|
}
|
|
980
1077
|
}
|
|
981
1078
|
function handleKeyDown(event) {
|
|
982
1079
|
if (!enabled || !kernel) return;
|
|
1080
|
+
if (isEditableTarget(event.target)) return;
|
|
983
1081
|
if (onKeyDown) {
|
|
984
1082
|
const handled = onKeyDown(event, focusedIndex);
|
|
985
1083
|
if (handled === true) {
|
|
@@ -987,19 +1085,18 @@ function keyboardNav(options = {}) {
|
|
|
987
1085
|
return;
|
|
988
1086
|
}
|
|
989
1087
|
}
|
|
1088
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
990
1089
|
const count = getItemCount();
|
|
991
1090
|
if (count === 0) return;
|
|
992
|
-
|
|
993
|
-
setFocus(0);
|
|
994
|
-
}
|
|
1091
|
+
const focusBeforeKey = focusedIndex;
|
|
995
1092
|
switch (event.key) {
|
|
996
1093
|
case "ArrowDown":
|
|
997
1094
|
if (orientation === "vertical" || orientation === "grid") {
|
|
998
1095
|
event.preventDefault();
|
|
999
|
-
if (
|
|
1000
|
-
setFocus(
|
|
1096
|
+
if (focusedIndex === null) {
|
|
1097
|
+
setFocus(0);
|
|
1001
1098
|
} else {
|
|
1002
|
-
setFocus(
|
|
1099
|
+
setFocus(focusedIndex + (orientation === "grid" ? columns : 1));
|
|
1003
1100
|
}
|
|
1004
1101
|
}
|
|
1005
1102
|
break;
|
|
@@ -1016,7 +1113,7 @@ function keyboardNav(options = {}) {
|
|
|
1016
1113
|
case "ArrowRight":
|
|
1017
1114
|
if (orientation === "horizontal" || orientation === "grid") {
|
|
1018
1115
|
event.preventDefault();
|
|
1019
|
-
setFocus(
|
|
1116
|
+
setFocus(focusedIndex === null ? 0 : focusedIndex + 1);
|
|
1020
1117
|
}
|
|
1021
1118
|
break;
|
|
1022
1119
|
case "ArrowLeft":
|
|
@@ -1035,35 +1132,35 @@ function keyboardNav(options = {}) {
|
|
|
1035
1132
|
break;
|
|
1036
1133
|
case "PageUp":
|
|
1037
1134
|
event.preventDefault();
|
|
1038
|
-
setFocus((focusedIndex ?? 0) - pageSize);
|
|
1135
|
+
setFocus((focusedIndex ?? 0) - pageSize, false);
|
|
1039
1136
|
break;
|
|
1040
1137
|
case "PageDown":
|
|
1041
1138
|
event.preventDefault();
|
|
1042
|
-
setFocus((focusedIndex ?? 0) + pageSize);
|
|
1139
|
+
setFocus((focusedIndex ?? 0) + pageSize, false);
|
|
1043
1140
|
break;
|
|
1044
1141
|
case "Enter":
|
|
1045
1142
|
case " ":
|
|
1046
|
-
event.preventDefault();
|
|
1047
1143
|
if (focusedIndex !== null) {
|
|
1144
|
+
event.preventDefault();
|
|
1048
1145
|
onSelect?.(focusedIndex);
|
|
1049
1146
|
}
|
|
1050
1147
|
break;
|
|
1051
1148
|
default:
|
|
1052
1149
|
if (typeahead && event.key.length === 1 && !event.ctrlKey && !event.metaKey) {
|
|
1053
1150
|
event.preventDefault();
|
|
1054
|
-
handleTypeahead(event.key);
|
|
1151
|
+
handleTypeahead(event.key, focusBeforeKey);
|
|
1055
1152
|
}
|
|
1056
1153
|
break;
|
|
1057
1154
|
}
|
|
1058
1155
|
}
|
|
1059
|
-
function handleTypeahead(char) {
|
|
1156
|
+
function handleTypeahead(char, focusBeforeKey) {
|
|
1060
1157
|
if (!kernel) return;
|
|
1061
1158
|
if (typeaheadTimer) {
|
|
1062
1159
|
clearTimeout(typeaheadTimer);
|
|
1063
1160
|
}
|
|
1064
1161
|
typeaheadBuffer += char.toLowerCase();
|
|
1065
1162
|
const count = getItemCount();
|
|
1066
|
-
const startIndex = focusedIndex !== null ?
|
|
1163
|
+
const startIndex = typeaheadBuffer.length > 1 ? focusedIndex ?? 0 : focusBeforeKey !== null ? focusBeforeKey + 1 : 0;
|
|
1067
1164
|
for (let i = 0; i < count; i++) {
|
|
1068
1165
|
const index = (startIndex + i) % count;
|
|
1069
1166
|
const text = getItemText(index).toLowerCase();
|
|
@@ -1132,6 +1229,9 @@ function keyboardNav(options = {}) {
|
|
|
1132
1229
|
type: "optional",
|
|
1133
1230
|
install(k) {
|
|
1134
1231
|
kernel = k;
|
|
1232
|
+
const remembered = focusByKernel.get(k) ?? null;
|
|
1233
|
+
const count = k.getItemCount();
|
|
1234
|
+
focusedIndex = remembered === null || count === 0 ? null : Math.min(remembered, count - 1);
|
|
1135
1235
|
document.addEventListener("keydown", handleKeyDown);
|
|
1136
1236
|
},
|
|
1137
1237
|
uninstall() {
|
|
@@ -1158,6 +1258,7 @@ function keyboardNav(options = {}) {
|
|
|
1158
1258
|
}
|
|
1159
1259
|
|
|
1160
1260
|
// src/plugins/optional/scroll-restoration.ts
|
|
1261
|
+
var restoredKernels = /* @__PURE__ */ new WeakMap();
|
|
1161
1262
|
function scrollRestoration(options) {
|
|
1162
1263
|
const {
|
|
1163
1264
|
key,
|
|
@@ -1171,6 +1272,8 @@ function scrollRestoration(options) {
|
|
|
1171
1272
|
} = options;
|
|
1172
1273
|
let kernel = null;
|
|
1173
1274
|
let debouncedSave = null;
|
|
1275
|
+
let restoreFrame = null;
|
|
1276
|
+
let awaitingContent = false;
|
|
1174
1277
|
function getStorage() {
|
|
1175
1278
|
if (typeof storage === "string") {
|
|
1176
1279
|
if (storage === "sessionStorage") {
|
|
@@ -1227,7 +1330,7 @@ function scrollRestoration(options) {
|
|
|
1227
1330
|
if (!kernel) return false;
|
|
1228
1331
|
const state = getSavedState();
|
|
1229
1332
|
if (!state) return false;
|
|
1230
|
-
kernel.scrollTo(state.scrollTop);
|
|
1333
|
+
kernel.scrollTo(kernel.getOptions().direction === "horizontal" ? state.scrollLeft : state.scrollTop);
|
|
1231
1334
|
if (includeIndex && state.focusedIndex !== void 0) {
|
|
1232
1335
|
const keyboardNav2 = kernel.getPlugin("keyboard-nav");
|
|
1233
1336
|
if (keyboardNav2?.api?.setFocusedIndex) {
|
|
@@ -1236,6 +1339,26 @@ function scrollRestoration(options) {
|
|
|
1236
1339
|
}
|
|
1237
1340
|
return true;
|
|
1238
1341
|
}
|
|
1342
|
+
function restoreOnceReachable() {
|
|
1343
|
+
if (!kernel || restoredKernels.get(kernel) === key) {
|
|
1344
|
+
awaitingContent = false;
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
const state = getSavedState();
|
|
1348
|
+
if (state) {
|
|
1349
|
+
const horizontal = kernel.getOptions().direction === "horizontal";
|
|
1350
|
+
const viewport = kernel.getViewport();
|
|
1351
|
+
const target = horizontal ? state.scrollLeft : state.scrollTop;
|
|
1352
|
+
const maxScroll = kernel.getTotalSize() - (horizontal ? viewport.width : viewport.height);
|
|
1353
|
+
if (target > maxScroll + 1) {
|
|
1354
|
+
awaitingContent = true;
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
awaitingContent = false;
|
|
1359
|
+
restoredKernels.set(kernel, key);
|
|
1360
|
+
restore();
|
|
1361
|
+
}
|
|
1239
1362
|
function clear() {
|
|
1240
1363
|
try {
|
|
1241
1364
|
getStorage().removeItem(getStorageKey());
|
|
@@ -1268,30 +1391,41 @@ function scrollRestoration(options) {
|
|
|
1268
1391
|
if (saveOnScroll) {
|
|
1269
1392
|
debouncedSave = debounce(save, debounceMs);
|
|
1270
1393
|
}
|
|
1271
|
-
if (restoreOnMount) {
|
|
1272
|
-
requestAnimationFrame(() => {
|
|
1273
|
-
|
|
1394
|
+
if (restoreOnMount && restoredKernels.get(k) !== key) {
|
|
1395
|
+
restoreFrame = requestAnimationFrame(() => {
|
|
1396
|
+
restoreFrame = null;
|
|
1397
|
+
restoreOnceReachable();
|
|
1274
1398
|
});
|
|
1275
1399
|
}
|
|
1276
1400
|
},
|
|
1277
1401
|
uninstall() {
|
|
1278
|
-
|
|
1402
|
+
const restorePending = restoreFrame !== null;
|
|
1403
|
+
if (restoreFrame !== null) {
|
|
1404
|
+
cancelAnimationFrame(restoreFrame);
|
|
1405
|
+
restoreFrame = null;
|
|
1406
|
+
}
|
|
1407
|
+
if (saveOnUnmount && !restorePending && !awaitingContent) {
|
|
1279
1408
|
save();
|
|
1280
1409
|
}
|
|
1281
1410
|
if (debouncedSave) {
|
|
1282
1411
|
debouncedSave.cancel();
|
|
1283
1412
|
debouncedSave = null;
|
|
1284
1413
|
}
|
|
1414
|
+
awaitingContent = false;
|
|
1285
1415
|
kernel = null;
|
|
1286
1416
|
},
|
|
1287
1417
|
hooks: {
|
|
1288
|
-
onScroll: handleScroll
|
|
1418
|
+
onScroll: handleScroll,
|
|
1419
|
+
onItemsChange: () => {
|
|
1420
|
+
if (awaitingContent) restoreOnceReachable();
|
|
1421
|
+
}
|
|
1289
1422
|
},
|
|
1290
1423
|
api
|
|
1291
1424
|
};
|
|
1292
1425
|
}
|
|
1293
1426
|
|
|
1294
1427
|
// src/plugins/optional/bidirectional.ts
|
|
1428
|
+
var positionedKernels = /* @__PURE__ */ new WeakSet();
|
|
1295
1429
|
function bidirectional(options = {}) {
|
|
1296
1430
|
const {
|
|
1297
1431
|
initialPosition = "bottom",
|
|
@@ -1306,18 +1440,25 @@ function bidirectional(options = {}) {
|
|
|
1306
1440
|
let isSticking = initialStickToBottom;
|
|
1307
1441
|
let previousTotalSize = 0;
|
|
1308
1442
|
let isInitialized = false;
|
|
1443
|
+
let initialFrame = null;
|
|
1444
|
+
let backwardTriggered = false;
|
|
1445
|
+
let forwardTriggered = false;
|
|
1446
|
+
function isHorizontal() {
|
|
1447
|
+
return kernel?.getOptions().direction === "horizontal";
|
|
1448
|
+
}
|
|
1449
|
+
function getOffset(position) {
|
|
1450
|
+
return isHorizontal() ? position.scrollLeft : position.scrollTop;
|
|
1451
|
+
}
|
|
1309
1452
|
function checkIsAtBottom() {
|
|
1310
1453
|
if (!kernel) return false;
|
|
1311
1454
|
const viewport = kernel.getViewport();
|
|
1312
|
-
const scrollPosition = kernel.getScrollPosition();
|
|
1313
1455
|
const totalSize = kernel.getTotalSize();
|
|
1314
|
-
const maxScroll = totalSize - viewport.height;
|
|
1315
|
-
return
|
|
1456
|
+
const maxScroll = totalSize - (isHorizontal() ? viewport.width : viewport.height);
|
|
1457
|
+
return getOffset(kernel.getScrollPosition()) >= maxScroll - stickThreshold;
|
|
1316
1458
|
}
|
|
1317
1459
|
function checkIsAtTop() {
|
|
1318
1460
|
if (!kernel) return false;
|
|
1319
|
-
|
|
1320
|
-
return scrollPosition.scrollTop <= stickThreshold;
|
|
1461
|
+
return getOffset(kernel.getScrollPosition()) <= stickThreshold;
|
|
1321
1462
|
}
|
|
1322
1463
|
function handleScroll(event) {
|
|
1323
1464
|
if (!kernel) return;
|
|
@@ -1327,16 +1468,30 @@ function bidirectional(options = {}) {
|
|
|
1327
1468
|
if (onLoadMore) {
|
|
1328
1469
|
const viewport = kernel.getViewport();
|
|
1329
1470
|
const totalSize = kernel.getTotalSize();
|
|
1330
|
-
|
|
1331
|
-
|
|
1471
|
+
const offset = getOffset(event);
|
|
1472
|
+
const viewportSize = isHorizontal() ? viewport.width : viewport.height;
|
|
1473
|
+
if (offset < loadMoreThreshold) {
|
|
1474
|
+
if (!backwardTriggered) {
|
|
1475
|
+
backwardTriggered = true;
|
|
1476
|
+
onLoadMore("backward");
|
|
1477
|
+
}
|
|
1478
|
+
} else {
|
|
1479
|
+
backwardTriggered = false;
|
|
1332
1480
|
}
|
|
1333
|
-
const distanceFromBottom = totalSize -
|
|
1481
|
+
const distanceFromBottom = totalSize - offset - viewportSize;
|
|
1334
1482
|
if (distanceFromBottom < loadMoreThreshold) {
|
|
1335
|
-
|
|
1483
|
+
if (!forwardTriggered) {
|
|
1484
|
+
forwardTriggered = true;
|
|
1485
|
+
onLoadMore("forward");
|
|
1486
|
+
}
|
|
1487
|
+
} else {
|
|
1488
|
+
forwardTriggered = false;
|
|
1336
1489
|
}
|
|
1337
1490
|
}
|
|
1338
1491
|
}
|
|
1339
1492
|
function handleItemsChange(count, previousCount) {
|
|
1493
|
+
backwardTriggered = false;
|
|
1494
|
+
forwardTriggered = false;
|
|
1340
1495
|
if (!kernel || !isInitialized) return;
|
|
1341
1496
|
const newTotalSize = kernel.getTotalSize();
|
|
1342
1497
|
const sizeDelta = newTotalSize - previousTotalSize;
|
|
@@ -1379,9 +1534,10 @@ function bidirectional(options = {}) {
|
|
|
1379
1534
|
adjustForPrepend(prependedHeight) {
|
|
1380
1535
|
if (!kernel || prependedHeight <= 0) return;
|
|
1381
1536
|
const scrollPosition = kernel.getScrollPosition();
|
|
1537
|
+
const horizontal = isHorizontal();
|
|
1382
1538
|
kernel.setScrollPosition({
|
|
1383
|
-
scrollTop: scrollPosition.scrollTop + prependedHeight,
|
|
1384
|
-
scrollLeft: scrollPosition.scrollLeft
|
|
1539
|
+
scrollTop: scrollPosition.scrollTop + (horizontal ? 0 : prependedHeight),
|
|
1540
|
+
scrollLeft: scrollPosition.scrollLeft + (horizontal ? prependedHeight : 0)
|
|
1385
1541
|
});
|
|
1386
1542
|
}
|
|
1387
1543
|
};
|
|
@@ -1393,10 +1549,15 @@ function bidirectional(options = {}) {
|
|
|
1393
1549
|
kernel = k;
|
|
1394
1550
|
k.getItemCount();
|
|
1395
1551
|
previousTotalSize = k.getTotalSize();
|
|
1396
|
-
|
|
1397
|
-
|
|
1552
|
+
isSticking = stickToBottom && (initialPosition === "bottom" && !positionedKernels.has(k) || checkIsAtBottom());
|
|
1553
|
+
if (initialPosition === "bottom" && !positionedKernels.has(k)) {
|
|
1554
|
+
initialFrame = requestAnimationFrame(() => {
|
|
1555
|
+
initialFrame = null;
|
|
1398
1556
|
if (kernel) {
|
|
1399
|
-
|
|
1557
|
+
if (!positionedKernels.has(kernel)) {
|
|
1558
|
+
positionedKernels.add(kernel);
|
|
1559
|
+
kernel.scrollTo(kernel.getTotalSize());
|
|
1560
|
+
}
|
|
1400
1561
|
isInitialized = true;
|
|
1401
1562
|
}
|
|
1402
1563
|
});
|
|
@@ -1405,8 +1566,14 @@ function bidirectional(options = {}) {
|
|
|
1405
1566
|
}
|
|
1406
1567
|
},
|
|
1407
1568
|
uninstall() {
|
|
1569
|
+
if (initialFrame !== null) {
|
|
1570
|
+
cancelAnimationFrame(initialFrame);
|
|
1571
|
+
initialFrame = null;
|
|
1572
|
+
}
|
|
1408
1573
|
kernel = null;
|
|
1409
1574
|
isInitialized = false;
|
|
1575
|
+
backwardTriggered = false;
|
|
1576
|
+
forwardTriggered = false;
|
|
1410
1577
|
},
|
|
1411
1578
|
hooks: {
|
|
1412
1579
|
onScroll: handleScroll,
|
|
@@ -1417,6 +1584,7 @@ function bidirectional(options = {}) {
|
|
|
1417
1584
|
}
|
|
1418
1585
|
|
|
1419
1586
|
// src/plugins/optional/drag-to-reorder.ts
|
|
1587
|
+
var dragByKernel = /* @__PURE__ */ new WeakMap();
|
|
1420
1588
|
function dragToReorder(options) {
|
|
1421
1589
|
const {
|
|
1422
1590
|
enabled: initialEnabled = true,
|
|
@@ -1436,6 +1604,9 @@ function dragToReorder(options) {
|
|
|
1436
1604
|
let draggedIndex = null;
|
|
1437
1605
|
let dropTargetIndex = null;
|
|
1438
1606
|
let autoScrollTimer = null;
|
|
1607
|
+
function rememberDrag() {
|
|
1608
|
+
if (kernel) dragByKernel.set(kernel, { dragged: draggedIndex, target: dropTargetIndex });
|
|
1609
|
+
}
|
|
1439
1610
|
function handleDragStart(event, index) {
|
|
1440
1611
|
if (!enabled || !canDrag(index)) {
|
|
1441
1612
|
event.preventDefault();
|
|
@@ -1443,6 +1614,7 @@ function dragToReorder(options) {
|
|
|
1443
1614
|
}
|
|
1444
1615
|
draggedIndex = index;
|
|
1445
1616
|
dropTargetIndex = index;
|
|
1617
|
+
rememberDrag();
|
|
1446
1618
|
event.dataTransfer?.setData("text/plain", String(index));
|
|
1447
1619
|
if (event.dataTransfer) {
|
|
1448
1620
|
event.dataTransfer.effectAllowed = "move";
|
|
@@ -1452,27 +1624,22 @@ function dragToReorder(options) {
|
|
|
1452
1624
|
startAutoScroll();
|
|
1453
1625
|
}
|
|
1454
1626
|
}
|
|
1455
|
-
function
|
|
1627
|
+
function finishDrag() {
|
|
1456
1628
|
const currentDraggedIndex = draggedIndex;
|
|
1457
|
-
const currentDropTargetIndex = dropTargetIndex;
|
|
1458
1629
|
stopAutoScroll();
|
|
1459
|
-
if (currentDraggedIndex !== null && currentDropTargetIndex !== null && currentDraggedIndex !== currentDropTargetIndex) {
|
|
1460
|
-
if (canDrop(currentDraggedIndex, currentDropTargetIndex)) {
|
|
1461
|
-
onReorder(currentDraggedIndex, currentDropTargetIndex);
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
1630
|
if (currentDraggedIndex !== null) {
|
|
1465
1631
|
onDragEnd?.(currentDraggedIndex);
|
|
1466
1632
|
}
|
|
1467
1633
|
draggedIndex = null;
|
|
1468
1634
|
dropTargetIndex = null;
|
|
1635
|
+
rememberDrag();
|
|
1636
|
+
}
|
|
1637
|
+
function handleDragEnd(_event) {
|
|
1638
|
+
finishDrag();
|
|
1469
1639
|
}
|
|
1470
1640
|
function handleDragOver(event, index) {
|
|
1471
1641
|
if (draggedIndex === null) return;
|
|
1472
1642
|
event.preventDefault();
|
|
1473
|
-
if (event.dataTransfer) {
|
|
1474
|
-
event.dataTransfer.dropEffect = canDrop(draggedIndex, index) ? "move" : "none";
|
|
1475
|
-
}
|
|
1476
1643
|
const target = event.currentTarget;
|
|
1477
1644
|
const rect = target.getBoundingClientRect();
|
|
1478
1645
|
let newDropTarget;
|
|
@@ -1487,6 +1654,10 @@ function dragToReorder(options) {
|
|
|
1487
1654
|
newDropTarget--;
|
|
1488
1655
|
}
|
|
1489
1656
|
dropTargetIndex = Math.max(0, Math.min(newDropTarget, (kernel?.getItemCount() ?? 1) - 1));
|
|
1657
|
+
rememberDrag();
|
|
1658
|
+
if (event.dataTransfer) {
|
|
1659
|
+
event.dataTransfer.dropEffect = canDrop(draggedIndex, dropTargetIndex) ? "move" : "none";
|
|
1660
|
+
}
|
|
1490
1661
|
}
|
|
1491
1662
|
function handleDragEnter(event, _index) {
|
|
1492
1663
|
event.preventDefault();
|
|
@@ -1495,6 +1666,14 @@ function dragToReorder(options) {
|
|
|
1495
1666
|
}
|
|
1496
1667
|
function handleDrop(event, _index) {
|
|
1497
1668
|
event.preventDefault();
|
|
1669
|
+
const currentDraggedIndex = draggedIndex;
|
|
1670
|
+
const currentDropTargetIndex = dropTargetIndex;
|
|
1671
|
+
if (currentDraggedIndex !== null && currentDropTargetIndex !== null && currentDraggedIndex !== currentDropTargetIndex) {
|
|
1672
|
+
if (canDrop(currentDraggedIndex, currentDropTargetIndex)) {
|
|
1673
|
+
onReorder(currentDraggedIndex, currentDropTargetIndex);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
finishDrag();
|
|
1498
1677
|
}
|
|
1499
1678
|
function startAutoScroll() {
|
|
1500
1679
|
if (autoScrollTimer) return;
|
|
@@ -1538,6 +1717,7 @@ function dragToReorder(options) {
|
|
|
1538
1717
|
stopAutoScroll();
|
|
1539
1718
|
draggedIndex = null;
|
|
1540
1719
|
dropTargetIndex = null;
|
|
1720
|
+
rememberDrag();
|
|
1541
1721
|
},
|
|
1542
1722
|
enable() {
|
|
1543
1723
|
enabled = true;
|
|
@@ -1557,6 +1737,14 @@ function dragToReorder(options) {
|
|
|
1557
1737
|
type: "optional",
|
|
1558
1738
|
install(k) {
|
|
1559
1739
|
kernel = k;
|
|
1740
|
+
const previous = dragByKernel.get(k);
|
|
1741
|
+
if (previous && previous.dragged !== null && enabled) {
|
|
1742
|
+
draggedIndex = previous.dragged;
|
|
1743
|
+
dropTargetIndex = previous.target;
|
|
1744
|
+
if (autoScroll) startAutoScroll();
|
|
1745
|
+
} else if (previous) {
|
|
1746
|
+
rememberDrag();
|
|
1747
|
+
}
|
|
1560
1748
|
},
|
|
1561
1749
|
uninstall() {
|
|
1562
1750
|
stopAutoScroll();
|
|
@@ -1590,6 +1778,7 @@ function debugPanel(options = {}) {
|
|
|
1590
1778
|
let lastFpsUpdate = performance.now();
|
|
1591
1779
|
let currentFps = 60;
|
|
1592
1780
|
let rafId = null;
|
|
1781
|
+
let eventUnsubscribers = [];
|
|
1593
1782
|
const emptyRange = {
|
|
1594
1783
|
startIndex: 0,
|
|
1595
1784
|
endIndex: 0,
|
|
@@ -1660,8 +1849,13 @@ function debugPanel(options = {}) {
|
|
|
1660
1849
|
memoryUsage = Math.round(memory.usedJSHeapSize / 1024 / 1024 * 100) / 100;
|
|
1661
1850
|
}
|
|
1662
1851
|
}
|
|
1663
|
-
const
|
|
1664
|
-
const
|
|
1852
|
+
const countOf = (range) => itemCount === 0 ? 0 : (range.end ?? range.endIndex) - (range.start ?? range.startIndex) + 1;
|
|
1853
|
+
const visibleItems = countOf(visibleRange);
|
|
1854
|
+
const renderedItems = countOf(renderRange);
|
|
1855
|
+
const horizontal = kernel.getOptions().direction === "horizontal";
|
|
1856
|
+
const viewport = kernel.getViewport();
|
|
1857
|
+
const scrollOffset = horizontal ? scrollPos.scrollLeft : scrollPos.scrollTop;
|
|
1858
|
+
const maxScroll = kernel.getTotalSize() - (horizontal ? viewport.width : viewport.height);
|
|
1665
1859
|
state.stats = {
|
|
1666
1860
|
...state.stats,
|
|
1667
1861
|
memoryUsage,
|
|
@@ -1672,8 +1866,8 @@ function debugPanel(options = {}) {
|
|
|
1672
1866
|
avgItemHeight: itemCount > 0 ? kernel.getTotalSize() / itemCount : 0,
|
|
1673
1867
|
scrollPosition: {
|
|
1674
1868
|
...scrollPos,
|
|
1675
|
-
offset:
|
|
1676
|
-
percentage:
|
|
1869
|
+
offset: scrollOffset,
|
|
1870
|
+
percentage: maxScroll > 0 ? Math.min(1, Math.max(0, scrollOffset / maxScroll)) : 0
|
|
1677
1871
|
},
|
|
1678
1872
|
visibleRange: {
|
|
1679
1873
|
...visibleRange,
|
|
@@ -1709,6 +1903,10 @@ function debugPanel(options = {}) {
|
|
|
1709
1903
|
}
|
|
1710
1904
|
function startMonitoring() {
|
|
1711
1905
|
if (rafId !== null) return;
|
|
1906
|
+
const now = performance.now();
|
|
1907
|
+
frameCount = 0;
|
|
1908
|
+
lastFpsUpdate = now;
|
|
1909
|
+
lastFrameTime = now;
|
|
1712
1910
|
updateFps();
|
|
1713
1911
|
statsUpdateTimer = setInterval(updateStats, resolvedOptions.statsUpdateInterval);
|
|
1714
1912
|
}
|
|
@@ -1806,23 +2004,24 @@ function debugPanel(options = {}) {
|
|
|
1806
2004
|
kernel = k;
|
|
1807
2005
|
const eventTypes = [
|
|
1808
2006
|
"scroll",
|
|
1809
|
-
"
|
|
1810
|
-
"
|
|
1811
|
-
"
|
|
1812
|
-
"
|
|
2007
|
+
"scroll-start",
|
|
2008
|
+
"scroll-end",
|
|
2009
|
+
"visible-range-change",
|
|
2010
|
+
"item-measured",
|
|
1813
2011
|
"resize",
|
|
1814
|
-
"
|
|
1815
|
-
"
|
|
2012
|
+
"load-more",
|
|
2013
|
+
"items-change"
|
|
1816
2014
|
];
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
});
|
|
2015
|
+
eventUnsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
2016
|
+
eventUnsubscribers = eventTypes.map((type) => k.on(type, logEvent));
|
|
1820
2017
|
if (state.visible) {
|
|
1821
2018
|
startMonitoring();
|
|
1822
2019
|
}
|
|
1823
2020
|
},
|
|
1824
2021
|
uninstall() {
|
|
1825
2022
|
stopMonitoring();
|
|
2023
|
+
eventUnsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
2024
|
+
eventUnsubscribers = [];
|
|
1826
2025
|
kernel = null;
|
|
1827
2026
|
},
|
|
1828
2027
|
api: extendedApi
|
|
@@ -1832,6 +2031,7 @@ function useDebugPanelState(plugin) {
|
|
|
1832
2031
|
const api = plugin.api;
|
|
1833
2032
|
const [state, setState] = useState(api.getState());
|
|
1834
2033
|
useEffect(() => {
|
|
2034
|
+
setState({ ...api.getState() });
|
|
1835
2035
|
return api.subscribe(() => {
|
|
1836
2036
|
setState({ ...api.getState() });
|
|
1837
2037
|
});
|
|
@@ -1866,7 +2066,8 @@ function DebugPanelOverlay({
|
|
|
1866
2066
|
}
|
|
1867
2067
|
}, [options.position]);
|
|
1868
2068
|
const themeStyles = useMemo(() => {
|
|
1869
|
-
const
|
|
2069
|
+
const prefersDark = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
2070
|
+
const isDark = options.theme === "dark" || options.theme === "auto" && prefersDark;
|
|
1870
2071
|
return isDark ? {
|
|
1871
2072
|
backgroundColor: "rgba(30, 30, 30, 0.95)",
|
|
1872
2073
|
color: "#e0e0e0",
|
|
@@ -2159,13 +2360,13 @@ function EventsPanel({ eventLog, onClear }) {
|
|
|
2159
2360
|
};
|
|
2160
2361
|
const typeColors = {
|
|
2161
2362
|
scroll: "#6bcb77",
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2363
|
+
"scroll-start": "#4d96ff",
|
|
2364
|
+
"scroll-end": "#4d96ff",
|
|
2365
|
+
"visible-range-change": "#ffd93d",
|
|
2366
|
+
"item-measured": "#9b59b6",
|
|
2166
2367
|
resize: "#ff6b6b",
|
|
2167
|
-
|
|
2168
|
-
|
|
2368
|
+
"load-more": "#1abc9c",
|
|
2369
|
+
"items-change": "#e74c3c"
|
|
2169
2370
|
};
|
|
2170
2371
|
return /* @__PURE__ */ jsxs("div", { children: [
|
|
2171
2372
|
/* @__PURE__ */ jsx("button", { style: buttonStyles, onClick: onClear, children: "Clear Log" }),
|