feat: create first page (#1410)
This commit is contained in:
@@ -67,16 +67,20 @@ export const BlockSuiteEditor = (props: EditorProps) => {
|
|||||||
} else {
|
} else {
|
||||||
console.debug('Initializing page with default content');
|
console.debug('Initializing page with default content');
|
||||||
// Add page block and surface block at root level
|
// Add page block and surface block at root level
|
||||||
const title =
|
const isFirstPage = page.meta.init === true;
|
||||||
localStorage.getItem(kFirstPage) === null ? exampleTitle : undefined;
|
if (isFirstPage) {
|
||||||
|
page.workspace.setPageMeta(page.id, {
|
||||||
|
init: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const title = isFirstPage ? exampleTitle : undefined;
|
||||||
const pageBlockId = page.addBlockByFlavour('affine:page', {
|
const pageBlockId = page.addBlockByFlavour('affine:page', {
|
||||||
title: new page.Text(title),
|
title: new page.Text(title),
|
||||||
});
|
});
|
||||||
page.addBlockByFlavour('affine:surface', {}, null);
|
page.addBlockByFlavour('affine:surface', {}, null);
|
||||||
const frameId = page.addBlockByFlavour('affine:frame', {}, pageBlockId);
|
const frameId = page.addBlockByFlavour('affine:frame', {}, pageBlockId);
|
||||||
page.addBlockByFlavour('affine:paragraph', {}, frameId);
|
page.addBlockByFlavour('affine:paragraph', {}, frameId);
|
||||||
if (localStorage.getItem(kFirstPage) === null) {
|
if (isFirstPage) {
|
||||||
// fixme(himself65): remove
|
|
||||||
editor.clipboard.importMarkdown(exampleText, frameId);
|
editor.clipboard.importMarkdown(exampleText, frameId);
|
||||||
props.blockSuiteWorkspace.setPageMeta(page.id, { title });
|
props.blockSuiteWorkspace.setPageMeta(page.id, { title });
|
||||||
localStorage.setItem(kFirstPage, 'true');
|
localStorage.setItem(kFirstPage, 'true');
|
||||||
|
|||||||
@@ -26,18 +26,12 @@ export function useCreateFirstWorkspace() {
|
|||||||
const workspace = await LocalPlugin.CRUD.get(id);
|
const workspace = await LocalPlugin.CRUD.get(id);
|
||||||
assertExists(workspace);
|
assertExists(workspace);
|
||||||
assertEquals(workspace.id, id);
|
assertEquals(workspace.id, id);
|
||||||
const newPageId = nanoid();
|
set([
|
||||||
workspace.blockSuiteWorkspace.slots.pageAdded.once(pageId => {
|
{
|
||||||
assertEquals(pageId, newPageId);
|
id: workspace.id,
|
||||||
set(workspaces => [
|
flavour: RemWorkspaceFlavour.LOCAL,
|
||||||
...workspaces,
|
},
|
||||||
{
|
]);
|
||||||
id: workspace.id,
|
|
||||||
flavour: RemWorkspaceFlavour.LOCAL,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
workspace.blockSuiteWorkspace.createPage(newPageId);
|
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
jotaiWorkspaces.length === 0 &&
|
jotaiWorkspaces.length === 0 &&
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ declare module '@blocksuite/store' {
|
|||||||
favorite?: boolean;
|
favorite?: boolean;
|
||||||
trash?: boolean;
|
trash?: boolean;
|
||||||
trashDate?: number;
|
trashDate?: number;
|
||||||
|
// whether to create the page with the default template
|
||||||
|
init?: boolean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { useTranslation } from '@affine/i18n';
|
import { useTranslation } from '@affine/i18n';
|
||||||
import { FolderIcon } from '@blocksuite/icons';
|
import { FolderIcon } from '@blocksuite/icons';
|
||||||
import { assertExists } from '@blocksuite/store';
|
import { assertExists, nanoid } from '@blocksuite/store';
|
||||||
import Head from 'next/head';
|
import Head from 'next/head';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import React, { useCallback } from 'react';
|
import React, { useCallback, useEffect } from 'react';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
QueryParamError,
|
QueryParamError,
|
||||||
@@ -21,6 +21,35 @@ const AllPage: NextPageWithLayout = () => {
|
|||||||
const [currentWorkspace] = useCurrentWorkspace();
|
const [currentWorkspace] = useCurrentWorkspace();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
useSyncRouterWithCurrentWorkspace(router);
|
useSyncRouterWithCurrentWorkspace(router);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!router.isReady) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (currentWorkspace) {
|
||||||
|
const doc = currentWorkspace.blockSuiteWorkspace.doc;
|
||||||
|
if (doc.store.clients.size === 1) {
|
||||||
|
const items = [...doc.store.clients.values()][0];
|
||||||
|
if (items.length <= 1) {
|
||||||
|
// this is a new workspace, so we should redirect to the new page
|
||||||
|
const pageId = nanoid();
|
||||||
|
currentWorkspace.blockSuiteWorkspace.slots.pageAdded.once(id => {
|
||||||
|
currentWorkspace.blockSuiteWorkspace.setPageMeta(id, {
|
||||||
|
init: true,
|
||||||
|
});
|
||||||
|
assertExists(pageId, id);
|
||||||
|
router.push({
|
||||||
|
pathname: '/workspace/[workspaceId]/[pageId]',
|
||||||
|
query: {
|
||||||
|
workspaceId: currentWorkspace.id,
|
||||||
|
pageId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
currentWorkspace.blockSuiteWorkspace.createPage(pageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [currentWorkspace, router]);
|
||||||
const onClickPage = useCallback(
|
const onClickPage = useCallback(
|
||||||
(pageId: string, newTab?: boolean) => {
|
(pageId: string, newTab?: boolean) => {
|
||||||
assertExists(currentWorkspace);
|
assertExists(currentWorkspace);
|
||||||
|
|||||||
Reference in New Issue
Block a user