Skip to content

Commit d14dd8e

Browse files
authored
Merge pull request #580 from streamich/slices-4
Peritext slices improvements
2 parents ce831fd + 5cdefcd commit d14dd8e

File tree

9 files changed

+364
-250
lines changed

9 files changed

+364
-250
lines changed

src/json-crdt-extensions/peritext/Peritext.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Peritext implements Printable {
4242
* @returns The point.
4343
*/
4444
public point(id: ITimestampStruct = this.str.id, anchor: Anchor = Anchor.After): Point {
45-
return new Point(this, id, anchor);
45+
return new Point(this.str, id, anchor);
4646
}
4747

4848
/**
@@ -92,7 +92,7 @@ export class Peritext implements Printable {
9292
* @returns A range with points in correct order.
9393
*/
9494
public rangeFromPoints(p1: Point, p2: Point): Range {
95-
return Range.from(this, p1, p2);
95+
return Range.from(this.str, p1, p2);
9696
}
9797

9898
/**
@@ -103,28 +103,19 @@ export class Peritext implements Printable {
103103
* @returns A range with the given start and end points.
104104
*/
105105
public range(start: Point, end: Point): Range {
106-
return new Range(this, start, end);
106+
return new Range(this.str, start, end);
107107
}
108108

109109
/**
110-
* Creates a range from a view position and a length.
110+
* A convenience method for creating a range from a view position and a length.
111+
* See {@link Range.at} for more information.
111112
*
112113
* @param start Position in the text.
113114
* @param length Length of the range.
114115
* @returns A range from the given position with the given length.
115116
*/
116117
public rangeAt(start: number, length: number = 0): Range {
117-
const str = this.str;
118-
if (!length) {
119-
const startId = !start ? str.id : str.find(start - 1) || str.id;
120-
const point = this.point(startId, Anchor.After);
121-
return this.range(point, point.clone());
122-
}
123-
const startId = str.find(start) || str.id;
124-
const endId = str.find(start + length - 1) || startId;
125-
const startEndpoint = this.point(startId, Anchor.Before);
126-
const endEndpoint = this.point(endId, Anchor.After);
127-
return this.range(startEndpoint, endEndpoint);
118+
return Range.at(this.str, start, length);
128119
}
129120

130121
// --------------------------------------------------------------- Insertions

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
/**

0 commit comments

Comments
 (0)