@oxog/scrollex 1.0.0 → 1.1.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,747 @@
1
+ 'use strict';
2
+
3
+ // src/plugins/core/list-renderer.ts
4
+ function listRendererPlugin(options = {}) {
5
+ const { getItemKey = (index) => index } = options;
6
+ let kernel = null;
7
+ let virtualItems = [];
8
+ let currentRange = {
9
+ startIndex: 0,
10
+ endIndex: 0,
11
+ overscanStartIndex: 0,
12
+ overscanEndIndex: 0
13
+ };
14
+ function buildVirtualItems(range) {
15
+ if (!kernel) return [];
16
+ const items = [];
17
+ const lastIndex = Math.min(range.overscanEndIndex, kernel.getItemCount() - 1);
18
+ for (let i = range.overscanStartIndex; i <= lastIndex; i++) {
19
+ const offset = kernel.getItemOffset(i);
20
+ const size = kernel.getCachedHeight(i) ?? kernel.getEstimatedHeight();
21
+ items.push({
22
+ index: i,
23
+ key: getItemKey(i),
24
+ start: offset,
25
+ end: offset + size,
26
+ size,
27
+ lane: 0
28
+ // For list, always lane 0
29
+ });
30
+ }
31
+ return items;
32
+ }
33
+ function recalculate() {
34
+ if (!kernel) return;
35
+ currentRange = kernel.getVisibleRange();
36
+ virtualItems = buildVirtualItems(currentRange);
37
+ }
38
+ const api = {
39
+ getVirtualItems() {
40
+ return virtualItems;
41
+ },
42
+ getTotalHeight() {
43
+ return kernel?.getTotalSize() ?? 0;
44
+ },
45
+ getStartOffset() {
46
+ if (virtualItems.length === 0) return 0;
47
+ return virtualItems[0]?.start ?? 0;
48
+ },
49
+ getEndOffset() {
50
+ if (virtualItems.length === 0) return 0;
51
+ const lastItem = virtualItems[virtualItems.length - 1];
52
+ return lastItem?.end ?? 0;
53
+ },
54
+ isItemVisible(index) {
55
+ if (!kernel || index >= kernel.getItemCount()) return false;
56
+ return index >= currentRange.startIndex && index <= currentRange.endIndex;
57
+ },
58
+ recalculate
59
+ };
60
+ return {
61
+ name: "list-renderer",
62
+ version: "1.0.0",
63
+ type: "core",
64
+ install(k) {
65
+ kernel = k;
66
+ recalculate();
67
+ },
68
+ uninstall() {
69
+ virtualItems = [];
70
+ kernel = null;
71
+ },
72
+ hooks: {
73
+ onScroll: () => recalculate(),
74
+ onResize: () => recalculate(),
75
+ onItemMeasured: () => recalculate(),
76
+ onVisibleRangeChange: (range) => {
77
+ currentRange = range;
78
+ virtualItems = buildVirtualItems(range);
79
+ },
80
+ onItemsChange: () => recalculate()
81
+ },
82
+ api
83
+ };
84
+ }
85
+
86
+ // src/utils/array.ts
87
+ function clamp(value, min, max) {
88
+ return Math.min(Math.max(value, min), max);
89
+ }
90
+ function normalizeColumnCount(columns) {
91
+ return Number.isFinite(columns) ? Math.max(1, Math.floor(columns)) : 1;
92
+ }
93
+ function autoColumnCount(containerWidth, minColumnWidth, gap) {
94
+ const slot = minColumnWidth + gap;
95
+ return slot > 0 ? normalizeColumnCount(Math.floor((containerWidth + gap) / slot)) : 1;
96
+ }
97
+ function normalizeOverscan(overscan) {
98
+ return Number.isNaN(overscan) ? 0 : Math.max(0, Math.floor(overscan));
99
+ }
100
+
101
+ // src/plugins/core/grid-renderer.ts
102
+ function gridRendererPlugin(options) {
103
+ const {
104
+ columns,
105
+ minColumnWidth = 100,
106
+ rowHeight,
107
+ gap = 0,
108
+ getItemKey = (index) => index,
109
+ getContainerWidth
110
+ } = options;
111
+ let kernel = null;
112
+ let virtualItems = [];
113
+ let columnCount = typeof columns === "number" ? normalizeColumnCount(columns) : 1;
114
+ let columnWidth = 0;
115
+ const gapX = typeof gap === "number" ? gap : gap.x;
116
+ const gapY = typeof gap === "number" ? gap : gap.y;
117
+ function calculateColumns(containerWidth) {
118
+ if (typeof columns === "number") {
119
+ columnCount = normalizeColumnCount(columns);
120
+ } else {
121
+ columnCount = autoColumnCount(containerWidth, minColumnWidth, gapX);
122
+ }
123
+ columnWidth = (containerWidth - (columnCount - 1) * gapX) / columnCount;
124
+ }
125
+ function getRowCount() {
126
+ if (!kernel) return 0;
127
+ return Math.ceil(kernel.getItemCount() / columnCount);
128
+ }
129
+ function buildVirtualItems() {
130
+ if (!kernel) return [];
131
+ const viewport = kernel.getViewport();
132
+ const scrollPosition = kernel.getScrollPosition();
133
+ const scrollTop = scrollPosition.scrollTop;
134
+ const effectiveRowHeight = rowHeight + gapY;
135
+ const startRow = Math.max(0, Math.floor(scrollTop / effectiveRowHeight));
136
+ const visibleRows = Math.ceil(viewport.height / effectiveRowHeight);
137
+ const overscan = kernel.getOptions().overscan;
138
+ const endRow = Math.min(getRowCount() - 1, startRow + visibleRows + overscan);
139
+ const overscanStartRow = Math.max(0, startRow - overscan);
140
+ const items = [];
141
+ const itemCount = kernel.getItemCount();
142
+ for (let row = overscanStartRow; row <= endRow; row++) {
143
+ for (let col = 0; col < columnCount; col++) {
144
+ const index = row * columnCount + col;
145
+ if (index >= itemCount) break;
146
+ const top = row * effectiveRowHeight;
147
+ items.push({
148
+ index,
149
+ key: getItemKey(index),
150
+ start: top,
151
+ end: top + rowHeight,
152
+ size: rowHeight,
153
+ lane: col
154
+ });
155
+ }
156
+ }
157
+ return items;
158
+ }
159
+ function recalculate() {
160
+ if (!kernel) return;
161
+ const viewport = kernel.getViewport();
162
+ calculateColumns(getContainerWidth?.() ?? viewport.width);
163
+ virtualItems = buildVirtualItems();
164
+ }
165
+ const api = {
166
+ getVirtualItems() {
167
+ return virtualItems;
168
+ },
169
+ getTotalHeight() {
170
+ const rowCount = getRowCount();
171
+ return rowCount === 0 ? 0 : rowCount * (rowHeight + gapY) - gapY;
172
+ },
173
+ getColumnCount() {
174
+ return columnCount;
175
+ },
176
+ getColumnWidth() {
177
+ return columnWidth;
178
+ },
179
+ getItemsPerRow() {
180
+ return columnCount;
181
+ },
182
+ getRowCount,
183
+ recalculate
184
+ };
185
+ return {
186
+ name: "grid-renderer",
187
+ version: "1.0.0",
188
+ type: "core",
189
+ install(k) {
190
+ kernel = k;
191
+ recalculate();
192
+ },
193
+ uninstall() {
194
+ virtualItems = [];
195
+ kernel = null;
196
+ },
197
+ hooks: {
198
+ onScroll: () => {
199
+ virtualItems = buildVirtualItems();
200
+ },
201
+ onResize: () => recalculate(),
202
+ onItemsChange: () => recalculate()
203
+ },
204
+ api
205
+ };
206
+ }
207
+
208
+ // src/utils/debounce.ts
209
+ function debounce(fn, wait) {
210
+ let timeoutId = null;
211
+ let lastArgs = null;
212
+ let lastThis = null;
213
+ const debounced = function(...args) {
214
+ lastArgs = args;
215
+ lastThis = this;
216
+ if (timeoutId) {
217
+ clearTimeout(timeoutId);
218
+ }
219
+ timeoutId = setTimeout(() => {
220
+ timeoutId = null;
221
+ if (lastArgs) {
222
+ fn.apply(lastThis, lastArgs);
223
+ lastArgs = null;
224
+ lastThis = null;
225
+ }
226
+ }, wait);
227
+ };
228
+ debounced.cancel = () => {
229
+ if (timeoutId) {
230
+ clearTimeout(timeoutId);
231
+ timeoutId = null;
232
+ }
233
+ lastArgs = null;
234
+ lastThis = null;
235
+ };
236
+ debounced.flush = () => {
237
+ if (timeoutId) {
238
+ clearTimeout(timeoutId);
239
+ timeoutId = null;
240
+ if (lastArgs) {
241
+ fn.apply(lastThis, lastArgs);
242
+ lastArgs = null;
243
+ lastThis = null;
244
+ }
245
+ }
246
+ };
247
+ return debounced;
248
+ }
249
+
250
+ // src/plugins/core/auto-measurer.ts
251
+ function autoMeasurerPlugin(options = {}) {
252
+ const {
253
+ estimatedItemHeight,
254
+ measureOnResize = true,
255
+ debounceMs = 100
256
+ } = options;
257
+ let kernel = null;
258
+ let resizeObserver = null;
259
+ const observedElements = /* @__PURE__ */ new WeakMap();
260
+ const elementsByIndex = /* @__PURE__ */ new Map();
261
+ let measureQueue = /* @__PURE__ */ new Set();
262
+ let flushScheduled = false;
263
+ function sizeAlongAxis(rect) {
264
+ return kernel?.getOptions().direction === "horizontal" ? rect.width : rect.height;
265
+ }
266
+ const debouncedFlush = debounce(() => {
267
+ flush();
268
+ }, debounceMs);
269
+ function flush() {
270
+ if (!kernel || measureQueue.size === 0) {
271
+ flushScheduled = false;
272
+ return;
273
+ }
274
+ const indicesToMeasure = Array.from(measureQueue);
275
+ measureQueue = /* @__PURE__ */ new Set();
276
+ flushScheduled = false;
277
+ const measurements = [];
278
+ for (const index of indicesToMeasure) {
279
+ const ref = elementsByIndex.get(index);
280
+ const element = ref?.deref();
281
+ if (element) {
282
+ const rect = element.getBoundingClientRect();
283
+ measurements.push({ index, height: sizeAlongAxis(rect) });
284
+ }
285
+ }
286
+ for (const { index, height } of measurements) {
287
+ if (height > 0) {
288
+ kernel.measureItem(index, height);
289
+ }
290
+ }
291
+ }
292
+ function scheduleFlush() {
293
+ if (!flushScheduled) {
294
+ flushScheduled = true;
295
+ requestAnimationFrame(() => {
296
+ flush();
297
+ });
298
+ }
299
+ }
300
+ function handleResize(entries) {
301
+ for (const entry of entries) {
302
+ const index = observedElements.get(entry.target);
303
+ if (index !== void 0) {
304
+ measureQueue.add(index);
305
+ }
306
+ }
307
+ if (measureOnResize && measureQueue.size > 0) {
308
+ debouncedFlush();
309
+ }
310
+ }
311
+ const api = {
312
+ measureElement(index, element) {
313
+ const rect = element.getBoundingClientRect();
314
+ const height = sizeAlongAxis(rect);
315
+ if (height > 0 && kernel) {
316
+ kernel.measureItem(index, height);
317
+ }
318
+ return height;
319
+ },
320
+ getCachedSize(index) {
321
+ return kernel?.getCachedHeight(index);
322
+ },
323
+ invalidate(index) {
324
+ kernel?.invalidateMeasurement(index);
325
+ },
326
+ invalidateAll() {
327
+ kernel?.invalidateAllMeasurements();
328
+ },
329
+ getMeasurementCache() {
330
+ const cache = /* @__PURE__ */ new Map();
331
+ if (!kernel) return cache;
332
+ const itemCount = kernel.getItemCount();
333
+ for (let i = 0; i < itemCount; i++) {
334
+ const height = kernel.getCachedHeight(i);
335
+ if (height !== void 0) {
336
+ cache.set(i, height);
337
+ }
338
+ }
339
+ return cache;
340
+ },
341
+ observe(index, element) {
342
+ const existingRef = elementsByIndex.get(index);
343
+ const existingElement = existingRef?.deref();
344
+ if (existingElement && existingElement !== element) {
345
+ resizeObserver?.unobserve(existingElement);
346
+ observedElements.delete(existingElement);
347
+ }
348
+ const previousIndex = observedElements.get(element);
349
+ if (previousIndex !== void 0 && previousIndex !== index) {
350
+ elementsByIndex.delete(previousIndex);
351
+ measureQueue.delete(previousIndex);
352
+ }
353
+ observedElements.set(element, index);
354
+ elementsByIndex.set(index, typeof WeakRef === "function" ? new WeakRef(element) : { deref: () => element });
355
+ resizeObserver?.observe(element);
356
+ measureQueue.add(index);
357
+ scheduleFlush();
358
+ },
359
+ unobserve(element) {
360
+ const index = observedElements.get(element);
361
+ if (index !== void 0) {
362
+ resizeObserver?.unobserve(element);
363
+ observedElements.delete(element);
364
+ elementsByIndex.delete(index);
365
+ measureQueue.delete(index);
366
+ }
367
+ }
368
+ };
369
+ return {
370
+ name: "auto-measurer",
371
+ version: "1.0.0",
372
+ type: "core",
373
+ install(k) {
374
+ kernel = k;
375
+ if (estimatedItemHeight !== void 0) {
376
+ kernel.configure({ estimatedItemHeight });
377
+ }
378
+ if (measureOnResize && typeof ResizeObserver !== "undefined") {
379
+ resizeObserver = new ResizeObserver(handleResize);
380
+ }
381
+ },
382
+ uninstall() {
383
+ if (resizeObserver) {
384
+ resizeObserver.disconnect();
385
+ resizeObserver = null;
386
+ }
387
+ debouncedFlush.cancel();
388
+ measureQueue.clear();
389
+ elementsByIndex.clear();
390
+ flushScheduled = false;
391
+ kernel = null;
392
+ },
393
+ api
394
+ };
395
+ }
396
+
397
+ // src/plugins/core/infinite-loader.ts
398
+ function initialLoaderState(direction) {
399
+ return { loading: false, loadGeneration: 0, hasMoreForward: true, hasMoreBackward: direction === "backward" || direction === "both" };
400
+ }
401
+ var sharedLoaderStates = /* @__PURE__ */ new WeakMap();
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 state = initialLoaderState(direction);
411
+ let lastCheckTime = 0;
412
+ const checkDebounceMs = 100;
413
+ let pendingEvent = null;
414
+ let trailingTimer = null;
415
+ function checkLoadMore(event) {
416
+ if (state.loading || !kernel) return;
417
+ const now = performance.now();
418
+ if (now - lastCheckTime < checkDebounceMs) {
419
+ pendingEvent = event;
420
+ if (trailingTimer === null) {
421
+ trailingTimer = setTimeout(() => {
422
+ trailingTimer = null;
423
+ const latest = pendingEvent;
424
+ pendingEvent = null;
425
+ if (latest) runCheck(latest);
426
+ }, Math.max(0, checkDebounceMs - (now - lastCheckTime)));
427
+ }
428
+ return;
429
+ }
430
+ runCheck(event);
431
+ }
432
+ function runCheck(event) {
433
+ if (state.loading || !kernel) return;
434
+ lastCheckTime = performance.now();
435
+ const viewport = kernel.getViewport();
436
+ const totalSize = kernel.getTotalSize();
437
+ const horizontal = kernel.getOptions().direction === "horizontal";
438
+ const scrollTop = horizontal ? event.scrollLeft : event.scrollTop;
439
+ const viewportSize = horizontal ? viewport.width : viewport.height;
440
+ if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
441
+ const distanceFromBottom = totalSize - scrollTop - viewportSize;
442
+ if (distanceFromBottom < threshold) {
443
+ triggerLoad("forward");
444
+ return;
445
+ }
446
+ }
447
+ if ((direction === "backward" || direction === "both") && state.hasMoreBackward) {
448
+ if (scrollTop < threshold) {
449
+ triggerLoad("backward");
450
+ return;
451
+ }
452
+ }
453
+ }
454
+ function checkLoadMoreByRange(range, forwardOnly = false) {
455
+ if (state.loading || !kernel) return;
456
+ const itemCount = kernel.getItemCount();
457
+ if ((direction === "forward" || direction === "both") && state.hasMoreForward) {
458
+ const itemsFromEnd = itemCount - 1 - range.endIndex;
459
+ if (itemsFromEnd <= thresholdItems) {
460
+ triggerLoad("forward");
461
+ return;
462
+ }
463
+ }
464
+ if (!forwardOnly && (direction === "backward" || direction === "both") && state.hasMoreBackward) {
465
+ if (range.startIndex <= thresholdItems) {
466
+ triggerLoad("backward");
467
+ return;
468
+ }
469
+ }
470
+ }
471
+ async function triggerLoad(loadDirection) {
472
+ if (state.loading || !kernel) return;
473
+ if (loadDirection === "forward" && !state.hasMoreForward) return;
474
+ if (loadDirection === "backward" && !state.hasMoreBackward) return;
475
+ state.loading = true;
476
+ const generation = ++state.loadGeneration;
477
+ kernel.emit({
478
+ type: "load-more",
479
+ direction: loadDirection
480
+ });
481
+ try {
482
+ await onLoadMore(loadDirection);
483
+ } catch (error) {
484
+ console.error("[Scrollex] Error in onLoadMore:", error);
485
+ } finally {
486
+ if (generation === state.loadGeneration) {
487
+ state.loading = false;
488
+ }
489
+ }
490
+ }
491
+ const api = {
492
+ isLoading() {
493
+ return state.loading;
494
+ },
495
+ setLoading(l) {
496
+ state.loading = l;
497
+ },
498
+ hasMore() {
499
+ return state.hasMoreForward || state.hasMoreBackward;
500
+ },
501
+ setHasMore(hasMore) {
502
+ state.hasMoreForward = hasMore;
503
+ state.hasMoreBackward = hasMore;
504
+ },
505
+ hasMoreForward() {
506
+ return state.hasMoreForward;
507
+ },
508
+ setHasMoreForward(hasMore) {
509
+ state.hasMoreForward = hasMore;
510
+ },
511
+ hasMoreBackward() {
512
+ return state.hasMoreBackward;
513
+ },
514
+ setHasMoreBackward(hasMore) {
515
+ state.hasMoreBackward = hasMore;
516
+ },
517
+ reset() {
518
+ state.loadGeneration++;
519
+ state.loading = false;
520
+ state.hasMoreForward = true;
521
+ state.hasMoreBackward = direction === "backward" || direction === "both";
522
+ if (trailingTimer !== null) {
523
+ clearTimeout(trailingTimer);
524
+ trailingTimer = null;
525
+ }
526
+ pendingEvent = null;
527
+ },
528
+ triggerLoad(loadDirection) {
529
+ triggerLoad(loadDirection);
530
+ }
531
+ };
532
+ return {
533
+ name: "infinite-loader",
534
+ version: "1.0.0",
535
+ type: "core",
536
+ install(k) {
537
+ kernel = k;
538
+ const shared = sharedLoaderStates.get(k);
539
+ if (shared) {
540
+ state = shared;
541
+ } else {
542
+ sharedLoaderStates.set(k, state);
543
+ checkLoadMoreByRange(k.getVisibleRange(), true);
544
+ }
545
+ },
546
+ uninstall() {
547
+ if (trailingTimer !== null) {
548
+ clearTimeout(trailingTimer);
549
+ trailingTimer = null;
550
+ }
551
+ pendingEvent = null;
552
+ kernel = null;
553
+ },
554
+ hooks: {
555
+ onScroll: checkLoadMore,
556
+ onVisibleRangeChange: (range) => checkLoadMoreByRange(range)
557
+ },
558
+ api
559
+ };
560
+ }
561
+
562
+ // src/utils/raf.ts
563
+ function raf(callback) {
564
+ if (typeof requestAnimationFrame !== "undefined") {
565
+ return requestAnimationFrame(callback);
566
+ }
567
+ return setTimeout(() => callback(performance.now()), 16);
568
+ }
569
+ function cancelRaf(id) {
570
+ if (typeof cancelAnimationFrame !== "undefined") {
571
+ cancelAnimationFrame(id);
572
+ } else {
573
+ clearTimeout(id);
574
+ }
575
+ }
576
+
577
+ // src/plugins/core/scroll-controller.ts
578
+ var easings = {
579
+ easeInOut: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t};
580
+ function scrollControllerPlugin(options = {}) {
581
+ const {
582
+ defaultDuration = 300,
583
+ defaultEasing = easings.easeInOut
584
+ } = options;
585
+ let kernel = null;
586
+ let animationId = null;
587
+ let animating = false;
588
+ let animationGeneration = 0;
589
+ function isHorizontal(k) {
590
+ return k.getOptions().direction === "horizontal";
591
+ }
592
+ function getOffset(k) {
593
+ const position = k.getScrollPosition();
594
+ return isHorizontal(k) ? position.scrollLeft : position.scrollTop;
595
+ }
596
+ function setOffset(k, offset) {
597
+ k.setScrollPosition(isHorizontal(k) ? { scrollLeft: offset } : { scrollTop: offset });
598
+ }
599
+ function getViewportSize(k) {
600
+ const viewport = k.getViewport();
601
+ return isHorizontal(k) ? viewport.width : viewport.height;
602
+ }
603
+ function animateScroll(targetOffset, duration, easing) {
604
+ if (!kernel) return;
605
+ stopAnimation();
606
+ const generation = animationGeneration;
607
+ const startTime = performance.now();
608
+ const startOffset = getOffset(kernel);
609
+ const distance = targetOffset - startOffset;
610
+ if (distance === 0 || !(duration > 0)) {
611
+ setOffset(kernel, targetOffset);
612
+ return;
613
+ }
614
+ animating = true;
615
+ function animate(currentTime) {
616
+ if (!kernel) {
617
+ animating = false;
618
+ return;
619
+ }
620
+ if (generation !== animationGeneration) return;
621
+ const elapsed = currentTime - startTime;
622
+ const progress = Math.max(0, Math.min(elapsed / duration, 1));
623
+ const easedProgress = easing(progress);
624
+ const currentOffset = startOffset + distance * easedProgress;
625
+ setOffset(kernel, currentOffset);
626
+ if (generation !== animationGeneration) return;
627
+ if (progress < 1) {
628
+ animationId = raf(animate);
629
+ } else {
630
+ animationId = null;
631
+ animating = false;
632
+ }
633
+ }
634
+ animationId = raf(animate);
635
+ }
636
+ function stopAnimation() {
637
+ animationGeneration++;
638
+ if (animationId !== null) {
639
+ cancelRaf(animationId);
640
+ animationId = null;
641
+ }
642
+ animating = false;
643
+ }
644
+ const api = {
645
+ scrollTo(offset, options2 = {}) {
646
+ if (!kernel) return;
647
+ const {
648
+ behavior = "auto",
649
+ duration = defaultDuration,
650
+ easing = defaultEasing
651
+ } = options2;
652
+ if (behavior === "auto") {
653
+ stopAnimation();
654
+ setOffset(kernel, offset);
655
+ } else {
656
+ animateScroll(offset, duration, easing);
657
+ }
658
+ },
659
+ scrollToIndex(index, options2 = {}) {
660
+ if (!kernel) return;
661
+ const {
662
+ align = "auto",
663
+ offset: additionalOffset = 0,
664
+ ...scrollOptions
665
+ } = options2;
666
+ const itemOffset = kernel.getItemOffset(index);
667
+ const itemSize = kernel.getCachedHeight(index) ?? kernel.getEstimatedHeight();
668
+ const viewportSize = getViewportSize(kernel);
669
+ let targetOffset;
670
+ switch (align) {
671
+ case "start":
672
+ targetOffset = itemOffset;
673
+ break;
674
+ case "center":
675
+ targetOffset = itemOffset - viewportSize / 2 + itemSize / 2;
676
+ break;
677
+ case "end":
678
+ targetOffset = itemOffset - viewportSize + itemSize;
679
+ break;
680
+ case "auto":
681
+ default: {
682
+ const visibleStart = getOffset(kernel);
683
+ const visibleEnd = visibleStart + viewportSize;
684
+ const itemEnd = itemOffset + itemSize;
685
+ if (itemOffset < visibleStart) {
686
+ targetOffset = itemOffset;
687
+ } else if (itemEnd > visibleEnd) {
688
+ targetOffset = itemEnd - viewportSize;
689
+ } else {
690
+ return;
691
+ }
692
+ break;
693
+ }
694
+ }
695
+ const maxScroll = Math.max(0, kernel.getTotalSize() - viewportSize);
696
+ targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
697
+ api.scrollTo(targetOffset, scrollOptions);
698
+ },
699
+ scrollToTop(options2 = {}) {
700
+ api.scrollTo(0, options2);
701
+ },
702
+ scrollToBottom(options2 = {}) {
703
+ if (!kernel) return;
704
+ const totalSize = kernel.getTotalSize();
705
+ const maxScroll = Math.max(0, totalSize - getViewportSize(kernel));
706
+ api.scrollTo(maxScroll, options2);
707
+ },
708
+ getScrollPosition() {
709
+ return kernel?.getScrollPosition() ?? { scrollTop: 0, scrollLeft: 0 };
710
+ },
711
+ isScrolling() {
712
+ return kernel?.isScrolling() ?? false;
713
+ },
714
+ isAnimating() {
715
+ return animating;
716
+ },
717
+ stopAnimation
718
+ };
719
+ return {
720
+ name: "scroll-controller",
721
+ version: "1.0.0",
722
+ type: "core",
723
+ install(k) {
724
+ kernel = k;
725
+ },
726
+ uninstall() {
727
+ stopAnimation();
728
+ kernel = null;
729
+ },
730
+ api
731
+ };
732
+ }
733
+
734
+ exports.autoColumnCount = autoColumnCount;
735
+ exports.autoMeasurerPlugin = autoMeasurerPlugin;
736
+ exports.cancelRaf = cancelRaf;
737
+ exports.clamp = clamp;
738
+ exports.debounce = debounce;
739
+ exports.gridRendererPlugin = gridRendererPlugin;
740
+ exports.infiniteLoaderPlugin = infiniteLoaderPlugin;
741
+ exports.listRendererPlugin = listRendererPlugin;
742
+ exports.normalizeColumnCount = normalizeColumnCount;
743
+ exports.normalizeOverscan = normalizeOverscan;
744
+ exports.raf = raf;
745
+ exports.scrollControllerPlugin = scrollControllerPlugin;
746
+ //# sourceMappingURL=chunk-ND5PV5MU.cjs.map
747
+ //# sourceMappingURL=chunk-ND5PV5MU.cjs.map