feat: seperate createDoc and createStore (#11182)
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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: [
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user