From dc982cd2f69769bf63352735bd6b6cac148946f5 Mon Sep 17 00:00:00 2001 From: streamich Date: Sat, 6 Apr 2024 18:49:08 +0200 Subject: [PATCH] =?UTF-8?q?feat(json-crdt-extensions):=20=F0=9F=8E=B8=20su?= =?UTF-8?q?pport=20left=20char=20retrieval=20for=20deleted=20points?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt-extensions/peritext/point/Point.ts | 8 +++++++- .../peritext/point/__tests__/Point.spec.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/json-crdt-extensions/peritext/point/Point.ts b/src/json-crdt-extensions/peritext/point/Point.ts index 0c2d16e475..989975027c 100644 --- a/src/json-crdt-extensions/peritext/point/Point.ts +++ b/src/json-crdt-extensions/peritext/point/Point.ts @@ -195,7 +195,13 @@ export class Point implements Pick, 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); diff --git a/src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts b/src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts index 453b2cce6d..da02c4fba6 100644 --- a/src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts +++ b/src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts @@ -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()', () => {