diff --git a/packages/frontend/core/src/desktop/pages/workspace/detail-page/detail-page.tsx b/packages/frontend/core/src/desktop/pages/workspace/detail-page/detail-page.tsx index 532da13295..1178dcf30e 100644 --- a/packages/frontend/core/src/desktop/pages/workspace/detail-page/detail-page.tsx +++ b/packages/frontend/core/src/desktop/pages/workspace/detail-page/detail-page.tsx @@ -22,6 +22,7 @@ import { DocService } from '@affine/core/modules/doc'; import { EditorService } from '@affine/core/modules/editor'; import { FeatureFlagService } from '@affine/core/modules/feature-flag'; import { GlobalContextService } from '@affine/core/modules/global-context'; +import { JournalService } from '@affine/core/modules/journal'; import { PeekViewService } from '@affine/core/modules/peek-view'; import { RecentDocsService } from '@affine/core/modules/quicksearch'; import { @@ -38,6 +39,8 @@ import { ServerFeature } from '@affine/graphql'; import track from '@affine/track'; import { DisposableGroup } from '@blocksuite/affine/global/disposable'; import { RefNodeSlotsProvider } from '@blocksuite/affine/inlines/reference'; +import { focusBlockEnd } from '@blocksuite/affine/shared/commands'; +import { getLastNoteBlock } from '@blocksuite/affine/shared/utils'; import { AiIcon, CommentIcon, @@ -184,10 +187,37 @@ const DetailPageImpl = memo(function DetailPageImpl() { useRegisterBlocksuiteEditorCommands(editor, isActiveView); + const journalService = useService(JournalService); + const isJournal = !!useLiveData(journalService.journalDate$(doc.id)); + const onLoad = useCallback( (editorContainer: AffineEditorContainer) => { const std = editorContainer.std; const disposable = new DisposableGroup(); + + // Check if journal and handle accordingly to set focus on input block. + if (isJournal) { + const rafId = requestAnimationFrame(() => { + try { + if (!editorContainer.isConnected) return; + const page = editorContainer.page; + const note = getLastNoteBlock(page); + const std = editorContainer.std; + if (note) { + const lastBlock = note.lastChild(); + if (lastBlock) { + const focusBlock = std.view.getBlock(lastBlock.id) ?? undefined; + std.command.exec(focusBlockEnd, { focusBlock, force: true }); + return; + } + } + std.command.exec(focusBlockEnd, { force: true }); + } catch (error) { + console.error('Failed to focus journal body', error); + } + }); + disposable.add(() => cancelAnimationFrame(rafId)); + } if (std) { const refNodeSlots = std.getOptional(RefNodeSlotsProvider); if (refNodeSlots) { @@ -265,7 +295,7 @@ const DetailPageImpl = memo(function DetailPageImpl() { disposable.dispose(); }; }, - [editor, workbench, peekView] + [editor, workbench, peekView, isJournal] ); const [hasScrollTop, setHasScrollTop] = useState(false);