File tree Expand file tree Collapse file tree 3 files changed +22
-8
lines changed
Expand file tree Collapse file tree 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import {
1111 KeyBindProviderPropsI ,
1212 ShortcutType ,
1313} from '../types' ;
14- import { findFirstPlatformMatch , isDuplicate } from '../utils' ;
14+ import { findFirstPlatformMatch , isDuplicate , logMsg } from '../utils' ;
1515import { useShortcuts } from '../hooks' ;
1616
1717export const KeyBindContext = createContext ( { } as KeyBindContextState ) ;
@@ -23,12 +23,19 @@ const KeyBindProvider: FC<KeyBindProviderPropsI> = ({
2323 const [ storeShortcuts , setStoreCommands ] = useState ( shortcuts ) ;
2424
2525 const registerShortcut = useCallback (
26- ( command : ShortcutType ) => {
27- if ( isDuplicate ( storeShortcuts , command ) ) {
28- console . warn ( 'Command already registered' , { storeShortcuts, command } ) ;
29- return ;
30- }
31- setStoreCommands ( prev => [ ...prev , command ] ) ;
26+ ( shortcut : ShortcutType ) => {
27+ setStoreCommands ( prev => {
28+ return [
29+ ...( prev ?. filter ( s => {
30+ const isDuplicated = isDuplicate ( prev , s ) ;
31+ if ( isDuplicated ) {
32+ console . warn ( logMsg ( s , shortcut ) ) ;
33+ }
34+ return ! isDuplicated ;
35+ } ) ?? [ ] ) ,
36+ shortcut ,
37+ ] ;
38+ } ) ;
3239 } ,
3340 [ storeShortcuts ]
3441 ) ;
Original file line number Diff line number Diff line change @@ -7,5 +7,5 @@ export const useRegisterShortcut = (shortcut: ShortcutType) => {
77
88 useEffect ( ( ) => {
99 registerShortcut ( shortcut ) ;
10- } , [ ] ) ;
10+ } , [ shortcut ] ) ;
1111} ;
Original file line number Diff line number Diff line change @@ -79,3 +79,10 @@ export const isDuplicate = (
7979) => {
8080 return ! ! findShortcut ( shortcuts , getShortcutKeys ( shortcut ) ) ;
8181} ;
82+
83+ export const logMsg = ( short : ShortcutType , short2 : ShortcutType ) =>
84+ `Shortcut is duplicated: label: "${ short . label } ", keys: "${ JSON . stringify (
85+ short . keys
86+ ) } " ,replacing for: label: "${ short2 . label } ", keys: "${ JSON . stringify (
87+ short2 . keys
88+ ) } "...`;
You can’t perform that action at this time.
0 commit comments