@@ -82,10 +82,15 @@ export interface CommandOption {
8282 suggested ?: boolean
8383 disabled ?: boolean
8484 hidden ?: boolean
85+ when ?: ( event : KeyboardEvent ) => boolean
8586 onSelect ?: ( source ?: "palette" | "keybind" | "slash" ) => void
8687 onHighlight ?: ( ) => ( ( ) => void ) | void
8788}
8889
90+ export function resolveKeybindOption ( candidates : CommandOption [ ] | undefined , event : KeyboardEvent ) {
91+ return candidates ?. find ( ( option ) => option . when ?.( event ) ) ?? candidates ?. find ( ( option ) => ! option . when )
92+ }
93+
8994type CommandSource = "palette" | "keybind" | "slash"
9095
9196export type CommandCatalogItem = {
@@ -334,7 +339,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
334339 } )
335340
336341 const keymap = createMemo ( ( ) => {
337- const map = new Map < string , CommandOption > ( )
342+ const map = new Map < string , CommandOption [ ] > ( )
338343 for ( const option of options ( ) ) {
339344 if ( option . id . startsWith ( SUGGESTED_PREFIX ) ) continue
340345 if ( option . disabled ) continue
@@ -344,8 +349,12 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
344349 for ( const kb of keybinds ) {
345350 if ( ! kb . key ) continue
346351 const sig = signature ( kb . key , kb . ctrl , kb . meta , kb . shift , kb . alt )
347- if ( map . has ( sig ) ) continue
348- map . set ( sig , option )
352+ const existing = map . get ( sig )
353+ if ( existing ) {
354+ existing . push ( option )
355+ continue
356+ }
357+ map . set ( sig , [ option ] )
349358 }
350359 }
351360 return map
@@ -374,7 +383,7 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
374383
375384 const sig = signatureFromEvent ( event )
376385 const isPalette = palette ( ) . has ( sig )
377- const option = keymap ( ) . get ( sig )
386+ const option = resolveKeybindOption ( keymap ( ) . get ( sig ) , event )
378387 const modified = event . ctrlKey || event . metaKey || event . altKey
379388 const isTab = event . key === "Tab"
380389
@@ -383,17 +392,19 @@ export const { use: useCommand, provider: CommandProvider } = createSimpleContex
383392
384393 if ( isPalette ) {
385394 event . preventDefault ( )
395+ event . stopPropagation ( )
386396 showPalette ( )
387397 return
388398 }
389399
390400 if ( ! option ) return
391401 event . preventDefault ( )
402+ event . stopPropagation ( )
392403 option . onSelect ?.( "keybind" )
393404 }
394405
395406 onMount ( ( ) => {
396- makeEventListener ( document , "keydown" , handleKeyDown )
407+ makeEventListener ( document , "keydown" , handleKeyDown , { capture : true } )
397408 } )
398409
399410 function register ( cb : ( ) => CommandOption [ ] ) : void
0 commit comments