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
@@ -155,7 +155,7 @@ export class DndService extends Service {
getBlocksuiteDndAPI(sourceDocId?: string) {
const collection = this.workspaceService.workspace.docCollection;
sourceDocId ??= collection.docs.keys().next().value;
const doc = sourceDocId ? collection.getDoc(sourceDocId) : null;
const doc = sourceDocId ? collection.getDoc(sourceDocId)?.getStore() : null;
if (!doc) {
return null;
@@ -206,8 +206,8 @@ export class DocsService extends Service {
schema: getAFFiNEWorkspaceSchema(),
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: [replaceIdMiddleware(collection.idGenerator)],
@@ -21,7 +21,11 @@ export class DocsStore extends Store {
}
getBlockSuiteDoc(id: string) {
return this.workspaceService.workspace.docCollection.getDoc(id);
return (
this.workspaceService.workspace.docCollection
.getDoc(id)
?.getStore({ id }) ?? null
);
}
getBlocksuiteCollection() {
@@ -29,7 +33,8 @@ export class DocsStore extends Store {
}
createBlockSuiteDoc() {
return this.workspaceService.workspace.docCollection.createDoc();
const doc = this.workspaceService.workspace.docCollection.createDoc();
return doc.getStore({ id: doc.id });
}
watchDocIds() {
@@ -192,8 +192,10 @@ function generateMarkdownPreviewBuilder(
schema: getAFFiNEWorkspaceSchema(),
blobCRUD: markdownPreviewDocCollection.blobSync,
docCRUD: {
create: (id: string) => markdownPreviewDocCollection.createDoc({ id }),
get: (id: string) => markdownPreviewDocCollection.getDoc(id),
create: (id: string) =>
markdownPreviewDocCollection.createDoc(id).getStore({ id }),
get: (id: string) =>
markdownPreviewDocCollection.getDoc(id)?.getStore({ id }) ?? null,
delete: (id: string) => markdownPreviewDocCollection.removeDoc(id),
},
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
@@ -67,7 +67,7 @@ export class IntegrationWriter extends Entity {
} else {
const collection = workspace.docCollection;
const doc = collection.getDoc(docId);
const doc = collection.getDoc(docId)?.getStore();
if (!doc) throw new Error('Doc not found');
doc.workspace.meta.setDocMeta(docId, {
@@ -69,7 +69,7 @@ function useImageBlob(
['workspace', 'image', docId, blockId],
{
fetcher: async ([_, __, pageId, blockId]) => {
const page = docCollection.getDoc(pageId);
const page = docCollection.getDoc(pageId)?.getStore();
const block = page?.getBlock(blockId);
if (!block) {
return null;
@@ -4,12 +4,9 @@ import {
} from '@blocksuite/affine/global/exceptions';
import { NoopLogger } from '@blocksuite/affine/global/utils';
import {
type CreateBlocksOptions,
type Doc,
type GetBlocksOptions,
type IdGenerator,
nanoid,
type Store,
type Workspace,
type WorkspaceMeta,
} from '@blocksuite/affine/store';
@@ -113,9 +110,9 @@ export class WorkspaceImpl implements Workspace {
* If the `init` parameter is passed, a `surface`, `note`, and `paragraph` block
* will be created in the doc simultaneously.
*/
createDoc(options: CreateBlocksOptions = {}) {
const { id: docId = this.idGenerator(), query, readonly } = options;
if (this._hasDoc(docId)) {
createDoc(docId?: string): Doc {
const id = docId ?? this.idGenerator();
if (this._hasDoc(id)) {
throw new BlockSuiteError(
ErrorCode.DocCollectionError,
'doc already exists'
@@ -123,17 +120,13 @@ export class WorkspaceImpl implements Workspace {
}
this.meta.addDocMeta({
id: docId,
id,
title: '',
createDate: Date.now(),
tags: [],
});
this.slots.docCreated.next(docId);
return this.getDoc(docId, {
id: docId,
query,
readonly,
}) as Store;
this.slots.docCreated.next(id);
return this.getDoc(id) as Doc;
}
private _getDoc(docId: string): Doc | null {
@@ -141,12 +134,9 @@ export class WorkspaceImpl implements Workspace {
return space ?? null;
}
getDoc(
docId: string,
options: GetBlocksOptions = { id: docId }
): Store | null {
const collection = this._getDoc(docId);
return collection?.getStore(options) ?? null;
getDoc(docId: string): Doc | null {
const doc = this._getDoc(docId);
return doc;
}
removeDoc(docId: string) {