import { assertExists } from '@blockeditor/global/utils'; import type { FromBlockSnapshotPayload, FromBlockSnapshotResult, FromPageSnapshotPayload, FromPageSnapshotResult, FromSliceSnapshotPayload, FromSliceSnapshotResult, ToBlockSnapshotPayload, ToPageSnapshotPayload, ToSliceSnapshotPayload, } from '@blockeditor/store'; import type { BlockSnapshot, PageSnapshot, SliceSnapshot, } from '@blockeditor/store'; import { BaseAdapter } from '@blockeditor/store'; import { decode, encode } from './utils.js'; type FileSnapshot = { name: string; type: string; content: string; }; export class ClipboardAdapter extends BaseAdapter { static MIME = 'BLOCKSUITE/SNAPSHOT'; override fromPageSnapshot( _payload: FromPageSnapshotPayload ): Promise> { throw new Error('not implemented'); } override toPageSnapshot( _payload: ToPageSnapshotPayload ): Promise { throw new Error('not implemented'); } override fromBlockSnapshot( _payload: FromBlockSnapshotPayload ): Promise> { throw new Error('not implemented'); } override toBlockSnapshot( _payload: ToBlockSnapshotPayload ): Promise { throw new Error('not implemented'); } override async fromSliceSnapshot( payload: FromSliceSnapshotPayload ): Promise> { const snapshot = payload.snapshot; const assets = payload.assets; assertExists(assets); const map = assets.getAssets(); const blobs: Record = {}; await Promise.all( Array.from(map.entries()).map(async ([id, blob]) => { const content = encode(await blob.arrayBuffer()); const file: FileSnapshot = { name: (blob as File).name, type: blob.type, content, }; blobs[id] = file; }) ); return { file: JSON.stringify({ snapshot, blobs, }), assetsIds: [], }; } override toSliceSnapshot( payload: ToSliceSnapshotPayload ): Promise { const json = JSON.parse(payload.file); const { blobs, snapshot } = json; const map = payload.assets?.getAssets(); Object.entries(blobs).forEach(([sourceId, file]) => { const blob = new Blob([decode(file.content)]); const f = new File([blob], file.name, { type: file.type, }); assertExists(map); map.set(sourceId, f); }); return Promise.resolve(snapshot); } }