-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/implement project hotkeys 79 (#143)
* Add project hotkeys * version
- Loading branch information
1 parent
49cd47d
commit 977152c
Showing
7 changed files
with
73 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const useCommandKeyString = () => { | ||
return window.windowApi.os() === 'darwin' ? '⌘' : 'Ctrl'; | ||
}; | ||
|
||
export default useCommandKeyString; |
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,53 @@ | ||
import { useEffect, useCallback, useMemo } from 'react'; | ||
import useStore from '../store/useStore'; | ||
import { saveProject } from '../utils/projectUtils'; | ||
|
||
const useProjectHotkeys = () => { | ||
const { setGenerateBookModalOpen, setPreviewEnabled, setNewBookModalOpen } = | ||
useStore.getState(); | ||
const isProjectOpen = useStore((state) => state.isProjectOpen); | ||
const isPreviewEnabled = useStore((state) => state.previewEnabled); | ||
const activeSectionId = useStore((state) => state.activeSectionId); | ||
|
||
const shortcuts = useMemo( | ||
() => ({ | ||
o: window.projectApi.openProject, | ||
g: () => { | ||
if (isProjectOpen) { | ||
setGenerateBookModalOpen(true); | ||
} | ||
}, | ||
s: saveProject, | ||
n: () => { | ||
setNewBookModalOpen(true); | ||
}, | ||
p: () => { | ||
if (isProjectOpen && activeSectionId !== '') { | ||
setPreviewEnabled(!isPreviewEnabled); | ||
} | ||
}, | ||
}), | ||
[isProjectOpen, isPreviewEnabled, activeSectionId] | ||
); | ||
const handleKeypress = useCallback( | ||
(event: KeyboardEvent) => { | ||
const commandKeyPressed = | ||
window.windowApi.os() === 'darwin' ? event.metaKey : event.ctrlKey; | ||
if (commandKeyPressed) { | ||
if (event.key in shortcuts) { | ||
shortcuts[event.key](); | ||
} | ||
} | ||
}, | ||
[isProjectOpen, isPreviewEnabled, activeSectionId] | ||
); | ||
|
||
useEffect(() => { | ||
document.addEventListener('keydown', handleKeypress); | ||
return () => { | ||
document.removeEventListener('keydown', handleKeypress); | ||
}; | ||
}, [handleKeypress]); | ||
}; | ||
|
||
export default useProjectHotkeys; |
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