@@ -64,6 +64,8 @@ export class VscodeListItem extends VscElement {
6464 indent : 8 ,
6565 multiSelect : false ,
6666 selectedItems : new Set ( ) ,
67+ allItems : null ,
68+ itemListUpToDate : false ,
6769 focusedItem : null ,
6870 prevFocusedItem : null ,
6971 focusItem : ( ) => {
@@ -116,28 +118,59 @@ export class VscodeListItem extends VscElement {
116118 return ;
117119 }
118120
119- const prevFocusedLevel = + ( prevFocused . dataset . level ?? '' ) ;
120- const focusedLevel = + ( this . dataset . level ?? '' ) ;
121+ if ( ! this . _listContextState . itemListUpToDate ) {
122+ this . _listContextState . allItems =
123+ this . _listContextState . rootElement ! . querySelectorAll (
124+ 'vscode-list-item'
125+ ) ;
121126
122- let closestAncestor : VscodeListItem | null ;
127+ if ( this . _listContextState . allItems ) {
128+ this . _listContextState . allItems . forEach ( ( li , i ) => {
129+ li . dataset . score = i . toString ( ) ;
130+ } ) ;
131+ }
123132
124- if ( focusedLevel > prevFocusedLevel ) {
125- closestAncestor = findAncestorOnSpecificLevel ( this , prevFocusedLevel ) ;
126- } else if ( focusedLevel < prevFocusedLevel ) {
127- closestAncestor = findAncestorOnSpecificLevel ( prevFocused , focusedLevel ) ;
128- } else {
129- closestAncestor = prevFocused ;
133+ this . _listContextState . itemListUpToDate = true ;
130134 }
131135
132- const from = + ( closestAncestor ? .dataset . index ?? '' ) ;
133- const to = + ( this . dataset . index ?? '' ) ;
136+ let from = + ( prevFocused . dataset . score ?? - 1 ) ;
137+ let to = + ( this . dataset . score ?? - 1 ) ;
134138
135- for ( let i = from ; i <= to ; i ++ ) {
136- const li = this . parentElement ?. querySelector (
137- `:scope > [data-index="${ i } "]`
138- ) as VscodeListItem ;
139- selectItemAndAllVisibleDescendants ( li ) ;
139+ if ( from > to ) {
140+ [ from , to ] = [ to , from ] ;
140141 }
142+
143+ this . _listContextState . selectedItems . forEach ( ( li ) => ( li . selected = false ) ) ;
144+ this . _listContextState . selectedItems . clear ( ) ;
145+
146+ this . _selectItemsAndAllVisibleDescendants ( from , to ) ;
147+ // console.log(from, to);
148+ }
149+
150+ private _selectItemsAndAllVisibleDescendants ( from : number , to : number ) {
151+ let i = from ;
152+
153+ while ( i <= to ) {
154+ if ( this . _listContextState . allItems ) {
155+ const item = this . _listContextState . allItems [ i ] ;
156+
157+ if ( item . branch && ! item . open ) {
158+ item . selected = true ;
159+ const numChildren = item . querySelectorAll ( 'vscode-list-item' ) . length ;
160+ i += numChildren ;
161+ } else if ( item . branch && item . open ) {
162+ item . selected = true ;
163+ i += this . _selectItemsAndAllVisibleDescendants ( i + 1 , to ) ;
164+ } else {
165+ item . selected = true ;
166+ i += 1 ;
167+ }
168+ }
169+ }
170+
171+ console . log ( i ) ;
172+
173+ return i ;
141174 }
142175
143176 private _mainSlotChange ( ) {
@@ -156,6 +189,7 @@ export class VscodeListItem extends VscElement {
156189
157190 private _handleMainSlotChange = ( ) => {
158191 this . _mainSlotChange ( ) ;
192+ this . _listContextState . itemListUpToDate = false ;
159193 } ;
160194
161195 private _handleComponentFocus = ( ) => {
@@ -180,16 +214,19 @@ export class VscodeListItem extends VscElement {
180214
181215 if ( isShiftDown ) {
182216 this . _selectRange ( ) ;
183- return ;
184- }
185-
186- this . _selectItem ( isCtrlDown ) ;
217+ } else {
218+ this . _selectItem ( isCtrlDown ) ;
187219
188- if ( this . branch && ! ( this . _listContextState . multiSelect && isCtrlDown ) ) {
189- this . open = ! this . open ;
220+ if ( this . branch && ! ( this . _listContextState . multiSelect && isCtrlDown ) ) {
221+ this . open = ! this . open ;
222+ }
190223 }
191224
192225 this . _focusItem ( this ) ;
226+
227+ if ( ! isShiftDown ) {
228+ this . _listContextState . prevFocusedItem = this ;
229+ }
193230 } ;
194231
195232 connectedCallback ( ) : void {
0 commit comments