-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fork/mapringg/mac-shortcuts'
- Loading branch information
Showing
10 changed files
with
88 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as React from 'react'; | ||
|
||
import { isMacUser } from '../util/pwaUtils'; | ||
|
||
|
||
export const ShortcutKeyName = { | ||
Esc: 'Escape', | ||
Left: 'ArrowLeft', | ||
Right: 'ArrowRight', | ||
}; | ||
|
||
export type GlobalShortcutDefinition = [key: string | false, useCtrl: boolean, useShift: boolean, useAltForNonMac: boolean, action: () => void]; | ||
|
||
/** | ||
* Registers multiple global keyboard shortcuts -> function mappings. | ||
* | ||
* Important notes below: | ||
* - [MAC only] the Alt key is ignored even if defined in the shortcut | ||
* - [MAC only] are not using the command key at the moment, as it interfered with browser shortcuts | ||
* - stabilize the shortcuts definition (e.g. React.useMemo()) to avoid re-registering the shortcuts at every render | ||
* | ||
*/ | ||
export const useGlobalShortcuts = (shortcuts: GlobalShortcutDefinition[]) => { | ||
React.useEffect(() => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
for (const [key, useCtrl, useShift, useAltForNonMac, action] of shortcuts) { | ||
if ( | ||
key && | ||
(useCtrl === event.ctrlKey) && | ||
(useShift === event.shiftKey) && | ||
(isMacUser /* Mac users won't need the Alt keys */ || useAltForNonMac === event.altKey) && | ||
event.key.toLowerCase() === key.toLowerCase() | ||
) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
action(); | ||
break; | ||
} | ||
} | ||
}; | ||
window.addEventListener('keydown', handleKeyDown); | ||
return () => window.removeEventListener('keydown', handleKeyDown); | ||
}, [shortcuts]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters