mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 14:28:51 +08:00
feat(editor): type safe draft model and transformer (#10486)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { Slot } from '@blocksuite/global/utils';
|
||||
|
||||
import type { DraftModel, Store } from '../model/index.js';
|
||||
import type { BlockModel, DraftModel, Store } from '../model/index.js';
|
||||
import type { AssetsManager } from './assets.js';
|
||||
import type { Slice } from './slice.js';
|
||||
import type {
|
||||
@@ -48,7 +48,7 @@ export type BeforeExportPayload =
|
||||
type: 'info';
|
||||
};
|
||||
|
||||
export type FinalPayload =
|
||||
export type AfterExportPayload =
|
||||
| {
|
||||
snapshot: BlockSnapshot;
|
||||
type: 'block';
|
||||
@@ -71,11 +71,34 @@ export type FinalPayload =
|
||||
type: 'info';
|
||||
};
|
||||
|
||||
export type AfterImportPayload =
|
||||
| {
|
||||
snapshot: BlockSnapshot;
|
||||
type: 'block';
|
||||
model: BlockModel;
|
||||
parent?: string;
|
||||
index?: number;
|
||||
}
|
||||
| {
|
||||
snapshot: DocSnapshot;
|
||||
type: 'page';
|
||||
page: Store;
|
||||
}
|
||||
| {
|
||||
snapshot: SliceSnapshot;
|
||||
type: 'slice';
|
||||
slice: Slice;
|
||||
}
|
||||
| {
|
||||
snapshot: CollectionInfoSnapshot;
|
||||
type: 'info';
|
||||
};
|
||||
|
||||
export type TransformerSlots = {
|
||||
beforeImport: Slot<BeforeImportPayload>;
|
||||
afterImport: Slot<FinalPayload>;
|
||||
afterImport: Slot<AfterImportPayload>;
|
||||
beforeExport: Slot<BeforeExportPayload>;
|
||||
afterExport: Slot<FinalPayload>;
|
||||
afterExport: Slot<AfterExportPayload>;
|
||||
};
|
||||
|
||||
type TransformerMiddlewareOptions = {
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import type { DraftModel, Store } from '../model/index';
|
||||
import {
|
||||
BlockModel,
|
||||
type DraftModel,
|
||||
type Store,
|
||||
toDraftModel,
|
||||
} from '../model/index';
|
||||
|
||||
type SliceData = {
|
||||
content: DraftModel[];
|
||||
@@ -21,9 +26,15 @@ export class Slice {
|
||||
|
||||
constructor(readonly data: SliceData) {}
|
||||
|
||||
static fromModels(doc: Store, models: DraftModel[]) {
|
||||
static fromModels(doc: Store, models: DraftModel[] | BlockModel[]) {
|
||||
const draftModels = models.map(model => {
|
||||
if (model instanceof BlockModel) {
|
||||
return toDraftModel(model);
|
||||
}
|
||||
return model;
|
||||
});
|
||||
return new Slice({
|
||||
content: models,
|
||||
content: draftModels,
|
||||
workspaceId: doc.workspace.id,
|
||||
pageId: doc.id,
|
||||
});
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { nextTick, Slot } from '@blocksuite/global/utils';
|
||||
|
||||
import type {
|
||||
import {
|
||||
BlockModel,
|
||||
BlockSchemaType,
|
||||
DraftModel,
|
||||
Store,
|
||||
type BlockSchemaType,
|
||||
type DraftModel,
|
||||
type Store,
|
||||
toDraftModel,
|
||||
} from '../model/index.js';
|
||||
import type { Schema } from '../schema/index.js';
|
||||
import { AssetsManager } from './assets.js';
|
||||
import { BaseBlockTransformer } from './base.js';
|
||||
import type {
|
||||
AfterExportPayload,
|
||||
AfterImportPayload,
|
||||
BeforeExportPayload,
|
||||
BeforeImportPayload,
|
||||
FinalPayload,
|
||||
TransformerMiddleware,
|
||||
TransformerSlots,
|
||||
} from './middleware.js';
|
||||
@@ -66,14 +68,19 @@ export class Transformer {
|
||||
|
||||
private readonly _slots: TransformerSlots = {
|
||||
beforeImport: new Slot<BeforeImportPayload>(),
|
||||
afterImport: new Slot<FinalPayload>(),
|
||||
afterImport: new Slot<AfterImportPayload>(),
|
||||
beforeExport: new Slot<BeforeExportPayload>(),
|
||||
afterExport: new Slot<FinalPayload>(),
|
||||
afterExport: new Slot<AfterExportPayload>(),
|
||||
};
|
||||
|
||||
blockToSnapshot = (model: DraftModel): BlockSnapshot | undefined => {
|
||||
blockToSnapshot = (
|
||||
model: DraftModel | BlockModel
|
||||
): BlockSnapshot | undefined => {
|
||||
try {
|
||||
const snapshot = this._blockToSnapshot(model);
|
||||
const draftModel =
|
||||
model instanceof BlockModel ? toDraftModel(model) : model;
|
||||
|
||||
const snapshot = this._blockToSnapshot(draftModel);
|
||||
|
||||
if (!snapshot) {
|
||||
return;
|
||||
@@ -103,7 +110,7 @@ export class Transformer {
|
||||
'Root block not found in doc'
|
||||
);
|
||||
}
|
||||
const blocks = this.blockToSnapshot(rootModel);
|
||||
const blocks = this.blockToSnapshot(toDraftModel(rootModel));
|
||||
if (!blocks) {
|
||||
return;
|
||||
}
|
||||
@@ -286,7 +293,8 @@ export class Transformer {
|
||||
|
||||
const contentBlocks = blockTree.children
|
||||
.map(tree => doc.getBlockById(tree.draft.id))
|
||||
.filter(Boolean) as DraftModel[];
|
||||
.filter((x): x is BlockModel => x !== null)
|
||||
.map(model => toDraftModel(model));
|
||||
|
||||
const slice = new Slice({
|
||||
content: contentBlocks,
|
||||
|
||||
Reference in New Issue
Block a user