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

View File

@@ -41,9 +41,9 @@ function createTestDoc(docId = 'doc0') {
const options = createTestOptions();
const collection = new TestWorkspace(options);
collection.meta.initialize();
const doc = collection.createDoc({ id: docId, extensions });
const doc = collection.createDoc(docId);
doc.load();
return doc;
return doc.getStore({ extensions });
}
describe('DatabaseManager', () => {

View File

@@ -34,8 +34,8 @@ export function createJob(middlewares?: TransformerMiddleware[]) {
blobCRUD: docCollection.blobSync,
middlewares: testMiddlewares,
docCRUD: {
create: (id: string) => docCollection.createDoc({ id }),
get: (id: string) => docCollection.getDoc(id),
create: (id: string) => docCollection.createDoc(id).getStore({ id }),
get: (id: string) => docCollection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => docCollection.removeDoc(id),
},
});

View File

@@ -86,7 +86,7 @@ export class BlockQueryDataSource extends DataSourceBase {
this.workspace.slots.docCreated.subscribe(id => {
const doc = this.workspace.getDoc(id);
if (doc) {
this.listenToDoc(doc);
this.listenToDoc(doc.getStore());
}
});
this.workspace.slots.docRemoved.subscribe(id => {

View File

@@ -412,7 +412,8 @@ export function createLinkedDocFromSlice(
snapshots: BlockSnapshot[],
docTitle?: string
) {
const linkedDoc = doc.workspace.createDoc({});
const _doc = doc.workspace.createDoc();
const linkedDoc = _doc.getStore();
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new Text(docTitle),

View File

@@ -239,9 +239,8 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent<EmbedLinke
}
get linkedDoc() {
return this.std.workspace.getDoc(this.model.props.pageId, {
id: this.model.props.pageId,
});
const doc = this.std.workspace.getDoc(this.model.props.pageId);
return doc?.getStore({ id: this.model.props.pageId });
}
private _handleDoubleClick(event: MouseEvent) {

View File

@@ -409,7 +409,8 @@ export class EmbedSyncedDocBlockComponent extends EmbedBlockComponent<EmbedSynce
get syncedDoc() {
const options: GetBlocksOptions = { readonly: true };
if (this.isPageMode) options.query = this._pageFilter;
return this.std.workspace.getDoc(this.model.props.pageId, options);
const doc = this.std.workspace.getDoc(this.model.props.pageId);
return doc?.getStore(options) ?? null;
}
private _checkCycle() {

View File

@@ -32,7 +32,8 @@ export function createLinkedDocFromNote(
note: NoteBlockModel,
docTitle?: string
) {
const linkedDoc = doc.workspace.createDoc({});
const _doc = doc.workspace.createDoc();
const linkedDoc = _doc.getStore();
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new Text(docTitle),
@@ -63,7 +64,8 @@ export function createLinkedDocFromEdgelessElements(
elements: GfxModel[],
docTitle?: string
) {
const linkedDoc = host.doc.workspace.createDoc({});
const _doc = host.doc.workspace.createDoc();
const linkedDoc = _doc.getStore();
linkedDoc.load(() => {
const rootId = linkedDoc.addBlock('affine:page', {
title: new Text(docTitle),

View File

@@ -96,8 +96,8 @@ async function importHTMLToDoc({
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [
@@ -164,8 +164,8 @@ async function importHTMLZip({
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [

View File

@@ -148,8 +148,8 @@ async function importMarkdownToDoc({
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [
@@ -215,8 +215,8 @@ async function importMarkdownZip({
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [

View File

@@ -128,8 +128,8 @@ async function importNotionZip({
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [defaultImageProxyMiddleware],

View File

@@ -18,8 +18,8 @@ async function exportDocs(
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [
@@ -109,8 +109,8 @@ async function importDocs(
schema,
blobCRUD: collection.blobSync,
docCRUD: {
create: (id: string) => collection.createDoc({ id }),
get: (id: string) => collection.getDoc(id),
create: (id: string) => collection.createDoc(id).getStore({ id }),
get: (id: string) => collection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => collection.removeDoc(id),
},
middlewares: [

View File

@@ -289,9 +289,8 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
this._referencedModel =
referencedModel && referencedModel.xywh ? referencedModel : null;
// TODO(@L-Sun): clear query cache
this._previewDoc = this.doc.workspace.getDoc(docId, {
readonly: true,
});
const doc = this.doc.workspace.getDoc(docId);
this._previewDoc = doc?.getStore({ readonly: true }) ?? null;
};
init();

View File

@@ -102,13 +102,9 @@ export class FramePreview extends WithDisposable(ShadowlessElement) {
}
private _initPreviewDoc() {
this._previewDoc = this._originalDoc.workspace.getDoc(
this._originalDoc.id,
{
query: this._docFilter,
readonly: true,
}
);
const doc = this._originalDoc.workspace.getDoc(this._originalDoc.id);
this._previewDoc =
doc?.getStore({ readonly: true, query: this._docFilter }) ?? null;
this.disposables.add(() => {
this._originalDoc.doc.clearQuery(this._docFilter);
});

View File

@@ -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;
}
/**

View File

@@ -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(() => {

View File

@@ -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;
}