mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
refactor(editor): job should not rely on doc collection directly (#9488)
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
interface BlobCRUD {
|
||||
get: (key: string) => Promise<Blob | null> | Blob | null;
|
||||
set: (key: string, value: Blob) => Promise<string> | string;
|
||||
delete: (key: string) => Promise<void> | void;
|
||||
list: () => Promise<string[]> | string[];
|
||||
}
|
||||
import type { BlobCRUD } from './type.js';
|
||||
|
||||
type AssetsManagerConfig = {
|
||||
blob: BlobCRUD;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { nextTick, Slot } from '@blocksuite/global/utils';
|
||||
|
||||
import type { BlockModel, BlockSchemaType } from '../schema/index.js';
|
||||
import type { Doc, DocCollection, DocMeta } from '../store/index.js';
|
||||
import type { BlockModel, BlockSchemaType, Schema } from '../schema/index.js';
|
||||
import type { Doc } from '../store/index.js';
|
||||
import { AssetsManager } from './assets.js';
|
||||
import { BaseBlockTransformer } from './base.js';
|
||||
import type { DraftModel } from './draft.js';
|
||||
@@ -15,20 +15,22 @@ import type {
|
||||
} from './middleware.js';
|
||||
import { Slice } from './slice.js';
|
||||
import type {
|
||||
BlobCRUD,
|
||||
BlockSnapshot,
|
||||
CollectionInfoSnapshot,
|
||||
DocCRUD,
|
||||
DocSnapshot,
|
||||
SliceSnapshot,
|
||||
} from './type.js';
|
||||
import {
|
||||
BlockSnapshotSchema,
|
||||
CollectionInfoSnapshotSchema,
|
||||
DocSnapshotSchema,
|
||||
SliceSnapshotSchema,
|
||||
} from './type.js';
|
||||
|
||||
export type JobConfig = {
|
||||
collection: DocCollection;
|
||||
schema: Schema;
|
||||
blobCRUD: BlobCRUD;
|
||||
docCRUD: DocCRUD;
|
||||
middlewares?: JobMiddleware[];
|
||||
};
|
||||
|
||||
@@ -52,7 +54,9 @@ export class Job {
|
||||
|
||||
private readonly _assetsManager: AssetsManager;
|
||||
|
||||
private readonly _collection: DocCollection;
|
||||
private readonly _schema: Schema;
|
||||
|
||||
private readonly _docCRUD: DocCRUD;
|
||||
|
||||
private readonly _slots: JobSlots = {
|
||||
beforeImport: new Slot<BeforeImportPayload>(),
|
||||
@@ -74,31 +78,6 @@ export class Job {
|
||||
}
|
||||
};
|
||||
|
||||
collectionInfoToSnapshot = (): CollectionInfoSnapshot | undefined => {
|
||||
try {
|
||||
this._slots.beforeExport.emit({
|
||||
type: 'info',
|
||||
});
|
||||
const collectionMeta = this._getCollectionMeta();
|
||||
const snapshot: CollectionInfoSnapshot = {
|
||||
type: 'info',
|
||||
id: this._collection.id,
|
||||
...collectionMeta,
|
||||
};
|
||||
this._slots.afterExport.emit({
|
||||
type: 'info',
|
||||
snapshot,
|
||||
});
|
||||
CollectionInfoSnapshotSchema.parse(snapshot);
|
||||
|
||||
return snapshot;
|
||||
} catch (error) {
|
||||
console.error(`Error when transforming collection info to snapshot:`);
|
||||
console.error(error);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
docToSnapshot = (doc: Doc): DocSnapshot | undefined => {
|
||||
try {
|
||||
this._slots.beforeExport.emit({
|
||||
@@ -199,7 +178,7 @@ export class Job {
|
||||
});
|
||||
DocSnapshotSchema.parse(snapshot);
|
||||
const { meta, blocks } = snapshot;
|
||||
const doc = this._collection.createDoc({ id: meta.id });
|
||||
const doc = this.docCRUD.create(meta.id);
|
||||
doc.load();
|
||||
await this.snapshotToBlock(blocks, doc);
|
||||
this._slots.afterImport.emit({
|
||||
@@ -328,19 +307,24 @@ export class Job {
|
||||
return this._assetsManager;
|
||||
}
|
||||
|
||||
get collection() {
|
||||
return this._collection;
|
||||
get schema() {
|
||||
return this._schema;
|
||||
}
|
||||
|
||||
constructor({ collection, middlewares = [] }: JobConfig) {
|
||||
this._collection = collection;
|
||||
this._assetsManager = new AssetsManager({ blob: collection.blobSync });
|
||||
get docCRUD() {
|
||||
return this._docCRUD;
|
||||
}
|
||||
|
||||
constructor({ blobCRUD, schema, docCRUD, middlewares = [] }: JobConfig) {
|
||||
this._assetsManager = new AssetsManager({ blob: blobCRUD });
|
||||
this._schema = schema;
|
||||
this._docCRUD = docCRUD;
|
||||
|
||||
middlewares.forEach(middleware => {
|
||||
middleware({
|
||||
slots: this._slots,
|
||||
docCRUD: this._docCRUD,
|
||||
assetsManager: this._assetsManager,
|
||||
collection: this._collection,
|
||||
adapterConfigs: this._adapterConfigs,
|
||||
});
|
||||
});
|
||||
@@ -465,20 +449,8 @@ export class Job {
|
||||
}
|
||||
}
|
||||
|
||||
private _getCollectionMeta() {
|
||||
const { meta } = this._collection;
|
||||
const { docs } = meta;
|
||||
if (!docs) {
|
||||
throw new BlockSuiteError(ErrorCode.TransformerError, 'Docs not found');
|
||||
}
|
||||
return {
|
||||
properties: {}, // for backward compatibility
|
||||
pages: JSON.parse(JSON.stringify(docs)) as DocMeta[],
|
||||
};
|
||||
}
|
||||
|
||||
private _getSchema(flavour: string) {
|
||||
const schema = this._collection.schema.flavourSchemaMap.get(flavour);
|
||||
const schema = this.schema.flavourSchemaMap.get(flavour);
|
||||
if (!schema) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.TransformerError,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import type { Slot } from '@blocksuite/global/utils';
|
||||
|
||||
import type { Doc, DocCollection } from '../store/index.js';
|
||||
import type { Doc } from '../store/index.js';
|
||||
import type { AssetsManager } from './assets.js';
|
||||
import type { DraftModel } from './draft.js';
|
||||
import type { Slice } from './slice.js';
|
||||
import type {
|
||||
BlockSnapshot,
|
||||
CollectionInfoSnapshot,
|
||||
DocCRUD,
|
||||
DocSnapshot,
|
||||
SliceSnapshot,
|
||||
} from './type.js';
|
||||
@@ -79,9 +80,9 @@ export type JobSlots = {
|
||||
};
|
||||
|
||||
type JobMiddlewareOptions = {
|
||||
collection: DocCollection;
|
||||
assetsManager: AssetsManager;
|
||||
slots: JobSlots;
|
||||
docCRUD: DocCRUD;
|
||||
adapterConfigs: Map<string, string>;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import type { Doc } from '../store/doc/doc.js';
|
||||
import type { DocMeta, DocsPropertiesMeta } from '../store/meta.js';
|
||||
|
||||
export type BlockSnapshot = {
|
||||
@@ -65,3 +66,16 @@ export const DocSnapshotSchema: z.ZodType<DocSnapshot> = z.object({
|
||||
meta: DocMetaSchema,
|
||||
blocks: BlockSnapshotSchema,
|
||||
});
|
||||
|
||||
export interface BlobCRUD {
|
||||
get: (key: string) => Promise<Blob | null> | Blob | null;
|
||||
set: (key: string, value: Blob) => Promise<string> | string;
|
||||
delete: (key: string) => Promise<void> | void;
|
||||
list: () => Promise<string[]> | string[];
|
||||
}
|
||||
|
||||
export interface DocCRUD {
|
||||
create: (id: string) => Doc;
|
||||
get: (id: string) => Doc | null;
|
||||
delete: (id: string) => void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user