refactor(editor): optimize pasting process of attachments and images (#12276)

Related to: [BS-3146](https://linear.app/affine-design/issue/BS-3146/import-paste-接口改进优化)
This commit is contained in:
fundon
2025-05-18 01:57:42 +00:00
parent f3693a91c3
commit 8726b0e462
14 changed files with 240 additions and 76 deletions

View File

@@ -1,5 +1,6 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { BlockProps } from '../model';
import type { BlobCRUD } from './type';
type AssetsManagerConfig = {
@@ -18,6 +19,16 @@ function makeNewNameWhenConflict(names: Set<string>, name: string) {
}
export class AssetsManager {
// `blockId` is the key.
readonly uploadingAssetsMap = new Map<
string,
{
blob: Blob;
abortController?: AbortController;
mapInto: (blobId: string) => Partial<BlockProps>;
}
>();
private readonly _assetsMap = new Map<string, Blob>();
private readonly _blob: BlobCRUD;

View File

@@ -245,8 +245,9 @@ export class Transformer {
parent?: string,
index?: number
): Promise<Slice | undefined> => {
SliceSnapshotSchema.parse(snapshot);
try {
SliceSnapshotSchema.parse(snapshot);
this._slots.beforeImport.next({
type: 'slice',
snapshot,
@@ -525,11 +526,11 @@ export class Transformer {
for (let index = 0; index < nodes.length; index++) {
const node = nodes[index];
const { draft } = node;
const { id, flavour } = draft;
const { id, flavour, props } = draft;
const actualIndex =
startIndex !== undefined ? startIndex + index : undefined;
doc.addBlock(flavour, { id, ...draft.props }, parentId, actualIndex);
doc.addBlock(flavour, { id, ...props }, parentId, actualIndex);
const model = doc.getBlock(id)?.model;
if (!model) {