Skip to content

Commit 0b72bc9

Browse files
committed
fix(json-crdt-extensions): 🐛 correct various selection set edge cases
1 parent 227f3fe commit 0b72bc9

File tree

5 files changed

+184
-164
lines changed

5 files changed

+184
-164
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,16 +1141,19 @@ export class Editor<T = string> implements Printable {
11411141
return typeof at === 'number' ? txt.pointAt(at) : Array.isArray(at) ? txt.pointAt(at[0], at[1]) : at;
11421142
}
11431143

1144-
public sel2range(at: EditorSelection<T>): Range<T> {
1145-
if (!Array.isArray(at)) return at;
1144+
public sel2range(at: EditorSelection<T>): [range: Range<T>, anchor: CursorAnchor] {
1145+
if (!Array.isArray(at)) return [at, CursorAnchor.End];
11461146
const [pos1, pos2] = at;
11471147
const p1 = this.pos2point(pos1);
11481148
const txt = this.txt;
11491149
if (pos2 === undefined) {
11501150
p1.refAfter();
1151-
return txt.range(p1);
1151+
return [txt.range(p1), CursorAnchor.End]
11521152
}
1153-
return txt.rangeFromPoints(p1, this.pos2point(pos2));
1153+
const p2 = this.pos2point(pos2);
1154+
const range = txt.rangeFromPoints(p1, p2);
1155+
const anchor: CursorAnchor = range.start === p1 ? CursorAnchor.Start : CursorAnchor.End;
1156+
return [range, anchor];
11541157
}
11551158

11561159
public end(): Point<T> {

0 commit comments

Comments
 (0)