@humanspeak/svelte-virtual-list 0.4.6 → 0.5.1
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.
- package/README.md +16 -63
- package/dist/SvelteVirtualList.svelte +26 -746
- package/dist/SvelteVirtualList.svelte.d.ts +9 -58
- package/dist/index.d.ts +2 -2
- package/dist/reactive-list-manager/INTEGRATION_EXAMPLE.md +0 -5
- package/dist/types.d.ts +0 -11
- package/dist/utils/heightCalculation.d.ts +1 -2
- package/dist/utils/heightCalculation.js +2 -2
- package/dist/utils/scrollCalculation.d.ts +3 -9
- package/dist/utils/scrollCalculation.js +13 -72
- package/dist/utils/types.d.ts +0 -3
- package/dist/utils/virtualList.d.ts +12 -18
- package/dist/utils/virtualList.js +60 -128
- package/package.json +29 -29
|
@@ -12,49 +12,44 @@
|
|
|
12
12
|
* - Implemented debounced measurements
|
|
13
13
|
* - Created height averaging mechanism for performance
|
|
14
14
|
*
|
|
15
|
-
* 3.
|
|
16
|
-
* - Added bottomToTop mode
|
|
17
|
-
* - Solved complex initialization issues with flexbox
|
|
18
|
-
* - Implemented careful scroll position management
|
|
19
|
-
*
|
|
20
|
-
* 4. Performance Optimizations ✓
|
|
15
|
+
* 3. Performance Optimizations ✓
|
|
21
16
|
* - Added element recycling through keyed each blocks
|
|
22
17
|
* - Implemented RAF for smooth animations
|
|
23
18
|
* - Optimized DOM updates with transform translations
|
|
24
19
|
*
|
|
25
|
-
*
|
|
20
|
+
* 4. Stability Improvements ✓
|
|
26
21
|
* - Added ResizeObserver for responsive updates
|
|
27
22
|
* - Implemented proper cleanup on component destruction
|
|
28
23
|
* - Added debug mode for development assistance
|
|
29
24
|
*
|
|
30
|
-
*
|
|
25
|
+
* 5. Large Dataset Optimizations ✓
|
|
31
26
|
* - Implemented chunked processing for 10k+ items
|
|
32
27
|
* - Added progressive initialization system
|
|
33
28
|
* - Deferred height calculations for better initial load
|
|
34
29
|
* - Optimized memory usage for large lists
|
|
35
30
|
* - Added progress tracking for initialization
|
|
36
31
|
*
|
|
37
|
-
*
|
|
32
|
+
* 6. Size Management Improvements ✓
|
|
38
33
|
* - Implemented height caching system for measured items
|
|
39
34
|
* - Added smart height estimation for unmeasured items
|
|
40
35
|
* - Optimized resize handling with debouncing
|
|
41
36
|
* - Added height recalculation on content changes
|
|
42
37
|
* - Implemented progressive height adjustments
|
|
43
38
|
*
|
|
44
|
-
*
|
|
39
|
+
* 7. Code Quality & Maintainability ✓
|
|
45
40
|
* - Extracted debug utilities for better testing
|
|
46
41
|
* - Improved type safety throughout
|
|
47
42
|
* - Added comprehensive documentation
|
|
48
43
|
* - Optimized debug output to reduce noise
|
|
49
44
|
*
|
|
50
|
-
*
|
|
45
|
+
* 8. Architecture Refactoring ✓
|
|
51
46
|
* - Extracted scroll calculation logic to scrollCalculation.ts utility
|
|
52
47
|
* - Extracted ResizeObserver utilities to resizeObserver.ts
|
|
53
48
|
* - Added comprehensive test coverage for extracted utilities
|
|
54
49
|
* - Improved separation of concerns and maintainability
|
|
55
50
|
* - Simplified initialization (removed unnecessary chunked processing)
|
|
56
51
|
*
|
|
57
|
-
*
|
|
52
|
+
* 9. Future Improvements (Planned)
|
|
58
53
|
* - Add horizontal scrolling support
|
|
59
54
|
* - Implement variable-sized item caching
|
|
60
55
|
* - Add keyboard navigation support
|
|
@@ -62,7 +57,6 @@
|
|
|
62
57
|
* - Add accessibility enhancements
|
|
63
58
|
*
|
|
64
59
|
* Technical Challenges Solved:
|
|
65
|
-
* - Bottom-to-top scrolling in flexbox layouts
|
|
66
60
|
* - Dynamic height calculations without layout thrashing
|
|
67
61
|
* - Smooth scrolling on various devices
|
|
68
62
|
* - Memory management for large lists
|
|
@@ -92,26 +86,6 @@ import { type SvelteVirtualListProps, type SvelteVirtualListScrollOptions } from
|
|
|
92
86
|
declare function $$render<TItem = unknown>(): {
|
|
93
87
|
props: SvelteVirtualListProps<TItem>;
|
|
94
88
|
exports: {
|
|
95
|
-
/**
|
|
96
|
-
* Runs a batch of updates with scroll corrections coalesced until the batch completes.
|
|
97
|
-
*
|
|
98
|
-
* Use this method when making multiple changes to the items array to prevent
|
|
99
|
-
* intermediate scroll corrections. The scroll position reconciliation is deferred
|
|
100
|
-
* until the batch exits, ensuring smooth visual updates.
|
|
101
|
-
*
|
|
102
|
-
* @param {() => void} fn - The function containing batch updates to execute.
|
|
103
|
-
* @returns {void}
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```typescript
|
|
107
|
-
* // Add multiple items without intermediate scroll corrections
|
|
108
|
-
* list.runInBatch(() => {
|
|
109
|
-
* items.push(newItem1);
|
|
110
|
-
* items.push(newItem2);
|
|
111
|
-
* items.push(newItem3);
|
|
112
|
-
* });
|
|
113
|
-
* ```
|
|
114
|
-
*/ runInBatch: (fn: () => void) => void;
|
|
115
89
|
/**
|
|
116
90
|
* Scrolls the virtual list to the item at the given index.
|
|
117
91
|
*
|
|
@@ -179,26 +153,6 @@ declare class __sveltets_Render<TItem = unknown> {
|
|
|
179
153
|
slots(): ReturnType<typeof $$render<TItem>>['slots'];
|
|
180
154
|
bindings(): "";
|
|
181
155
|
exports(): {
|
|
182
|
-
/**
|
|
183
|
-
* Runs a batch of updates with scroll corrections coalesced until the batch completes.
|
|
184
|
-
*
|
|
185
|
-
* Use this method when making multiple changes to the items array to prevent
|
|
186
|
-
* intermediate scroll corrections. The scroll position reconciliation is deferred
|
|
187
|
-
* until the batch exits, ensuring smooth visual updates.
|
|
188
|
-
*
|
|
189
|
-
* @param {() => void} fn - The function containing batch updates to execute.
|
|
190
|
-
* @returns {void}
|
|
191
|
-
*
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* // Add multiple items without intermediate scroll corrections
|
|
195
|
-
* list.runInBatch(() => {
|
|
196
|
-
* items.push(newItem1);
|
|
197
|
-
* items.push(newItem2);
|
|
198
|
-
* items.push(newItem3);
|
|
199
|
-
* });
|
|
200
|
-
* ```
|
|
201
|
-
*/ runInBatch: (fn: () => void) => void;
|
|
202
156
|
/**
|
|
203
157
|
* Scrolls the virtual list to the item at the given index.
|
|
204
158
|
*
|
|
@@ -268,14 +222,13 @@ interface $$IsomorphicComponent {
|
|
|
268
222
|
* SvelteVirtualList
|
|
269
223
|
*
|
|
270
224
|
* A high-performance, memory-efficient virtualized list component for Svelte 5.
|
|
271
|
-
* Renders only visible items plus a buffer, supporting dynamic item heights
|
|
272
|
-
*
|
|
225
|
+
* Renders only visible items plus a buffer, supporting dynamic item heights
|
|
226
|
+
* and programmatic control.
|
|
273
227
|
*
|
|
274
228
|
* =============================
|
|
275
229
|
* == Key Features ==
|
|
276
230
|
* =============================
|
|
277
231
|
* - Dynamic item height support (no fixed height required)
|
|
278
|
-
* - Top-to-bottom and bottom-to-top (chat-style) scrolling
|
|
279
232
|
* - Programmatic scrolling with flexible alignment (top, bottom, auto)
|
|
280
233
|
* - Smooth scrolling and buffer size configuration
|
|
281
234
|
* - SSR compatible and hydration-friendly
|
|
@@ -291,7 +244,6 @@ interface $$IsomorphicComponent {
|
|
|
291
244
|
* ```svelte
|
|
292
245
|
* <SvelteVirtualList
|
|
293
246
|
* items={data}
|
|
294
|
-
* mode="bottomToTop"
|
|
295
247
|
* bind:this={listRef}
|
|
296
248
|
* >
|
|
297
249
|
* {#snippet renderItem(item)}
|
|
@@ -309,7 +261,6 @@ interface $$IsomorphicComponent {
|
|
|
309
261
|
* - Handles resize events and dynamic content changes
|
|
310
262
|
* - Optimized for very large lists through virtualization
|
|
311
263
|
* - Modular architecture with extracted utility functions
|
|
312
|
-
* - Bi-directional support: mode="topToBottom" or "bottomToTop"
|
|
313
264
|
* - Designed for extensibility and easy debugging
|
|
314
265
|
*
|
|
315
266
|
* =============================
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import SvelteVirtualList from './SvelteVirtualList.svelte';
|
|
2
|
-
import type { SvelteVirtualListDebugInfo,
|
|
3
|
-
export type { SvelteVirtualListDebugInfo,
|
|
2
|
+
import type { SvelteVirtualListDebugInfo, SvelteVirtualListProps, SvelteVirtualListScrollAlign, SvelteVirtualListScrollOptions } from './types.js';
|
|
3
|
+
export type { SvelteVirtualListDebugInfo, SvelteVirtualListProps, SvelteVirtualListScrollAlign, SvelteVirtualListScrollOptions };
|
|
4
4
|
export { ReactiveListManager } from './reactive-list-manager/index.js';
|
|
5
5
|
export type { ListManagerConfig } from './reactive-list-manager/index.js';
|
|
6
6
|
export { formatBytes, getCurrentFps, getMemoryUsage, isPerfEnabled, measureAsync, measureSync, perfMetrics, recordDuration, startFpsTracking, startMeasure, stopFpsTracking } from './utils/perfMetrics.js';
|
|
@@ -56,11 +56,6 @@ const updateHeight = () => {
|
|
|
56
56
|
// }))
|
|
57
57
|
// heightManager.processDirtyHeights(heightChanges)
|
|
58
58
|
|
|
59
|
-
// Handle scroll correction (bottomToTop mode)
|
|
60
|
-
if (result.heightChanges.length > 0 && mode === 'bottomToTop') {
|
|
61
|
-
handleHeightChangesScrollCorrection(result.heightChanges)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
59
|
// ... rest of callback logic
|
|
65
60
|
}
|
|
66
61
|
)
|
package/dist/types.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
-
/**
|
|
3
|
-
* Defines the scroll direction and rendering mode for the virtual list.
|
|
4
|
-
*
|
|
5
|
-
* @typedef {'topToBottom' | 'bottomToTop'} SvelteVirtualListMode
|
|
6
|
-
*/
|
|
7
|
-
export type SvelteVirtualListMode = 'topToBottom' | 'bottomToTop';
|
|
8
2
|
/**
|
|
9
3
|
* Configuration properties for the SvelteVirtualList component.
|
|
10
4
|
*
|
|
@@ -46,11 +40,6 @@ export type SvelteVirtualListProps<TItem = any> = {
|
|
|
46
40
|
* CSS class to apply to individual item containers.
|
|
47
41
|
*/
|
|
48
42
|
itemsClass?: string;
|
|
49
|
-
/**
|
|
50
|
-
* Determines the scroll and render direction.
|
|
51
|
-
* @default 'topToBottom'
|
|
52
|
-
*/
|
|
53
|
-
mode?: SvelteVirtualListMode;
|
|
54
43
|
/**
|
|
55
44
|
* Svelte snippet function that defines how each item should be rendered. Receives the item and its index as arguments.
|
|
56
45
|
*/
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { SvelteVirtualListMode } from '../types.js';
|
|
2
1
|
/**
|
|
3
2
|
* Calculates and updates the average height of visible items with debouncing.
|
|
4
3
|
*
|
|
@@ -84,4 +83,4 @@ export declare const calculateAverageHeightDebounced: (isCalculatingHeight: bool
|
|
|
84
83
|
newHeight: number;
|
|
85
84
|
delta: number;
|
|
86
85
|
}>;
|
|
87
|
-
}) => void, debounceTime: number, dirtyItems: Set<number>, currentTotalHeight?: number, currentValidCount?: number
|
|
86
|
+
}) => void, debounceTime: number, dirtyItems: Set<number>, currentTotalHeight?: number, currentValidCount?: number) => NodeJS.Timeout | null;
|
|
@@ -71,7 +71,7 @@ import { BROWSER } from 'esm-env';
|
|
|
71
71
|
*/
|
|
72
72
|
export const calculateAverageHeightDebounced = (isCalculatingHeight, heightUpdateTimeout, visibleItems, itemElements, heightCache, lastMeasuredIndex, calculatedItemHeight,
|
|
73
73
|
/* trunk-ignore(eslint/no-unused-vars) */
|
|
74
|
-
onUpdate, debounceTime, dirtyItems, currentTotalHeight = 0, currentValidCount = 0
|
|
74
|
+
onUpdate, debounceTime, dirtyItems, currentTotalHeight = 0, currentValidCount = 0) => {
|
|
75
75
|
if (!BROWSER || isCalculatingHeight)
|
|
76
76
|
return null;
|
|
77
77
|
const currentIndex = visibleItems.start;
|
|
@@ -80,7 +80,7 @@ onUpdate, debounceTime, dirtyItems, currentTotalHeight = 0, currentValidCount =
|
|
|
80
80
|
if (heightUpdateTimeout)
|
|
81
81
|
clearTimeout(heightUpdateTimeout);
|
|
82
82
|
return setTimeout(() => {
|
|
83
|
-
const { newHeight, newLastMeasuredIndex, updatedHeightCache, clearedDirtyItems, newTotalHeight, newValidCount, heightChanges } = calculateAverageHeight(itemElements, visibleItems, heightCache, calculatedItemHeight, dirtyItems, currentTotalHeight, currentValidCount
|
|
83
|
+
const { newHeight, newLastMeasuredIndex, updatedHeightCache, clearedDirtyItems, newTotalHeight, newValidCount, heightChanges } = calculateAverageHeight(itemElements, visibleItems, heightCache, calculatedItemHeight, dirtyItems, currentTotalHeight, currentValidCount);
|
|
84
84
|
if (Math.abs(newHeight - calculatedItemHeight) > 1 || dirtyItems.size > 0) {
|
|
85
85
|
onUpdate({
|
|
86
86
|
newHeight,
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SvelteVirtualListScrollAlign } from '../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Calculates the scroll target for aligning an item to a specific edge.
|
|
4
4
|
*
|
|
5
|
-
* This helper consolidates the shared alignment logic between bottomToTop
|
|
6
|
-
* and topToBottom scroll calculations, reducing code duplication.
|
|
7
|
-
*
|
|
8
5
|
* @param {number} itemTop - The top position of the item in pixels
|
|
9
6
|
* @param {number} itemBottom - The bottom position of the item in pixels
|
|
10
7
|
* @param {number} scrollTop - Current scroll position in pixels
|
|
@@ -38,7 +35,6 @@ export declare const alignVisibleToNearestEdge: (itemTop: number, itemBottom: nu
|
|
|
38
35
|
* Parameters for calculating scroll target position
|
|
39
36
|
*/
|
|
40
37
|
export interface ScrollTargetParams {
|
|
41
|
-
mode: SvelteVirtualListMode;
|
|
42
38
|
align: SvelteVirtualListScrollAlign;
|
|
43
39
|
targetIndex: number;
|
|
44
40
|
itemsLength: number;
|
|
@@ -52,9 +48,8 @@ export interface ScrollTargetParams {
|
|
|
52
48
|
/**
|
|
53
49
|
* Calculates the target scroll position for scrolling to a specific item index.
|
|
54
50
|
*
|
|
55
|
-
* This function handles
|
|
56
|
-
*
|
|
57
|
-
* viewport state and calculates the optimal scroll position.
|
|
51
|
+
* This function handles different alignment options (auto, top, bottom, nearest)
|
|
52
|
+
* and calculates the optimal scroll position based on the current viewport state.
|
|
58
53
|
*
|
|
59
54
|
* @param params - Parameters for scroll target calculation
|
|
60
55
|
* @returns The target scroll position in pixels, or null if no scroll is needed
|
|
@@ -62,7 +57,6 @@ export interface ScrollTargetParams {
|
|
|
62
57
|
* @example
|
|
63
58
|
* ```typescript
|
|
64
59
|
* const scrollTarget = calculateScrollTarget({
|
|
65
|
-
* mode: 'topToBottom',
|
|
66
60
|
* align: 'auto',
|
|
67
61
|
* targetIndex: 100,
|
|
68
62
|
* itemsLength: 1000,
|
|
@@ -2,9 +2,6 @@ import { clampValue, getScrollOffsetForIndex } from './virtualList.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Calculates the scroll target for aligning an item to a specific edge.
|
|
4
4
|
*
|
|
5
|
-
* This helper consolidates the shared alignment logic between bottomToTop
|
|
6
|
-
* and topToBottom scroll calculations, reducing code duplication.
|
|
7
|
-
*
|
|
8
5
|
* @param {number} itemTop - The top position of the item in pixels
|
|
9
6
|
* @param {number} itemBottom - The bottom position of the item in pixels
|
|
10
7
|
* @param {number} scrollTop - Current scroll position in pixels
|
|
@@ -64,9 +61,8 @@ export const alignVisibleToNearestEdge = (itemTop, itemBottom, scrollTop, viewpo
|
|
|
64
61
|
/**
|
|
65
62
|
* Calculates the target scroll position for scrolling to a specific item index.
|
|
66
63
|
*
|
|
67
|
-
* This function handles
|
|
68
|
-
*
|
|
69
|
-
* viewport state and calculates the optimal scroll position.
|
|
64
|
+
* This function handles different alignment options (auto, top, bottom, nearest)
|
|
65
|
+
* and calculates the optimal scroll position based on the current viewport state.
|
|
70
66
|
*
|
|
71
67
|
* @param params - Parameters for scroll target calculation
|
|
72
68
|
* @returns The target scroll position in pixels, or null if no scroll is needed
|
|
@@ -74,7 +70,6 @@ export const alignVisibleToNearestEdge = (itemTop, itemBottom, scrollTop, viewpo
|
|
|
74
70
|
* @example
|
|
75
71
|
* ```typescript
|
|
76
72
|
* const scrollTarget = calculateScrollTarget({
|
|
77
|
-
* mode: 'topToBottom',
|
|
78
73
|
* align: 'auto',
|
|
79
74
|
* targetIndex: 100,
|
|
80
75
|
* itemsLength: 1000,
|
|
@@ -92,71 +87,17 @@ export const alignVisibleToNearestEdge = (itemTop, itemBottom, scrollTop, viewpo
|
|
|
92
87
|
* ```
|
|
93
88
|
*/
|
|
94
89
|
export const calculateScrollTarget = (params) => {
|
|
95
|
-
const {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
heightCache
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
return calculateTopToBottomScrollTarget({
|
|
111
|
-
align,
|
|
112
|
-
targetIndex,
|
|
113
|
-
calculatedItemHeight,
|
|
114
|
-
height,
|
|
115
|
-
scrollTop,
|
|
116
|
-
firstVisibleIndex,
|
|
117
|
-
lastVisibleIndex,
|
|
118
|
-
heightCache
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* Calculates the target scroll position for bottom-to-top mode.
|
|
124
|
-
*
|
|
125
|
-
* In bottom-to-top mode, items are rendered from the bottom of the viewport upward,
|
|
126
|
-
* which requires different scroll calculations than the standard top-to-bottom mode.
|
|
127
|
-
* This function handles the coordinate system translation and alignment logic.
|
|
128
|
-
*
|
|
129
|
-
* @param {BottomToTopScrollParams} params - Parameters for scroll calculation.
|
|
130
|
-
* @returns {number | null} The target scroll position in pixels, or null if no
|
|
131
|
-
* scroll is needed (item already visible with 'nearest' alignment).
|
|
132
|
-
*/
|
|
133
|
-
const calculateBottomToTopScrollTarget = (params) => {
|
|
134
|
-
const { align, targetIndex, itemsLength, calculatedItemHeight, height, scrollTop, firstVisibleIndex, lastVisibleIndex, heightCache } = params;
|
|
135
|
-
// Use getScrollOffsetForIndex for accurate positioning with height cache
|
|
136
|
-
const totalHeight = getScrollOffsetForIndex(heightCache, calculatedItemHeight, itemsLength);
|
|
137
|
-
const itemOffset = getScrollOffsetForIndex(heightCache, calculatedItemHeight, targetIndex);
|
|
138
|
-
const itemHeight = calculatedItemHeight;
|
|
139
|
-
// Calculate item boundaries in bottomToTop coordinate space
|
|
140
|
-
const itemTop = totalHeight - (itemOffset + itemHeight);
|
|
141
|
-
const itemBottom = totalHeight - itemOffset;
|
|
142
|
-
if (align === 'auto') {
|
|
143
|
-
// If item is above the viewport, align to top
|
|
144
|
-
if (targetIndex < firstVisibleIndex) {
|
|
145
|
-
return alignToEdge(itemTop, itemBottom, scrollTop, height, 'top');
|
|
146
|
-
}
|
|
147
|
-
else if (targetIndex > lastVisibleIndex - 1) {
|
|
148
|
-
// In bottomToTop, "below" means higher indices that need HIGHER scrollTop
|
|
149
|
-
return alignToEdge(itemTop, itemBottom, scrollTop, height, 'bottom');
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
// Item is visible - align to nearest edge (always returns a value)
|
|
153
|
-
return alignVisibleToNearestEdge(itemTop, itemBottom, scrollTop, height);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (align === 'top' || align === 'bottom' || align === 'nearest') {
|
|
157
|
-
return alignToEdge(itemTop, itemBottom, scrollTop, height, align);
|
|
158
|
-
}
|
|
159
|
-
return null;
|
|
90
|
+
const { align, targetIndex, calculatedItemHeight, height, scrollTop, firstVisibleIndex, lastVisibleIndex, heightCache } = params;
|
|
91
|
+
return calculateTopToBottomScrollTarget({
|
|
92
|
+
align,
|
|
93
|
+
targetIndex,
|
|
94
|
+
calculatedItemHeight,
|
|
95
|
+
height,
|
|
96
|
+
scrollTop,
|
|
97
|
+
firstVisibleIndex,
|
|
98
|
+
lastVisibleIndex,
|
|
99
|
+
heightCache
|
|
100
|
+
});
|
|
160
101
|
};
|
|
161
102
|
/**
|
|
162
103
|
* Calculates the target scroll position for top-to-bottom mode.
|
package/dist/utils/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { SvelteVirtualListMode } from '../types.js';
|
|
2
1
|
/**
|
|
3
2
|
* Represents the internal state of a virtual list component.
|
|
4
3
|
*
|
|
@@ -7,7 +6,6 @@ import type { SvelteVirtualListMode } from '../types.js';
|
|
|
7
6
|
* the DOM elements involved and the current scroll metrics.
|
|
8
7
|
*
|
|
9
8
|
* @property {boolean} initialized - Indicates whether the virtual list has completed its initial setup
|
|
10
|
-
* @property {SvelteVirtualListMode} mode - Defines the scrolling behavior ('topToBottom' or 'bottomToTop')
|
|
11
9
|
* @property {HTMLElement | null} containerElement - Reference to the outer container DOM element
|
|
12
10
|
* @property {HTMLElement | null} viewportElement - Reference to the viewport DOM element that clips visible content
|
|
13
11
|
* @property {number} calculatedItemHeight - The computed height of each list item in pixels
|
|
@@ -16,7 +14,6 @@ import type { SvelteVirtualListMode } from '../types.js';
|
|
|
16
14
|
*/
|
|
17
15
|
export type VirtualListState = {
|
|
18
16
|
initialized: boolean;
|
|
19
|
-
mode: SvelteVirtualListMode;
|
|
20
17
|
containerElement: HTMLElement | null;
|
|
21
18
|
viewportElement: HTMLElement | null;
|
|
22
19
|
calculatedItemHeight: number;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SvelteVirtualListPreviousVisibleRange } from '../types.js';
|
|
2
2
|
import type { VirtualListSetters, VirtualListState } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Validates a height value and returns it if valid, otherwise returns the fallback.
|
|
@@ -58,17 +58,16 @@ export interface VisibleRangeOptions {
|
|
|
58
58
|
totalItems: number;
|
|
59
59
|
/** Number of extra items to render outside the visible area. */
|
|
60
60
|
bufferSize: number;
|
|
61
|
-
mode: SvelteVirtualListMode;
|
|
62
61
|
/** Pre-calculated total content height; defaults to `totalItems * itemHeight`. */
|
|
63
62
|
totalContentHeight?: number;
|
|
64
|
-
/** Measured item heights keyed by index; used
|
|
63
|
+
/** Measured item heights keyed by index; used to walk actual heights instead of dividing by average. */
|
|
65
64
|
heightCache?: Record<number, number>;
|
|
66
65
|
}
|
|
67
66
|
/**
|
|
68
67
|
* Determines the range of items that should be rendered in the virtual list.
|
|
69
68
|
*
|
|
70
|
-
* Calculates which items should be visible based on the current scroll position
|
|
71
|
-
* viewport size
|
|
69
|
+
* Calculates which items should be visible based on the current scroll position
|
|
70
|
+
* and viewport size. Includes a buffer zone to enable smooth scrolling
|
|
72
71
|
* and prevent visible gaps during rapid scroll movements.
|
|
73
72
|
*
|
|
74
73
|
* @param options - Inputs used to compute the visible range (see {@link VisibleRangeOptions}).
|
|
@@ -81,36 +80,31 @@ export interface VisibleRangeOptions {
|
|
|
81
80
|
* viewportHeight: 400,
|
|
82
81
|
* itemHeight: 40,
|
|
83
82
|
* totalItems: 1000,
|
|
84
|
-
* bufferSize: 2
|
|
85
|
-
* mode: 'topToBottom'
|
|
83
|
+
* bufferSize: 2
|
|
86
84
|
* })
|
|
87
85
|
* // range => { start: 3, end: 15 }
|
|
88
86
|
* ```
|
|
89
87
|
*/
|
|
90
|
-
export declare const calculateVisibleRange: ({ scrollTop, viewportHeight, itemHeight, totalItems, bufferSize,
|
|
88
|
+
export declare const calculateVisibleRange: ({ scrollTop, viewportHeight, itemHeight, totalItems, bufferSize, totalContentHeight, heightCache }: VisibleRangeOptions) => SvelteVirtualListPreviousVisibleRange;
|
|
91
89
|
/**
|
|
92
90
|
* Calculates the CSS transform value for positioning the virtual list items.
|
|
93
91
|
*
|
|
94
92
|
* This function determines the vertical offset needed to position the visible items
|
|
95
|
-
* correctly within the viewport
|
|
96
|
-
* visible range.
|
|
93
|
+
* correctly within the viewport.
|
|
97
94
|
*
|
|
98
|
-
* @param {SvelteVirtualListMode} mode - Scroll direction mode
|
|
99
95
|
* @param {number} totalItems - Total number of items in the list
|
|
100
|
-
* @param {number} visibleEnd - Index of the last visible item
|
|
101
96
|
* @param {number} visibleStart - Index of the first visible item
|
|
102
97
|
* @param {number} itemHeight - Height of each list item in pixels
|
|
103
|
-
* @param {number}
|
|
98
|
+
* @param {Record<number, number>} [heightCache] - Cache of measured item heights
|
|
104
99
|
* @returns {number} The calculated transform Y value in pixels
|
|
105
100
|
*/
|
|
106
|
-
export declare const calculateTransformY: (
|
|
101
|
+
export declare const calculateTransformY: (totalItems: number, visibleStart: number, itemHeight: number, heightCache?: Record<number, number>) => number;
|
|
107
102
|
/**
|
|
108
103
|
* Updates the virtual list's height and scroll position when necessary.
|
|
109
104
|
*
|
|
110
105
|
* This function handles dynamic updates to the virtual list's dimensions and scroll
|
|
111
|
-
* position, particularly important when the container size changes
|
|
112
|
-
*
|
|
113
|
-
* height and scroll position.
|
|
106
|
+
* position, particularly important when the container size changes. When immediate
|
|
107
|
+
* is true, it forces an immediate update of the height and scroll position.
|
|
114
108
|
*
|
|
115
109
|
* @param {VirtualListState} state - Current state of the virtual list
|
|
116
110
|
* @param {VirtualListSetters} setters - State setters for updating list properties
|
|
@@ -147,7 +141,7 @@ export declare const updateHeightAndScroll: (state: VirtualListState, setters: V
|
|
|
147
141
|
export declare const calculateAverageHeight: (itemElements: HTMLElement[], visibleRange: {
|
|
148
142
|
start: number;
|
|
149
143
|
end: number;
|
|
150
|
-
}, heightCache: Record<number, number>, currentItemHeight: number, dirtyItems: Set<number>, currentTotalHeight?: number, currentValidCount?: number
|
|
144
|
+
}, heightCache: Record<number, number>, currentItemHeight: number, dirtyItems: Set<number>, currentTotalHeight?: number, currentValidCount?: number) => {
|
|
151
145
|
newHeight: number;
|
|
152
146
|
newLastMeasuredIndex: number;
|
|
153
147
|
updatedHeightCache: Record<number, number>;
|