@@ -11,56 +11,58 @@ const CommandPalette = {
1111 this . highlightSelection ( this . search_results [ this . selected ] ) ;
1212 } ;
1313
14- document . addEventListener ( "keydown" , ( e ) => {
15- if ( e . key === "k" && e . metaKey ) {
16- e . preventDefault ( ) ;
17- liveSocket . execJS ( this . el , this . el . getAttribute ( "data-show-modal" ) ) ;
14+ const handleKeyDown = ( e ) => {
15+ switch ( e . key ) {
16+ case "ArrowUp" :
17+ e . preventDefault ( ) ;
1818
19- fetchDomNodes ( ) ;
20- } else {
21- switch ( e . key ) {
22- case "ArrowUp" :
23- e . preventDefault ( ) ;
19+ this . removeHighlight ( this . search_results [ this . selected ] ) ;
20+ this . selected =
21+ ( this . selected - 1 + this . search_results . length ) %
22+ this . search_results . length ;
2423
25- this . removeHighlight ( this . search_results [ this . selected ] ) ;
26- this . selected =
27- ( this . selected - 1 + this . search_results . length ) %
28- this . search_results . length ;
24+ this . highlightSelection ( this . search_results [ this . selected ] ) ;
2925
30- this . highlightSelection ( this . search_results [ this . selected ] ) ;
26+ break ;
27+ case "ArrowDown" :
28+ e . preventDefault ( ) ;
3129
32- break ;
33- case "ArrowDown" :
34- e . preventDefault ( ) ;
30+ this . removeHighlight ( this . search_results [ this . selected ] ) ;
31+ this . selected = ( this . selected + 1 ) % this . search_results . length ;
3532
36- this . removeHighlight ( this . search_results [ this . selected ] ) ;
37- this . selected = ( this . selected + 1 ) % this . search_results . length ;
33+ this . highlightSelection ( this . search_results [ this . selected ] ) ;
3834
39- this . highlightSelection ( this . search_results [ this . selected ] ) ;
35+ break ;
36+ case "Enter" :
37+ e . preventDefault ( ) ;
38+ const target = this . search_results [ this . selected ] ;
39+ clickableTarget = target . querySelector ( "a, button, [data-clickable]" ) ;
4040
41- break ;
42- case "Enter" :
43- e . preventDefault ( ) ;
44- const target = this . search_results [ this . selected ] ;
45- clickableTarget = target . querySelector (
46- "a, button, [data-clickable]"
47- ) ;
41+ clickableTarget && clickableTarget . click ( ) ;
42+ break ;
43+ case "Tab" :
44+ e . preventDefault ( ) ;
45+ console . log ( "Do something on tab" ) ;
46+ break ;
47+ default :
48+ this . selected &&
49+ this . removeHighlight ( this . search_results [ this . selected ] ) ;
50+ }
51+ } ;
52+
53+ document . addEventListener ( "keydown" , ( e ) => {
54+ if ( e . key === "k" && e . metaKey ) {
55+ e . preventDefault ( ) ;
56+ liveSocket . execJS ( this . el , this . el . getAttribute ( "data-show-modal" ) ) ;
4857
49- clickableTarget && clickableTarget . click ( ) ;
50- break ;
51- case "Tab" :
52- e . preventDefault ( ) ;
53- console . log ( "Do something on tab" ) ;
54- break ;
55- default :
56- this . selected &&
57- this . removeHighlight ( this . search_results [ this . selected ] ) ;
58- }
58+ ! this . search_results && fetchDomNodes ( ) ;
59+ } else {
60+ this . el . addEventListener ( "keydown" , handleKeyDown ) ;
5961 }
6062 } ) ;
63+
6164 this . handleEvent ( "search-results-ready" , fetchDomNodes ) ;
6265 } ,
63- destroyed ( ) { } ,
6466 highlightSelection ( target ) {
6567 if ( ! target ) return ;
6668 target . setAttribute ( "aria-selected" , true ) ;
0 commit comments