Skip to content

Commit

Permalink
fix(json-crdt-extensions): 🐛 skip first empty virtual block
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 6, 2025
1 parent 9954c69 commit b257f2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/json-crdt-extensions/peritext/block/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export class Fragment extends Range implements Printable, Stateful {
let pair: ReturnType<typeof iterator>;
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '<p>Hello world</p>';
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'],
]);
});

0 comments on commit b257f2e

Please sign in to comment.