|
149 | 149 | let clientWidth: number = $state(0); |
150 | 150 |
|
151 | 151 | // viewport overfetch trigger in px |
152 | | - let overfetchBufferInPx: number = 100; |
| 152 | + // let overfetchBufferInPx: number = 100; |
153 | 153 |
|
154 | 154 | let itemKey: 'index' | ((item: any, index: number) => any); |
155 | 155 |
|
|
270 | 270 |
|
271 | 271 | $effect(() => { |
272 | 272 | console.log('onVisibleRangeUpdate'); |
273 | | - // const vr = getVisibleRange(isHorizontal ? clientWidth : clientHeight, offset); |
274 | | - onVisibleRangeUpdate?.({ start: startIdx, end: endIdx }); |
| 273 | +
|
| 274 | + if (onVisibleRangeUpdate) { |
| 275 | + const vr = getVisibleRange(isHorizontal ? clientWidth : clientHeight, curState.offset); |
| 276 | + //onVisibleRangeUpdate({ start: startIdx, end: endIdx }); |
| 277 | + onVisibleRangeUpdate(vr); |
| 278 | + } |
275 | 279 | }); |
276 | 280 |
|
277 | 281 | //TODO see if effect.pre not a better option |
|
361 | 365 | //resetItem |
362 | 366 | lastMeasuredIndex = Math.min(lastMeasuredIndex, startIndex - 1); |
363 | 367 | refreshOffsets(); |
364 | | - // const vr = getVisibleRange(isHorizontal ? clientWidth : clientHeight, offset); |
365 | | - // onVisibleRangeUpdate?.({ start: vr.start, end: vr.end }); |
366 | 368 | } |
367 | 369 |
|
368 | 370 | function onscroll(event: Event): void { |
|
386 | 388 | } |
387 | 389 |
|
388 | 390 | // return the index of the starting boundary |
389 | | - function getStart(): number { |
390 | | - const startPosition = |
391 | | - getScroll(listContainer) - getPaddingStart(listContainer) - overfetchBufferInPx; |
392 | | - return findNearestItem(startPosition); |
393 | | - } |
| 391 | + // function getStart(): number { |
| 392 | + // const startPosition = |
| 393 | + // getScroll(listContainer) - getPaddingStart(listContainer) - overfetchBufferInPx; |
| 394 | + // return findNearestItem(startPosition); |
| 395 | + // } |
394 | 396 |
|
395 | 397 | // return the index of the closing boundary |
396 | | - function getEnd() { |
397 | | - const endPosition = |
398 | | - getScroll(listContainer) - |
399 | | - getPaddingStart(listContainer) + |
400 | | - getClientSize(listContainer) + |
401 | | - overfetchBufferInPx; |
402 | | -
|
403 | | - return findNearestItem(endPosition); |
404 | | - } |
| 398 | + // function getEnd() { |
| 399 | + // const endPosition = |
| 400 | + // getScroll(listContainer) - |
| 401 | + // getPaddingStart(listContainer) + |
| 402 | + // getClientSize(listContainer) + |
| 403 | + // overfetchBufferInPx; |
| 404 | +
|
| 405 | + // return findNearestItem(endPosition); |
| 406 | + // } |
405 | 407 |
|
406 | 408 | function getOffsetForIndex( |
407 | 409 | index: number, |
|
546 | 548 | avgSizeInPx = getAvgSize(); |
547 | 549 | } |
548 | 550 |
|
549 | | - startIdx = getStart(); |
550 | | - endIdx = getEnd(); |
| 551 | + const vr = getVisibleRange(isHorizontal ? clientWidth : clientHeight, curState.offset,3); |
| 552 | + |
| 553 | + startIdx = vr.start; |
| 554 | + endIdx = vr.end; |
551 | 555 |
|
552 | 556 | let vi0 = 0; |
553 | 557 |
|
|
617 | 621 | } |
618 | 622 |
|
619 | 623 | // returns an index range |
620 | | - function getVisibleRange(containerSize: number = 0, scrollbarOffset: number) { |
| 624 | + function getVisibleRange( |
| 625 | + containerSize: number = 0, |
| 626 | + scrollbarOffset: number, |
| 627 | + windowOverPaddingCount: number = 0 |
| 628 | + ) { |
621 | 629 | const totalSize = totalViewportSize; |
622 | 630 |
|
623 | 631 | if (totalSize === 0) return { start: 0, end: 0 }; |
|
637 | 645 | offset += sizes[endIdx]; |
638 | 646 | } |
639 | 647 |
|
| 648 | + if (windowOverPaddingCount > 0) { |
| 649 | + startIdx = Math.max(0, startIdx - windowOverPaddingCount); |
| 650 | + endIdx = Math.min(endIdx + windowOverPaddingCount, items.length - 1); |
| 651 | + } |
| 652 | +
|
640 | 653 | return { |
641 | 654 | start: startIdx, |
642 | 655 | end: endIdx |
|
0 commit comments