Skip to content

Commit

Permalink
fix(editor): missing resource files in exported snapshot zip (#9450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 31, 2024
1 parent 7aba836 commit 0f03c3f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { FromSnapshotPayload, SnapshotNode } from '@blocksuite/store';
import type {
BlockSnapshotLeaf,
FromSnapshotPayload,
SnapshotNode,
ToSnapshotPayload,
} from '@blocksuite/store';
import { BaseBlockTransformer } from '@blocksuite/store';

import type { AttachmentBlockProps } from './attachment-model.js';
Expand All @@ -14,4 +19,16 @@ export class AttachmentBlockTransformer extends BaseBlockTransformer<AttachmentB

return snapshotRet;
}

override toSnapshot(
snapshot: ToSnapshotPayload<AttachmentBlockProps>
): BlockSnapshotLeaf {
const snapshotRet = super.toSnapshot(snapshot);
const sourceId = snapshot.model.sourceId;
if (sourceId) {
const pathBlobIdMap = snapshot.assets.getPathBlobIdMap();
pathBlobIdMap.set(snapshot.model.id, sourceId);
}
return snapshotRet;
}
}
19 changes: 18 additions & 1 deletion blocksuite/affine/model/src/blocks/image/image-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { FromSnapshotPayload, SnapshotNode } from '@blocksuite/store';
import type {
BlockSnapshotLeaf,
FromSnapshotPayload,
SnapshotNode,
ToSnapshotPayload,
} from '@blocksuite/store';
import { BaseBlockTransformer } from '@blocksuite/store';

import type { ImageBlockProps } from './image-model.js';
Expand All @@ -14,4 +19,16 @@ export class ImageBlockTransformer extends BaseBlockTransformer<ImageBlockProps>

return snapshotRet;
}

override toSnapshot(
snapshot: ToSnapshotPayload<ImageBlockProps>
): BlockSnapshotLeaf {
const snapshotRet = super.toSnapshot(snapshot);
const sourceId = snapshot.model.sourceId;
if (sourceId) {
const pathBlobIdMap = snapshot.assets.getPathBlobIdMap();
pathBlobIdMap.set(snapshot.model.id, sourceId);
}
return snapshotRet;
}
}
16 changes: 11 additions & 5 deletions blocksuite/blocks/src/_common/transformers/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ async function exportDocs(collection: DocCollection, docs: Doc[]) {
);

const assets = zip.folder('assets');
const pathBlobIdMap = job.assetsManager.getPathBlobIdMap();
const assetsMap = job.assets;

for (const [id, blob] of assetsMap) {
const ext = getAssetName(assetsMap, id).split('.').at(-1);
const name = `${id}.${ext}`;
await assets.file(name, blob);
}
await Promise.all(
Array.from(pathBlobIdMap.values()).map(async blobId => {
await job.assetsManager.readFromBlob(blobId);
const ext = getAssetName(assetsMap, blobId).split('.').at(-1);
const blob = assetsMap.get(blobId);
if (blob) {
await assets.file(`${blobId}.${ext}`, blob);
}
})
);

const downloadBlob = await zip.generate();
return download(downloadBlob, `${collection.id}.bs.zip`);
Expand Down
2 changes: 1 addition & 1 deletion blocksuite/framework/store/src/transformer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { DraftModel } from './draft.js';
import { fromJSON, toJSON } from './json.js';
import type { BlockSnapshot } from './type.js';

type BlockSnapshotLeaf = Pick<
export type BlockSnapshotLeaf = Pick<
BlockSnapshot,
'id' | 'flavour' | 'props' | 'version'
>;
Expand Down

0 comments on commit 0f03c3f

Please sign in to comment.