@@ -13,6 +13,7 @@ export type Hotkey = {
13
13
desc : string ;
14
14
hotkeys : string [ ] ;
15
15
platformKeys : string [ ] [ ] ;
16
+ isEnabled : boolean ;
16
17
} ;
17
18
18
19
type HotkeyCategoryData = { title : string ; hotkeys : Record < string , Hotkey > } ;
@@ -60,14 +61,15 @@ export const useHotkeyData = (): HotkeysData => {
60
61
} ,
61
62
} ;
62
63
63
- const addHotkey = ( category : HotkeyCategory , id : string , keys : string [ ] ) => {
64
+ const addHotkey = ( category : HotkeyCategory , id : string , keys : string [ ] , isEnabled : boolean = true ) => {
64
65
data [ category ] . hotkeys [ id ] = {
65
66
id,
66
67
category,
67
68
title : t ( `hotkeys.${ category } .${ id } .title` ) ,
68
69
desc : t ( `hotkeys.${ category } .${ id } .desc` ) ,
69
70
hotkeys : keys ,
70
71
platformKeys : formatKeysForPlatform ( keys , isMacOS ) ,
72
+ isEnabled,
71
73
} ;
72
74
} ;
73
75
@@ -189,17 +191,28 @@ type UseRegisteredHotkeysArg = {
189
191
} ;
190
192
191
193
/**
192
- * A wrapper around `useHotkeys` that registers the hotkey with the hotkey registry.
193
- *
194
- * Registered hotkeys will be displayed in the hotkeys modal.
194
+ * A wrapper around `useHotkeys` that adds a handler for a registered hotkey.
195
195
*/
196
196
export const useRegisteredHotkeys = ( { id, category, callback, options, dependencies } : UseRegisteredHotkeysArg ) => {
197
197
const hotkeysData = useHotkeyData ( ) ;
198
- const keys = useMemo ( ( ) => {
199
- const _keys = hotkeysData [ category ] . hotkeys [ id ] ?. hotkeys ;
200
- assert ( _keys !== undefined , `Hotkey ${ category } .${ id } not found` ) ;
201
- return _keys ;
198
+ const data = useMemo ( ( ) => {
199
+ const _data = hotkeysData [ category ] . hotkeys [ id ] ;
200
+ assert ( _data !== undefined , `Hotkey ${ category } .${ id } not found` ) ;
201
+ return _data ;
202
202
} , [ category , hotkeysData , id ] ) ;
203
-
204
- return useHotkeys ( keys , callback , options , dependencies ) ;
203
+ const _options = useMemo ( ( ) => {
204
+ // If no options are provided, return the default. This includes if the hotkey is globally disabled.
205
+ if ( ! options ) {
206
+ return {
207
+ enabled : data . isEnabled ,
208
+ } satisfies Options ;
209
+ }
210
+ // Otherwise, return the provided optiosn, but override the enabled state.
211
+ return {
212
+ ...options ,
213
+ enabled : data . isEnabled ? options . enabled : false ,
214
+ } satisfies Options ;
215
+ } , [ data . isEnabled , options ] ) ;
216
+
217
+ return useHotkeys ( data . hotkeys , callback , _options , dependencies ) ;
205
218
} ;
0 commit comments