@@ -13,23 +13,39 @@ import {
1313 computeMatches ,
1414 getHelperValues
1515} from "./filters.js" ;
16- import { searchCommandStyles } from "./search- command-styles.js" ;
16+ import { commandPaletteStyles } from "./command-palette -styles.js" ;
1717import {
1818 renderFlagPanel ,
1919 renderRangePanel ,
2020 renderListPanel ,
2121 renderFilterList ,
2222 renderPresets ,
23+ renderActions ,
2324 renderResults
24- } from "./search- command-panels.js" ;
25+ } from "./command-palette -panels.js" ;
2526import "./search-chip.js" ;
2627
27- class SearchCommand extends LitElement {
28+ // CONSTANTS
29+ const kActions = [
30+ { id : "toggle_theme" , shortcut : "t" }
31+ ] ;
32+
33+ function resolveKbd ( shortcut ) {
34+ if ( ! shortcut ) {
35+ return null ;
36+ }
37+
38+ const key = shortcut . toUpperCase ( ) ;
39+
40+ return navigator . userAgent . includes ( "Mac" ) ? `⌥${ key } ` : `Alt+${ key } ` ;
41+ }
42+
43+ class CommandPalette extends LitElement {
2844 #linker = null ;
2945 #network = null ;
3046 #packages = [ ] ;
3147
32- static styles = searchCommandStyles ;
48+ static styles = commandPaletteStyles ;
3349
3450 static properties = {
3551 open : { type : Boolean } ,
@@ -55,6 +71,16 @@ class SearchCommand extends LitElement {
5571
5672 if ( event . key === "Escape" && this . open ) {
5773 this . #close( ) ;
74+
75+ return ;
76+ }
77+
78+ if ( this . open && event . altKey ) {
79+ const action = kActions . find ( ( a ) => a . shortcut && `Key${ a . shortcut . toUpperCase ( ) } ` === event . code ) ;
80+ if ( action ) {
81+ event . preventDefault ( ) ;
82+ this . #executeAction( action ) ;
83+ }
5884 }
5985 } ;
6086
@@ -84,12 +110,12 @@ class SearchCommand extends LitElement {
84110 connectedCallback ( ) {
85111 super . connectedCallback ( ) ;
86112 document . addEventListener ( "keydown" , this . #handleKeydown) ;
87- window . addEventListener ( EVENTS . SEARCH_COMMAND_INIT , this . #init) ;
113+ window . addEventListener ( EVENTS . COMMAND_PALETTE_INIT , this . #init) ;
88114 }
89115
90116 disconnectedCallback ( ) {
91117 document . removeEventListener ( "keydown" , this . #handleKeydown) ;
92- window . removeEventListener ( EVENTS . SEARCH_COMMAND_INIT , this . #init) ;
118+ window . removeEventListener ( EVENTS . COMMAND_PALETTE_INIT , this . #init) ;
93119 super . disconnectedCallback ( ) ;
94120 }
95121
@@ -323,6 +349,17 @@ class SearchCommand extends LitElement {
323349 this . #close( ) ;
324350 }
325351
352+ #executeAction( action ) {
353+ if ( action . id === "toggle_theme" ) {
354+ const nextTheme = window . settings . config . theme === "dark" ? "light" : "dark" ;
355+ window . dispatchEvent ( new CustomEvent ( EVENTS . SETTINGS_SAVED , {
356+ detail : { ...window . settings . config , theme : nextTheme }
357+ } ) ) ;
358+ }
359+
360+ this . #close( ) ;
361+ }
362+
326363 #getEmptyQueryMessage( ) {
327364 const i18n = window . i18n [ currentLang ( ) ] . search_command ;
328365 if ( this . queries . length === 1 ) {
@@ -407,6 +444,20 @@ class SearchCommand extends LitElement {
407444 }
408445 }
409446
447+ #resolveActions( ) {
448+ const i18n = window . i18n [ currentLang ( ) ] . search_command ;
449+ const currentTheme = window . settings ?. config ?. theme ?? "light" ;
450+ const targetTheme = currentTheme === "dark" ? "light" : "dark" ;
451+
452+ return kActions . map ( ( action ) => {
453+ return {
454+ ...action ,
455+ label : i18n [ `action_${ action . id } _to_${ targetTheme } ` ] ,
456+ kbd : resolveKbd ( action . shortcut )
457+ } ;
458+ } ) ;
459+ }
460+
410461 render ( ) {
411462 if ( ! this . open ) {
412463 return nothing ;
@@ -483,6 +534,10 @@ class SearchCommand extends LitElement {
483534 presets : PRESETS ,
484535 onApply : ( preset ) => this . #addQuery( preset . filter , preset . value )
485536 } ) : nothing }
537+ ${ showRichPlaceholder ? renderActions ( {
538+ actions : this . #resolveActions( ) ,
539+ onExecute : ( action ) => this . #executeAction( action )
540+ } ) : nothing }
486541 ${ renderResults ( {
487542 results : this . results ,
488543 selectedIndex : this . selectedIndex ,
@@ -505,4 +560,4 @@ class SearchCommand extends LitElement {
505560 }
506561}
507562
508- customElements . define ( "search- command" , SearchCommand ) ;
563+ customElements . define ( "command-palette " , CommandPalette ) ;
0 commit comments