diff --git a/blocksuite/affine/block-embed/src/common/render-linked-doc.ts b/blocksuite/affine/block-embed/src/common/render-linked-doc.ts index 471122854..3ed5f5cd4 100644 --- a/blocksuite/affine/block-embed/src/common/render-linked-doc.ts +++ b/blocksuite/affine/block-embed/src/common/render-linked-doc.ts @@ -404,7 +404,6 @@ export function createLinkedDocFromSlice( snapshots: BlockSnapshot[], docTitle?: string ) { - // const modelsWithChildren = (list:BlockModel[]):BlockModel[]=>list.flatMap(model=>[model,...modelsWithChildren(model.children)]) const linkedDoc = doc.workspace.createDoc({}); linkedDoc.load(() => { const rootId = linkedDoc.addBlock('affine:page', { diff --git a/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts b/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts index 06be04669..75131e560 100644 --- a/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts +++ b/blocksuite/affine/block-embed/src/embed-linked-doc-block/embed-linked-doc-block.ts @@ -246,7 +246,9 @@ export class EmbedLinkedDocBlockComponent extends EmbedBlockComponent(), - true: new Map(), - false: new Map(), - }; + private readonly _storeMap = new Map(); // doc/space container. private readonly _handleYEvents = (events: Y.YEvent[]) => { @@ -189,8 +185,8 @@ export class TestDoc implements Doc { this._collection = collection; } - private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' { - return (readonly?.toString() as 'true' | 'false') ?? 'undefined'; + private _getReadonlyKey(readonly?: boolean): 'true' | 'false' { + return (readonly?.toString() as 'true' | 'false') ?? 'false'; } private _handleVersion() { @@ -253,9 +249,8 @@ export class TestDoc implements Doc { } clearQuery(query: Query, readonly?: boolean) { - const readonlyKey = this._getReadonlyKey(readonly); - - this._docMap[readonlyKey].delete(JSON.stringify(query)); + const key = this._getQueryKey({ readonly, query }); + this._storeMap.delete(key); } private _destroy() { @@ -273,13 +268,40 @@ export class TestDoc implements Doc { } } - getStore({ readonly, query, provider, extensions }: GetBlocksOptions = {}) { + private readonly _getQueryKey = ( + idOrOptions: string | { readonly?: boolean; query?: Query } + ) => { + if (typeof idOrOptions === 'string') { + return idOrOptions; + } + const { readonly, query } = idOrOptions; const readonlyKey = this._getReadonlyKey(readonly); + const key = JSON.stringify({ + readonlyKey, + query, + }); + return key; + }; - const key = JSON.stringify(query); + getStore({ + readonly, + query, + provider, + extensions, + id, + }: GetBlocksOptions = {}) { + let idOrOptions: string | { readonly?: boolean; query?: Query }; + if (id) { + idOrOptions = id; + } else if (readonly === undefined && query === undefined) { + idOrOptions = this.spaceDoc.guid; + } else { + idOrOptions = { readonly, query }; + } + const key = this._getQueryKey(idOrOptions); - if (this._docMap[readonlyKey].has(key)) { - return this._docMap[readonlyKey].get(key)!; + if (this._storeMap.has(key)) { + return this._storeMap.get(key)!; } const doc = new Store({ @@ -293,7 +315,7 @@ export class TestDoc implements Doc { ), }); - this._docMap[readonlyKey].set(key, doc); + this._storeMap.set(key, doc); return doc; } diff --git a/blocksuite/framework/store/src/test/test-workspace.ts b/blocksuite/framework/store/src/test/test-workspace.ts index 4fc4ce57f..ef5a743fc 100644 --- a/blocksuite/framework/store/src/test/test-workspace.ts +++ b/blocksuite/framework/store/src/test/test-workspace.ts @@ -180,7 +180,11 @@ export class TestWorkspace implements Workspace { tags: [], }); this.slots.docCreated.emit(docId); - return this.getDoc(docId, { query, readonly }) as Store; + return this.getDoc(docId, { + id: docId, + query, + readonly, + }) as Store; } dispose() { @@ -202,7 +206,10 @@ export class TestWorkspace implements Workspace { return space ?? null; } - getDoc(docId: string, options?: GetBlocksOptions): Store | null { + getDoc( + docId: string, + options: GetBlocksOptions = { id: docId } + ): Store | null { const collection = this.getBlockCollection(docId); return collection?.getStore(options) ?? null; } diff --git a/packages/frontend/core/src/modules/workspace/impls/doc.ts b/packages/frontend/core/src/modules/workspace/impls/doc.ts index 3c347829b..6b607b3fc 100644 --- a/packages/frontend/core/src/modules/workspace/impls/doc.ts +++ b/packages/frontend/core/src/modules/workspace/impls/doc.ts @@ -26,11 +26,7 @@ export class DocImpl implements Doc { private readonly _collection: Workspace; - private readonly _docMap = { - undefined: new Map(), - true: new Map(), - false: new Map(), - }; + private readonly _storeMap = new Map(); // doc/space container. private readonly _handleYEvents = (events: Y.YEvent[]) => { @@ -174,8 +170,8 @@ export class DocImpl implements Doc { this._collection = collection; } - private _getReadonlyKey(readonly?: boolean): 'true' | 'false' | 'undefined' { - return (readonly?.toString() as 'true' | 'false') ?? 'undefined'; + private _getReadonlyKey(readonly?: boolean): 'true' | 'false' { + return (readonly?.toString() as 'true' | 'false') ?? 'false'; } private _handleVersion() { @@ -238,9 +234,8 @@ export class DocImpl implements Doc { } clearQuery(query: Query, readonly?: boolean) { - const readonlyKey = this._getReadonlyKey(readonly); - - this._docMap[readonlyKey].delete(JSON.stringify(query)); + const key = this._getQueryKey({ readonly, query }); + this._storeMap.delete(key); } private _destroy() { @@ -258,13 +253,40 @@ export class DocImpl implements Doc { } } - getStore({ readonly, query, provider, extensions }: GetBlocksOptions = {}) { + private readonly _getQueryKey = ( + idOrOptions: string | { readonly?: boolean; query?: Query } + ) => { + if (typeof idOrOptions === 'string') { + return idOrOptions; + } + const { readonly, query } = idOrOptions; const readonlyKey = this._getReadonlyKey(readonly); + const key = JSON.stringify({ + readonlyKey, + query, + }); + return key; + }; - const key = JSON.stringify(query); + getStore({ + readonly, + query, + provider, + extensions, + id, + }: GetBlocksOptions = {}) { + let idOrOptions: string | { readonly?: boolean; query?: Query }; + if (readonly || query) { + idOrOptions = { readonly, query }; + } else if (!id) { + idOrOptions = this.workspace.idGenerator(); + } else { + idOrOptions = id; + } + const key = this._getQueryKey(idOrOptions); - if (this._docMap[readonlyKey].has(key)) { - return this._docMap[readonlyKey].get(key) as Store; + if (this._storeMap.has(key)) { + return this._storeMap.get(key) as Store; } const storeExtensions = SpecProvider.getInstance().getSpec('store'); @@ -281,7 +303,7 @@ export class DocImpl implements Doc { extensions: Array.from(extensionSet), }); - this._docMap[readonlyKey].set(key, doc); + this._storeMap.set(key, doc); return doc; } diff --git a/packages/frontend/core/src/modules/workspace/impls/workspace.ts b/packages/frontend/core/src/modules/workspace/impls/workspace.ts index 3ed3d17ca..d0e684442 100644 --- a/packages/frontend/core/src/modules/workspace/impls/workspace.ts +++ b/packages/frontend/core/src/modules/workspace/impls/workspace.ts @@ -143,7 +143,11 @@ export class WorkspaceImpl implements Workspace { tags: [], }); this.slots.docCreated.emit(docId); - return this.getDoc(docId, { query, readonly }) as Store; + return this.getDoc(docId, { + id: docId, + query, + readonly, + }) as Store; } dispose() { @@ -155,7 +159,10 @@ export class WorkspaceImpl implements Workspace { return space ?? null; } - getDoc(docId: string, options?: GetBlocksOptions): Store | null { + getDoc( + docId: string, + options: GetBlocksOptions = { id: docId } + ): Store | null { const collection = this._getDoc(docId); return collection?.getStore(options) ?? null; }