From b257f2e7ef02b365703cd2042d4e2afc4069f87e Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 6 Jan 2025 10:00:53 +0100 Subject: [PATCH] =?UTF-8?q?fix(json-crdt-extensions):=20=F0=9F=90=9B=20ski?= =?UTF-8?q?p=20first=20empty=20virtual=20block?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../peritext/block/Fragment.ts | 2 ++ .../lazy/__tests__/import-export-html.spec.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/json-crdt-extensions/peritext/lazy/__tests__/import-export-html.spec.ts diff --git a/src/json-crdt-extensions/peritext/block/Fragment.ts b/src/json-crdt-extensions/peritext/block/Fragment.ts index e8e231e53f..414358c7c3 100644 --- a/src/json-crdt-extensions/peritext/block/Fragment.ts +++ b/src/json-crdt-extensions/peritext/block/Fragment.ts @@ -80,6 +80,8 @@ export class Fragment extends Range implements Printable, Stateful { let pair: ReturnType; while ((pair = iterator())) { const [p1, p2] = pair; + const skipFirstVirtualBlock = !p1 && this.start.isAbsStart() && p2 && p2.viewPos() === 0; + if (skipFirstVirtualBlock) continue; const type = p1 ? p1.type() : CommonSliceType.p; const path = type instanceof Array ? type : [type]; const block = this.insertBlock(parent, path, p1, p2); diff --git a/src/json-crdt-extensions/peritext/lazy/__tests__/import-export-html.spec.ts b/src/json-crdt-extensions/peritext/lazy/__tests__/import-export-html.spec.ts new file mode 100644 index 0000000000..b63b2742cc --- /dev/null +++ b/src/json-crdt-extensions/peritext/lazy/__tests__/import-export-html.spec.ts @@ -0,0 +1,16 @@ +import {setupKit} from '../../__tests__/setup'; +import {CommonSliceType} from '../../slice'; +import {fromHtml, toViewRange} from '../import-html'; + +test('a single paragraph', () => { + const {peritext} = setupKit(); + const html = '

Hello world

'; + const peritextMl = fromHtml(html); + const rangeView = toViewRange(peritextMl); + peritext.editor.import(0, rangeView); + peritext.refresh(); + const json = peritext.blocks.toJson(); + expect(json).toEqual(['', null, + [CommonSliceType.p, null, 'Hello world'], + ]); +});