diff --git a/packages/blocks/src/_common/transformers/index.ts b/packages/blocks/src/_common/transformers/index.ts index 5c6955acf0b8..308a5166a690 100644 --- a/packages/blocks/src/_common/transformers/index.ts +++ b/packages/blocks/src/_common/transformers/index.ts @@ -1,3 +1,8 @@ export * as HtmlTransformer from './html.js'; export * as MarkdownTransformer from './markdown.js'; +export { + customImageProxyMiddleware, + defaultImageProxyMiddleware, + replaceIdMiddleware, +} from './middlewares.js'; export * as ZipTransformer from './zip.js'; diff --git a/packages/blocks/src/_common/transformers/middlewares.ts b/packages/blocks/src/_common/transformers/middlewares.ts index 0d27555fb190..ea0a6c629b4a 100644 --- a/packages/blocks/src/_common/transformers/middlewares.ts +++ b/packages/blocks/src/_common/transformers/middlewares.ts @@ -1,7 +1,8 @@ import { assertExists } from '@blocksuite/global/utils'; -import type { JobMiddleware } from '@blocksuite/store'; +import type { DeltaOperation, JobMiddleware } from '@blocksuite/store'; import type { DatabaseBlockModel } from '../../database-block/index.js'; +import type { ListBlockModel, ParagraphBlockModel } from '../../models.js'; export const replaceIdMiddleware: JobMiddleware = ({ slots, workspace }) => { const idMap = new Map(); @@ -18,10 +19,44 @@ export const replaceIdMiddleware: JobMiddleware = ({ slots, workspace }) => { } }); } + + // replace LinkedPage pageId with new id in paragraph blocks + if ( + payload.type === 'block' && + ['affine:paragraph', 'affine:list'].includes(payload.snapshot.flavour) + ) { + const model = payload.model as ParagraphBlockModel | ListBlockModel; + let prev = 0; + const delta: DeltaOperation[] = []; + for (const d of model.text.toDelta()) { + if (d.attributes?.reference?.pageId) { + if (prev > 0) { + delta.push({ retain: prev }); + } + delta.push({ + retain: d.insert?.length ?? 0, + attributes: { + reference: { + ...d.attributes.reference, + pageId: idMap.get(d.attributes.reference.pageId)!, + }, + }, + }); + prev = 0; + } else { + prev += d.insert?.length ?? 0; + } + } + if (delta.length > 0) { + model.text.applyDelta(delta); + } + } }); slots.beforeImport.on(payload => { if (payload.type === 'page') { - payload.snapshot.meta.id = workspace.idGenerator('page'); + const newId = workspace.idGenerator('page'); + idMap.set(payload.snapshot.meta.id, newId); + payload.snapshot.meta.id = newId; return; }