@@ -9,62 +9,22 @@ import {
99} from "@blueprintjs/core" ;
1010import Description from "roamjs-components/components/Description" ;
1111import { DISCOURSE_TOOL_SHORTCUT_KEY } from "~/data/userSettings" ;
12+ import { setPersonalSetting } from "~/components/settings/utils/accessors" ;
13+ import { comboToString } from "~/components/DiscourseNodeMenu" ;
1214
1315type KeyboardShortcutInputProps = {
1416 onloadArgs : OnloadArgs ;
1517 settingKey : string ;
18+ blockPropKey : string ;
1619 label : string ;
1720 description : string ;
1821 placeholder ?: string ;
1922} ;
2023
21- // Reuse the keyboard combo utilities from NodeMenuTriggerComponent
22- const isMac = ( ) => {
23- const platform =
24- typeof navigator !== "undefined" ? navigator . platform : undefined ;
25- return platform == null ? false : / M a c | i P o d | i P h o n e | i P a d / . test ( platform ) ;
26- } ;
27-
28- const MODIFIER_BIT_MASKS = {
29- alt : 1 ,
30- ctrl : 2 ,
31- meta : 4 ,
32- shift : 8 ,
33- } ;
34-
35- const ALIASES : { [ key : string ] : string } = {
36- cmd : "meta" ,
37- command : "meta" ,
38- escape : "esc" ,
39- minus : "-" ,
40- mod : isMac ( ) ? "meta" : "ctrl" ,
41- option : "alt" ,
42- plus : "+" ,
43- return : "enter" ,
44- win : "meta" ,
45- } ;
46-
47- const normalizeKeyCombo = ( combo : string ) => {
48- const keys = combo . replace ( / \s / g, "" ) . split ( "+" ) ;
49- return keys . map ( ( key ) => {
50- const keyName = ALIASES [ key ] != null ? ALIASES [ key ] : key ;
51- return keyName === "meta" ? ( isMac ( ) ? "cmd" : "win" ) : keyName ;
52- } ) ;
53- } ;
54-
55- const getModifiersFromCombo = ( comboKey : IKeyCombo ) => {
56- if ( ! comboKey ) return [ ] ;
57- return [
58- comboKey . modifiers & MODIFIER_BIT_MASKS . alt && "alt" ,
59- comboKey . modifiers & MODIFIER_BIT_MASKS . ctrl && "ctrl" ,
60- comboKey . modifiers & MODIFIER_BIT_MASKS . shift && "shift" ,
61- comboKey . modifiers & MODIFIER_BIT_MASKS . meta && "meta" ,
62- ] . filter ( Boolean ) ;
63- } ;
64-
6524const KeyboardShortcutInput = ( {
6625 onloadArgs,
6726 settingKey,
27+ blockPropKey,
6828 label,
6929 description,
7030 placeholder = "Click to set shortcut" ,
@@ -104,6 +64,7 @@ const KeyboardShortcutInput = ({
10464 extensionAPI . settings
10565 . set ( settingKey , comboObj )
10666 . catch ( ( ) => console . error ( "Failed to set setting" ) ) ;
67+ setPersonalSetting ( [ blockPropKey ] , comboObj ) ;
10768 }
10869 return ;
10970 }
@@ -112,28 +73,26 @@ const KeyboardShortcutInput = ({
11273 const comboObj = getKeyCombo ( e . nativeEvent ) ;
11374 if ( ! comboObj . key ) return ;
11475
115- setComboKey ( { key : comboObj . key , modifiers : comboObj . modifiers } ) ;
76+ const combo = { key : comboObj . key , modifiers : comboObj . modifiers } ;
77+ setComboKey ( combo ) ;
11678 extensionAPI . settings
117- . set ( settingKey , comboObj )
79+ . set ( settingKey , combo )
11880 . catch ( ( ) => console . error ( "Failed to set setting" ) ) ;
81+ setPersonalSetting ( [ blockPropKey ] , combo ) ;
11982 } ,
120- [ extensionAPI , settingKey ] ,
83+ [ extensionAPI , settingKey , blockPropKey ] ,
12184 ) ;
12285
123- const shortcut = useMemo ( ( ) => {
124- if ( ! comboKey . key ) return "" ;
125-
126- const modifiers = getModifiersFromCombo ( comboKey ) ;
127- const comboString = [ ...modifiers , comboKey . key ] . join ( "+" ) ;
128- return normalizeKeyCombo ( comboString ) . join ( "+" ) ;
129- } , [ comboKey ] ) ;
86+ const shortcut = useMemo ( ( ) => comboToString ( comboKey ) , [ comboKey ] ) ;
13087
13188 const handleClear = useCallback ( ( ) => {
132- setComboKey ( { modifiers : 0 , key : "" } ) ;
89+ const clearedCombo = { modifiers : 0 , key : "" } ;
90+ setComboKey ( clearedCombo ) ;
13391 extensionAPI . settings
134- . set ( settingKey , { modifiers : 0 , key : "" } )
92+ . set ( settingKey , clearedCombo )
13593 . catch ( ( ) => console . error ( "Failed to set setting" ) ) ;
136- } , [ extensionAPI , settingKey ] ) ;
94+ setPersonalSetting ( [ blockPropKey ] , clearedCombo ) ;
95+ } , [ extensionAPI , settingKey , blockPropKey ] ) ;
13796
13897 return (
13998 < Label >
0 commit comments