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