Skip to content

Commit

Permalink
Merge pull request #580 from streamich/slices-4
Browse files Browse the repository at this point in the history
Peritext slices improvements
  • Loading branch information
streamich authored Apr 19, 2024
2 parents ce831fd + 5cdefcd commit d14dd8e
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 250 deletions.
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

0 comments on commit d14dd8e

Please sign in to comment.