From 8ead48a6d8e381453d6879964e3573b40ff270b8 Mon Sep 17 00:00:00 2001 From: Qi <474021214@qq.com> Date: Thu, 16 Feb 2023 00:45:05 +0800 Subject: [PATCH] feat: modify interaction of new workspace (#1031) --- apps/web/src/components/editor/index.tsx | 26 +++++++++---------- .../src/components/workspace-modal/index.tsx | 2 +- .../workspace/[workspaceId]/[pageId].tsx | 14 ++++++++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx index 35afd53e4d..1f0f155453 100644 --- a/apps/web/src/components/editor/index.tsx +++ b/apps/web/src/components/editor/index.tsx @@ -2,7 +2,6 @@ import { useEffect, useRef } from 'react'; import type { Page, Workspace } from '@blocksuite/store'; import '@blocksuite/blocks'; import { EditorContainer } from '@blocksuite/editor'; -import exampleMarkdown from '@/templates/Welcome-to-AFFiNE-Alpha-Downhills.md'; import { styled } from '@affine/component'; const StyledEditorContainer = styled('div')(() => { @@ -16,11 +15,18 @@ type Props = { page: Page; workspace: Workspace; setEditor: (editor: EditorContainer) => void; + templateMarkdown?: string; + templateTitle?: string; }; -export const Editor = ({ page, workspace, setEditor }: Props) => { +export const Editor = ({ + page, + workspace, + setEditor, + templateMarkdown, + templateTitle = '', +}: Props) => { const editorContainer = useRef(null); - // const { currentWorkspace, currentPage, setEditor } = useAppState(); useEffect(() => { const ret = () => { const node = editorContainer.current; @@ -34,14 +40,9 @@ export const Editor = ({ page, workspace, setEditor }: Props) => { editorContainer.current?.appendChild(editor); if (page.isEmpty) { - const isFirstPage = workspace?.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 } = page.meta; - const title = metaTitle - ? metaTitle - : isFirstPage - ? 'Welcome to AFFiNE Alpha "Downhills"' - : ''; + const title = metaTitle ? metaTitle : templateTitle; workspace?.setPageMeta(page.id, { title }); const pageBlockId = page.addBlockByFlavour('affine:page', { title }); page.addBlockByFlavour('affine:surface', {}, null); @@ -49,10 +50,9 @@ export const Editor = ({ page, workspace, setEditor }: Props) => { const frameId = page.addBlockByFlavour('affine:frame', {}, pageBlockId); // Add paragraph block inside frame block // If this is a first page in workspace, init an introduction markdown - if (isFirstPage) { - editor.clipboard.importMarkdown(exampleMarkdown, frameId); + if (templateMarkdown) { + editor.clipboard.importMarkdown(templateMarkdown, frameId); workspace.setPageMeta(page.id, { title }); - page.resetHistory(); } else { page.addBlockByFlavour('affine:paragraph', {}, frameId); } @@ -61,7 +61,7 @@ export const Editor = ({ page, workspace, setEditor }: Props) => { setEditor(editor); return ret; - }, [workspace, page, setEditor]); + }, [workspace, page, setEditor, templateTitle, templateMarkdown]); return ; }; diff --git a/apps/web/src/components/workspace-modal/index.tsx b/apps/web/src/components/workspace-modal/index.tsx index 827a1d4d01..424f8c4ada 100644 --- a/apps/web/src/components/workspace-modal/index.tsx +++ b/apps/web/src/components/workspace-modal/index.tsx @@ -86,7 +86,7 @@ export const WorkspaceModal = ({ open, onClose }: WorkspaceModalProps) => { { - router.replace(`/workspace/${workspaceData.id}`); + router.replace(`/workspace/${workspaceData.id}/all`); onClose(); }} key={index} diff --git a/apps/web/src/pages/workspace/[workspaceId]/[pageId].tsx b/apps/web/src/pages/workspace/[workspaceId]/[pageId].tsx index 315bcd2ca6..7c04823a62 100644 --- a/apps/web/src/pages/workspace/[workspaceId]/[pageId].tsx +++ b/apps/web/src/pages/workspace/[workspaceId]/[pageId].tsx @@ -10,6 +10,8 @@ import dynamic from 'next/dynamic'; import Head from 'next/head'; import { useTranslation } from '@affine/i18n'; import { useGlobalState } from '@/store/app'; +import exampleMarkdown from '@/templates/Welcome-to-AFFiNE-Alpha-Downhills.md'; + const DynamicBlocksuite = dynamic(() => import('@/components/editor'), { ssr: false, }); @@ -44,6 +46,10 @@ const Page: NextPageWithLayout = () => { const { t } = useTranslation(); + // Only first workspace and first page will have template markdown + const shouldInitTemplateContent = + currentPage?.isEmpty && + currentWorkspace?.blocksuiteWorkspace?.meta.pageMetas.length === 1; return ( <> @@ -58,6 +64,14 @@ const Page: NextPageWithLayout = () => { page={currentPage} workspace={currentWorkspace.blocksuiteWorkspace} setEditor={setEditor} + templateMarkdown={ + shouldInitTemplateContent ? exampleMarkdown : undefined + } + templateTitle={ + shouldInitTemplateContent + ? 'Welcome to AFFiNE Alpha "Downhills"' + : undefined + } />