Skip to content

Commit

Permalink
Feature/implement project hotkeys 79 (#143)
Browse files Browse the repository at this point in the history
* Add project hotkeys

* version
  • Loading branch information
zachhannum authored Sep 14, 2022
1 parent 49cd47d commit 977152c
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SidebarPane, PreviewPane, MainPane, Modals } from './panes';
import theme from './theme/theme';
import GlobalStyles from './theme/globalStyles';
import { WinControls } from './controls';
import { useOpenProject } from './hooks';
import { useOpenProject, useProjectHotkeys } from './hooks';

const AppContainer = styled.div`
display: flex;
Expand All @@ -19,6 +19,7 @@ const AppContainer = styled.div`

const App = () => {
useOpenProject();
useProjectHotkeys();
return (
<ThemeProvider theme={theme}>
{window.windowApi.os() === 'win32' && <WinControls />}
Expand Down
10 changes: 6 additions & 4 deletions app/renderer/components/MoreOptionsSidebarMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../icons';
import useStore from '../store/useStore';
import { saveProject } from '../utils/projectUtils';
import { useCommandKeyString } from '../hooks';

const StyledMenuDivider = styled.div`
height: 2px;
Expand All @@ -29,6 +30,7 @@ const MoreOptionsSidebarMenu = () => {
const theme = useTheme();
const [showMenu, setShowMenu] = useState(false);
const buttonRef = useRef<HTMLAnchorElement>(null);
const commandKeyString = useCommandKeyString();
const isProjectOpen = useStore((state) => state.isProjectOpen);
const activeSectionId = useStore((state) => state.activeSectionId);
const previewEnabled = useStore((state) => state.previewEnabled);
Expand Down Expand Up @@ -75,7 +77,7 @@ const MoreOptionsSidebarMenu = () => {
<MoreOptionsSidebarItem
hover
iconElement={<NewBookIcon />}
rightElement={<span>N</span>}
rightElement={<span>{commandKeyString}N</span>}
label="New Book"
onClick={() => {
setShowMenu(false);
Expand All @@ -85,7 +87,7 @@ const MoreOptionsSidebarMenu = () => {
<MoreOptionsSidebarItem
hover
iconElement={<OpenBookIcon />}
rightElement={<span>O</span>}
rightElement={<span>{commandKeyString}O</span>}
label="Open Book"
onClick={() => {
setShowMenu(false);
Expand All @@ -97,7 +99,7 @@ const MoreOptionsSidebarMenu = () => {
<MoreOptionsSidebarItem
hover
iconElement={<SaveIcon />}
rightElement={<span>S</span>}
rightElement={<span>{commandKeyString}S</span>}
label="Save Book"
onClick={() => {
if (isProjectOpen) {
Expand All @@ -109,7 +111,7 @@ const MoreOptionsSidebarMenu = () => {
<MoreOptionsSidebarItem
hover
iconElement={<GenerateBookIcon />}
rightElement={<span>G</span>}
rightElement={<span>{commandKeyString}G</span>}
label="Generate Book"
onClick={() => {
setShowMenu(false);
Expand Down
2 changes: 2 additions & 0 deletions app/renderer/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export { default as useOnWheel } from './useOnWheel';
export { default as useOnClickOutside } from './useOnClickOutside';
export { default as useIsHovering } from './useIsHovering';
export { default as useOnBookPdfGenerated } from './useOnBookPdfGenerated';
export { default as useProjectHotkeys } from './useProjectHotkeys';
export { default as useCommandKeyString } from './useCommandKeyString';
5 changes: 5 additions & 0 deletions app/renderer/hooks/useCommandKeyString.ts
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;
53 changes: 53 additions & 0 deletions app/renderer/hooks/useProjectHotkeys.ts
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;
6 changes: 4 additions & 2 deletions app/renderer/panes/MainPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styled from 'styled-components';
import Color from 'color';
import { Editor } from '../components';
import useStore from '../store/useStore';
import { useCommandKeyString } from '../hooks';

const MainContent = styled.div`
--top-padding: calc(
Expand Down Expand Up @@ -70,6 +71,7 @@ const MainPane = () => {
const isProjectOpen = useStore((state) => state.isProjectOpen);
const activeSectionId = useStore((state) => state.activeSectionId);
const activeSectionName = useStore((state) => state.activeSectionName);
const commandKeyString = useCommandKeyString();
return (
<MainContent>
{isProjectOpen && activeSectionId !== '' ? (
Expand All @@ -86,11 +88,11 @@ const MainPane = () => {
<NoProjectHotkeys>
<NoProjectHotkey>
<span>Open a project</span>
<span>O</span>
<span>{commandKeyString}O</span>
</NoProjectHotkey>
<NoProjectHotkey>
<span>Start a new project</span>
<span>N</span>
<span>{commandKeyString}N</span>
</NoProjectHotkey>
</NoProjectHotkeys>
</NoProjectDiv>
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "calamus",
"version": "0.5.7-pre-alpha",
"version": "0.5.8-pre-alpha",
"description": "Write and Publish Books with Ease",
"main": "./dist/main/main.js",
"author": {
Expand Down

0 comments on commit 977152c

Please sign in to comment.