feat(core): append to today's joruanl via CMDK (#5692)
This commit is contained in:
@@ -5,7 +5,12 @@ import {
|
|||||||
} from '@affine/core/hooks/use-block-suite-page-meta';
|
} from '@affine/core/hooks/use-block-suite-page-meta';
|
||||||
import type { Collection } from '@affine/env/filter';
|
import type { Collection } from '@affine/env/filter';
|
||||||
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
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 type { PageMeta } from '@blocksuite/store';
|
||||||
import { Workspace } from '@toeverything/infra';
|
import { Workspace } from '@toeverything/infra';
|
||||||
import { getCurrentStore } from '@toeverything/infra/atom';
|
import { getCurrentStore } from '@toeverything/infra/atom';
|
||||||
@@ -23,6 +28,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
|
|||||||
|
|
||||||
import { pageSettingsAtom, recentPageIdsBaseAtom } from '../../../atoms';
|
import { pageSettingsAtom, recentPageIdsBaseAtom } from '../../../atoms';
|
||||||
import { currentPageIdAtom } from '../../../atoms/mode';
|
import { currentPageIdAtom } from '../../../atoms/mode';
|
||||||
|
import { useJournalHelper } from '../../../hooks/use-journal';
|
||||||
import { useNavigateHelper } from '../../../hooks/use-navigate-helper';
|
import { useNavigateHelper } from '../../../hooks/use-navigate-helper';
|
||||||
import { CollectionService } from '../../../modules/collection';
|
import { CollectionService } from '../../../modules/collection';
|
||||||
import { WorkspaceSubPath } from '../../../shared';
|
import { WorkspaceSubPath } from '../../../shared';
|
||||||
@@ -161,6 +167,7 @@ export const usePageCommands = () => {
|
|||||||
const pageMetaHelper = usePageMetaHelper(workspace.blockSuiteWorkspace);
|
const pageMetaHelper = usePageMetaHelper(workspace.blockSuiteWorkspace);
|
||||||
const query = useAtomValue(cmdkQueryAtom);
|
const query = useAtomValue(cmdkQueryAtom);
|
||||||
const navigationHelper = useNavigateHelper();
|
const navigationHelper = useNavigateHelper();
|
||||||
|
const journalHelper = useJournalHelper(workspace.blockSuiteWorkspace);
|
||||||
const t = useAFFiNEI18N();
|
const t = useAFFiNEI18N();
|
||||||
|
|
||||||
const [searchTime, setSearchTime] = useState<number>(0);
|
const [searchTime, setSearchTime] = useState<number>(0);
|
||||||
@@ -253,6 +260,24 @@ export const usePageCommands = () => {
|
|||||||
|
|
||||||
// check if the pages have exact match. if not, we should show the "create page" command
|
// check if the pages have exact match. if not, we should show the "create page" command
|
||||||
if (results.every(command => command.originalValue !== query)) {
|
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: <TodayIcon />,
|
||||||
|
});
|
||||||
|
|
||||||
results.push({
|
results.push({
|
||||||
id: 'affine:pages:create-page',
|
id: 'affine:pages:create-page',
|
||||||
label: t['com.affine.cmdk.affine.create-new-page-as']({
|
label: t['com.affine.cmdk.affine.create-new-page-as']({
|
||||||
@@ -294,6 +319,7 @@ export const usePageCommands = () => {
|
|||||||
t,
|
t,
|
||||||
workspace,
|
workspace,
|
||||||
pages,
|
pages,
|
||||||
|
journalHelper,
|
||||||
pageHelper,
|
pageHelper,
|
||||||
pageMetaHelper,
|
pageMetaHelper,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import * as styles from './not-found.css';
|
|||||||
|
|
||||||
export const NotFoundGroup = () => {
|
export const NotFoundGroup = () => {
|
||||||
const query = useAtomValue(cmdkQueryAtom);
|
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 =
|
const renderNoResult =
|
||||||
useCommandState(state => state.filtered.count === 2) || false;
|
useCommandState(state => state.filtered.count === 3) || false;
|
||||||
|
|
||||||
if (!renderNoResult) {
|
if (!renderNoResult) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -104,6 +104,21 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
|
|||||||
[getJournalDateString]
|
[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(
|
return useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
getJournalsByDate,
|
getJournalsByDate,
|
||||||
@@ -112,6 +127,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
|
|||||||
getLocalizedJournalDateString,
|
getLocalizedJournalDateString,
|
||||||
isPageJournal,
|
isPageJournal,
|
||||||
isPageTodayJournal,
|
isPageTodayJournal,
|
||||||
|
appendContentToToday,
|
||||||
}),
|
}),
|
||||||
[
|
[
|
||||||
getJournalByDate,
|
getJournalByDate,
|
||||||
@@ -120,6 +136,7 @@ export const useJournalHelper = (workspace: BlockSuiteWorkspace) => {
|
|||||||
getLocalizedJournalDateString,
|
getLocalizedJournalDateString,
|
||||||
isPageJournal,
|
isPageJournal,
|
||||||
isPageTodayJournal,
|
isPageTodayJournal,
|
||||||
|
appendContentToToday,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1069,5 +1069,6 @@
|
|||||||
"com.affine.journal.daily-count-updated-empty-tips": "You haven't updated anything yet",
|
"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.conflict-show-more": "{{count}} more articles",
|
||||||
"com.affine.journal.app-sidebar-title": "Journals",
|
"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"
|
"com.affine.editor.reference-not-found": "Linked page not found"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user