From 746a6d9ab56893bab9ddf027c681b5d215c15b9b Mon Sep 17 00:00:00 2001 From: himself65 Date: Mon, 23 Jan 2023 13:52:40 -0600 Subject: [PATCH] fix: listen on title change --- .../workspace/[workspaceId]/[pageId].tsx | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/app/src/pages/workspace/[workspaceId]/[pageId].tsx b/packages/app/src/pages/workspace/[workspaceId]/[pageId].tsx index 05c2c17eba..77cbdb5a18 100644 --- a/packages/app/src/pages/workspace/[workspaceId]/[pageId].tsx +++ b/packages/app/src/pages/workspace/[workspaceId]/[pageId].tsx @@ -16,6 +16,7 @@ import type { NextPageWithLayout } from '../..//_app'; import WorkspaceLayout from '@/components/workspace-layout'; import { useRouter } from 'next/router'; import { usePageHelper } from '@/hooks/use-page-helper'; +import { Disposable } from '@blocksuite/store/dist/utils/disposable'; const StyledEditorContainer = styled('div')(() => { return { @@ -24,6 +25,8 @@ const StyledEditorContainer = styled('div')(() => { }; }); +const firstPageTitle = 'Welcome to AFFiNE Alpha "Abbey Wood"' as const; + const Page: NextPageWithLayout = () => { const editorContainer = useRef(null); const { createEditor, setEditor, currentPage, currentWorkspace } = @@ -36,6 +39,7 @@ const Page: NextPageWithLayout = () => { node.removeChild(node.firstChild); } }; + let disposable: Disposable | undefined; const editor = createEditor?.current?.(currentPage!); if (editor) { @@ -45,11 +49,7 @@ const Page: NextPageWithLayout = () => { const isFirstPage = currentWorkspace?.meta.pageMetas.length === 1; // Can not use useCurrentPageMeta to get new title, cause meta title will trigger rerender, but the second time can not remove title const { title: metaTitle } = currentPage!.meta; - const title = metaTitle - ? metaTitle - : isFirstPage - ? 'Welcome to AFFiNE Alpha "Abbey Wood"' - : ''; + const title = metaTitle ? metaTitle : isFirstPage ? firstPageTitle : ''; currentWorkspace?.setPageMeta(currentPage!.id, { title }); const pageId = currentPage!.addBlock({ @@ -72,11 +72,17 @@ const Page: NextPageWithLayout = () => { }); } currentPage!.resetHistory(); + disposable = editor.pageBlockModel?.propsUpdated.on(() => { + document.title = isFirstPage + ? firstPageTitle + : currentPage?.meta.title || 'Untitled'; + }); } } - - document.title = currentPage?.meta.title || 'Untitled'; - return ret; + return () => { + ret(); + disposable?.dispose(); + }; }, [currentWorkspace, currentPage, createEditor, setEditor]); return (