From 0c3cc783a6aed3cda6715821f9bdd084487989e3 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Thu, 28 Nov 2024 02:06:59 +0000 Subject: [PATCH] fix(blocks): newline handling in MixTextAdapter and paste middleware (#8821) Fix https://github.com/toeverything/blocksuite/issues/8816 Also related to https://github.com/toeverything/blocksuite/pull/8575 Revert and adjust newline insertion logic to improve text handling in the MixTextAdapter and clipboard paste functionality. --- packages/blocks/src/_common/adapters/mix-text.ts | 8 ++++---- .../blocks/src/root-block/clipboard/middlewares/paste.ts | 5 +---- .../clipboard-copy-nested-items-clipboard.md | 2 +- .../clipboard-copy-nested-items-clipboard2.md | 2 +- ...ton-the-clipboard-data-should-be-complete-clipboard.md | 2 +- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/blocks/src/_common/adapters/mix-text.ts b/packages/blocks/src/_common/adapters/mix-text.ts index be4857fb0557..7d567f0b9c74 100644 --- a/packages/blocks/src/_common/adapters/mix-text.ts +++ b/packages/blocks/src/_common/adapters/mix-text.ts @@ -83,24 +83,24 @@ export class MixTextAdapter extends BaseAdapter { const text = (o.node.props.text ?? { delta: [] }) as { delta: DeltaInsert[]; }; + if (buffer.length > 0) { + buffer += '\n'; + } switch (o.node.flavour) { case 'affine:code': { buffer += text.delta.map(delta => delta.insert).join(''); - buffer += '\n'; break; } case 'affine:paragraph': { buffer += text.delta.map(delta => delta.insert).join(''); - buffer += '\n'; break; } case 'affine:list': { buffer += text.delta.map(delta => delta.insert).join(''); - buffer += '\n'; break; } case 'affine:divider': { - buffer += '---\n'; + buffer += '---'; break; } } diff --git a/packages/blocks/src/root-block/clipboard/middlewares/paste.ts b/packages/blocks/src/root-block/clipboard/middlewares/paste.ts index 47c9a46374b5..75e6f4e0fa4e 100644 --- a/packages/blocks/src/root-block/clipboard/middlewares/paste.ts +++ b/packages/blocks/src/root-block/clipboard/middlewares/paste.ts @@ -114,12 +114,9 @@ class PasteTr { this.snapshot.content.forEach((blockSnapshot, i) => { if (blockSnapshot.props.text) { const text = this._textFromSnapshot(blockSnapshot); - if (i != 0 && i !== this.snapshot.content.length - 1) { + if (i > 0) { deltas.push({ insert: '\n' }); } - if (i != 0 && i === this.snapshot.content.length - 1) { - this.lastIndex -= 1; - } deltas.push(...text.delta); } }); diff --git a/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard.md b/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard.md index 1ce2e2c68d28..6833e8ae1cbd 100644 --- a/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard.md +++ b/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard.md @@ -1,2 +1,2 @@ bc -d +d \ No newline at end of file diff --git a/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard2.md b/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard2.md index e75390d3faf4..02fa1f0285ac 100644 --- a/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard2.md +++ b/tests/snapshots/clipboard/clipboard.spec.ts/clipboard-copy-nested-items-clipboard2.md @@ -1,2 +1,2 @@ hi -j +j \ No newline at end of file diff --git a/tests/snapshots/clipboard/list.spec.ts/copy-a-nested-list-by-clicking-button-the-clipboard-data-should-be-complete-clipboard.md b/tests/snapshots/clipboard/list.spec.ts/copy-a-nested-list-by-clicking-button-the-clipboard-data-should-be-complete-clipboard.md index 1802a7474429..8cc58960380e 100644 --- a/tests/snapshots/clipboard/list.spec.ts/copy-a-nested-list-by-clicking-button-the-clipboard-data-should-be-complete-clipboard.md +++ b/tests/snapshots/clipboard/list.spec.ts/copy-a-nested-list-by-clicking-button-the-clipboard-data-should-be-complete-clipboard.md @@ -1,3 +1,3 @@ aaa bbb -ccc +ccc \ No newline at end of file