From 6b350b17354e58e6fc8181f156440bb9a875baaf Mon Sep 17 00:00:00 2001 From: Cats Juice Date: Tue, 30 Jan 2024 23:08:25 +0800 Subject: [PATCH] feat(core): append to today's joruanl via CMDK (#5692) --- .../core/src/components/pure/cmdk/data.tsx | 28 ++++++++++++++++++- .../src/components/pure/cmdk/not-found.tsx | 4 +-- .../frontend/core/src/hooks/use-journal.ts | 17 +++++++++++ packages/frontend/i18n/src/resources/en.json | 1 + 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/packages/frontend/core/src/components/pure/cmdk/data.tsx b/packages/frontend/core/src/components/pure/cmdk/data.tsx index 298dcd3b0b..fa305d98e4 100644 --- a/packages/frontend/core/src/components/pure/cmdk/data.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/data.tsx @@ -5,7 +5,12 @@ import { } from '@affine/core/hooks/use-block-suite-page-meta'; import type { Collection } from '@affine/env/filter'; import { useAFFiNEI18N } from '@affine/i18n/hooks'; -import { EdgelessIcon, PageIcon, ViewLayersIcon } from '@blocksuite/icons'; +import { + EdgelessIcon, + PageIcon, + TodayIcon, + ViewLayersIcon, +} from '@blocksuite/icons'; import type { PageMeta } from '@blocksuite/store'; import { Workspace } from '@toeverything/infra'; import { getCurrentStore } from '@toeverything/infra/atom'; @@ -23,6 +28,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react'; import { pageSettingsAtom, recentPageIdsBaseAtom } from '../../../atoms'; import { currentPageIdAtom } from '../../../atoms/mode'; +import { useJournalHelper } from '../../../hooks/use-journal'; import { useNavigateHelper } from '../../../hooks/use-navigate-helper'; import { CollectionService } from '../../../modules/collection'; import { WorkspaceSubPath } from '../../../shared'; @@ -161,6 +167,7 @@ export const usePageCommands = () => { const pageMetaHelper = usePageMetaHelper(workspace.blockSuiteWorkspace); const query = useAtomValue(cmdkQueryAtom); const navigationHelper = useNavigateHelper(); + const journalHelper = useJournalHelper(workspace.blockSuiteWorkspace); const t = useAFFiNEI18N(); const [searchTime, setSearchTime] = useState(0); @@ -253,6 +260,24 @@ export const usePageCommands = () => { // check if the pages have exact match. if not, we should show the "create page" command if (results.every(command => command.originalValue !== query)) { + results.push({ + id: 'affine:pages:append-to-journal', + label: t['com.affine.journal.cmdk.append-to-today'](), + value: 'affine::append-journal' + query, // hack to make the page always showing in the search result + category: 'affine:creation', + run: async () => { + const appendRes = await journalHelper.appendContentToToday(query); + if (!appendRes) return; + const { page, blockId } = appendRes; + navigationHelper.jumpToPageBlock( + page.workspace.id, + page.id, + blockId + ); + }, + icon: , + }); + results.push({ id: 'affine:pages:create-page', label: t['com.affine.cmdk.affine.create-new-page-as']({ @@ -294,6 +319,7 @@ export const usePageCommands = () => { t, workspace, pages, + journalHelper, pageHelper, pageMetaHelper, ]); diff --git a/packages/frontend/core/src/components/pure/cmdk/not-found.tsx b/packages/frontend/core/src/components/pure/cmdk/not-found.tsx index fd96315339..0d73a9e2a5 100644 --- a/packages/frontend/core/src/components/pure/cmdk/not-found.tsx +++ b/packages/frontend/core/src/components/pure/cmdk/not-found.tsx @@ -7,9 +7,9 @@ import * as styles from './not-found.css'; export const NotFoundGroup = () => { const query = useAtomValue(cmdkQueryAtom); - // hack: we know that the filtered count is 2 when there is no result (create page & edgeless) + // hack: we know that the filtered count is 3 when there is no result (create page & edgeless & append to journal) const renderNoResult = - useCommandState(state => state.filtered.count === 2) || false; + useCommandState(state => state.filtered.count === 3) || false; if (!renderNoResult) { return null; diff --git a/packages/frontend/core/src/hooks/use-journal.ts b/packages/frontend/core/src/hooks/use-journal.ts index 8f6a04827a..3a063d0bb2 100644 --- a/packages/frontend/core/src/hooks/use-journal.ts +++ b/packages/frontend/core/src/hooks/use-journal.ts @@ -104,6 +104,21 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => { [getJournalDateString] ); + const appendContentToToday = useCallback( + async (content: string) => { + if (!content) return; + const page = getJournalByDate(dayjs().format(JOURNAL_DATE_FORMAT)); + if (!page) return; + const blockId = page.addBlock( + 'affine:paragraph', + { text: new page.Text(content) }, + page.getBlockByFlavour('affine:note')[0].id + ); + return { page, blockId }; + }, + [getJournalByDate] + ); + return useMemo( () => ({ getJournalsByDate, @@ -112,6 +127,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => { getLocalizedJournalDateString, isPageJournal, isPageTodayJournal, + appendContentToToday, }), [ getJournalByDate, @@ -120,6 +136,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => { getLocalizedJournalDateString, isPageJournal, isPageTodayJournal, + appendContentToToday, ] ); }; diff --git a/packages/frontend/i18n/src/resources/en.json b/packages/frontend/i18n/src/resources/en.json index bc317d99d2..39943c4d4e 100644 --- a/packages/frontend/i18n/src/resources/en.json +++ b/packages/frontend/i18n/src/resources/en.json @@ -1069,5 +1069,6 @@ "com.affine.journal.daily-count-updated-empty-tips": "You haven't updated anything yet", "com.affine.journal.conflict-show-more": "{{count}} more articles", "com.affine.journal.app-sidebar-title": "Journals", + "com.affine.journal.cmdk.append-to-today": "Append to Journal", "com.affine.editor.reference-not-found": "Linked page not found" }