feat: seperate createDoc and createStore (#11182)

This commit is contained in:
Saul-Mirone
2025-03-26 11:03:47 +00:00
parent d6093e1d66
commit 0a8d8e0a6b
70 changed files with 337 additions and 312 deletions
@@ -2,13 +2,14 @@ import { ZipTransformer } from '@blocksuite/affine/blocks/root';
import { AffineSchemas } from '@blocksuite/affine/schemas';
import { Schema, Text, type Workspace } from '@blocksuite/affine/store';
export async function affineSnapshot(collection: Workspace, id: string) {
const doc = collection.createDoc({ id });
const doc = collection.createDoc(id);
doc.load();
const store = doc.getStore();
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text('Affine Snapshot Test'),
});
doc.addBlock('affine:surface', {}, rootId);
store.addBlock('affine:surface', {}, rootId);
const path = '/apps/starter/data/snapshots/affine-default.zip';
const response = await fetch(path);
@@ -15,24 +15,25 @@ import { viewPresets } from '@blocksuite/data-view/view-presets';
import type { InitFn } from './utils.js';
export const database: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const doc = collection.createDoc(id);
const store = doc.getStore();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text('BlockSuite Playground'),
});
doc.addBlock('affine:surface', {}, rootId);
store.addBlock('affine:surface', {}, rootId);
// Add note block inside root block
const noteId = doc.addBlock('affine:note', {}, rootId);
const pId = doc.addBlock('affine:paragraph', {}, noteId);
const model = doc.getModelById(pId);
const noteId = store.addBlock('affine:note', {}, rootId);
const pId = store.addBlock('affine:paragraph', {}, noteId);
const model = store.getModelById(pId);
if (!model) {
throw new Error('model is not found');
}
const addDatabase = (title: string, group = true) => {
const databaseId = doc.addBlock(
const databaseId = store.addBlock(
'affine:database',
{
columns: [],
@@ -40,7 +41,7 @@ export const database: InitFn = (collection: Workspace, id: string) => {
},
noteId
);
const database = doc.getModelById(databaseId) as DatabaseBlockModel;
const database = store.getModelById(databaseId) as DatabaseBlockModel;
const datasource = new DatabaseBlockDataSource(database);
datasource.viewManager.viewAdd('table');
database.props.title = new Text(title);
@@ -74,7 +75,7 @@ export const database: InitFn = (collection: Workspace, id: string) => {
'h6',
];
paragraphTypes.forEach(type => {
const id = doc.addBlock(
const id = store.addBlock(
'affine:paragraph',
{ type: type, text: new Text(`Paragraph type ${type}`) },
databaseId
@@ -90,7 +91,7 @@ export const database: InitFn = (collection: Workspace, id: string) => {
const listTypes: ListType[] = ['numbered', 'bulleted', 'todo', 'toggle'];
listTypes.forEach(type => {
const id = doc.addBlock(
const id = store.addBlock(
'affine:list',
{ type: type, text: new Text(`List type ${type}`) },
databaseId
@@ -104,11 +105,11 @@ export const database: InitFn = (collection: Workspace, id: string) => {
}
});
// Add a paragraph after database
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
doc.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
datasource.viewManager.viewAdd(viewPresets.kanbanViewMeta.type);
doc.resetHistory();
@@ -3,30 +3,31 @@ import { Text, type Workspace } from '@blocksuite/affine/store';
import type { InitFn } from './utils.js';
export const embed: InitFn = (collection: Workspace, id: string) => {
const doc = collection.getDoc(id) ?? collection.createDoc({ id });
doc.doc.clear();
const doc = collection.getDoc(id) ?? collection.createDoc(id);
const store = doc.getStore();
doc.clear();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text(),
});
const surfaceId = doc.addBlock('affine:surface', {}, rootId);
const surfaceId = store.addBlock('affine:surface', {}, rootId);
// Add note block inside root block
const noteId = doc.addBlock('affine:note', {}, rootId);
const noteId = store.addBlock('affine:note', {}, rootId);
// Add paragraph block inside note block
doc.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
doc.addBlock(
store.addBlock(
'affine:embed-github',
{
url: 'https://github.com/toeverything/AFFiNE/pull/5453',
},
noteId
);
doc.addBlock(
store.addBlock(
'affine:embed-github',
{
url: 'https://www.github.com/toeverything/blocksuite/pull/5927',
@@ -35,7 +36,7 @@ export const embed: InitFn = (collection: Workspace, id: string) => {
},
surfaceId
);
doc.addBlock(
store.addBlock(
'affine:embed-github',
{
url: 'https://github.com/Milkdown/milkdown/pull/1215',
@@ -43,7 +44,7 @@ export const embed: InitFn = (collection: Workspace, id: string) => {
},
surfaceId
);
doc.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
});
doc.resetHistory();
@@ -3,21 +3,22 @@ import { Text, type Workspace } from '@blocksuite/affine/store';
import type { InitFn } from './utils.js';
export const empty: InitFn = (collection: Workspace, id: string) => {
const doc = collection.getDoc(id) ?? collection.createDoc({ id });
doc.doc.clear();
const doc = collection.getDoc(id) ?? collection.createDoc(id);
const store = doc.getStore();
doc.clear();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text(),
});
doc.addBlock('affine:surface', {}, rootId);
store.addBlock('affine:surface', {}, rootId);
// Add note block inside root block
const noteId = doc.addBlock('affine:note', {}, rootId);
const noteId = store.addBlock('affine:note', {}, rootId);
// Add paragraph block inside note block
doc.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
});
doc.resetHistory();
@@ -50,16 +50,17 @@ export const heavyWhiteboard: InitFn = (collection: Workspace, id: string) => {
const count = Number(params.get('count')) || SHAPES_COUNT;
const enableShapes = !!params.get('shapes');
const doc = collection.createDoc({ id });
const doc = collection.createDoc(id);
const store = doc.getStore();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text(),
});
const surfaceBlocks = enableShapes ? createShapes(count) : {};
doc.addBlock(
store.addBlock(
'affine:surface',
{
elements: new Boxed(native2Y(surfaceBlocks, { deep: false })) as Boxed<
@@ -74,7 +75,7 @@ export const heavyWhiteboard: InitFn = (collection: Workspace, id: string) => {
for (i = 0; i < count; i++) {
const x = Math.random() * RANGE - RANGE / 2;
const y = Math.random() * RANGE - RANGE / 2;
const noteId = doc.addBlock(
const noteId = store.addBlock(
'affine:note',
{
xywh: `[${x}, ${y}, 100, 50]` as SerializedXYWH,
@@ -82,7 +83,7 @@ export const heavyWhiteboard: InitFn = (collection: Workspace, id: string) => {
rootId
);
// Add paragraph block inside note block
doc.addBlock(
store.addBlock(
'affine:paragraph',
{
text: new Text('Note #' + i),
@@ -7,19 +7,20 @@ const params = new URLSearchParams(location.search);
export const heavy: InitFn = (collection: Workspace, docId: string) => {
const count = Number(params.get('count')) || 1000;
const doc = collection.createDoc({ id: docId });
const doc = collection.createDoc(docId);
const store = doc.getStore();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text(),
});
doc.addBlock('affine:surface', {}, rootId);
store.addBlock('affine:surface', {}, rootId);
// Add note block inside root block
const noteId = doc.addBlock('affine:note', {}, rootId);
const noteId = store.addBlock('affine:note', {}, rootId);
for (let i = 0; i < count; i++) {
// Add paragraph block inside note block
doc.addBlock(
store.addBlock(
'affine:paragraph',
{
text: new Text('Hello, world! ' + i),
@@ -3,13 +3,15 @@ import { Text, type Workspace } from '@blocksuite/affine/store';
import type { InitFn } from './utils.js';
export const linked: InitFn = (collection: Workspace, id: string) => {
const docA = collection.getDoc(id) ?? collection.createDoc({ id });
const docA =
collection.getDoc(id)?.getStore({ id }) ??
collection.createDoc(id).getStore({ id });
const docBId = 'doc:linked-page';
const docB = collection.createDoc({ id: docBId });
const docB = collection.createDoc(docBId).getStore();
const docCId = 'doc:linked-edgeless';
const docC = collection.createDoc({ id: docCId });
const docC = collection.createDoc(docCId).getStore();
docA.doc.clear();
docB.doc.clear();
@@ -4,7 +4,7 @@ import { createTestEditor } from '../utils/extensions.js';
import type { InitFn } from './utils.js';
export const multiEditor: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const doc = collection.createDoc(id).getStore({ id });
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
@@ -39,7 +39,7 @@ export const multiEditorVertical: InitFn = (
collection: Workspace,
docId: string
) => {
const doc = collection.createDoc({ id: docId });
const doc = collection.createDoc(docId).getStore();
doc.load(() => {
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
@@ -4,8 +4,8 @@ import * as Y from 'yjs';
import type { InitFn } from './utils.js';
export const pendingStructs: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const tempDoc = collection.createDoc({ id: 'tempDoc' });
const doc = collection.createDoc(id).getStore({ id });
const tempDoc = collection.createDoc('tempDoc').getStore();
doc.load();
tempDoc.load(() => {
const rootId = tempDoc.addBlock('affine:page', {
@@ -6,7 +6,7 @@ import type { InitFn } from './utils.js';
const presetMarkdown = `Click the 🔁 button to switch between editors dynamically - they are fully compatible!`;
export const preset: InitFn = async (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const doc = collection.createDoc(id).getStore({ id });
doc.load();
// Add root block and surface block at root level
const rootId = doc.addBlock('affine:page', {
@@ -12,13 +12,17 @@ This article has also been published [in PDF format](https://www.inkandswitch.co
We welcome your feedback: [@inkandswitch](https://twitter.com/inkandswitch) or hello@inkandswitch.com.`;
export const synced: InitFn = (collection: Workspace, id: string) => {
const docMain = collection.getDoc(id) ?? collection.createDoc({ id });
const docMain =
collection.getDoc(id)?.getStore({ id }) ??
collection.createDoc(id).getStore({ id });
const docSyncedPageId = 'doc:synced-page';
const docSyncedPage = collection.createDoc({ id: docSyncedPageId });
const docSyncedPage = collection.createDoc(docSyncedPageId).getStore();
const docSyncedEdgelessId = 'doc:synced-edgeless';
const docSyncedEdgeless = collection.createDoc({ id: docSyncedEdgelessId });
const docSyncedEdgeless = collection
.createDoc(docSyncedEdgelessId)
.getStore();
docMain.doc.clear();
docSyncedPage.doc.clear();
@@ -4,8 +4,8 @@ import * as Y from 'yjs';
import type { InitFn } from './utils.js';
export const versionMismatch: InitFn = (collection: Workspace, id: string) => {
const doc = collection.createDoc({ id });
const tempDoc = collection.createDoc({ id: 'tempDoc' });
const doc = collection.createDoc(id).getStore({ id });
const tempDoc = collection.createDoc('tempDoc').getStore();
doc.load();
tempDoc.load(() => {