@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.
@@ -0,0 +1,2181 @@
1
+ import { useMemo, useCallback, useState, useEffect } from 'react';
2
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
3
+
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
+ function listRendererPlugin(options = {}) {
32
+ const { getItemKey = (index) => index } = options;
33
+ let kernel = null;
34
+ let virtualItems = [];
35
+ let currentRange = {
36
+ startIndex: 0,
37
+ endIndex: 0,
38
+ overscanStartIndex: 0,
39
+ overscanEndIndex: 0
40
+ };
41
+ const itemPool = new VirtualItemPool();
42
+ function buildVirtualItems(range) {
43
+ if (!kernel) return [];
44
+ itemPool.releaseAll(virtualItems);
45
+ const items = [];
46
+ range.startIndex;
47
+ range.endIndex;
48
+ for (let i = range.overscanStartIndex; i <= range.overscanEndIndex; i++) {
49
+ const item = itemPool.acquire();
50
+ const offset = kernel.getItemOffset(i);
51
+ const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
52
+ item.index = i;
53
+ item.key = getItemKey(i);
54
+ item.start = offset;
55
+ item.end = offset + size;
56
+ item.size = size;
57
+ item.lane = 0;
58
+ items.push(item);
59
+ }
60
+ return items;
61
+ }
62
+ function recalculate() {
63
+ if (!kernel) return;
64
+ currentRange = kernel.getVisibleRange();
65
+ virtualItems = buildVirtualItems(currentRange);
66
+ }
67
+ const api = {
68
+ getVirtualItems() {
69
+ return virtualItems;
70
+ },
71
+ getTotalHeight() {
72
+ return kernel?.getTotalSize() ?? 0;
73
+ },
74
+ getStartOffset() {
75
+ if (virtualItems.length === 0) return 0;
76
+ return virtualItems[0]?.start ?? 0;
77
+ },
78
+ getEndOffset() {
79
+ if (virtualItems.length === 0) return 0;
80
+ const lastItem = virtualItems[virtualItems.length - 1];
81
+ return lastItem?.end ?? 0;
82
+ },
83
+ isItemVisible(index) {
84
+ return index >= currentRange.startIndex && index <= currentRange.endIndex;
85
+ },
86
+ recalculate
87
+ };
88
+ return {
89
+ name: "list-renderer",
90
+ version: "1.0.0",
91
+ type: "core",
92
+ install(k) {
93
+ kernel = k;
94
+ recalculate();
95
+ },
96
+ uninstall() {
97
+ itemPool.releaseAll(virtualItems);
98
+ virtualItems = [];
99
+ kernel = null;
100
+ },
101
+ hooks: {
102
+ onScroll: () => recalculate(),
103
+ onResize: () => recalculate(),
104
+ onItemMeasured: () => recalculate(),
105
+ onVisibleRangeChange: (range) => {
106
+ currentRange = range;
107
+ virtualItems = buildVirtualItems(range);
108
+ },
109
+ onItemsChange: () => recalculate()
110
+ },
111
+ api
112
+ };
113
+ }
114
+
115
+ // src/plugins/core/grid-renderer.ts
116
+ function gridRendererPlugin(options) {
117
+ const {
118
+ columns,
119
+ minColumnWidth = 100,
120
+ rowHeight,
121
+ gap = 0,
122
+ getItemKey = (index) => index
123
+ } = options;
124
+ let kernel = null;
125
+ let virtualItems = [];
126
+ let columnCount = typeof columns === "number" ? columns : 1;
127
+ let columnWidth = 0;
128
+ let gapX = typeof gap === "number" ? gap : gap.x;
129
+ let gapY = typeof gap === "number" ? gap : gap.y;
130
+ function calculateColumns(containerWidth) {
131
+ if (typeof columns === "number") {
132
+ columnCount = columns;
133
+ } else {
134
+ columnCount = Math.max(1, Math.floor((containerWidth + gapX) / (minColumnWidth + gapX)));
135
+ }
136
+ columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
137
+ }
138
+ function getRowCount() {
139
+ if (!kernel) return 0;
140
+ return Math.ceil(kernel.getItemCount() / columnCount);
141
+ }
142
+ function buildVirtualItems() {
143
+ if (!kernel) return [];
144
+ const viewport = kernel.getViewport();
145
+ const scrollPosition = kernel.getScrollPosition();
146
+ const scrollTop = scrollPosition.scrollTop;
147
+ const effectiveRowHeight = rowHeight + gapY;
148
+ const startRow = Math.max(0, Math.floor(scrollTop / effectiveRowHeight));
149
+ const visibleRows = Math.ceil(viewport.height / effectiveRowHeight);
150
+ const overscan = kernel.getOptions().overscan;
151
+ const endRow = Math.min(getRowCount() - 1, startRow + visibleRows + overscan);
152
+ const overscanStartRow = Math.max(0, startRow - overscan);
153
+ const items = [];
154
+ const itemCount = kernel.getItemCount();
155
+ for (let row = overscanStartRow; row <= endRow; row++) {
156
+ for (let col = 0; col < columnCount; col++) {
157
+ const index = row * columnCount + col;
158
+ if (index >= itemCount) break;
159
+ const top = row * effectiveRowHeight;
160
+ items.push({
161
+ index,
162
+ key: getItemKey(index),
163
+ start: top,
164
+ end: top + rowHeight,
165
+ size: rowHeight,
166
+ lane: col
167
+ });
168
+ }
169
+ }
170
+ return items;
171
+ }
172
+ function recalculate() {
173
+ if (!kernel) return;
174
+ const viewport = kernel.getViewport();
175
+ calculateColumns(viewport.width);
176
+ virtualItems = buildVirtualItems();
177
+ }
178
+ const api = {
179
+ getVirtualItems() {
180
+ return virtualItems;
181
+ },
182
+ getTotalHeight() {
183
+ return getRowCount() * (rowHeight + gapY) - gapY;
184
+ },
185
+ getColumnCount() {
186
+ return columnCount;
187
+ },
188
+ getColumnWidth() {
189
+ return columnWidth;
190
+ },
191
+ getItemsPerRow() {
192
+ return columnCount;
193
+ },
194
+ getRowCount,
195
+ recalculate
196
+ };
197
+ return {
198
+ name: "grid-renderer",
199
+ version: "1.0.0",
200
+ type: "core",
201
+ install(k) {
202
+ kernel = k;
203
+ recalculate();
204
+ },
205
+ uninstall() {
206
+ virtualItems = [];
207
+ kernel = null;
208
+ },
209
+ hooks: {
210
+ onScroll: () => {
211
+ virtualItems = buildVirtualItems();
212
+ },
213
+ onResize: () => recalculate(),
214
+ onItemsChange: () => recalculate()
215
+ },
216
+ api
217
+ };
218
+ }
219
+
220
+ // src/utils/debounce.ts
221
+ function debounce(fn, wait) {
222
+ let timeoutId = null;
223
+ let lastArgs = null;
224
+ let lastThis = null;
225
+ const debounced = function(...args) {
226
+ lastArgs = args;
227
+ lastThis = this;
228
+ if (timeoutId) {
229
+ clearTimeout(timeoutId);
230
+ }
231
+ timeoutId = setTimeout(() => {
232
+ timeoutId = null;
233
+ if (lastArgs) {
234
+ fn.apply(lastThis, lastArgs);
235
+ lastArgs = null;
236
+ lastThis = null;
237
+ }
238
+ }, wait);
239
+ };
240
+ debounced.cancel = () => {
241
+ if (timeoutId) {
242
+ clearTimeout(timeoutId);
243
+ timeoutId = null;
244
+ }
245
+ lastArgs = null;
246
+ lastThis = null;
247
+ };
248
+ debounced.flush = () => {
249
+ if (timeoutId) {
250
+ clearTimeout(timeoutId);
251
+ timeoutId = null;
252
+ if (lastArgs) {
253
+ fn.apply(lastThis, lastArgs);
254
+ lastArgs = null;
255
+ lastThis = null;
256
+ }
257
+ }
258
+ };
259
+ return debounced;
260
+ }
261
+
262
+ // src/plugins/core/auto-measurer.ts
263
+ function autoMeasurerPlugin(options = {}) {
264
+ const {
265
+ estimatedItemHeight = 50,
266
+ measureOnResize = true,
267
+ debounceMs = 100
268
+ } = options;
269
+ let kernel = null;
270
+ let resizeObserver = null;
271
+ const observedElements = /* @__PURE__ */ new WeakMap();
272
+ const elementsByIndex = /* @__PURE__ */ new Map();
273
+ let measureQueue = /* @__PURE__ */ new Set();
274
+ let flushScheduled = false;
275
+ const debouncedFlush = debounce(() => {
276
+ flush();
277
+ }, debounceMs);
278
+ function flush() {
279
+ if (!kernel || measureQueue.size === 0) {
280
+ flushScheduled = false;
281
+ return;
282
+ }
283
+ const indicesToMeasure = Array.from(measureQueue);
284
+ measureQueue = /* @__PURE__ */ new Set();
285
+ flushScheduled = false;
286
+ const measurements = [];
287
+ for (const index of indicesToMeasure) {
288
+ const ref = elementsByIndex.get(index);
289
+ const element = ref?.deref();
290
+ if (element) {
291
+ const rect = element.getBoundingClientRect();
292
+ measurements.push({ index, height: rect.height });
293
+ }
294
+ }
295
+ for (const { index, height } of measurements) {
296
+ if (height > 0) {
297
+ kernel.measureItem(index, height);
298
+ }
299
+ }
300
+ }
301
+ function scheduleFlush() {
302
+ if (!flushScheduled) {
303
+ flushScheduled = true;
304
+ requestAnimationFrame(() => {
305
+ flush();
306
+ });
307
+ }
308
+ }
309
+ function handleResize(entries) {
310
+ for (const entry of entries) {
311
+ const index = observedElements.get(entry.target);
312
+ if (index !== void 0) {
313
+ measureQueue.add(index);
314
+ }
315
+ }
316
+ if (measureOnResize && measureQueue.size > 0) {
317
+ debouncedFlush();
318
+ }
319
+ }
320
+ const api = {
321
+ measureElement(index, element) {
322
+ const rect = element.getBoundingClientRect();
323
+ const height = rect.height;
324
+ if (height > 0 && kernel) {
325
+ kernel.measureItem(index, height);
326
+ }
327
+ return height;
328
+ },
329
+ getCachedSize(index) {
330
+ return kernel?.getCachedHeight(index);
331
+ },
332
+ invalidate(index) {
333
+ kernel?.invalidateMeasurement(index);
334
+ },
335
+ invalidateAll() {
336
+ kernel?.invalidateAllMeasurements();
337
+ },
338
+ getMeasurementCache() {
339
+ const cache = /* @__PURE__ */ new Map();
340
+ if (!kernel) return cache;
341
+ const itemCount = kernel.getItemCount();
342
+ for (let i = 0; i < itemCount; i++) {
343
+ const height = kernel.getCachedHeight(i);
344
+ if (height !== void 0) {
345
+ cache.set(i, height);
346
+ }
347
+ }
348
+ return cache;
349
+ },
350
+ observe(index, element) {
351
+ if (!resizeObserver) return;
352
+ const existingRef = elementsByIndex.get(index);
353
+ const existingElement = existingRef?.deref();
354
+ if (existingElement && existingElement !== element) {
355
+ resizeObserver.unobserve(existingElement);
356
+ observedElements.delete(existingElement);
357
+ }
358
+ observedElements.set(element, index);
359
+ elementsByIndex.set(index, new WeakRef(element));
360
+ resizeObserver.observe(element);
361
+ measureQueue.add(index);
362
+ scheduleFlush();
363
+ },
364
+ unobserve(element) {
365
+ if (!resizeObserver) return;
366
+ const index = observedElements.get(element);
367
+ if (index !== void 0) {
368
+ resizeObserver.unobserve(element);
369
+ observedElements.delete(element);
370
+ elementsByIndex.delete(index);
371
+ measureQueue.delete(index);
372
+ }
373
+ }
374
+ };
375
+ return {
376
+ name: "auto-measurer",
377
+ version: "1.0.0",
378
+ type: "core",
379
+ install(k) {
380
+ kernel = k;
381
+ kernel.configure({ estimatedItemHeight });
382
+ if (measureOnResize && typeof ResizeObserver !== "undefined") {
383
+ resizeObserver = new ResizeObserver(handleResize);
384
+ }
385
+ },
386
+ uninstall() {
387
+ if (resizeObserver) {
388
+ resizeObserver.disconnect();
389
+ resizeObserver = null;
390
+ }
391
+ debouncedFlush.cancel();
392
+ measureQueue.clear();
393
+ elementsByIndex.clear();
394
+ flushScheduled = false;
395
+ kernel = null;
396
+ },
397
+ api
398
+ };
399
+ }
400
+
401
+ // src/plugins/core/infinite-loader.ts
402
+ function infiniteLoaderPlugin(options) {
403
+ const {
404
+ threshold = 200,
405
+ thresholdItems = 5,
406
+ direction = "forward",
407
+ onLoadMore
408
+ } = options;
409
+ let kernel = null;
410
+ let loading = false;
411
+ let hasMoreForward = true;
412
+ let hasMoreBackward = direction === "backward" || direction === "both";
413
+ let lastCheckTime = 0;
414
+ const checkDebounceMs = 100;
415
+ function checkLoadMore(event) {
416
+ if (loading || !kernel) return;
417
+ const now = performance.now();
418
+ if (now - lastCheckTime < checkDebounceMs) return;
419
+ lastCheckTime = now;
420
+ const viewport = kernel.getViewport();
421
+ const totalSize = kernel.getTotalSize();
422
+ const { scrollTop } = event;
423
+ if ((direction === "forward" || direction === "both") && hasMoreForward) {
424
+ const distanceFromBottom = totalSize - scrollTop - viewport.height;
425
+ if (distanceFromBottom < threshold) {
426
+ triggerLoad("forward");
427
+ return;
428
+ }
429
+ }
430
+ if ((direction === "backward" || direction === "both") && hasMoreBackward) {
431
+ if (scrollTop < threshold) {
432
+ triggerLoad("backward");
433
+ return;
434
+ }
435
+ }
436
+ }
437
+ function checkLoadMoreByRange(range) {
438
+ if (loading || !kernel) return;
439
+ const itemCount = kernel.getItemCount();
440
+ if ((direction === "forward" || direction === "both") && hasMoreForward) {
441
+ const itemsFromEnd = itemCount - 1 - range.endIndex;
442
+ if (itemsFromEnd <= thresholdItems) {
443
+ triggerLoad("forward");
444
+ return;
445
+ }
446
+ }
447
+ if ((direction === "backward" || direction === "both") && hasMoreBackward) {
448
+ if (range.startIndex <= thresholdItems) {
449
+ triggerLoad("backward");
450
+ return;
451
+ }
452
+ }
453
+ }
454
+ async function triggerLoad(loadDirection) {
455
+ if (loading || !kernel) return;
456
+ if (loadDirection === "forward" && !hasMoreForward) return;
457
+ if (loadDirection === "backward" && !hasMoreBackward) return;
458
+ loading = true;
459
+ kernel.emit({
460
+ type: "load-more",
461
+ direction: loadDirection
462
+ });
463
+ try {
464
+ await onLoadMore(loadDirection);
465
+ } catch (error) {
466
+ console.error("[Scrollex] Error in onLoadMore:", error);
467
+ } finally {
468
+ loading = false;
469
+ }
470
+ }
471
+ const api = {
472
+ isLoading() {
473
+ return loading;
474
+ },
475
+ setLoading(l) {
476
+ loading = l;
477
+ },
478
+ hasMore() {
479
+ return hasMoreForward || hasMoreBackward;
480
+ },
481
+ setHasMore(hasMore) {
482
+ hasMoreForward = hasMore;
483
+ hasMoreBackward = hasMore;
484
+ },
485
+ hasMoreForward() {
486
+ return hasMoreForward;
487
+ },
488
+ setHasMoreForward(hasMore) {
489
+ hasMoreForward = hasMore;
490
+ },
491
+ hasMoreBackward() {
492
+ return hasMoreBackward;
493
+ },
494
+ setHasMoreBackward(hasMore) {
495
+ hasMoreBackward = hasMore;
496
+ },
497
+ reset() {
498
+ loading = false;
499
+ hasMoreForward = true;
500
+ hasMoreBackward = direction === "backward" || direction === "both";
501
+ },
502
+ triggerLoad(loadDirection) {
503
+ triggerLoad(loadDirection);
504
+ }
505
+ };
506
+ return {
507
+ name: "infinite-loader",
508
+ version: "1.0.0",
509
+ type: "core",
510
+ install(k) {
511
+ kernel = k;
512
+ },
513
+ uninstall() {
514
+ kernel = null;
515
+ loading = false;
516
+ },
517
+ hooks: {
518
+ onScroll: checkLoadMore,
519
+ onVisibleRangeChange: (range) => checkLoadMoreByRange(range)
520
+ },
521
+ api
522
+ };
523
+ }
524
+
525
+ // src/utils/raf.ts
526
+ function raf(callback) {
527
+ if (typeof requestAnimationFrame !== "undefined") {
528
+ return requestAnimationFrame(callback);
529
+ }
530
+ return setTimeout(() => callback(Date.now()), 16);
531
+ }
532
+ function cancelRaf(id) {
533
+ if (typeof cancelAnimationFrame !== "undefined") {
534
+ cancelAnimationFrame(id);
535
+ } else {
536
+ clearTimeout(id);
537
+ }
538
+ }
539
+
540
+ // src/plugins/core/scroll-controller.ts
541
+ var easings = {
542
+ easeInOut: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t};
543
+ function scrollControllerPlugin(options = {}) {
544
+ const {
545
+ defaultDuration = 300,
546
+ defaultEasing = easings.easeInOut
547
+ } = options;
548
+ let kernel = null;
549
+ let animationId = null;
550
+ let animating = false;
551
+ function animateScroll(targetOffset, duration, easing) {
552
+ if (!kernel) return;
553
+ stopAnimation();
554
+ const startTime = performance.now();
555
+ const startOffset = kernel.getScrollPosition().scrollTop;
556
+ const distance = targetOffset - startOffset;
557
+ if (distance === 0 || duration === 0) {
558
+ kernel.setScrollPosition({ scrollTop: targetOffset, scrollLeft: 0 });
559
+ return;
560
+ }
561
+ animating = true;
562
+ function animate(currentTime) {
563
+ if (!kernel) {
564
+ animating = false;
565
+ return;
566
+ }
567
+ const elapsed = currentTime - startTime;
568
+ const progress = Math.min(elapsed / duration, 1);
569
+ const easedProgress = easing(progress);
570
+ const currentOffset = startOffset + distance * easedProgress;
571
+ kernel.setScrollPosition({ scrollTop: currentOffset, scrollLeft: 0 });
572
+ if (progress < 1) {
573
+ animationId = raf(animate);
574
+ } else {
575
+ animationId = null;
576
+ animating = false;
577
+ }
578
+ }
579
+ animationId = raf(animate);
580
+ }
581
+ function stopAnimation() {
582
+ if (animationId !== null) {
583
+ cancelRaf(animationId);
584
+ animationId = null;
585
+ }
586
+ animating = false;
587
+ }
588
+ const api = {
589
+ scrollTo(offset, options2 = {}) {
590
+ if (!kernel) return;
591
+ const {
592
+ behavior = "auto",
593
+ duration = defaultDuration,
594
+ easing = defaultEasing
595
+ } = options2;
596
+ if (behavior === "auto") {
597
+ stopAnimation();
598
+ kernel.setScrollPosition({ scrollTop: offset, scrollLeft: 0 });
599
+ } else {
600
+ animateScroll(offset, duration, easing);
601
+ }
602
+ },
603
+ scrollToIndex(index, options2 = {}) {
604
+ if (!kernel) return;
605
+ const {
606
+ align = "auto",
607
+ offset: additionalOffset = 0,
608
+ ...scrollOptions
609
+ } = options2;
610
+ const itemOffset = kernel.getItemOffset(index);
611
+ const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
612
+ const viewport = kernel.getViewport();
613
+ let targetOffset;
614
+ switch (align) {
615
+ case "start":
616
+ targetOffset = itemOffset;
617
+ break;
618
+ case "center":
619
+ targetOffset = itemOffset - viewport.height / 2 + itemSize / 2;
620
+ break;
621
+ case "end":
622
+ targetOffset = itemOffset - viewport.height + itemSize;
623
+ break;
624
+ case "auto":
625
+ default: {
626
+ const scrollPosition = kernel.getScrollPosition();
627
+ const visibleStart = scrollPosition.scrollTop;
628
+ const visibleEnd = visibleStart + viewport.height;
629
+ const itemEnd = itemOffset + itemSize;
630
+ if (itemOffset < visibleStart) {
631
+ targetOffset = itemOffset;
632
+ } else if (itemEnd > visibleEnd) {
633
+ targetOffset = itemEnd - viewport.height;
634
+ } else {
635
+ return;
636
+ }
637
+ break;
638
+ }
639
+ }
640
+ const maxScroll = Math.max(0, kernel.getTotalSize() - viewport.height);
641
+ targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
642
+ api.scrollTo(targetOffset, scrollOptions);
643
+ },
644
+ scrollToTop(options2 = {}) {
645
+ api.scrollTo(0, options2);
646
+ },
647
+ scrollToBottom(options2 = {}) {
648
+ if (!kernel) return;
649
+ const viewport = kernel.getViewport();
650
+ const totalSize = kernel.getTotalSize();
651
+ const maxScroll = Math.max(0, totalSize - viewport.height);
652
+ api.scrollTo(maxScroll, options2);
653
+ },
654
+ getScrollPosition() {
655
+ return kernel?.getScrollPosition() ?? { scrollTop: 0, scrollLeft: 0 };
656
+ },
657
+ isScrolling() {
658
+ return kernel?.isScrolling() ?? false;
659
+ },
660
+ isAnimating() {
661
+ return animating;
662
+ },
663
+ stopAnimation
664
+ };
665
+ return {
666
+ name: "scroll-controller",
667
+ version: "1.0.0",
668
+ type: "core",
669
+ install(k) {
670
+ kernel = k;
671
+ },
672
+ uninstall() {
673
+ stopAnimation();
674
+ kernel = null;
675
+ },
676
+ api
677
+ };
678
+ }
679
+
680
+ // src/plugins/optional/masonry-renderer.ts
681
+ function masonryRenderer(options) {
682
+ const {
683
+ columns,
684
+ minColumnWidth = 200,
685
+ gap = 0,
686
+ getItemHeight,
687
+ getItemKey = (index) => index
688
+ } = options;
689
+ let kernel = null;
690
+ let virtualItems = [];
691
+ let columnCount = typeof columns === "number" ? columns : 1;
692
+ let columnWidth = 0;
693
+ let columnHeights = [];
694
+ let totalHeight = 0;
695
+ const layoutCache = /* @__PURE__ */ new Map();
696
+ function calculateColumns(containerWidth) {
697
+ if (typeof columns === "number") {
698
+ columnCount = columns;
699
+ } else {
700
+ columnCount = Math.max(1, Math.floor((containerWidth + gap) / (minColumnWidth + gap)));
701
+ }
702
+ columnWidth = (containerWidth - (columnCount - 1) * gap) / columnCount;
703
+ }
704
+ function calculateLayout() {
705
+ if (!kernel) return;
706
+ const itemCount = kernel.getItemCount();
707
+ columnHeights = new Array(columnCount).fill(0);
708
+ layoutCache.clear();
709
+ for (let i = 0; i < itemCount; i++) {
710
+ let shortestCol = 0;
711
+ for (let j = 1; j < columnCount; j++) {
712
+ if ((columnHeights[j] ?? 0) < (columnHeights[shortestCol] ?? 0)) {
713
+ shortestCol = j;
714
+ }
715
+ }
716
+ const itemHeight = getItemHeight(i, columnWidth);
717
+ const top = columnHeights[shortestCol] ?? 0;
718
+ layoutCache.set(i, { top, lane: shortestCol, height: itemHeight });
719
+ columnHeights[shortestCol] = top + itemHeight + gap;
720
+ }
721
+ totalHeight = Math.max(...columnHeights, 0);
722
+ if (totalHeight > 0) {
723
+ totalHeight -= gap;
724
+ }
725
+ }
726
+ function buildVirtualItems() {
727
+ if (!kernel) return [];
728
+ const viewport = kernel.getViewport();
729
+ const scrollPosition = kernel.getScrollPosition();
730
+ const scrollTop = scrollPosition.scrollTop;
731
+ const viewportHeight = viewport.height;
732
+ const overscan = kernel.getOptions().overscan;
733
+ const overscanPixels = overscan * 100;
734
+ const viewStart = Math.max(0, scrollTop - overscanPixels);
735
+ const viewEnd = scrollTop + viewportHeight + overscanPixels;
736
+ const items = [];
737
+ const itemCount = kernel.getItemCount();
738
+ for (let i = 0; i < itemCount; i++) {
739
+ const layout = layoutCache.get(i);
740
+ if (!layout) continue;
741
+ const itemEnd = layout.top + layout.height;
742
+ if (itemEnd >= viewStart && layout.top <= viewEnd) {
743
+ items.push({
744
+ index: i,
745
+ key: getItemKey(i),
746
+ start: layout.top,
747
+ end: itemEnd,
748
+ size: layout.height,
749
+ lane: layout.lane
750
+ });
751
+ }
752
+ }
753
+ return items;
754
+ }
755
+ function recalculate() {
756
+ if (!kernel) return;
757
+ const viewport = kernel.getViewport();
758
+ calculateColumns(viewport.width);
759
+ calculateLayout();
760
+ virtualItems = buildVirtualItems();
761
+ }
762
+ const api = {
763
+ getVirtualItems() {
764
+ return virtualItems;
765
+ },
766
+ getTotalHeight() {
767
+ return totalHeight;
768
+ },
769
+ getColumnHeights() {
770
+ return [...columnHeights];
771
+ },
772
+ getShortestColumn() {
773
+ if (columnHeights.length === 0) return 0;
774
+ let shortest = 0;
775
+ for (let i = 1; i < columnHeights.length; i++) {
776
+ if ((columnHeights[i] ?? 0) < (columnHeights[shortest] ?? 0)) {
777
+ shortest = i;
778
+ }
779
+ }
780
+ return shortest;
781
+ },
782
+ getLongestColumn() {
783
+ if (columnHeights.length === 0) return 0;
784
+ let longest = 0;
785
+ for (let i = 1; i < columnHeights.length; i++) {
786
+ if ((columnHeights[i] ?? 0) > (columnHeights[longest] ?? 0)) {
787
+ longest = i;
788
+ }
789
+ }
790
+ return longest;
791
+ },
792
+ getColumnCount() {
793
+ return columnCount;
794
+ },
795
+ getColumnWidth() {
796
+ return columnWidth;
797
+ },
798
+ recalculate
799
+ };
800
+ return {
801
+ name: "masonry-renderer",
802
+ version: "1.0.0",
803
+ type: "optional",
804
+ install(k) {
805
+ kernel = k;
806
+ recalculate();
807
+ },
808
+ uninstall() {
809
+ virtualItems = [];
810
+ layoutCache.clear();
811
+ kernel = null;
812
+ },
813
+ hooks: {
814
+ onScroll: () => {
815
+ virtualItems = buildVirtualItems();
816
+ },
817
+ onResize: () => recalculate(),
818
+ onItemsChange: () => recalculate()
819
+ },
820
+ api
821
+ };
822
+ }
823
+
824
+ // src/plugins/optional/sticky-headers.ts
825
+ function stickyHeaders(options) {
826
+ const {
827
+ headerHeight,
828
+ isHeader,
829
+ getGroupId,
830
+ zIndex = 10,
831
+ stickyOffset = 0
832
+ } = options;
833
+ let kernel = null;
834
+ let currentStickyHeader = null;
835
+ let headerIndices = [];
836
+ let groupIdMap = /* @__PURE__ */ new Map();
837
+ function buildHeaderCache() {
838
+ if (!kernel) return;
839
+ headerIndices = [];
840
+ groupIdMap = /* @__PURE__ */ new Map();
841
+ const itemCount = kernel.getItemCount();
842
+ for (let i = 0; i < itemCount; i++) {
843
+ const groupId = getGroupId(i);
844
+ if (isHeader(i)) {
845
+ headerIndices.push(i);
846
+ }
847
+ const groupItems = groupIdMap.get(groupId);
848
+ if (groupItems) {
849
+ groupItems.push(i);
850
+ } else {
851
+ groupIdMap.set(groupId, [i]);
852
+ }
853
+ }
854
+ }
855
+ function updateStickyHeader(event) {
856
+ if (!kernel || headerIndices.length === 0) {
857
+ currentStickyHeader = null;
858
+ return;
859
+ }
860
+ const scrollTop = event.scrollTop + stickyOffset;
861
+ let newStickyHeader = null;
862
+ for (const headerIndex of headerIndices) {
863
+ const headerOffset = kernel.getItemOffset(headerIndex);
864
+ if (headerOffset <= scrollTop) {
865
+ newStickyHeader = headerIndex;
866
+ } else {
867
+ break;
868
+ }
869
+ }
870
+ currentStickyHeader = newStickyHeader;
871
+ }
872
+ function findHeaderForGroup(groupId) {
873
+ const items = groupIdMap.get(groupId);
874
+ if (!items || items.length === 0) return null;
875
+ const firstItem = items[0];
876
+ if (firstItem !== void 0 && isHeader(firstItem)) {
877
+ return firstItem;
878
+ }
879
+ return null;
880
+ }
881
+ const api = {
882
+ getCurrentStickyHeader() {
883
+ return currentStickyHeader;
884
+ },
885
+ getGroupForIndex(index) {
886
+ return getGroupId(index);
887
+ },
888
+ scrollToGroup(groupId) {
889
+ if (!kernel) return;
890
+ const headerIndex = findHeaderForGroup(groupId);
891
+ if (headerIndex !== null) {
892
+ kernel.scrollToIndex(headerIndex, { align: "start" });
893
+ }
894
+ },
895
+ getHeaderIndices() {
896
+ return [...headerIndices];
897
+ },
898
+ getGroupIds() {
899
+ return Array.from(groupIdMap.keys());
900
+ },
901
+ isHeader(index) {
902
+ return isHeader(index);
903
+ }
904
+ };
905
+ return {
906
+ name: "sticky-headers",
907
+ version: "1.0.0",
908
+ type: "optional",
909
+ install(k) {
910
+ kernel = k;
911
+ buildHeaderCache();
912
+ },
913
+ uninstall() {
914
+ headerIndices = [];
915
+ groupIdMap.clear();
916
+ currentStickyHeader = null;
917
+ kernel = null;
918
+ },
919
+ hooks: {
920
+ onScroll: updateStickyHeader,
921
+ onItemsChange: () => buildHeaderCache()
922
+ },
923
+ api
924
+ };
925
+ }
926
+
927
+ // src/plugins/optional/keyboard-nav.ts
928
+ function keyboardNav(options = {}) {
929
+ const {
930
+ enabled: initialEnabled = true,
931
+ loop = false,
932
+ orientation = "vertical",
933
+ pageSize = 10,
934
+ columns = 1,
935
+ typeahead = false,
936
+ typeaheadTimeout = 1e3,
937
+ getItemText = () => "",
938
+ onFocus,
939
+ onSelect,
940
+ onKeyDown
941
+ } = options;
942
+ let kernel = null;
943
+ let enabled = initialEnabled;
944
+ let focusedIndex = null;
945
+ let typeaheadBuffer = "";
946
+ let typeaheadTimer = null;
947
+ function getItemCount() {
948
+ return kernel?.getItemCount() ?? 0;
949
+ }
950
+ function clampIndex(index) {
951
+ const count = getItemCount();
952
+ if (count === 0) return 0;
953
+ return Math.max(0, Math.min(index, count - 1));
954
+ }
955
+ function setFocus(index) {
956
+ if (index === null) {
957
+ focusedIndex = null;
958
+ return;
959
+ }
960
+ const count = getItemCount();
961
+ if (count === 0) {
962
+ focusedIndex = null;
963
+ return;
964
+ }
965
+ let newIndex = index;
966
+ if (loop) {
967
+ if (newIndex < 0) {
968
+ newIndex = count - 1;
969
+ } else if (newIndex >= count) {
970
+ newIndex = 0;
971
+ }
972
+ } else {
973
+ newIndex = clampIndex(newIndex);
974
+ }
975
+ if (newIndex !== focusedIndex) {
976
+ focusedIndex = newIndex;
977
+ onFocus?.(newIndex);
978
+ kernel?.scrollToIndex(newIndex, { align: "auto" });
979
+ }
980
+ }
981
+ function handleKeyDown(event) {
982
+ if (!enabled || !kernel) return;
983
+ if (onKeyDown) {
984
+ const handled = onKeyDown(event, focusedIndex);
985
+ if (handled === true) {
986
+ event.preventDefault();
987
+ return;
988
+ }
989
+ }
990
+ const count = getItemCount();
991
+ if (count === 0) return;
992
+ if (focusedIndex === null && count > 0) {
993
+ setFocus(0);
994
+ }
995
+ switch (event.key) {
996
+ case "ArrowDown":
997
+ if (orientation === "vertical" || orientation === "grid") {
998
+ event.preventDefault();
999
+ if (orientation === "grid") {
1000
+ setFocus((focusedIndex ?? 0) + columns);
1001
+ } else {
1002
+ setFocus((focusedIndex ?? 0) + 1);
1003
+ }
1004
+ }
1005
+ break;
1006
+ case "ArrowUp":
1007
+ if (orientation === "vertical" || orientation === "grid") {
1008
+ event.preventDefault();
1009
+ if (orientation === "grid") {
1010
+ setFocus((focusedIndex ?? 0) - columns);
1011
+ } else {
1012
+ setFocus((focusedIndex ?? 0) - 1);
1013
+ }
1014
+ }
1015
+ break;
1016
+ case "ArrowRight":
1017
+ if (orientation === "horizontal" || orientation === "grid") {
1018
+ event.preventDefault();
1019
+ setFocus((focusedIndex ?? 0) + 1);
1020
+ }
1021
+ break;
1022
+ case "ArrowLeft":
1023
+ if (orientation === "horizontal" || orientation === "grid") {
1024
+ event.preventDefault();
1025
+ setFocus((focusedIndex ?? 0) - 1);
1026
+ }
1027
+ break;
1028
+ case "Home":
1029
+ event.preventDefault();
1030
+ setFocus(0);
1031
+ break;
1032
+ case "End":
1033
+ event.preventDefault();
1034
+ setFocus(count - 1);
1035
+ break;
1036
+ case "PageUp":
1037
+ event.preventDefault();
1038
+ setFocus((focusedIndex ?? 0) - pageSize);
1039
+ break;
1040
+ case "PageDown":
1041
+ event.preventDefault();
1042
+ setFocus((focusedIndex ?? 0) + pageSize);
1043
+ break;
1044
+ case "Enter":
1045
+ case " ":
1046
+ event.preventDefault();
1047
+ if (focusedIndex !== null) {
1048
+ onSelect?.(focusedIndex);
1049
+ }
1050
+ break;
1051
+ default:
1052
+ if (typeahead && event.key.length === 1 && !event.ctrlKey && !event.metaKey) {
1053
+ event.preventDefault();
1054
+ handleTypeahead(event.key);
1055
+ }
1056
+ break;
1057
+ }
1058
+ }
1059
+ function handleTypeahead(char) {
1060
+ if (!kernel) return;
1061
+ if (typeaheadTimer) {
1062
+ clearTimeout(typeaheadTimer);
1063
+ }
1064
+ typeaheadBuffer += char.toLowerCase();
1065
+ const count = getItemCount();
1066
+ const startIndex = focusedIndex !== null ? focusedIndex + 1 : 0;
1067
+ for (let i = 0; i < count; i++) {
1068
+ const index = (startIndex + i) % count;
1069
+ const text = getItemText(index).toLowerCase();
1070
+ if (text.startsWith(typeaheadBuffer)) {
1071
+ setFocus(index);
1072
+ break;
1073
+ }
1074
+ }
1075
+ typeaheadTimer = setTimeout(() => {
1076
+ typeaheadBuffer = "";
1077
+ typeaheadTimer = null;
1078
+ }, typeaheadTimeout);
1079
+ }
1080
+ const api = {
1081
+ getFocusedIndex() {
1082
+ return focusedIndex;
1083
+ },
1084
+ setFocusedIndex(index) {
1085
+ setFocus(index);
1086
+ },
1087
+ focusNext() {
1088
+ if (orientation === "grid") {
1089
+ setFocus((focusedIndex ?? -1) + columns);
1090
+ } else {
1091
+ setFocus((focusedIndex ?? -1) + 1);
1092
+ }
1093
+ },
1094
+ focusPrevious() {
1095
+ if (orientation === "grid") {
1096
+ setFocus((focusedIndex ?? getItemCount()) - columns);
1097
+ } else {
1098
+ setFocus((focusedIndex ?? getItemCount()) - 1);
1099
+ }
1100
+ },
1101
+ focusFirst() {
1102
+ setFocus(0);
1103
+ },
1104
+ focusLast() {
1105
+ setFocus(getItemCount() - 1);
1106
+ },
1107
+ selectFocused() {
1108
+ if (focusedIndex !== null) {
1109
+ onSelect?.(focusedIndex);
1110
+ }
1111
+ },
1112
+ enable() {
1113
+ enabled = true;
1114
+ },
1115
+ disable() {
1116
+ enabled = false;
1117
+ },
1118
+ isEnabled() {
1119
+ return enabled;
1120
+ },
1121
+ clearTypeahead() {
1122
+ typeaheadBuffer = "";
1123
+ if (typeaheadTimer) {
1124
+ clearTimeout(typeaheadTimer);
1125
+ typeaheadTimer = null;
1126
+ }
1127
+ }
1128
+ };
1129
+ return {
1130
+ name: "keyboard-nav",
1131
+ version: "1.0.0",
1132
+ type: "optional",
1133
+ install(k) {
1134
+ kernel = k;
1135
+ document.addEventListener("keydown", handleKeyDown);
1136
+ },
1137
+ uninstall() {
1138
+ document.removeEventListener("keydown", handleKeyDown);
1139
+ if (typeaheadTimer) {
1140
+ clearTimeout(typeaheadTimer);
1141
+ }
1142
+ focusedIndex = null;
1143
+ typeaheadBuffer = "";
1144
+ kernel = null;
1145
+ },
1146
+ hooks: {
1147
+ onItemsChange: () => {
1148
+ if (focusedIndex !== null) {
1149
+ const count = getItemCount();
1150
+ if (focusedIndex >= count) {
1151
+ setFocus(count > 0 ? count - 1 : null);
1152
+ }
1153
+ }
1154
+ }
1155
+ },
1156
+ api
1157
+ };
1158
+ }
1159
+
1160
+ // src/plugins/optional/scroll-restoration.ts
1161
+ function scrollRestoration(options) {
1162
+ const {
1163
+ key,
1164
+ storage = "sessionStorage",
1165
+ restoreOnMount = true,
1166
+ saveOnUnmount = true,
1167
+ saveOnScroll = false,
1168
+ debounceMs = 500,
1169
+ includeIndex = false,
1170
+ maxAge = 0
1171
+ } = options;
1172
+ let kernel = null;
1173
+ let debouncedSave = null;
1174
+ function getStorage() {
1175
+ if (typeof storage === "string") {
1176
+ if (storage === "sessionStorage") {
1177
+ return sessionStorage;
1178
+ }
1179
+ return localStorage;
1180
+ }
1181
+ return storage;
1182
+ }
1183
+ function getStorageKey() {
1184
+ return `scrollex:${key}`;
1185
+ }
1186
+ function save() {
1187
+ if (!kernel) return;
1188
+ const scrollPosition = kernel.getScrollPosition();
1189
+ const state = {
1190
+ scrollTop: scrollPosition.scrollTop,
1191
+ scrollLeft: scrollPosition.scrollLeft,
1192
+ timestamp: Date.now()
1193
+ };
1194
+ if (includeIndex) {
1195
+ const keyboardNav2 = kernel.getPlugin("keyboard-nav");
1196
+ if (keyboardNav2?.api?.getFocusedIndex) {
1197
+ const focusedIndex = keyboardNav2.api.getFocusedIndex();
1198
+ if (focusedIndex !== null) {
1199
+ state.focusedIndex = focusedIndex;
1200
+ }
1201
+ }
1202
+ }
1203
+ try {
1204
+ getStorage().setItem(getStorageKey(), JSON.stringify(state));
1205
+ } catch (error) {
1206
+ console.warn("[Scrollex] Failed to save scroll position:", error);
1207
+ }
1208
+ }
1209
+ function getSavedState() {
1210
+ try {
1211
+ const stored = getStorage().getItem(getStorageKey());
1212
+ if (!stored) return null;
1213
+ const state = JSON.parse(stored);
1214
+ if (maxAge > 0) {
1215
+ const age = Date.now() - state.timestamp;
1216
+ if (age > maxAge) {
1217
+ clear();
1218
+ return null;
1219
+ }
1220
+ }
1221
+ return state;
1222
+ } catch {
1223
+ return null;
1224
+ }
1225
+ }
1226
+ function restore() {
1227
+ if (!kernel) return false;
1228
+ const state = getSavedState();
1229
+ if (!state) return false;
1230
+ kernel.scrollTo(state.scrollTop);
1231
+ if (includeIndex && state.focusedIndex !== void 0) {
1232
+ const keyboardNav2 = kernel.getPlugin("keyboard-nav");
1233
+ if (keyboardNav2?.api?.setFocusedIndex) {
1234
+ keyboardNav2.api.setFocusedIndex(state.focusedIndex);
1235
+ }
1236
+ }
1237
+ return true;
1238
+ }
1239
+ function clear() {
1240
+ try {
1241
+ getStorage().removeItem(getStorageKey());
1242
+ } catch (error) {
1243
+ console.warn("[Scrollex] Failed to clear scroll position:", error);
1244
+ }
1245
+ }
1246
+ function handleScroll(_event) {
1247
+ if (saveOnScroll && debouncedSave) {
1248
+ debouncedSave();
1249
+ }
1250
+ }
1251
+ const api = {
1252
+ save,
1253
+ restore,
1254
+ clear,
1255
+ getSavedPosition() {
1256
+ return getSavedState();
1257
+ },
1258
+ hasSavedPosition() {
1259
+ return getSavedState() !== null;
1260
+ }
1261
+ };
1262
+ return {
1263
+ name: "scroll-restoration",
1264
+ version: "1.0.0",
1265
+ type: "optional",
1266
+ install(k) {
1267
+ kernel = k;
1268
+ if (saveOnScroll) {
1269
+ debouncedSave = debounce(save, debounceMs);
1270
+ }
1271
+ if (restoreOnMount) {
1272
+ requestAnimationFrame(() => {
1273
+ restore();
1274
+ });
1275
+ }
1276
+ },
1277
+ uninstall() {
1278
+ if (saveOnUnmount) {
1279
+ save();
1280
+ }
1281
+ if (debouncedSave) {
1282
+ debouncedSave.cancel();
1283
+ debouncedSave = null;
1284
+ }
1285
+ kernel = null;
1286
+ },
1287
+ hooks: {
1288
+ onScroll: handleScroll
1289
+ },
1290
+ api
1291
+ };
1292
+ }
1293
+
1294
+ // src/plugins/optional/bidirectional.ts
1295
+ function bidirectional(options = {}) {
1296
+ const {
1297
+ initialPosition = "bottom",
1298
+ stickToBottom: initialStickToBottom = true,
1299
+ stickThreshold = 50,
1300
+ maintainPositionOnPrepend = true,
1301
+ onLoadMore,
1302
+ loadMoreThreshold = 200
1303
+ } = options;
1304
+ let kernel = null;
1305
+ let stickToBottom = initialStickToBottom;
1306
+ let isSticking = initialStickToBottom;
1307
+ let previousTotalSize = 0;
1308
+ let isInitialized = false;
1309
+ function checkIsAtBottom() {
1310
+ if (!kernel) return false;
1311
+ const viewport = kernel.getViewport();
1312
+ const scrollPosition = kernel.getScrollPosition();
1313
+ const totalSize = kernel.getTotalSize();
1314
+ const maxScroll = totalSize - viewport.height;
1315
+ return scrollPosition.scrollTop >= maxScroll - stickThreshold;
1316
+ }
1317
+ function checkIsAtTop() {
1318
+ if (!kernel) return false;
1319
+ const scrollPosition = kernel.getScrollPosition();
1320
+ return scrollPosition.scrollTop <= stickThreshold;
1321
+ }
1322
+ function handleScroll(event) {
1323
+ if (!kernel) return;
1324
+ if (stickToBottom) {
1325
+ isSticking = checkIsAtBottom();
1326
+ }
1327
+ if (onLoadMore) {
1328
+ const viewport = kernel.getViewport();
1329
+ const totalSize = kernel.getTotalSize();
1330
+ if (event.scrollTop < loadMoreThreshold) {
1331
+ onLoadMore("backward");
1332
+ }
1333
+ const distanceFromBottom = totalSize - event.scrollTop - viewport.height;
1334
+ if (distanceFromBottom < loadMoreThreshold) {
1335
+ onLoadMore("forward");
1336
+ }
1337
+ }
1338
+ }
1339
+ function handleItemsChange(count, previousCount) {
1340
+ if (!kernel || !isInitialized) return;
1341
+ const newTotalSize = kernel.getTotalSize();
1342
+ const sizeDelta = newTotalSize - previousTotalSize;
1343
+ if (count > previousCount) {
1344
+ if (maintainPositionOnPrepend && sizeDelta > 0) {
1345
+ const scrollPosition = kernel.getScrollPosition();
1346
+ if (scrollPosition.scrollTop > stickThreshold) ;
1347
+ }
1348
+ if (isSticking && stickToBottom) {
1349
+ requestAnimationFrame(() => {
1350
+ kernel?.scrollTo(kernel?.getTotalSize() ?? 0);
1351
+ });
1352
+ }
1353
+ }
1354
+ previousTotalSize = newTotalSize;
1355
+ }
1356
+ const api = {
1357
+ isAtBottom() {
1358
+ return checkIsAtBottom();
1359
+ },
1360
+ isAtTop() {
1361
+ return checkIsAtTop();
1362
+ },
1363
+ scrollToBottom(smooth = false) {
1364
+ if (!kernel) return;
1365
+ const totalSize = kernel.getTotalSize();
1366
+ kernel.scrollTo(totalSize, { behavior: smooth ? "smooth" : "auto" });
1367
+ },
1368
+ scrollToTop(smooth = false) {
1369
+ if (!kernel) return;
1370
+ kernel.scrollTo(0, { behavior: smooth ? "smooth" : "auto" });
1371
+ },
1372
+ setStickToBottom(stick) {
1373
+ stickToBottom = stick;
1374
+ isSticking = stick && checkIsAtBottom();
1375
+ },
1376
+ isStickingToBottom() {
1377
+ return isSticking;
1378
+ },
1379
+ adjustForPrepend(prependedHeight) {
1380
+ if (!kernel || prependedHeight <= 0) return;
1381
+ const scrollPosition = kernel.getScrollPosition();
1382
+ kernel.setScrollPosition({
1383
+ scrollTop: scrollPosition.scrollTop + prependedHeight,
1384
+ scrollLeft: scrollPosition.scrollLeft
1385
+ });
1386
+ }
1387
+ };
1388
+ return {
1389
+ name: "bidirectional",
1390
+ version: "1.0.0",
1391
+ type: "optional",
1392
+ install(k) {
1393
+ kernel = k;
1394
+ k.getItemCount();
1395
+ previousTotalSize = k.getTotalSize();
1396
+ if (initialPosition === "bottom") {
1397
+ requestAnimationFrame(() => {
1398
+ if (kernel) {
1399
+ kernel.scrollTo(kernel.getTotalSize());
1400
+ isInitialized = true;
1401
+ }
1402
+ });
1403
+ } else {
1404
+ isInitialized = true;
1405
+ }
1406
+ },
1407
+ uninstall() {
1408
+ kernel = null;
1409
+ isInitialized = false;
1410
+ },
1411
+ hooks: {
1412
+ onScroll: handleScroll,
1413
+ onItemsChange: handleItemsChange
1414
+ },
1415
+ api
1416
+ };
1417
+ }
1418
+
1419
+ // src/plugins/optional/drag-to-reorder.ts
1420
+ function dragToReorder(options) {
1421
+ const {
1422
+ enabled: initialEnabled = true,
1423
+ axis = "vertical",
1424
+ handle = true,
1425
+ autoScroll = true,
1426
+ autoScrollSpeed = 10,
1427
+ dropIndicator = "line",
1428
+ onDragStart,
1429
+ onDragEnd,
1430
+ onReorder,
1431
+ canDrag = () => true,
1432
+ canDrop = () => true
1433
+ } = options;
1434
+ let kernel = null;
1435
+ let enabled = initialEnabled;
1436
+ let draggedIndex = null;
1437
+ let dropTargetIndex = null;
1438
+ let autoScrollTimer = null;
1439
+ function handleDragStart(event, index) {
1440
+ if (!enabled || !canDrag(index)) {
1441
+ event.preventDefault();
1442
+ return;
1443
+ }
1444
+ draggedIndex = index;
1445
+ dropTargetIndex = index;
1446
+ event.dataTransfer?.setData("text/plain", String(index));
1447
+ if (event.dataTransfer) {
1448
+ event.dataTransfer.effectAllowed = "move";
1449
+ }
1450
+ onDragStart?.(index);
1451
+ if (autoScroll) {
1452
+ startAutoScroll();
1453
+ }
1454
+ }
1455
+ function handleDragEnd(event) {
1456
+ const currentDraggedIndex = draggedIndex;
1457
+ const currentDropTargetIndex = dropTargetIndex;
1458
+ stopAutoScroll();
1459
+ if (currentDraggedIndex !== null && currentDropTargetIndex !== null && currentDraggedIndex !== currentDropTargetIndex) {
1460
+ if (canDrop(currentDraggedIndex, currentDropTargetIndex)) {
1461
+ onReorder(currentDraggedIndex, currentDropTargetIndex);
1462
+ }
1463
+ }
1464
+ if (currentDraggedIndex !== null) {
1465
+ onDragEnd?.(currentDraggedIndex);
1466
+ }
1467
+ draggedIndex = null;
1468
+ dropTargetIndex = null;
1469
+ }
1470
+ function handleDragOver(event, index) {
1471
+ if (draggedIndex === null) return;
1472
+ event.preventDefault();
1473
+ if (event.dataTransfer) {
1474
+ event.dataTransfer.dropEffect = canDrop(draggedIndex, index) ? "move" : "none";
1475
+ }
1476
+ const target = event.currentTarget;
1477
+ const rect = target.getBoundingClientRect();
1478
+ let newDropTarget;
1479
+ if (axis === "vertical") {
1480
+ const midY = rect.top + rect.height / 2;
1481
+ newDropTarget = event.clientY < midY ? index : index + 1;
1482
+ } else {
1483
+ const midX = rect.left + rect.width / 2;
1484
+ newDropTarget = event.clientX < midX ? index : index + 1;
1485
+ }
1486
+ if (newDropTarget > draggedIndex) {
1487
+ newDropTarget--;
1488
+ }
1489
+ dropTargetIndex = Math.max(0, Math.min(newDropTarget, (kernel?.getItemCount() ?? 1) - 1));
1490
+ }
1491
+ function handleDragEnter(event, _index) {
1492
+ event.preventDefault();
1493
+ }
1494
+ function handleDragLeave(_event, _index) {
1495
+ }
1496
+ function handleDrop(event, _index) {
1497
+ event.preventDefault();
1498
+ }
1499
+ function startAutoScroll() {
1500
+ if (autoScrollTimer) return;
1501
+ autoScrollTimer = setInterval(() => {
1502
+ if (!kernel || draggedIndex === null) {
1503
+ stopAutoScroll();
1504
+ return;
1505
+ }
1506
+ }, 16);
1507
+ }
1508
+ function stopAutoScroll() {
1509
+ if (autoScrollTimer) {
1510
+ clearInterval(autoScrollTimer);
1511
+ autoScrollTimer = null;
1512
+ }
1513
+ }
1514
+ function getDragHandleProps(index) {
1515
+ return {
1516
+ draggable: enabled && canDrag(index),
1517
+ onDragStart: (event) => handleDragStart(event, index),
1518
+ onDragEnd: (event) => handleDragEnd(),
1519
+ onDragOver: (event) => handleDragOver(event, index),
1520
+ onDragEnter: (event) => handleDragEnter(event),
1521
+ onDragLeave: (event) => handleDragLeave(),
1522
+ onDrop: (event) => handleDrop(event),
1523
+ "data-drag-index": String(index),
1524
+ "aria-grabbed": draggedIndex === index
1525
+ };
1526
+ }
1527
+ const api = {
1528
+ isDragging() {
1529
+ return draggedIndex !== null;
1530
+ },
1531
+ getDraggedIndex() {
1532
+ return draggedIndex;
1533
+ },
1534
+ getDropTargetIndex() {
1535
+ return dropTargetIndex;
1536
+ },
1537
+ cancelDrag() {
1538
+ stopAutoScroll();
1539
+ draggedIndex = null;
1540
+ dropTargetIndex = null;
1541
+ },
1542
+ enable() {
1543
+ enabled = true;
1544
+ },
1545
+ disable() {
1546
+ enabled = false;
1547
+ api.cancelDrag();
1548
+ },
1549
+ isEnabled() {
1550
+ return enabled;
1551
+ },
1552
+ getDragHandleProps
1553
+ };
1554
+ return {
1555
+ name: "drag-to-reorder",
1556
+ version: "1.0.0",
1557
+ type: "optional",
1558
+ install(k) {
1559
+ kernel = k;
1560
+ },
1561
+ uninstall() {
1562
+ stopAutoScroll();
1563
+ draggedIndex = null;
1564
+ dropTargetIndex = null;
1565
+ kernel = null;
1566
+ },
1567
+ api
1568
+ };
1569
+ }
1570
+
1571
+ // src/plugins/optional/debug-panel/debug-panel.ts
1572
+ function debugPanel(options = {}) {
1573
+ const resolvedOptions = {
1574
+ enabled: options.enabled ?? true,
1575
+ collapsed: options.collapsed ?? false,
1576
+ position: options.position ?? "bottom-right",
1577
+ theme: options.theme ?? "auto",
1578
+ showFps: options.showFps ?? true,
1579
+ showViewportOverlay: options.showViewportOverlay ?? false,
1580
+ showItemBoundaries: options.showItemBoundaries ?? false,
1581
+ logEvents: options.logEvents ?? false,
1582
+ maxEventLogEntries: options.maxEventLogEntries ?? 100,
1583
+ statsUpdateInterval: options.statsUpdateInterval ?? 100
1584
+ };
1585
+ let kernel = null;
1586
+ let eventIdCounter = 0;
1587
+ let statsUpdateTimer = null;
1588
+ let frameCount = 0;
1589
+ let lastFrameTime = performance.now();
1590
+ let lastFpsUpdate = performance.now();
1591
+ let currentFps = 60;
1592
+ let rafId = null;
1593
+ const emptyRange = {
1594
+ startIndex: 0,
1595
+ endIndex: 0,
1596
+ overscanStartIndex: 0,
1597
+ overscanEndIndex: 0,
1598
+ start: 0,
1599
+ end: 0
1600
+ };
1601
+ const emptyScrollPosition = {
1602
+ scrollTop: 0,
1603
+ scrollLeft: 0,
1604
+ offset: 0,
1605
+ percentage: 0
1606
+ };
1607
+ const state = {
1608
+ visible: resolvedOptions.enabled,
1609
+ collapsed: resolvedOptions.collapsed,
1610
+ activeTab: "stats",
1611
+ stats: createEmptyStats(),
1612
+ eventLog: [],
1613
+ showViewportOverlay: resolvedOptions.showViewportOverlay,
1614
+ showItemBoundaries: resolvedOptions.showItemBoundaries
1615
+ };
1616
+ const listeners = /* @__PURE__ */ new Set();
1617
+ function createEmptyStats() {
1618
+ return {
1619
+ fps: 60,
1620
+ frameTime: 16.67,
1621
+ memoryUsage: null,
1622
+ totalItems: 0,
1623
+ visibleItems: 0,
1624
+ renderedItems: 0,
1625
+ overscanItems: 0,
1626
+ avgItemHeight: 0,
1627
+ scrollPosition: { ...emptyScrollPosition },
1628
+ visibleRange: { ...emptyRange },
1629
+ renderRange: { ...emptyRange },
1630
+ scrollVelocity: 0,
1631
+ isScrolling: false
1632
+ };
1633
+ }
1634
+ function notifyListeners() {
1635
+ listeners.forEach((listener) => listener());
1636
+ }
1637
+ function updateFps() {
1638
+ const now = performance.now();
1639
+ frameCount++;
1640
+ if (now - lastFpsUpdate >= 1e3) {
1641
+ currentFps = Math.round(frameCount * 1e3 / (now - lastFpsUpdate));
1642
+ frameCount = 0;
1643
+ lastFpsUpdate = now;
1644
+ }
1645
+ state.stats.fps = currentFps;
1646
+ state.stats.frameTime = now - lastFrameTime;
1647
+ lastFrameTime = now;
1648
+ rafId = requestAnimationFrame(updateFps);
1649
+ }
1650
+ function updateStats() {
1651
+ if (!kernel) return;
1652
+ const scrollPos = kernel.getScrollPosition();
1653
+ const visibleRange = kernel.getVisibleRange();
1654
+ const renderRange = kernel.getRenderRange();
1655
+ const itemCount = kernel.getItemCount();
1656
+ let memoryUsage = null;
1657
+ if ("memory" in performance) {
1658
+ const memory = performance.memory;
1659
+ if (memory) {
1660
+ memoryUsage = Math.round(memory.usedJSHeapSize / 1024 / 1024 * 100) / 100;
1661
+ }
1662
+ }
1663
+ const visibleItems = (visibleRange.end ?? visibleRange.endIndex) - (visibleRange.start ?? visibleRange.startIndex);
1664
+ const renderedItems = (renderRange.end ?? renderRange.endIndex) - (renderRange.start ?? renderRange.startIndex);
1665
+ state.stats = {
1666
+ ...state.stats,
1667
+ memoryUsage,
1668
+ totalItems: itemCount,
1669
+ visibleItems,
1670
+ renderedItems,
1671
+ overscanItems: renderedItems - visibleItems,
1672
+ avgItemHeight: itemCount > 0 ? kernel.getTotalSize() / itemCount : 0,
1673
+ scrollPosition: {
1674
+ ...scrollPos,
1675
+ offset: scrollPos.scrollTop,
1676
+ percentage: kernel.getTotalSize() > 0 ? scrollPos.scrollTop / kernel.getTotalSize() : 0
1677
+ },
1678
+ visibleRange: {
1679
+ ...visibleRange,
1680
+ start: visibleRange.startIndex,
1681
+ end: visibleRange.endIndex
1682
+ },
1683
+ renderRange: {
1684
+ ...renderRange,
1685
+ start: renderRange.startIndex,
1686
+ end: renderRange.endIndex
1687
+ },
1688
+ scrollVelocity: 0,
1689
+ isScrolling: kernel.isScrolling()
1690
+ };
1691
+ notifyListeners();
1692
+ }
1693
+ function logEvent(event) {
1694
+ if (!resolvedOptions.logEvents && state.activeTab !== "events") return;
1695
+ const entry = {
1696
+ id: eventIdCounter++,
1697
+ type: event.type,
1698
+ timestamp: Date.now(),
1699
+ data: event
1700
+ };
1701
+ state.eventLog.unshift(entry);
1702
+ if (state.eventLog.length > resolvedOptions.maxEventLogEntries) {
1703
+ state.eventLog = state.eventLog.slice(0, resolvedOptions.maxEventLogEntries);
1704
+ }
1705
+ if (resolvedOptions.logEvents) {
1706
+ console.log(`[Scrollex Debug] ${event.type}`, event);
1707
+ }
1708
+ notifyListeners();
1709
+ }
1710
+ function startMonitoring() {
1711
+ if (rafId !== null) return;
1712
+ updateFps();
1713
+ statsUpdateTimer = setInterval(updateStats, resolvedOptions.statsUpdateInterval);
1714
+ }
1715
+ function stopMonitoring() {
1716
+ if (rafId !== null) {
1717
+ cancelAnimationFrame(rafId);
1718
+ rafId = null;
1719
+ }
1720
+ if (statsUpdateTimer !== null) {
1721
+ clearInterval(statsUpdateTimer);
1722
+ statsUpdateTimer = null;
1723
+ }
1724
+ }
1725
+ const api = {
1726
+ toggle() {
1727
+ state.visible = !state.visible;
1728
+ if (state.visible) {
1729
+ startMonitoring();
1730
+ } else {
1731
+ stopMonitoring();
1732
+ }
1733
+ notifyListeners();
1734
+ },
1735
+ show() {
1736
+ if (!state.visible) {
1737
+ state.visible = true;
1738
+ startMonitoring();
1739
+ notifyListeners();
1740
+ }
1741
+ },
1742
+ hide() {
1743
+ if (state.visible) {
1744
+ state.visible = false;
1745
+ stopMonitoring();
1746
+ notifyListeners();
1747
+ }
1748
+ },
1749
+ isVisible() {
1750
+ return state.visible;
1751
+ },
1752
+ setCollapsed(collapsed) {
1753
+ state.collapsed = collapsed;
1754
+ notifyListeners();
1755
+ },
1756
+ isCollapsed() {
1757
+ return state.collapsed;
1758
+ },
1759
+ getStats() {
1760
+ return { ...state.stats };
1761
+ },
1762
+ getEventLog() {
1763
+ return [...state.eventLog];
1764
+ },
1765
+ clearEventLog() {
1766
+ state.eventLog = [];
1767
+ notifyListeners();
1768
+ },
1769
+ setActiveTab(tab) {
1770
+ state.activeTab = tab;
1771
+ notifyListeners();
1772
+ },
1773
+ getActiveTab() {
1774
+ return state.activeTab;
1775
+ },
1776
+ toggleViewportOverlay() {
1777
+ state.showViewportOverlay = !state.showViewportOverlay;
1778
+ notifyListeners();
1779
+ },
1780
+ toggleItemBoundaries() {
1781
+ state.showItemBoundaries = !state.showItemBoundaries;
1782
+ notifyListeners();
1783
+ },
1784
+ exportData() {
1785
+ return JSON.stringify({
1786
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1787
+ stats: state.stats,
1788
+ eventLog: state.eventLog,
1789
+ options: resolvedOptions
1790
+ }, null, 2);
1791
+ }
1792
+ };
1793
+ const extendedApi = api;
1794
+ extendedApi.subscribe = (listener) => {
1795
+ listeners.add(listener);
1796
+ return () => listeners.delete(listener);
1797
+ };
1798
+ extendedApi.getState = () => state;
1799
+ extendedApi.getOptions = () => resolvedOptions;
1800
+ extendedApi.getKernel = () => kernel;
1801
+ return {
1802
+ name: "debug-panel",
1803
+ version: "1.0.0",
1804
+ type: "optional",
1805
+ install(k) {
1806
+ kernel = k;
1807
+ const eventTypes = [
1808
+ "scroll",
1809
+ "scrollStart",
1810
+ "scrollEnd",
1811
+ "visibleRangeChange",
1812
+ "itemMeasured",
1813
+ "resize",
1814
+ "loadMore",
1815
+ "itemsChange"
1816
+ ];
1817
+ eventTypes.forEach((type) => {
1818
+ kernel?.on(type, logEvent);
1819
+ });
1820
+ if (state.visible) {
1821
+ startMonitoring();
1822
+ }
1823
+ },
1824
+ uninstall() {
1825
+ stopMonitoring();
1826
+ kernel = null;
1827
+ },
1828
+ api: extendedApi
1829
+ };
1830
+ }
1831
+ function useDebugPanelState(plugin) {
1832
+ const api = plugin.api;
1833
+ const [state, setState] = useState(api.getState());
1834
+ useEffect(() => {
1835
+ return api.subscribe(() => {
1836
+ setState({ ...api.getState() });
1837
+ });
1838
+ }, [api]);
1839
+ return state;
1840
+ }
1841
+ function DebugPanelOverlay({
1842
+ plugin,
1843
+ className = "",
1844
+ style
1845
+ }) {
1846
+ const api = plugin.api;
1847
+ const state = useDebugPanelState(plugin);
1848
+ const options = api.getOptions();
1849
+ const positionStyles = useMemo(() => {
1850
+ const base = {
1851
+ position: "fixed",
1852
+ zIndex: 9999,
1853
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace",
1854
+ fontSize: "12px"
1855
+ };
1856
+ switch (options.position) {
1857
+ case "top-left":
1858
+ return { ...base, top: 10, left: 10 };
1859
+ case "top-right":
1860
+ return { ...base, top: 10, right: 10 };
1861
+ case "bottom-left":
1862
+ return { ...base, bottom: 10, left: 10 };
1863
+ case "bottom-right":
1864
+ default:
1865
+ return { ...base, bottom: 10, right: 10 };
1866
+ }
1867
+ }, [options.position]);
1868
+ const themeStyles = useMemo(() => {
1869
+ const isDark = options.theme === "dark" || options.theme === "auto" && window.matchMedia("(prefers-color-scheme: dark)").matches;
1870
+ return isDark ? {
1871
+ backgroundColor: "rgba(30, 30, 30, 0.95)",
1872
+ color: "#e0e0e0",
1873
+ border: "1px solid #444"
1874
+ } : {
1875
+ backgroundColor: "rgba(255, 255, 255, 0.95)",
1876
+ color: "#333",
1877
+ border: "1px solid #ddd"
1878
+ };
1879
+ }, [options.theme]);
1880
+ const handleTabClick = useCallback((tab) => {
1881
+ api.setActiveTab(tab);
1882
+ }, [api]);
1883
+ const handleToggleCollapse = useCallback(() => {
1884
+ api.setCollapsed(!state.collapsed);
1885
+ }, [api, state.collapsed]);
1886
+ const handleClose = useCallback(() => {
1887
+ api.hide();
1888
+ }, [api]);
1889
+ if (!state.visible) {
1890
+ return null;
1891
+ }
1892
+ const panelStyles = {
1893
+ ...positionStyles,
1894
+ ...themeStyles,
1895
+ borderRadius: "8px",
1896
+ boxShadow: "0 4px 12px rgba(0, 0, 0, 0.15)",
1897
+ overflow: "hidden",
1898
+ minWidth: state.collapsed ? "auto" : "300px",
1899
+ maxWidth: "400px",
1900
+ maxHeight: state.collapsed ? "auto" : "400px",
1901
+ ...style
1902
+ };
1903
+ const headerStyles = {
1904
+ display: "flex",
1905
+ alignItems: "center",
1906
+ justifyContent: "space-between",
1907
+ padding: "8px 12px",
1908
+ borderBottom: state.collapsed ? "none" : "1px solid currentColor",
1909
+ opacity: 0.8,
1910
+ cursor: "pointer"
1911
+ };
1912
+ return /* @__PURE__ */ jsxs("div", { className: `scrollex-debug-panel ${className}`, style: panelStyles, children: [
1913
+ /* @__PURE__ */ jsxs("div", { style: headerStyles, onClick: handleToggleCollapse, children: [
1914
+ /* @__PURE__ */ jsx("span", { style: { fontWeight: 600 }, children: "Scrollex Debug" }),
1915
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: "8px" }, children: [
1916
+ options.showFps && /* @__PURE__ */ jsxs("span", { style: { color: state.stats.fps < 30 ? "#ff6b6b" : state.stats.fps < 50 ? "#ffd93d" : "#6bcb77" }, children: [
1917
+ state.stats.fps,
1918
+ " FPS"
1919
+ ] }),
1920
+ /* @__PURE__ */ jsx(
1921
+ "button",
1922
+ {
1923
+ onClick: (e) => {
1924
+ e.stopPropagation();
1925
+ handleClose();
1926
+ },
1927
+ style: {
1928
+ background: "none",
1929
+ border: "none",
1930
+ cursor: "pointer",
1931
+ color: "inherit",
1932
+ padding: 0,
1933
+ fontSize: "14px"
1934
+ },
1935
+ children: "\xD7"
1936
+ }
1937
+ )
1938
+ ] })
1939
+ ] }),
1940
+ !state.collapsed && /* @__PURE__ */ jsxs(Fragment, { children: [
1941
+ /* @__PURE__ */ jsx(TabBar, { activeTab: state.activeTab, onTabClick: handleTabClick }),
1942
+ /* @__PURE__ */ jsxs("div", { style: { padding: "12px", overflowY: "auto", maxHeight: "300px" }, children: [
1943
+ state.activeTab === "stats" && /* @__PURE__ */ jsx(StatsPanel, { stats: state.stats }),
1944
+ state.activeTab === "viewport" && /* @__PURE__ */ jsx(
1945
+ ViewportPanel,
1946
+ {
1947
+ stats: state.stats,
1948
+ showOverlay: state.showViewportOverlay,
1949
+ showBoundaries: state.showItemBoundaries,
1950
+ onToggleOverlay: () => api.toggleViewportOverlay(),
1951
+ onToggleBoundaries: () => api.toggleItemBoundaries()
1952
+ }
1953
+ ),
1954
+ state.activeTab === "items" && /* @__PURE__ */ jsx(ItemsPanel, { stats: state.stats }),
1955
+ state.activeTab === "events" && /* @__PURE__ */ jsx(EventsPanel, { eventLog: state.eventLog, onClear: () => api.clearEventLog() })
1956
+ ] })
1957
+ ] })
1958
+ ] });
1959
+ }
1960
+ function TabBar({ activeTab, onTabClick }) {
1961
+ const tabs = [
1962
+ { id: "stats", label: "Stats" },
1963
+ { id: "viewport", label: "Viewport" },
1964
+ { id: "items", label: "Items" },
1965
+ { id: "events", label: "Events" }
1966
+ ];
1967
+ const tabStyles = (isActive) => ({
1968
+ flex: 1,
1969
+ padding: "6px 8px",
1970
+ border: "none",
1971
+ background: isActive ? "rgba(100, 100, 255, 0.2)" : "transparent",
1972
+ color: "inherit",
1973
+ cursor: "pointer",
1974
+ fontSize: "11px",
1975
+ fontWeight: isActive ? 600 : 400
1976
+ });
1977
+ return /* @__PURE__ */ jsx("div", { style: { display: "flex", borderBottom: "1px solid currentColor", opacity: 0.6 }, children: tabs.map((tab) => /* @__PURE__ */ jsx("button", { style: tabStyles(activeTab === tab.id), onClick: () => onTabClick(tab.id), children: tab.label }, tab.id)) });
1978
+ }
1979
+ function StatsPanel({ stats }) {
1980
+ const rowStyles = {
1981
+ display: "flex",
1982
+ justifyContent: "space-between",
1983
+ padding: "4px 0",
1984
+ borderBottom: "1px solid rgba(128, 128, 128, 0.2)"
1985
+ };
1986
+ const labelStyles = {
1987
+ opacity: 0.7
1988
+ };
1989
+ const valueStyles = {
1990
+ fontWeight: 500
1991
+ };
1992
+ return /* @__PURE__ */ jsxs("div", { children: [
1993
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
1994
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "FPS" }),
1995
+ /* @__PURE__ */ jsx("span", { style: valueStyles, children: stats.fps })
1996
+ ] }),
1997
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
1998
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Frame Time" }),
1999
+ /* @__PURE__ */ jsxs("span", { style: valueStyles, children: [
2000
+ stats.frameTime.toFixed(2),
2001
+ "ms"
2002
+ ] })
2003
+ ] }),
2004
+ stats.memoryUsage !== null && /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2005
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Memory" }),
2006
+ /* @__PURE__ */ jsxs("span", { style: valueStyles, children: [
2007
+ stats.memoryUsage,
2008
+ "MB"
2009
+ ] })
2010
+ ] }),
2011
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2012
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Total Items" }),
2013
+ /* @__PURE__ */ jsx("span", { style: valueStyles, children: stats.totalItems.toLocaleString() })
2014
+ ] }),
2015
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2016
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Rendered" }),
2017
+ /* @__PURE__ */ jsx("span", { style: valueStyles, children: stats.renderedItems })
2018
+ ] }),
2019
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2020
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Scroll Position" }),
2021
+ /* @__PURE__ */ jsxs("span", { style: valueStyles, children: [
2022
+ Math.round(stats.scrollPosition.offset ?? stats.scrollPosition.scrollTop),
2023
+ "px"
2024
+ ] })
2025
+ ] }),
2026
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2027
+ /* @__PURE__ */ jsx("span", { style: labelStyles, children: "Scrolling" }),
2028
+ /* @__PURE__ */ jsx("span", { style: valueStyles, children: stats.isScrolling ? "Yes" : "No" })
2029
+ ] })
2030
+ ] });
2031
+ }
2032
+ function ViewportPanel({
2033
+ stats,
2034
+ showOverlay,
2035
+ showBoundaries,
2036
+ onToggleOverlay,
2037
+ onToggleBoundaries
2038
+ }) {
2039
+ const rowStyles = {
2040
+ display: "flex",
2041
+ justifyContent: "space-between",
2042
+ padding: "4px 0",
2043
+ borderBottom: "1px solid rgba(128, 128, 128, 0.2)"
2044
+ };
2045
+ const buttonStyles = {
2046
+ padding: "4px 8px",
2047
+ border: "1px solid currentColor",
2048
+ borderRadius: "4px",
2049
+ background: "transparent",
2050
+ color: "inherit",
2051
+ cursor: "pointer",
2052
+ fontSize: "11px",
2053
+ marginTop: "8px",
2054
+ marginRight: "8px"
2055
+ };
2056
+ return /* @__PURE__ */ jsxs("div", { children: [
2057
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2058
+ /* @__PURE__ */ jsx("span", { children: "Visible Range" }),
2059
+ /* @__PURE__ */ jsxs("span", { children: [
2060
+ stats.visibleRange.start,
2061
+ " - ",
2062
+ stats.visibleRange.end
2063
+ ] })
2064
+ ] }),
2065
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2066
+ /* @__PURE__ */ jsx("span", { children: "Render Range" }),
2067
+ /* @__PURE__ */ jsxs("span", { children: [
2068
+ stats.renderRange.start,
2069
+ " - ",
2070
+ stats.renderRange.end
2071
+ ] })
2072
+ ] }),
2073
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2074
+ /* @__PURE__ */ jsx("span", { children: "Overscan Items" }),
2075
+ /* @__PURE__ */ jsx("span", { children: stats.overscanItems })
2076
+ ] }),
2077
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2078
+ /* @__PURE__ */ jsx("span", { children: "Avg Item Height" }),
2079
+ /* @__PURE__ */ jsxs("span", { children: [
2080
+ stats.avgItemHeight.toFixed(1),
2081
+ "px"
2082
+ ] })
2083
+ ] }),
2084
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2085
+ /* @__PURE__ */ jsx("span", { children: "Scroll %" }),
2086
+ /* @__PURE__ */ jsxs("span", { children: [
2087
+ ((stats.scrollPosition.percentage ?? 0) * 100).toFixed(1),
2088
+ "%"
2089
+ ] })
2090
+ ] }),
2091
+ /* @__PURE__ */ jsxs("div", { style: { marginTop: "12px" }, children: [
2092
+ /* @__PURE__ */ jsxs("button", { style: { ...buttonStyles, opacity: showOverlay ? 1 : 0.5 }, onClick: onToggleOverlay, children: [
2093
+ showOverlay ? "\u2713" : "",
2094
+ " Viewport Overlay"
2095
+ ] }),
2096
+ /* @__PURE__ */ jsxs("button", { style: { ...buttonStyles, opacity: showBoundaries ? 1 : 0.5 }, onClick: onToggleBoundaries, children: [
2097
+ showBoundaries ? "\u2713" : "",
2098
+ " Item Boundaries"
2099
+ ] })
2100
+ ] })
2101
+ ] });
2102
+ }
2103
+ function ItemsPanel({ stats }) {
2104
+ const rowStyles = {
2105
+ display: "flex",
2106
+ justifyContent: "space-between",
2107
+ padding: "4px 0",
2108
+ borderBottom: "1px solid rgba(128, 128, 128, 0.2)"
2109
+ };
2110
+ const efficiency = stats.totalItems > 0 ? (stats.renderedItems / stats.totalItems * 100).toFixed(2) : "0";
2111
+ return /* @__PURE__ */ jsxs("div", { children: [
2112
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2113
+ /* @__PURE__ */ jsx("span", { children: "Total Items" }),
2114
+ /* @__PURE__ */ jsx("span", { children: stats.totalItems.toLocaleString() })
2115
+ ] }),
2116
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2117
+ /* @__PURE__ */ jsx("span", { children: "Visible Items" }),
2118
+ /* @__PURE__ */ jsx("span", { children: stats.visibleItems })
2119
+ ] }),
2120
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2121
+ /* @__PURE__ */ jsx("span", { children: "Rendered Items" }),
2122
+ /* @__PURE__ */ jsx("span", { children: stats.renderedItems })
2123
+ ] }),
2124
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2125
+ /* @__PURE__ */ jsx("span", { children: "Overscan Items" }),
2126
+ /* @__PURE__ */ jsx("span", { children: stats.overscanItems })
2127
+ ] }),
2128
+ /* @__PURE__ */ jsxs("div", { style: rowStyles, children: [
2129
+ /* @__PURE__ */ jsx("span", { children: "Render Efficiency" }),
2130
+ /* @__PURE__ */ jsxs("span", { children: [
2131
+ efficiency,
2132
+ "% rendered"
2133
+ ] })
2134
+ ] }),
2135
+ /* @__PURE__ */ jsx("div", { style: { ...rowStyles, marginTop: "8px", fontStyle: "italic", opacity: 0.7 }, children: /* @__PURE__ */ jsxs("span", { children: [
2136
+ "Only ",
2137
+ stats.renderedItems,
2138
+ " of ",
2139
+ stats.totalItems.toLocaleString(),
2140
+ " items in DOM"
2141
+ ] }) })
2142
+ ] });
2143
+ }
2144
+ function EventsPanel({ eventLog, onClear }) {
2145
+ const buttonStyles = {
2146
+ padding: "4px 8px",
2147
+ border: "1px solid currentColor",
2148
+ borderRadius: "4px",
2149
+ background: "transparent",
2150
+ color: "inherit",
2151
+ cursor: "pointer",
2152
+ fontSize: "11px",
2153
+ marginBottom: "8px"
2154
+ };
2155
+ const eventStyles = {
2156
+ padding: "4px 0",
2157
+ borderBottom: "1px solid rgba(128, 128, 128, 0.2)",
2158
+ fontSize: "10px"
2159
+ };
2160
+ const typeColors = {
2161
+ scroll: "#6bcb77",
2162
+ scrollStart: "#4d96ff",
2163
+ scrollEnd: "#4d96ff",
2164
+ visibleRangeChange: "#ffd93d",
2165
+ itemMeasured: "#9b59b6",
2166
+ resize: "#ff6b6b",
2167
+ loadMore: "#1abc9c",
2168
+ itemsChange: "#e74c3c"
2169
+ };
2170
+ return /* @__PURE__ */ jsxs("div", { children: [
2171
+ /* @__PURE__ */ jsx("button", { style: buttonStyles, onClick: onClear, children: "Clear Log" }),
2172
+ /* @__PURE__ */ jsx("div", { style: { maxHeight: "200px", overflowY: "auto" }, children: eventLog.length === 0 ? /* @__PURE__ */ jsx("div", { style: { opacity: 0.5, fontStyle: "italic" }, children: "No events logged" }) : eventLog.map((entry) => /* @__PURE__ */ jsxs("div", { style: eventStyles, children: [
2173
+ /* @__PURE__ */ jsx("span", { style: { color: typeColors[entry.type] ?? "#888" }, children: entry.type }),
2174
+ /* @__PURE__ */ jsx("span", { style: { opacity: 0.5, marginLeft: "8px" }, children: new Date(entry.timestamp).toLocaleTimeString() })
2175
+ ] }, entry.id)) })
2176
+ ] });
2177
+ }
2178
+
2179
+ export { DebugPanelOverlay, autoMeasurerPlugin, bidirectional, debugPanel, dragToReorder, gridRendererPlugin, infiniteLoaderPlugin, keyboardNav, listRendererPlugin, masonryRenderer, scrollControllerPlugin, scrollRestoration, stickyHeaders };
2180
+ //# sourceMappingURL=index.js.map
2181
+ //# sourceMappingURL=index.js.map