Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 support right character retrieval for d…
Browse files Browse the repository at this point in the history
…eleted points
  • Loading branch information
streamich committed Apr 6, 2024
1 parent dc982cd commit fa7923b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/json-crdt-extensions/peritext/point/Point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
return chunk ? new ChunkSlice(chunk, 0, 1) : undefined;
}
let chunk = this.chunk();
if (!chunk || chunk.del) return;
if (!chunk) return;
if (chunk.del) {
const nextId = this.nextId();
if (!nextId) return;
const tmp = new Point(this.txt, nextId, Anchor.Before);
return tmp.rightChar();
}
if (this.anchor === Anchor.Before) {
const off = this.id.time - chunk.id.time;
return new ChunkSlice(chunk, off, 1);
Expand Down
24 changes: 20 additions & 4 deletions src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,16 @@ describe('.rightChar()', () => {
expect(end.rightChar()).toBe(undefined);
});

test('retrieves left char of a deleted point', () => {
const {peritext, chunkD1} = setupWithChunkedText();
test('retrieves right char of a deleted point', () => {
const {peritext, chunkD1, chunkD2} = setupWithChunkedText();
const p1 = peritext.point(chunkD1.id, Anchor.Before);
expect(p1.leftChar()!.view()).toBe('3');
expect(p1.rightChar()!.view()).toBe('4');
const p2 = peritext.point(chunkD1.id, Anchor.After);
expect(p2.leftChar()!.view()).toBe('3');
expect(p2.rightChar()!.view()).toBe('4');
const p3 = peritext.point(chunkD2.id, Anchor.Before);
expect(p3.rightChar()!.view()).toBe('7');
const p4 = peritext.point(chunkD2.id, Anchor.After);
expect(p4.rightChar()!.view()).toBe('7');
});
});

Expand Down Expand Up @@ -630,4 +634,16 @@ describe('.leftChar()', () => {
expect(char.view()).toBe(res[i]);
}
});

test('retrieves left char of a deleted point', () => {
const {peritext, chunkD1, chunkD2} = setupWithChunkedText();
const p1 = peritext.point(chunkD1.id, Anchor.Before);
expect(p1.leftChar()!.view()).toBe('3');
const p2 = peritext.point(chunkD1.id, Anchor.After);
expect(p2.leftChar()!.view()).toBe('3');
const p3 = peritext.point(chunkD2.id, Anchor.Before);
expect(p3.leftChar()!.view()).toBe('6');
const p4 = peritext.point(chunkD2.id, Anchor.After);
expect(p4.leftChar()!.view()).toBe('6');
});
});

0 comments on commit fa7923b

Please sign in to comment.