Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 improve .move() method and fix .viewPos()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 6, 2024
1 parent 2e48abf commit 45b1241
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/json-crdt-extensions/peritext/point/Point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
*/
public viewPos(): number {
const pos = this.pos();
if (pos < 0) return 0;
if (pos < 0) return this.isStartOfStr() ? 0 : this.txt.str.length();
return this.anchor === Anchor.Before ? pos : pos + 1;
}

Expand Down Expand Up @@ -313,14 +313,23 @@ export class Point implements Pick<Stateful, 'refresh'>, Printable {
* and negative distances.
*/
public move(skip: number): void {
// TODO: handle cases when cursor reaches ends of string, it should adjust anchor positions as well
if (!skip) return;
const anchor = this.anchor;
if (anchor !== Anchor.After) this.refAfter();
if (skip > 0) {
const nextId = this.nextId(skip);
if (nextId) this.id = nextId;
if (!nextId) this.refEnd();
else {
this.id = nextId;
if (anchor !== Anchor.After) this.refBefore();
}
} else {
const prevId = this.prevId(-skip);
if (prevId) this.id = prevId;
if (!prevId) this.refStart();
else {
this.id = prevId;
if (anchor !== Anchor.After) this.refBefore();
}
}
}

Expand Down
83 changes: 82 additions & 1 deletion src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,15 +820,96 @@ describe('.refAfter()', () => {
});

describe('.move()', () => {
test('can move forward', () => {
test('smoke test', () => {
const {peritext} = setupWithChunkedText();
const p = peritext.pointAt(1, Anchor.After);
expect(p.viewPos()).toBe(2);
p.move(1);
expect(p.viewPos()).toBe(3);
p.move(2);
expect(p.viewPos()).toBe(5);
p.move(2);
expect(p.viewPos()).toBe(7);
p.move(-3);
expect(p.viewPos()).toBe(4);
p.move(-3);
expect(p.viewPos()).toBe(1);
p.move(-3);
expect(p.viewPos()).toBe(0);
});

test('can reach the end of str', () => {
const {peritext} = setupWithChunkedText();
const p = peritext.pointAt(0, Anchor.After);
p.move(1);
p.move(2);
p.move(3);
p.move(4);
p.move(5);
p.move(6);
expect(p.isEndOfStr()).toBe(true);
expect(p.viewPos()).toBe(9);
expect(p.leftChar()!.view()).toBe('9');
expect(p.anchor).toBe(Anchor.Before);
});

test('can reach the start of str', () => {
const {peritext} = setupWithChunkedText();
const p = peritext.pointAt(8, Anchor.Before);
p.move(-22);
expect(p.isStartOfStr()).toBe(true);
expect(p.viewPos()).toBe(0);
expect(p.rightChar()!.view()).toBe('1');
expect(p.anchor).toBe(Anchor.After);
});

test('can move forward, when anchor = Before', () => {
const {peritext, model} = setupWithChunkedText();
model.api.str(['text']).del(4, 1);
const txt = '12346789';
for (let i = 0; i < txt.length - 1; i++) {
const p = peritext.pointAt(i, Anchor.Before);
expect(p.pos()).toBe(i);
for (let j = i + 1; j < txt.length - 1; j++) {
const p2 = p.clone();
p2.move(j - i);
expect(p2.pos()).toBe(j);
expect(p2.anchor).toBe(Anchor.Before);
expect(p2.rightChar()!.view()).toBe(txt[j]);
}
}
});

test('can move forward, when anchor = After', () => {
const {peritext, model} = setupWithChunkedText();
model.api.str(['text']).del(4, 1);
const txt = '12346789';
for (let i = 0; i < txt.length - 1; i++) {
const p = peritext.pointAt(i, Anchor.After);
expect(p.pos()).toBe(i);
expect(p.leftChar()!.view()).toBe(txt[i]);
for (let j = i + 1; j < txt.length - 1; j++) {
const p2 = p.clone();
p2.move(j - i);
expect(p2.pos()).toBe(j);
expect(p2.anchor).toBe(Anchor.After);
expect(p2.leftChar()!.view()).toBe(txt[j]);
}
}
});

test('can move backward, when anchor = Before', () => {
const {peritext, model} = setupWithChunkedText();
model.api.str(['text']).del(4, 1);
const txt = '12346789';
for (let i = txt.length - 1; i > 0; i--) {
const p = peritext.pointAt(i, Anchor.Before);
expect(p.viewPos()).toBe(i);
for (let j = i - 1; j > 0; j--) {
const p2 = p.clone();
p2.move(j - i);
expect(p2.pos()).toBe(j);
expect(p2.anchor).toBe(Anchor.Before);
expect(p2.rightChar()!.view()).toBe(txt[j]);
}
}
Expand Down

0 comments on commit 45b1241

Please sign in to comment.