From 1ddae3d9670c0a6a35fde381bcc8e050a69a4593 Mon Sep 17 00:00:00 2001 From: fourdim Date: Thu, 7 Nov 2024 10:08:21 -0500 Subject: [PATCH] fix(blocks): only split paragraphs based on newlines --- .../blocks/src/_common/adapters/markdown.ts | 4 +- .../blocks/src/_common/adapters/mix-text.ts | 101 ++++++++++++++++-- 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/packages/blocks/src/_common/adapters/markdown.ts b/packages/blocks/src/_common/adapters/markdown.ts index 0ca0c55c239ca..1560449b8e651 100644 --- a/packages/blocks/src/_common/adapters/markdown.ts +++ b/packages/blocks/src/_common/adapters/markdown.ts @@ -1313,10 +1313,10 @@ export class MarkdownAdapter extends BaseAdapter { displayMode: NoteDisplayMode.DocAndEdgeless, }, children: [], - }; + } as BlockSnapshot; const contentSlice = (await this._traverseMarkdown( markdownAst, - blockSnapshotRoot as BlockSnapshot, + blockSnapshotRoot, payload.assets )) as BlockSnapshot; if (contentSlice.children.length === 0) { diff --git a/packages/blocks/src/_common/adapters/mix-text.ts b/packages/blocks/src/_common/adapters/mix-text.ts index 39981b9498e70..3ce070b1baafb 100644 --- a/packages/blocks/src/_common/adapters/mix-text.ts +++ b/packages/blocks/src/_common/adapters/mix-text.ts @@ -46,6 +46,28 @@ export class MixTextAdapter extends BaseAdapter { this._markdownAdapter = new MarkdownAdapter(job); } + private _splitDeltas(deltas: DeltaInsert[]): DeltaInsert[][] { + const result: DeltaInsert[][] = [[]]; + const pending: DeltaInsert[] = deltas; + while (pending.length > 0) { + const delta = pending.shift(); + if (!delta) { + break; + } + if (delta.insert.includes('\n')) { + const [before, after] = delta.insert.split('\n', 2); + result[result.length - 1].push({ ...delta, insert: before }); + result.push([]); + if (after) { + pending.unshift({ ...delta, insert: after }); + } + } else { + result[result.length - 1].push(delta); + } + } + return result; + } + private async _traverseSnapshot( snapshot: BlockSnapshot ): Promise<{ mixtext: string }> { @@ -245,13 +267,6 @@ export class MixTextAdapter extends BaseAdapter { return null; } payload.file = payload.file.replaceAll('\r', ''); - const lines = payload.file.split('\n'); - for (let i = 0; i < lines.length - 1; i++) { - if (lines[i].length !== 0 && lines[i + 1].length !== 0) { - lines[i] += '\n'; - } - } - payload.file = lines.join('\n'); const sliceSnapshot = await this._markdownAdapter.toSliceSnapshot({ file: payload.file, assets: payload.assets, @@ -260,6 +275,78 @@ export class MixTextAdapter extends BaseAdapter { workspaceId: payload.workspaceId, pageId: payload.pageId, }); + if (!sliceSnapshot) { + return null; + } + for (const contentSlice of sliceSnapshot.content) { + const blockSnapshotRoot = { + type: 'block', + id: nanoid(), + flavour: 'affine:note', + props: { + xywh: '[0,0,800,95]', + background: DEFAULT_NOTE_BACKGROUND_COLOR, + index: 'a0', + hidden: false, + displayMode: NoteDisplayMode.DocAndEdgeless, + }, + children: [], + } as BlockSnapshot; + const walker = new ASTWalker(); + walker.setONodeTypeGuard( + (node): node is BlockSnapshot => + BlockSnapshotSchema.safeParse(node).success + ); + walker.setEnter((o, context) => { + switch (o.node.flavour) { + case 'affine:note': { + break; + } + case 'affine:paragraph': { + if (o.parent?.node.flavour !== 'affine:note') { + context.openNode({ ...o.node, children: [] }); + break; + } + const text = (o.node.props.text ?? { delta: [] }) as { + delta: DeltaInsert[]; + }; + const newDeltas = this._splitDeltas(text.delta); + for (const [i, delta] of newDeltas.entries()) { + context.openNode({ + ...o.node, + props: { + ...o.node.props, + text: { + '$blocksuite:internal:text$': true, + delta, + }, + }, + children: [], + }); + if (i < newDeltas.length - 1) { + context.closeNode(); + } + } + break; + } + default: { + context.openNode({ ...o.node, children: [] }); + } + } + }); + walker.setLeave((o, context) => { + switch (o.node.flavour) { + case 'affine:note': { + break; + } + default: { + context.closeNode(); + } + } + }); + await walker.walk(contentSlice, blockSnapshotRoot); + contentSlice.children = blockSnapshotRoot.children; + } return sliceSnapshot; } }