Skip to content

Commit

Permalink
feat(json-crdt-extensions): 🎸 add ability to find current block marker
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 11, 2024
1 parent 573a2ec commit 3700013
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class MarkerOverlayPoint<T = string> extends OverlayPoint<T> implements H
// ---------------------------------------------------------------- Printable

public toStringName(): string {
return 'OverlayPoint';
return 'MarkerOverlayPoint';
}

public toStringHeader(tab: string, lite?: boolean): string {
Expand Down
39 changes: 37 additions & 2 deletions src/json-crdt-extensions/peritext/overlay/Overlay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {printTree} from 'tree-dump/lib/printTree';
import {printBinary} from 'tree-dump/lib/printBinary';
import {first, insertLeft, insertRight, last, next, prev, remove} from 'sonic-forest/lib/util';
import {first2, insert2, next2, remove2} from 'sonic-forest/lib/util2';
import {first2, insert2, last2, next2, remove2} from 'sonic-forest/lib/util2';
import {splay} from 'sonic-forest/lib/splay/util';
import {Anchor} from '../rga/constants';
import {OverlayPoint} from './OverlayPoint';
Expand Down Expand Up @@ -59,10 +59,18 @@ export class Overlay<T = string> implements Printable, Stateful {
public first(): OverlayPoint<T> | undefined {
return this.root ? first(this.root) : undefined;
}

public last(): OverlayPoint<T> | undefined {
return this.root ? last(this.root) : undefined;
}

public firstMarker(): MarkerOverlayPoint<T> | undefined {
return this.root2 ? first2(this.root2) : undefined;
}

public lastMarker(): MarkerOverlayPoint<T> | undefined {
return this.root2 ? last2(this.root2) : undefined;
}

/**
* Retrieve overlay point or the previous one, measured in spacial dimension.
Expand Down Expand Up @@ -116,6 +124,33 @@ export class Overlay<T = string> implements Printable, Stateful {
return result;
}

/**
* Retrieve a {@link MarkerOverlayPoint} at the specified point or the
* previous one, measured in spacial dimension.
*/
public getOrNextLowerMarker(point: Point<T>): MarkerOverlayPoint<T> | undefined {
if (point.isAbsStart()) {
const first = this.firstMarker();
if (!first) return;
if (first.isAbsStart()) return first;
point = first;
} else if (point.isAbsEnd()) return this.lastMarker();
let curr: MarkerOverlayPoint<T> | undefined = this.root2;
let result: MarkerOverlayPoint<T> | undefined = undefined;
while (curr) {
const cmp = curr.cmpSpatial(point);
if (cmp === 0) return curr;
if (cmp > 0) curr = curr.l2;
else {
const next = curr.r2;
result = curr;
if (!next) return result;
curr = next;
}
}
return result;
}

/** @todo Rename to `chunks()`. */
/** @todo Rewrite this as `UndefIterator`. */
public chunkSlices0(
Expand Down

0 comments on commit 3700013

Please sign in to comment.