Skip to content

Commit

Permalink
fix: replace id issue for page ref (#5883)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 authored Dec 28, 2023
1 parent 214616d commit e4239b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/blocks/src/_common/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -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';
39 changes: 37 additions & 2 deletions packages/blocks/src/_common/transformers/middlewares.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>();
Expand All @@ -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;
}

Expand Down

1 comment on commit e4239b3

@vercel
Copy link

@vercel vercel bot commented on e4239b3 Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blocksuite – ./packages/playground

blocksuite-toeverything.vercel.app
try-blocksuite.vercel.app
blocksuite-git-master-toeverything.vercel.app

Please sign in to comment.