From de81527e294ee99865ae7218fa4d22ad0660bf34 Mon Sep 17 00:00:00 2001 From: pengx17 Date: Wed, 5 Jun 2024 14:09:20 +0000 Subject: [PATCH] refactor: move command registry to frontend/core (#7135) move command registeration related logic out of infra module. --- packages/common/infra/package.json | 1 - packages/common/infra/src/index.ts | 1 - packages/frontend/core/package.json | 1 + .../core/src/commands/affine-creation.tsx | 2 +- .../frontend/core/src/commands/affine-help.tsx | 2 +- .../frontend/core/src/commands/affine-layout.tsx | 2 +- .../core/src/commands/affine-navigation.tsx | 2 +- .../core/src/commands/affine-settings.tsx | 7 ++----- .../frontend/core/src/commands/affine-updates.tsx | 3 ++- packages/frontend/core/src/commands/index.ts | 1 + .../core/src/commands/registry}/README.md | 0 .../core/src/commands/registry}/command.ts | 0 .../core/src/commands/registry}/index.ts | 0 .../core/src/commands/registry}/registry.ts | 0 .../core/src/components/pure/cmdk/data-hooks.tsx | 15 +++++++-------- .../src/components/pure/cmdk/filter-commands.ts | 2 +- .../core/src/components/pure/cmdk/main.tsx | 2 +- .../core/src/components/pure/cmdk/types.ts | 3 ++- .../use-register-blocksuite-editor-commands.tsx | 6 ++++-- .../affine/use-register-copy-link-commands.tsx | 2 +- .../affine/use-register-find-in-page-commands.ts | 3 ++- .../view/use-register-navigation-commands.ts | 4 ++-- packages/frontend/electron/package.json | 1 - yarn.lock | 3 +-- 24 files changed, 31 insertions(+), 32 deletions(-) rename packages/{common/infra/src/command => frontend/core/src/commands/registry}/README.md (100%) rename packages/{common/infra/src/command => frontend/core/src/commands/registry}/command.ts (100%) rename packages/{common/infra/src/command => frontend/core/src/commands/registry}/index.ts (100%) rename packages/{common/infra/src/command => frontend/core/src/commands/registry}/registry.ts (100%) diff --git a/packages/common/infra/package.json b/packages/common/infra/package.json index 42b27bfc3..6ee89f2c4 100644 --- a/packages/common/infra/package.json +++ b/packages/common/infra/package.json @@ -23,7 +23,6 @@ "lodash-es": "^4.17.21", "nanoid": "^5.0.7", "react": "18.3.1", - "tinykeys": "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch", "yjs": "^13.6.14", "zod": "^3.22.4" }, diff --git a/packages/common/infra/src/index.ts b/packages/common/infra/src/index.ts index 03155ccf9..e06e9400f 100644 --- a/packages/common/infra/src/index.ts +++ b/packages/common/infra/src/index.ts @@ -1,7 +1,6 @@ export * from './app-config-storage'; export * from './atom'; export * from './blocksuite'; -export * from './command'; export * from './framework'; export * from './initialization'; export * from './livedata'; diff --git a/packages/frontend/core/package.json b/packages/frontend/core/package.json index d73370d5e..eccc9c293 100644 --- a/packages/frontend/core/package.json +++ b/packages/frontend/core/package.json @@ -83,6 +83,7 @@ "ses": "^1.4.1", "socket.io-client": "^4.7.5", "swr": "2.2.5", + "tinykeys": "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch", "uuid": "^9.0.1", "valtio": "^1.13.2", "y-protocols": "^1.0.6", diff --git a/packages/frontend/core/src/commands/affine-creation.tsx b/packages/frontend/core/src/commands/affine-creation.tsx index 3dd502e76..2f7cdd4b0 100644 --- a/packages/frontend/core/src/commands/affine-creation.tsx +++ b/packages/frontend/core/src/commands/affine-creation.tsx @@ -1,10 +1,10 @@ import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { ImportIcon, PlusIcon } from '@blocksuite/icons'; -import { registerAffineCommand } from '@toeverything/infra'; import type { createStore } from 'jotai'; import { openCreateWorkspaceModalAtom } from '../atoms'; import type { usePageHelper } from '../components/blocksuite/block-suite-page-list/utils'; +import { registerAffineCommand } from './registry'; export function registerAffineCreationCommands({ store, diff --git a/packages/frontend/core/src/commands/affine-help.tsx b/packages/frontend/core/src/commands/affine-help.tsx index 130f86b30..7edb5708e 100644 --- a/packages/frontend/core/src/commands/affine-help.tsx +++ b/packages/frontend/core/src/commands/affine-help.tsx @@ -1,10 +1,10 @@ import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { ContactWithUsIcon, NewIcon } from '@blocksuite/icons'; -import { registerAffineCommand } from '@toeverything/infra'; import type { createStore } from 'jotai'; import { openSettingModalAtom } from '../atoms'; import { popupWindow } from '../utils'; +import { registerAffineCommand } from './registry'; export function registerAffineHelpCommands({ t, diff --git a/packages/frontend/core/src/commands/affine-layout.tsx b/packages/frontend/core/src/commands/affine-layout.tsx index 6906f5ea7..748accb07 100644 --- a/packages/frontend/core/src/commands/affine-layout.tsx +++ b/packages/frontend/core/src/commands/affine-layout.tsx @@ -1,9 +1,9 @@ import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { SidebarIcon } from '@blocksuite/icons'; -import { registerAffineCommand } from '@toeverything/infra'; import type { createStore } from 'jotai'; import { appSidebarOpenAtom } from '../components/app-sidebar'; +import { registerAffineCommand } from './registry'; export function registerAffineLayoutCommands({ t, diff --git a/packages/frontend/core/src/commands/affine-navigation.tsx b/packages/frontend/core/src/commands/affine-navigation.tsx index 5e6454175..8ae99bd28 100644 --- a/packages/frontend/core/src/commands/affine-navigation.tsx +++ b/packages/frontend/core/src/commands/affine-navigation.tsx @@ -2,12 +2,12 @@ import { WorkspaceSubPath } from '@affine/core/shared'; import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { ArrowRightBigIcon } from '@blocksuite/icons'; import type { DocCollection } from '@blocksuite/store'; -import { registerAffineCommand } from '@toeverything/infra'; import type { createStore } from 'jotai'; import { openSettingModalAtom, openWorkspaceListModalAtom } from '../atoms'; import type { useNavigateHelper } from '../hooks/use-navigate-helper'; import { mixpanel } from '../utils/mixpanel'; +import { registerAffineCommand } from './registry'; export function registerAffineNavigationCommands({ t, diff --git a/packages/frontend/core/src/commands/affine-settings.tsx b/packages/frontend/core/src/commands/affine-settings.tsx index 242993194..1bc2b56e6 100644 --- a/packages/frontend/core/src/commands/affine-settings.tsx +++ b/packages/frontend/core/src/commands/affine-settings.tsx @@ -1,17 +1,14 @@ import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { SettingsIcon } from '@blocksuite/icons'; import type { AffineEditorContainer } from '@blocksuite/presets'; -import { - appSettingAtom, - PreconditionStrategy, - registerAffineCommand, -} from '@toeverything/infra'; +import { appSettingAtom } from '@toeverything/infra'; import type { createStore } from 'jotai'; import type { useTheme } from 'next-themes'; import { openQuickSearchModalAtom } from '../atoms'; import type { useLanguageHelper } from '../hooks/affine/use-language-helper'; import { mixpanel } from '../utils'; +import { PreconditionStrategy, registerAffineCommand } from './registry'; export function registerAffineSettingsCommands({ t, diff --git a/packages/frontend/core/src/commands/affine-updates.tsx b/packages/frontend/core/src/commands/affine-updates.tsx index ba2274dee..28092f10f 100644 --- a/packages/frontend/core/src/commands/affine-updates.tsx +++ b/packages/frontend/core/src/commands/affine-updates.tsx @@ -2,9 +2,10 @@ import { updateReadyAtom } from '@affine/core/hooks/use-app-updater'; import { apis } from '@affine/electron-api'; import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import { ResetIcon } from '@blocksuite/icons'; -import { registerAffineCommand } from '@toeverything/infra'; import type { createStore } from 'jotai'; +import { registerAffineCommand } from './registry'; + export function registerAffineUpdatesCommands({ t, store, diff --git a/packages/frontend/core/src/commands/index.ts b/packages/frontend/core/src/commands/index.ts index b2b5ddafa..8a496f145 100644 --- a/packages/frontend/core/src/commands/index.ts +++ b/packages/frontend/core/src/commands/index.ts @@ -4,3 +4,4 @@ export * from './affine-layout'; export * from './affine-navigation'; export * from './affine-settings'; export * from './affine-updates'; +export * from './registry'; diff --git a/packages/common/infra/src/command/README.md b/packages/frontend/core/src/commands/registry/README.md similarity index 100% rename from packages/common/infra/src/command/README.md rename to packages/frontend/core/src/commands/registry/README.md diff --git a/packages/common/infra/src/command/command.ts b/packages/frontend/core/src/commands/registry/command.ts similarity index 100% rename from packages/common/infra/src/command/command.ts rename to packages/frontend/core/src/commands/registry/command.ts diff --git a/packages/common/infra/src/command/index.ts b/packages/frontend/core/src/commands/registry/index.ts similarity index 100% rename from packages/common/infra/src/command/index.ts rename to packages/frontend/core/src/commands/registry/index.ts diff --git a/packages/common/infra/src/command/registry.ts b/packages/frontend/core/src/commands/registry/registry.ts similarity index 100% rename from packages/common/infra/src/command/registry.ts rename to packages/frontend/core/src/commands/registry/registry.ts diff --git a/packages/frontend/core/src/components/pure/cmdk/data-hooks.tsx b/packages/frontend/core/src/components/pure/cmdk/data-hooks.tsx index 402c99cd7..1ca902190 100644 --- a/packages/frontend/core/src/components/pure/cmdk/data-hooks.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/data-hooks.tsx @@ -1,3 +1,9 @@ +import { + type AffineCommand, + AffineCommandRegistry, + type CommandCategory, + PreconditionStrategy, +} from '@affine/core/commands'; import { useDocMetaHelper } from '@affine/core/hooks/use-block-suite-page-meta'; import { useGetDocCollectionPageTitle } from '@affine/core/hooks/use-block-suite-workspace-page-title'; import { useJournalHelper } from '@affine/core/hooks/use-journal'; @@ -12,17 +18,10 @@ import { TodayIcon, ViewLayersIcon, } from '@blocksuite/icons'; -import type { - AffineCommand, - CommandCategory, - DocRecord, - Workspace, -} from '@toeverything/infra'; +import type { DocRecord, Workspace } from '@toeverything/infra'; import { - AffineCommandRegistry, DocsService, GlobalContextService, - PreconditionStrategy, useLiveData, useService, WorkspaceService, diff --git a/packages/frontend/core/src/components/pure/cmdk/filter-commands.ts b/packages/frontend/core/src/components/pure/cmdk/filter-commands.ts index 6c442b655..9bb20138e 100644 --- a/packages/frontend/core/src/components/pure/cmdk/filter-commands.ts +++ b/packages/frontend/core/src/components/pure/cmdk/filter-commands.ts @@ -1,4 +1,4 @@ -import type { CommandCategory } from '@toeverything/infra'; +import type { CommandCategory } from '@affine/core/commands'; import { groupBy } from 'lodash-es'; import { commandScore } from './command-score'; diff --git a/packages/frontend/core/src/components/pure/cmdk/main.tsx b/packages/frontend/core/src/components/pure/cmdk/main.tsx index 7458eb288..95588701f 100644 --- a/packages/frontend/core/src/components/pure/cmdk/main.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/main.tsx @@ -1,10 +1,10 @@ import { Loading } from '@affine/component/ui/loading'; +import type { CommandCategory } from '@affine/core/commands'; import { formatDate } from '@affine/core/components/page-list'; import { useDocEngineStatus } from '@affine/core/hooks/affine/use-doc-engine-status'; import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks'; import type { DocMeta } from '@blocksuite/store'; -import type { CommandCategory } from '@toeverything/infra'; import clsx from 'clsx'; import { Command } from 'cmdk'; import { useDebouncedValue } from 'foxact/use-debounced-value'; diff --git a/packages/frontend/core/src/components/pure/cmdk/types.ts b/packages/frontend/core/src/components/pure/cmdk/types.ts index b73105b12..dbf6af500 100644 --- a/packages/frontend/core/src/components/pure/cmdk/types.ts +++ b/packages/frontend/core/src/components/pure/cmdk/types.ts @@ -1,4 +1,5 @@ -import type { CommandCategory, DocMode } from '@toeverything/infra'; +import type { CommandCategory } from '@affine/core/commands'; +import type { DocMode } from '@toeverything/infra'; export interface CommandContext { docMode: DocMode | undefined; diff --git a/packages/frontend/core/src/hooks/affine/use-register-blocksuite-editor-commands.tsx b/packages/frontend/core/src/hooks/affine/use-register-blocksuite-editor-commands.tsx index ee11999e8..f7704eda4 100644 --- a/packages/frontend/core/src/hooks/affine/use-register-blocksuite-editor-commands.tsx +++ b/packages/frontend/core/src/hooks/affine/use-register-blocksuite-editor-commands.tsx @@ -1,4 +1,8 @@ import { toast } from '@affine/component'; +import { + PreconditionStrategy, + registerAffineCommand, +} from '@affine/core/commands'; import { useDocMetaHelper } from '@affine/core/hooks/use-block-suite-page-meta'; import { FavoriteItemsAdapter } from '@affine/core/modules/properties'; import { mixpanel } from '@affine/core/utils'; @@ -8,8 +12,6 @@ import { assertExists } from '@blocksuite/global/utils'; import { EdgelessIcon, HistoryIcon, PageIcon } from '@blocksuite/icons'; import { DocService, - PreconditionStrategy, - registerAffineCommand, useLiveData, useService, WorkspaceService, diff --git a/packages/frontend/core/src/hooks/affine/use-register-copy-link-commands.tsx b/packages/frontend/core/src/hooks/affine/use-register-copy-link-commands.tsx index 31d3a039f..d4beb9618 100644 --- a/packages/frontend/core/src/hooks/affine/use-register-copy-link-commands.tsx +++ b/packages/frontend/core/src/hooks/affine/use-register-copy-link-commands.tsx @@ -1,5 +1,5 @@ +import { registerAffineCommand } from '@affine/core/commands'; import { useSharingUrl } from '@affine/core/hooks/affine/use-share-url'; -import { registerAffineCommand } from '@toeverything/infra'; import { useEffect } from 'react'; export function useRegisterCopyLinkCommands({ diff --git a/packages/frontend/core/src/hooks/affine/use-register-find-in-page-commands.ts b/packages/frontend/core/src/hooks/affine/use-register-find-in-page-commands.ts index 4a5c140d5..11cb88d81 100644 --- a/packages/frontend/core/src/hooks/affine/use-register-find-in-page-commands.ts +++ b/packages/frontend/core/src/hooks/affine/use-register-find-in-page-commands.ts @@ -1,5 +1,6 @@ +import { registerAffineCommand } from '@affine/core/commands'; import { FindInPageService } from '@affine/core/modules/find-in-page/services/find-in-page'; -import { registerAffineCommand, useService } from '@toeverything/infra'; +import { useService } from '@toeverything/infra'; import { useCallback, useEffect } from 'react'; export function useRegisterFindInPageCommands() { diff --git a/packages/frontend/core/src/modules/navigation/view/use-register-navigation-commands.ts b/packages/frontend/core/src/modules/navigation/view/use-register-navigation-commands.ts index 400ff95ef..c349fed65 100644 --- a/packages/frontend/core/src/modules/navigation/view/use-register-navigation-commands.ts +++ b/packages/frontend/core/src/modules/navigation/view/use-register-navigation-commands.ts @@ -1,8 +1,8 @@ import { PreconditionStrategy, registerAffineCommand, - useService, -} from '@toeverything/infra'; +} from '@affine/core/commands'; +import { useService } from '@toeverything/infra'; import { useEffect } from 'react'; import { NavigatorService } from '../services/navigator'; diff --git a/packages/frontend/electron/package.json b/packages/frontend/electron/package.json index b8166bcaf..621018a21 100644 --- a/packages/frontend/electron/package.json +++ b/packages/frontend/electron/package.json @@ -68,7 +68,6 @@ "react-router-dom": "^6.22.3", "rxjs": "^7.8.1", "semver": "^7.6.0", - "tinykeys": "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch", "tree-kill": "^1.2.2", "ts-node": "^10.9.2", "undici": "^6.12.0", diff --git a/yarn.lock b/yarn.lock index ef84cb824..3fdbd52c7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -400,6 +400,7 @@ __metadata: ses: "npm:^1.4.1" socket.io-client: "npm:^4.7.5" swr: "npm:2.2.5" + tinykeys: "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch" uuid: "npm:^9.0.1" valtio: "npm:^1.13.2" vitest: "npm:1.6.0" @@ -490,7 +491,6 @@ __metadata: react-router-dom: "npm:^6.22.3" rxjs: "npm:^7.8.1" semver: "npm:^7.6.0" - tinykeys: "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch" tree-kill: "npm:^1.2.2" ts-node: "npm:^10.9.2" undici: "npm:^6.12.0" @@ -13908,7 +13908,6 @@ __metadata: nanoid: "npm:^5.0.7" react: "npm:^18.2.0" rxjs: "npm:^7.8.1" - tinykeys: "patch:tinykeys@npm%3A2.1.0#~/.yarn/patches/tinykeys-npm-2.1.0-819feeaed0.patch" vite: "npm:^5.2.8" vite-plugin-dts: "npm:3.9.1" vitest: "npm:1.6.0"