File tree Expand file tree Collapse file tree 3 files changed +33
-25
lines changed
Expand file tree Collapse file tree 3 files changed +33
-25
lines changed Original file line number Diff line number Diff line change 1111 createScroller ,
1212 createVirtualStore ,
1313 getScrollSize as _getScrollSize ,
14+ sort ,
1415 } from " ../core/index.js" ;
15- import { defaultGetKey , styleToString , iterRange } from " ./utils.js" ;
16+ import { defaultGetKey , styleToString } from " ./utils.js" ;
1617 import ListItem from " ./ListItem.svelte" ;
1718 import type {
1819 VirtualizerHandle ,
6667 let isScrolling = $derived (stateVersion && store .$isScrolling ());
6768 let totalSize = $derived (stateVersion && store .$getTotalSize ());
6869
70+ let indexes = $derived .by (() => {
71+ const [start, end] = range ;
72+ const arr: number [] = [];
73+ if (keepMounted ) {
74+ const mounted = new Set (keepMounted );
75+ for (let i = start ; i <= end ; i ++ ) {
76+ mounted .add (i );
77+ }
78+ for (const index of sort ([... mounted ])) {
79+ arr .push (index );
80+ }
81+ } else {
82+ for (let i = start ; i <= end ; i ++ ) {
83+ arr .push (i );
84+ }
85+ }
86+ return arr ;
87+ });
88+
6989 onMount (() => {
7090 const assignRef = (scrollable : HTMLElement ) => {
7191 resizer .$observeRoot (scrollable );
146166 Customizable list virtualizer for advanced usage. See {@link VirtualizerProps} and {@link VirtualizerHandle}.
147167-->
148168<svelte:element this ={as } bind:this ={containerRef } style ={containerStyle }>
149- {#each iterRange ( range , keepMounted ) as index (getKey (data [index ]! , index ))}
169+ {#each indexes as index (getKey (data [index ]! , index ))}
150170 {@const item = data [index ]! }
151171 <ListItem
152172 {children }
Original file line number Diff line number Diff line change 1111 createWindowResizer ,
1212 createWindowScroller ,
1313 } from " ../core/index.js" ;
14- import { defaultGetKey , iterRange , styleToString } from " ./utils.js" ;
14+ import { defaultGetKey , styleToString } from " ./utils.js" ;
1515 import ListItem from " ./ListItem.svelte" ;
1616 import type {
1717 WindowVirtualizerHandle ,
6161 let isScrolling = $derived (stateVersion && store .$isScrolling ());
6262 let totalSize = $derived (stateVersion && store .$getTotalSize ());
6363
64+ let indexes = $derived .by (() => {
65+ const [start, end] = range ;
66+ const arr: number [] = [];
67+ for (let i = start ; i <= end ; i ++ ) {
68+ arr .push (i );
69+ }
70+ return arr ;
71+ });
72+
6473 onMount (() => {
6574 resizer .$observeRoot (containerRef ! );
6675 scroller .$observe (containerRef ! );
117126 {@link Virtualizer} controlled by the window scrolling. See {@link WindowVirtualizerProps} and {@link WindowVirtualizerHandle}.
118127-->
119128<div bind:this ={containerRef } style ={containerStyle }>
120- {#each iterRange ( range ) as index (getKey (data [index ]! , index ))}
129+ {#each indexes as index (getKey (data [index ]! , index ))}
121130 {@const item = data [index ]! }
122131 <ListItem
123132 {children }
Original file line number Diff line number Diff line change 1- import { sort , type ItemsRange } from "../core/index.js" ;
2-
31export const styleToString = (
42 obj : Record < string , string | undefined >
53) : string => {
@@ -13,22 +11,3 @@ export const styleToString = (
1311} ;
1412
1513export const defaultGetKey = ( _data : unknown , i : number ) => "_" + i ;
16-
17- export function * iterRange (
18- [ start , end ] : ItemsRange ,
19- keepMounted ?: readonly number [ ]
20- ) {
21- if ( keepMounted ) {
22- const mounted = new Set ( keepMounted ) ;
23- for ( let i = start ; i <= end ; i ++ ) {
24- mounted . add ( i ) ;
25- }
26- for ( const index of sort ( [ ...mounted ] ) ) {
27- yield index ;
28- }
29- } else {
30- for ( let i = start ; i <= end ; i ++ ) {
31- yield i ;
32- }
33- }
34- }
You can’t perform that action at this time.
0 commit comments