Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 support left char retrieval for deleted…
Browse files Browse the repository at this point in the history
… points
  • Loading branch information
streamich committed Apr 6, 2024
1 parent f794696 commit dc982cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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 @@ -195,7 +195,13 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {

public leftChar(): ChunkSlice | undefined {
let chunk = this.chunk();
if (!chunk || chunk.del) return;
if (!chunk) return;
if (chunk.del) {
const prevId = this.prevId();
if (!prevId) return;
const tmp = new Point(this.txt, prevId, Anchor.After);
return tmp.leftChar();
}
if (this.anchor === Anchor.After) {
const off = this.id.time - chunk.id.time;
return new ChunkSlice(chunk, off, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ describe('.rightChar()', () => {
const end = peritext.pointAt(res.length - 1, Anchor.After);
expect(end.rightChar()).toBe(undefined);
});

test('retrieves left char of a deleted point', () => {
const {peritext, chunkD1} = 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');
});
});

describe('.leftChar()', () => {
Expand Down

0 comments on commit dc982cd

Please sign in to comment.