@@ -279,6 +279,99 @@ useEventListener('keydown', (event) => {
279
279
}
280
280
})
281
281
282
+ useEventListener (' keydown' , (event ) => {
283
+ if (! activeComponentId .value )
284
+ return
285
+
286
+ switch (event .key ) {
287
+ case ' ArrowRight' :
288
+ handleArrowRight ()
289
+ break
290
+ case ' ArrowLeft' :
291
+ handleArrowLeft ()
292
+ break
293
+ case ' ArrowDown' :
294
+ handleArrowDown ()
295
+ event .preventDefault ()
296
+ break
297
+ case ' ArrowUp' :
298
+ handleArrowUp ()
299
+ event .preventDefault ()
300
+ break
301
+ case ' ' :
302
+ case ' Enter' : {
303
+ handleEnter ()
304
+ event .preventDefault ()
305
+ break
306
+ }
307
+ }
308
+ })
309
+
310
+ function handleArrowRight() {
311
+ if (! expandedTreeNodes .value .includes (activeComponentId .value )) {
312
+ expandedTreeNodes .value .push (activeComponentId .value )
313
+ }
314
+ }
315
+
316
+ function handleArrowLeft() {
317
+ if (expandedTreeNodes .value .includes (activeComponentId .value )) {
318
+ expandedTreeNodes .value .splice (expandedTreeNodes .value .indexOf (activeComponentId .value ), 1 )
319
+ }
320
+ }
321
+
322
+ function handleArrowDown() {
323
+ const activeComponentIdIndex = flattenedTreeNodesIds .value .indexOf (activeComponentId .value )
324
+ const isActiveComponentExpanded = expandedTreeNodes .value .includes (activeComponentId .value )
325
+
326
+ if (isActiveComponentExpanded && activeComponentIdIndex >= 0 && activeComponentIdIndex < flattenedTreeNodesIds .value .length - 1 ) {
327
+ activeComponentId .value = flattenedTreeNodesIds .value [activeComponentIdIndex + 1 ]
328
+ }
329
+ else if (activeComponentIdIndex === 0 ) {
330
+ return false
331
+ }
332
+ else {
333
+ const listIndex = treeNodeLinkedList .value .findIndex (arr => arr .includes (activeComponentId .value ))
334
+ const nextTree = treeNodeLinkedList .value [listIndex + 1 ]
335
+ if (nextTree ) {
336
+ activeComponentId .value = nextTree [1 ]
337
+ }
338
+ }
339
+ }
340
+
341
+ function handleArrowUp() {
342
+ const isSubTreeRoot = treeNodeLinkedList .value .some (chain => chain [1 ] === activeComponentId .value )
343
+
344
+ if (isSubTreeRoot ) {
345
+ activeComponentId .value = getPrevExpandedNode ()
346
+ }
347
+ else {
348
+ const currentIndex = flattenedTreeNodesIds .value .indexOf (activeComponentId .value )
349
+ if (currentIndex > 0 ) {
350
+ activeComponentId .value = flattenedTreeNodesIds .value [currentIndex - 1 ]
351
+ }
352
+ }
353
+ }
354
+
355
+ function handleEnter() {
356
+ const node = flattenedTreeNodes .value .find (item => item .id === activeComponentId .value )
357
+ if (! node ?.children ?.length )
358
+ return
359
+
360
+ const index = expandedTreeNodes .value .indexOf (activeComponentId .value )
361
+ if (index === - 1 )
362
+ expandedTreeNodes .value .push (activeComponentId .value )
363
+ else expandedTreeNodes .value .splice (index , 1 )
364
+ }
365
+
366
+ function getPrevExpandedNode() {
367
+ const list = treeNodeLinkedList .value
368
+ const listIndex = list .findIndex ((chain : string []) => chain .includes (activeComponentId .value ))
369
+ if (listIndex > 0 ) {
370
+ return list [listIndex - 1 ].find (id => ! expandedTreeNodes .value .includes (id )) || list [listIndex - 1 ][1 ]
371
+ }
372
+ return list [listIndex ][0 ]
373
+ }
374
+
282
375
function scrollToComponent() {
283
376
rpc .value .scrollToComponent (activeComponentId .value )
284
377
}
0 commit comments