Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/json-crdt-extensions/peritext/Peritext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Peritext implements Printable {
* @returns The point.
*/
public point(id: ITimestampStruct = this.str.id, anchor: Anchor = Anchor.After): Point {
return new Point(this, id, anchor);
return new Point(this.str, id, anchor);
}

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Peritext implements Printable {
* @returns A range with points in correct order.
*/
public rangeFromPoints(p1: Point, p2: Point): Range {
return Range.from(this, p1, p2);
return Range.from(this.str, p1, p2);
}

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

/**
* Creates a range from a view position and a length.
* A convenience method for creating a range from a view position and a length.
* See {@link Range.at} for more information.
*
* @param start Position in the text.
* @param length Length of the range.
* @returns A range from the given position with the given length.
*/
public rangeAt(start: number, length: number = 0): Range {
const str = this.str;
if (!length) {
const startId = !start ? str.id : str.find(start - 1) || str.id;
const point = this.point(startId, Anchor.After);
return this.range(point, point.clone());
}
const startId = str.find(start) || str.id;
const endId = str.find(start + length - 1) || startId;
const startEndpoint = this.point(startId, Anchor.Before);
const endEndpoint = this.point(endId, Anchor.After);
return this.range(startEndpoint, endEndpoint);
return Range.at(this.str, start, length);
}

// --------------------------------------------------------------- Insertions
Expand Down
7 changes: 4 additions & 3 deletions src/json-crdt-extensions/peritext/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class Editor implements Printable {
const api = model.api;
api.builder.del(str.id, range);
api.apply();
if (start.anchor === Anchor.After) cursor.setCaret(start.id);
else cursor.setCaret(start.prevId() || str.id);
if (start.anchor === Anchor.After) cursor.setAfter(start.id);
else cursor.setAfter(start.prevId() || str.id);
}
return cursor.start.id;
}
Expand All @@ -68,7 +68,8 @@ export class Editor implements Printable {
if (!text) return;
const after = this.collapseSelection();
const textId = this.txt.ins(after, text);
this.cursor.setCaret(textId, text.length - 1);
const shift = text.length - 1;
this.cursor.setAfter(shift ? tick(textId, shift) : textId);
}

/**
Expand Down
Loading