Skip to content

Commit 25dc2f1

Browse files
committed
refactor: baby steps
1 parent 6ae7202 commit 25dc2f1

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/lib/new2/SizeAndPositionManager.ts

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default class SizeAndPositionManager {
2727
itemSizeAndPositionData: SizeAndOffsetData = {};
2828
private lastMeasuredIndex: number = -1;
2929

30-
3130
private totalSize?: number;
3231

3332
constructor(
@@ -219,45 +218,38 @@ export default class SizeAndPositionManager {
219218
}
220219

221220
// // returns an index range
222-
// getVisibleRangeOffsets(
223-
// containerSize: number = 0,
224-
// scrollbarOffset: number
225-
// ): VLRange {
226-
// const totalSize = this.getTotalSize();
227-
228-
// if (totalSize === 0) {
229-
// return { start: 0, end: 0 };
230-
// }
221+
getVisibleRangeOffsets(containerSize: number = 0, scrollbarOffset: number) {
222+
const totalSize = this.getTotalSize();
231223

232-
// const maxOffset = scrollbarOffset + containerSize;
233-
// // let startIdx = this.findNearestItem(scrollbarOffset);
224+
if (totalSize === 0) {
225+
return { start: 0, end: 0 };
226+
}
234227

235-
// if (startIdx === undefined) {
236-
// throw Error(`Invalid offset ${scrollbarOffset} specified`);
237-
// }
228+
const maxOffset = scrollbarOffset + containerSize;
238229

239-
// const datum = this.getSizeAndPositionForIndex(startIdx);
240-
// scrollbarOffset = datum.offset + datum.size;
230+
const startOffset = scrollbarOffset;
231+
let startIdx = this.findNearestItem(scrollbarOffset);
241232

242-
// let endIdx = startIdx;
233+
if (startIdx === undefined) {
234+
throw Error(`Invalid offset ${scrollbarOffset} specified`);
235+
}
243236

244-
// while (scrollbarOffset < maxOffset && endIdx < this.model.length - 1) {
245-
// endIdx++;
246-
// scrollbarOffset += this.getSizeAndPositionForIndex(endIdx).size;
247-
// }
237+
const datum = this.getSizeAndPositionForIndex(startIdx);
248238

249-
// if (windowOverPaddingCount) {
250-
// startIdx = Math.max(0, startIdx - windowOverPaddingCount);
251-
// endIdx = Math.min(endIdx + windowOverPaddingCount, this.model.length - 1);
252-
// }
239+
scrollbarOffset = datum.offset + datum.size;
253240

254-
// return {
255-
// start: startIdx,
256-
// end: endIdx
257-
// };
258-
// }
241+
let endIdx = startIdx;
259242

243+
while (scrollbarOffset < maxOffset && endIdx < this.model.length - 1) {
244+
endIdx++;
245+
scrollbarOffset += this.getSizeAndPositionForIndex(endIdx).size;
246+
}
260247

248+
return {
249+
startOffset,
250+
endOffset: scrollbarOffset
251+
};
252+
}
261253

262254
// returns an index range
263255
getVisibleRangeIndex(

src/lib/new2/VirtualList.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import SizeAndPositionManager from './SizeAndPositionManager';
3434
import {
3535
ALIGNMENT,
36-
DIRECTION,
3736
SCROLL_BEHAVIOR,
3837
SCROLL_CHANGE_REASON,
3938
type AfterScrollEvent,
@@ -156,8 +155,13 @@
156155
const listStyle = $derived(clsx(!isDisabled && 'overflow:auto;', style));
157156
158157
const listInnerStyle = $derived.by(() => {
159-
const startOffset = curState.offset;
160-
const endOffset = startOffset + (isHorizontal ? clientWidth : clientHeight);
158+
const { offset } = curState;
159+
const p = sizeAndPositionManager.getVisibleRangeOffsets(
160+
isHorizontal ? clientWidth : clientHeight,
161+
offset
162+
);
163+
const startOffset = p.startOffset;
164+
const endOffset = p.endOffset;
161165
return clsx(
162166
!isTable && 'display:flex;',
163167
!isTable && ((!isHorizontal && 'flex-direction:column;') || 'flex-direction:row;'),

0 commit comments

Comments
 (0)