Skip to content

Commit 9954c69

Browse files
committed
fix(json-crdt-extensions): 🐛 improve how block elements are imported
1 parent b142e28 commit 9954c69

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/json-crdt-extensions/peritext/lazy/__tests__/import-html.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,27 @@ describe('.toViewRange()', () => {
5454
const html = '<p>Hello world</p>';
5555
const peritextMl = fromHtml(html);
5656
const view = toViewRange(peritextMl);
57-
expect(view).toEqual(['Hello world', 0, [[0, 0, 0, 0]]]);
57+
expect(view).toEqual(['\nHello world', 0, [[0, 0, 0, 0]]]);
58+
});
59+
60+
test('two consecutive paragraphs', () => {
61+
const html = '<p>Hello world</p><p>Goodbye world</p>';
62+
const peritextMl = fromHtml(html);
63+
const view = toViewRange(peritextMl);
64+
expect(view).toEqual(['\nHello world\nGoodbye world', 0, [
65+
[0, 0, 0, 0],
66+
[0, 12, 12, 0],
67+
]]);
68+
});
69+
70+
test('two paragraphs with whitespace gap', () => {
71+
const html = ' <p>Hello world</p>\n <p>Goodbye world</p>';
72+
const peritextMl = fromHtml(html);
73+
const view = toViewRange(peritextMl);
74+
expect(view).toEqual(['\nHello world\nGoodbye world', 0, [
75+
[0, 0, 0, 0],
76+
[0, 12, 12, 0],
77+
]]);
5878
});
5979

6080
test('single inline annotation', () => {

src/json-crdt-extensions/peritext/lazy/import-html.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,47 @@ class ViewRangeBuilder {
2020

2121
constructor (private registry: SliceRegistry) {}
2222

23-
private build0(node: PeritextMlNode): void {
23+
private build0(node: PeritextMlNode, depth = 0): void {
24+
const skipWhitespace = depth < 2;
2425
if (typeof node === 'string') {
26+
if (skipWhitespace && !node.trim()) return;
2527
this.text += node;
2628
return;
2729
}
2830
const [type, attr] = node;
2931
const start = this.text.length;
3032
const length = node.length;
3133
const inline = !!attr?.inline;
32-
for (let i = 2; i < length; i++) this.build0(node[i] as PeritextMlNode);
3334
if (!!type || type === 0) {
3435
let end: number = 0, header: number = 0;
3536
if (!inline) {
37+
this.text += '\n';
3638
end = start;
3739
header =
3840
(SliceBehavior.Marker << SliceHeaderShift.Behavior) +
3941
(Anchor.Before << SliceHeaderShift.X1Anchor) +
4042
(Anchor.Before << SliceHeaderShift.X2Anchor);
41-
} else {
43+
const slice: ViewSlice = [header, start, end, type];
44+
const data = attr?.data;
45+
if (data) slice.push(data);
46+
this.slices.push(slice);
47+
}
48+
}
49+
for (let i = 2; i < length; i++) this.build0(node[i] as PeritextMlNode, depth + 1);
50+
if (!!type || type === 0) {
51+
let end: number = 0, header: number = 0;
52+
if (inline) {
4253
end = this.text.length;
4354
const behavior: SliceBehavior = attr?.behavior ?? SliceBehavior.Many;
4455
header =
4556
(behavior << SliceHeaderShift.Behavior) +
4657
(Anchor.Before << SliceHeaderShift.X1Anchor) +
4758
(Anchor.After << SliceHeaderShift.X2Anchor);
59+
const slice: ViewSlice = [header, start, end, type];
60+
const data = attr?.data;
61+
if (data) slice.push(data);
62+
this.slices.push(slice);
4863
}
49-
const slice: ViewSlice = [header, start, end, type];
50-
const data = attr?.data;
51-
if (data) slice.push(data);
52-
this.slices.push(slice);
5364
}
5465
}
5566

src/json-crdt-extensions/quill-delta/QuillDeltaNode.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {ArrNode} from '../../json-crdt/nodes/arr/ArrNode';
1111
import type {StringChunk} from '../peritext/util/types';
1212
import type {OverlayTuple} from '../peritext/overlay/types';
1313
import type {QuillDataNode, QuillDeltaAttributes, QuillDeltaOp, QuillDeltaOpInsert} from './types';
14-
import {Point} from '../peritext/rga/Point';
1514

1615
export class QuillDeltaNode extends ExtNode<QuillDataNode> {
1716
public readonly txt: Peritext<string>;

0 commit comments

Comments
 (0)