Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 chop off block sentinel text from inlin…
Browse files Browse the repository at this point in the history
…e node materializati
  • Loading branch information
streamich committed Jun 8, 2024
1 parent a914764 commit d1ee62c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/json-crdt-extensions/peritext/block/Inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {SliceBehavior, SliceTypes} from '../slice/constants';
import {Range} from '../rga/Range';
import {ChunkSlice} from '../util/ChunkSlice';
import {updateNum} from '../../../json-hash';
import {MarkerOverlayPoint} from '../overlay/MarkerOverlayPoint';
import type {AbstractRga} from '../../../json-crdt/nodes/rga';
import type {Printable} from 'tree-dump/lib/types';
import type {PathStep} from '../../../json-pointer';
Expand Down Expand Up @@ -141,6 +142,11 @@ export class Inline extends Range implements Printable {
return attr;
}

public text(): string {
const str = super.text();
return this.start instanceof MarkerOverlayPoint ? str.slice(1) : str;
}

// ---------------------------------------------------------------- Printable

public toString(tab: string = ''): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('tuples', () => {
const text1 = tuples1.map(([p1, p2]) => Inline.create(peritext, p1, p2).text()).join('');
const text2 = tuples2.map(([p1, p2]) => Inline.create(peritext, p1, p2).text()).join('');
expect(text1).toBe('hello ');
expect(text2).toBe('\nworld');
expect(text2).toBe('world');
});
});

Expand All @@ -137,6 +137,6 @@ describe('texts', () => {
const text1 = [...block1.texts()].map((inline) => inline.text()).join('');
const text2 = [...block2.texts()].map((inline) => inline.text()).join('');
expect(text1).toBe('hello ');
expect(text2).toBe('\nworld');
expect(text2).toBe('world');
});
});
12 changes: 12 additions & 0 deletions src/json-crdt-extensions/peritext/block/__tests__/Blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ test('can construct a two-paragraph document', () => {
expect(paragraph1.marker).toBe(undefined);
expect(paragraph2.marker instanceof MarkerOverlayPoint).toBe(true);
});

test('first inline element does not contain marker text', () => {
const {peritext} = setupHelloWorldKit();
peritext.editor.cursor.setAt(6);
peritext.editor.saved.insMarker('p');
peritext.editor.delCursors();
peritext.refresh();
expect(peritext.strApi().view()).toBe('hello \nworld');
const [block1, block2] = peritext.blocks.root.children;
expect([...block1.texts()][0].text()).toBe('hello ');
expect([...block2.texts()][0].text()).toBe('world');
});
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,15 @@ const runStrTests = (setup: () => Kit) => {
test('correctly reports *Contained* positions', () => {
const {peritext} = setup();
peritext.editor.cursor.setAt(2, 6);
peritext.editor.saved.insStack(['a', 1, 'b', 2]);
const [slice] = peritext.editor.saved.insStack(['a', 1, 'b', 2]);
peritext.editor.cursor.set(slice.start);
peritext.refresh();
const str = peritext.strApi().view();
const [inline1, inline2, inline3] = peritext.blocks.root.children[0]!.texts();
expect(inline1.text()).toBe(str.slice(0, 2));
expect(inline2.text()).toBe(str.slice(2, 8));
expect(inline2.attr()).toEqual({
[SliceTypes.Cursor]: [[[CursorAnchor.Start]], InlineAttrPos.Contained],
[SliceTypes.Cursor]: [[[CursorAnchor.Start]], InlineAttrPos.Collapsed],
'a,1,b,2': [[void 0], InlineAttrPos.Contained],
});
expect(inline3.text()).toBe(str.slice(8));
Expand Down

0 comments on commit d1ee62c

Please sign in to comment.