feat: page mode shortcut (#2985)

This commit is contained in:
danielchim
2023-07-03 16:23:53 +00:00
committed by GitHub
parent 7b2acec7c3
commit 32692bd54a
2 changed files with 57 additions and 32 deletions
@@ -1,8 +1,10 @@
import { Tooltip } from '@affine/component';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { assertExists } from '@blocksuite/store'; import { assertExists } from '@blocksuite/store';
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta'; import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
import { useAtom } from 'jotai'; import { useAtom } from 'jotai';
import type { CSSProperties } from 'react'; import type { CSSProperties } from 'react';
import { useEffect } from 'react';
import { pageSettingFamily } from '../../../../atoms'; import { pageSettingFamily } from '../../../../atoms';
import type { BlockSuiteWorkspace } from '../../../../shared'; import type { BlockSuiteWorkspace } from '../../../../shared';
@@ -30,39 +32,59 @@ export const EditorModeSwitch = ({
const t = useAFFiNEI18N(); const t = useAFFiNEI18N();
assertExists(pageMeta); assertExists(pageMeta);
const { trash } = pageMeta; const { trash } = pageMeta;
useEffect(() => {
return ( const keydown = (e: KeyboardEvent) => {
<StyledEditorModeSwitch if ((e.key === 's' && e.metaKey) || (e.key === 's' && e.altKey)) {
style={style} e.preventDefault();
switchLeft={currentMode === 'page'} setSetting(setting => {
showAlone={trash} if (setting?.mode !== 'page') {
> toast(t['com.affine.pageMode']());
<PageSwitchItem
data-testid="switch-page-mode-button"
active={currentMode === 'page'}
hide={trash && currentMode !== 'page'}
onClick={() => {
setSetting(setting => {
if (setting?.mode !== 'page') {
toast(t['com.affine.pageMode']());
}
return { ...setting, mode: 'page' }; return { ...setting, mode: 'page' };
}); } else {
}} toast(t['com.affine.edgelessMode']());
/>
<EdgelessSwitchItem
data-testid="switch-edgeless-mode-button"
active={currentMode === 'edgeless'}
hide={trash && currentMode !== 'edgeless'}
onClick={() => {
setSetting(setting => {
if (setting?.mode !== 'edgeless') {
toast(t['com.affine.pageMode']());
}
return { ...setting, mode: 'edgeless' }; return { ...setting, mode: 'edgeless' };
}); }
}} });
/> }
</StyledEditorModeSwitch> };
document.addEventListener('keydown', keydown, { capture: true });
return () =>
document.removeEventListener('keydown', keydown, { capture: true });
}, [setSetting, t]);
return (
<Tooltip content={'Switch ⌘ + S'}>
<StyledEditorModeSwitch
style={style}
switchLeft={currentMode === 'page'}
showAlone={trash}
>
<PageSwitchItem
data-testid="switch-page-mode-button"
active={currentMode === 'page'}
hide={trash && currentMode !== 'page'}
onClick={() => {
setSetting(setting => {
if (setting?.mode !== 'page') {
toast(t['com.affine.pageMode']());
}
return { ...setting, mode: 'page' };
});
}}
/>
<EdgelessSwitchItem
data-testid="switch-edgeless-mode-button"
active={currentMode === 'edgeless'}
hide={trash && currentMode !== 'edgeless'}
onClick={() => {
setSetting(setting => {
if (setting?.mode !== 'edgeless') {
toast(t['com.affine.edgelessMode']());
}
return { ...setting, mode: 'edgeless' };
});
}}
/>
</StyledEditorModeSwitch>
</Tooltip>
); );
}; };
@@ -91,6 +91,7 @@ export const useWinEdgelessKeyboardShortcuts = (): ShortcutTip => {
[t['Pen']()]: 'P', [t['Pen']()]: 'P',
[t['Hand']()]: 'H', [t['Hand']()]: 'H',
[t['Note']()]: 'N', [t['Note']()]: 'N',
['Switch']: 'Alt + S',
// not implement yet // not implement yet
// [t['Group']()]: 'Ctrl + G', // [t['Group']()]: 'Ctrl + G',
// [t['Ungroup']()]: 'Ctrl + Shift + G', // [t['Ungroup']()]: 'Ctrl + Shift + G',
@@ -122,6 +123,7 @@ export const useMacPageKeyboardShortcuts = (): ShortcutTip => {
[t['Increase indent']()]: 'Tab', [t['Increase indent']()]: 'Tab',
[t['Reduce indent']()]: '⇧+Tab', [t['Reduce indent']()]: '⇧+Tab',
[t['Group as Database']()]: '⌘ + G', [t['Group as Database']()]: '⌘ + G',
['Switch']: '⌘ + S',
// not implement yet // not implement yet
// [t['Move Up']()]: '⌘ + ⌥ + ↑', // [t['Move Up']()]: '⌘ + ⌥ + ↑',
// [t['Move Down']()]: '⌘ + ⌥ + ↓', // [t['Move Down']()]: '⌘ + ⌥ + ↓',
@@ -176,6 +178,7 @@ export const useWinPageKeyboardShortcuts = (): ShortcutTip => {
[t['Increase indent']()]: 'Tab', [t['Increase indent']()]: 'Tab',
[t['Reduce indent']()]: 'Shift+Tab', [t['Reduce indent']()]: 'Shift+Tab',
[t['Group as Database']()]: 'Ctrl + G', [t['Group as Database']()]: 'Ctrl + G',
['Switch']: 'Alt + S',
// not implement yet // not implement yet
// [t['Move Up']()]: 'Ctrl + Alt + ↑', // [t['Move Up']()]: 'Ctrl + Alt + ↑',
// [t['Move Down']()]: 'Ctrl + Alt + ↓', // [t['Move Down']()]: 'Ctrl + Alt + ↓',