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
@@ -60,7 +60,8 @@ export function affine(strings: TemplateStringsArray, ...values: any[]) {
// Create a new doc
const workspace = new TestWorkspace({});
workspace.meta.initialize();
const doc = workspace.createDoc({ id: 'test-doc', extensions });
const doc = workspace.createDoc('test-doc');
const store = doc.getStore({ extensions });
// Use DOMParser to parse HTML string
doc.load(() => {
@@ -72,11 +73,11 @@ export function affine(strings: TemplateStringsArray, ...values: any[]) {
throw new Error('Template must contain a root element');
}
buildDocFromElement(doc, root, null);
buildDocFromElement(store, root, null);
});
// Create and return a host object with the document
return createTestHost(doc);
return createTestHost(store);
}
/**
@@ -103,7 +104,8 @@ export function block(
// Create a temporary doc to hold the block
const workspace = new TestWorkspace({});
workspace.meta.initialize();
const doc = workspace.createDoc({ id: 'temp-doc', extensions });
const doc = workspace.createDoc('temp-doc');
const store = doc.getStore({ extensions });
let blockId: string | null = null;
@@ -117,11 +119,11 @@ export function block(
throw new Error('Template must contain a root element');
}
blockId = buildDocFromElement(doc, root, null);
blockId = buildDocFromElement(store, root, null);
});
// Return the created block
return blockId ? (doc.getBlock(blockId) ?? null) : null;
return blockId ? (store.getBlock(blockId) ?? null) : null;
}
/**
@@ -100,7 +100,9 @@ export class DocDisplayMetaService
return signal(DocDisplayMetaService.icons.deleted);
}
let icon$ = this.iconMap.get(doc);
const store = doc.getStore();
let icon$ = this.iconMap.get(store);
if (!icon$) {
icon$ = signal(
@@ -128,11 +130,11 @@ export class DocDisplayMetaService
this.disposables.splice(index, 1);
disposable.unsubscribe();
}
this.iconMap.delete(doc);
this.iconMap.delete(store);
}
});
this.disposables.push(docRemovedSubscription);
this.iconMap.set(doc, icon$);
this.iconMap.set(store, icon$);
}
return computed(() => {
@@ -165,7 +167,9 @@ export class DocDisplayMetaService
return signal(title || 'Deleted doc');
}
let title$ = this.titleMap.get(doc);
const store = doc.getStore();
let title$ = this.titleMap.get(store);
if (!title$) {
title$ = signal(doc.meta?.title || 'Untitled');
@@ -185,11 +189,11 @@ export class DocDisplayMetaService
this.disposables.splice(index, 1);
disposable.unsubscribe();
}
this.iconMap.delete(doc);
this.iconMap.delete(store);
}
});
this.disposables.push(docRemovedSubscription);
this.titleMap.set(doc, title$);
this.titleMap.set(store, title$);
}
return computed(() => {
@@ -9,29 +9,30 @@ export function createDefaultDoc(
collection: Workspace,
options: { id?: string; title?: string } = {}
) {
const doc = collection.createDoc({ id: options.id });
const doc = collection.createDoc(options.id);
doc.load();
const store = doc.getStore();
const title = options.title ?? '';
const rootId = doc.addBlock('affine:page', {
const rootId = store.addBlock('affine:page', {
title: new Text(title),
});
collection.meta.setDocMeta(doc.id, {
title,
});
doc.addBlock('affine:surface', {}, rootId);
const noteId = doc.addBlock(
store.addBlock('affine:surface', {}, rootId);
const noteId = store.addBlock(
'affine:note',
{
xywh: `[0, 0, ${DEFAULT_PAGE_BLOCK_WIDTH}, ${DEFAULT_PAGE_BLOCK_HEIGHT}]`,
},
rootId
);
doc.addBlock('affine:paragraph', {}, noteId);
store.addBlock('affine:paragraph', {}, noteId);
// To make sure the content of new doc would not be clear
// By undo operation for the first time
doc.resetHistory();
return doc;
return store;
}