Skip to content

Commit 6e4ca48

Browse files
committed
refactor(json-crdt-extensions): 💡 rename setCaret() to setAfter()
1 parent 03ccf42 commit 6e4ca48

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/json-crdt-extensions/peritext/editor/Editor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export class Editor implements Printable {
5454
const api = model.api;
5555
api.builder.del(str.id, range);
5656
api.apply();
57-
if (start.anchor === Anchor.After) cursor.setCaret(start.id);
58-
else cursor.setCaret(start.prevId() || str.id);
57+
if (start.anchor === Anchor.After) cursor.setAfter(start.id);
58+
else cursor.setAfter(start.prevId() || str.id);
5959
}
6060
return cursor.start.id;
6161
}
@@ -68,7 +68,8 @@ export class Editor implements Printable {
6868
if (!text) return;
6969
const after = this.collapseSelection();
7070
const textId = this.txt.ins(after, text);
71-
this.cursor.setCaret(textId, text.length - 1);
71+
const shift = text.length - 1;
72+
this.cursor.setAfter(shift ? tick(textId, shift) : textId);
7273
}
7374

7475
/**

src/json-crdt-extensions/peritext/point/Point.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export class Point<T = string> implements Pick<Stateful, 'refresh'>, Printable {
5858
* @returns Returns 0 if the two points are equal, -1 if this point is less
5959
* than the other point, and 1 if this point is greater than the other
6060
* point.
61+
*
62+
* @todo Rename to `cmp`.
6163
*/
6264
public compare(other: Point<T>): -1 | 0 | 1 {
6365
const cmp = compare(this.id, other.id);
@@ -74,6 +76,8 @@ export class Point<T = string> implements Pick<Stateful, 'refresh'>, Printable {
7476
* @returns Returns 0 if the two points are equal, negative if this point is
7577
* less than the other point, and positive if this point is greater
7678
* than the other point.
79+
*
80+
* @todo Rename to `cmpSpatial`.
7781
*/
7882
public compareSpatial(other: Point<T>): number {
7983
const thisId = this.id;

src/json-crdt-extensions/peritext/slice/Range.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,17 @@ export class Range<T = string> implements Printable {
143143
this.setRange(range);
144144
}
145145

146-
/** @todo Can this be moved to Cursor? */
147-
public setCaret(after: ITimestampStruct, shift: number = 0): void {
148-
const id = shift ? tick(after, shift) : after;
149-
const caretAfter = new Point(this.rga, id, Anchor.After);
150-
this.set(caretAfter);
146+
public setAfter(id: ITimestampStruct): void {
147+
const point = new Point(this.rga, id, Anchor.After);
148+
this.set(point);
151149
}
152150

153151
public contains(range: Range<T>): boolean {
154152
return this.start.compareSpatial(range.start) <= 0 && this.end.compareSpatial(range.end) >= 0;
155153
}
156154

157-
public containsPoint(range: Point<T>): boolean {
158-
return this.start.compareSpatial(range) <= 0 && this.end.compareSpatial(range) >= 0;
155+
public containsPoint(point: Point<T>): boolean {
156+
return this.start.compareSpatial(point) <= 0 && this.end.compareSpatial(point) >= 0;
159157
}
160158

161159
/**
@@ -278,6 +276,8 @@ export class Range<T = string> implements Printable {
278276
const name = lite ? '' : `${this.constructor.name} `;
279277
const start = this.start.toString(tab, lite);
280278
const end = this.end.toString(tab, lite);
281-
return `${name}${start}${end}`;
279+
let text = this.text();
280+
if (text.length > 16) text = text.slice(0, 16) + '...';
281+
return `${JSON.stringify(text)} ${name}${start}${end}`;
282282
}
283283
}

src/json-crdt-extensions/peritext/slice/__tests__/Range.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,21 @@ describe('.contains()', () => {
346346
});
347347
});
348348

349+
describe('.containsPoint()', () => {
350+
test('returns true if slice is contained', () => {
351+
const {peritext} = setup();
352+
const point = peritext.pointAt(2, Anchor.After);
353+
const range1 = peritext.rangeAt(2, 2);
354+
const range2 = peritext.rangeAt(3, 2);
355+
expect(range1.containsPoint(point)).toBe(true);
356+
expect(range2.containsPoint(point)).toBe(false);
357+
range2.start.refAfter();
358+
expect(range2.containsPoint(point)).toBe(true);
359+
const range3 = peritext.rangeAt(1, 2);
360+
expect(range3.containsPoint(point)).toBe(true);
361+
});
362+
});
363+
349364
describe('.isCollapsed()', () => {
350365
test('returns true when endpoints point to the same location', () => {
351366
const {peritext} = setup();

0 commit comments

Comments
 (0)