From efca651429cfb65c6ef4109878a74f97a8ad5f1c Mon Sep 17 00:00:00 2001 From: JimmFly Date: Mon, 16 Oct 2023 20:27:06 +0800 Subject: [PATCH] feat(core): add history shortcut (#4595) --- .../src/components/root-app-sidebar/index.tsx | 7 +- apps/core/src/hooks/affine/use-shortcuts.ts | 10 +- apps/core/src/hooks/use-shortcut-commands.ts | 49 ++++++ .../src/components/app-sidebar/index.tsx | 5 +- .../app-sidebar/sidebar-header/index.tsx | 85 +++++++--- tests/affine-desktop/e2e/basic.spec.ts | 153 ++++++++++-------- 6 files changed, 209 insertions(+), 100 deletions(-) create mode 100644 apps/core/src/hooks/use-shortcut-commands.ts diff --git a/apps/core/src/components/root-app-sidebar/index.tsx b/apps/core/src/components/root-app-sidebar/index.tsx index c6c496211..7c97d09e7 100644 --- a/apps/core/src/components/root-app-sidebar/index.tsx +++ b/apps/core/src/components/root-app-sidebar/index.tsx @@ -27,7 +27,9 @@ import { forwardRef, useCallback, useEffect, useMemo } from 'react'; import { openWorkspaceListModalAtom } from '../../atoms'; import { useHistoryAtom } from '../../atoms/history'; import { useAppSetting } from '../../atoms/settings'; +import { useGeneralShortcuts } from '../../hooks/affine/use-shortcuts'; import { useTrashModalHelper } from '../../hooks/affine/use-trash-modal-helper'; +import { useRegisterBlocksuiteEditorCommands } from '../../hooks/use-shortcut-commands'; import type { AllWorkspace } from '../../shared'; import { currentCollectionsAtom } from '../../utils/user-setting'; import { CollectionsList } from '../pure/workspace-slider-bar/collections'; @@ -105,6 +107,8 @@ export const RootAppSidebar = ({ const [openUserWorkspaceList, setOpenUserWorkspaceList] = useAtom( openWorkspaceListModalAtom ); + const generalShortcutsInfo = useGeneralShortcuts(); + const onClickNewPage = useCallback(async () => { const page = createPage(); await page.waitForLoaded(); @@ -161,7 +165,7 @@ export const RootAppSidebar = ({ const closeUserWorkspaceList = useCallback(() => { setOpenUserWorkspaceList(false); }, [setOpenUserWorkspaceList]); - + useRegisterBlocksuiteEditorCommands(router.back, router.forward); return ( <> { // not implement yet // [t('appendDailyNote')]: 'Ctrl + Alt + A', [t('expandOrCollapseSidebar')]: ['Ctrl', '/'], - // not implement yet - // [t('goBack')]: 'Ctrl + [', - // [t('goForward')]: 'Ctrl + ]', + [t('goBack')]: ['Ctrl + ['], + [t('goForward')]: ['Ctrl + ]'], }), [t] ); @@ -98,9 +97,8 @@ export const useMacGeneralKeyboardShortcuts = (): ShortcutMap => { // not implement yet // [t('appendDailyNote')]: '⌘ + ⌥ + A', [t('expandOrCollapseSidebar')]: ['⌘', '/'], - // not implement yet - // [t('goBack')]: '⌘ + [', - // [t('goForward')]: '⌘ + ]', + [t('goBack')]: ['⌘ + ['], + [t('goForward')]: ['⌘ + ]'], }), [t] ); diff --git a/apps/core/src/hooks/use-shortcut-commands.ts b/apps/core/src/hooks/use-shortcut-commands.ts new file mode 100644 index 000000000..e8003ddac --- /dev/null +++ b/apps/core/src/hooks/use-shortcut-commands.ts @@ -0,0 +1,49 @@ +import { + PreconditionStrategy, + registerAffineCommand, +} from '@toeverything/infra/command'; +import { useEffect } from 'react'; + +export function useRegisterBlocksuiteEditorCommands( + back: () => unknown, + forward: () => unknown +) { + useEffect(() => { + const unsubs: Array<() => void> = []; + + unsubs.push( + registerAffineCommand({ + id: 'affine:shortcut-history-go-back', + category: 'affine:general', + preconditionStrategy: PreconditionStrategy.Never, + icon: 'none', + label: 'go back', + keyBinding: { + binding: '$mod+[', + }, + run() { + back(); + }, + }) + ); + unsubs.push( + registerAffineCommand({ + id: 'affine:shortcut-history-go-forward', + category: 'affine:general', + preconditionStrategy: PreconditionStrategy.Never, + icon: 'none', + label: 'go forward', + keyBinding: { + binding: '$mod+]', + }, + run() { + forward(); + }, + }) + ); + + return () => { + unsubs.forEach(unsub => unsub()); + }; + }, [back, forward]); +} diff --git a/packages/component/src/components/app-sidebar/index.tsx b/packages/component/src/components/app-sidebar/index.tsx index 49e3b597e..78afc6023 100644 --- a/packages/component/src/components/app-sidebar/index.tsx +++ b/packages/component/src/components/app-sidebar/index.tsx @@ -110,7 +110,10 @@ export function AppSidebar(props: AppSidebarProps): ReactElement { data-enable-animation={enableAnimation && !isResizing} >