json-joy
Version:
Collection of libraries for building collaborative editing apps.
163 lines (162 loc) • 8.07 kB
TypeScript
import { OverlayPoint } from './OverlayPoint';
import { MarkerOverlayPoint } from './MarkerOverlayPoint';
import { type ITimestampStruct } from '../../../json-crdt-patch/clock';
import { UndefEndIter, type UndefIterator } from '../../../util/iterator';
import type { Point } from '../rga/Point';
import type { Range } from '../rga/Range';
import type { Chunk } from '../../../json-crdt/nodes/rga';
import type { Peritext } from '../Peritext';
import type { Stateful } from '../types';
import type { Printable } from 'tree-dump/lib/types';
import type { Slice, SliceType } from '../slice/types';
import type { MarkerOverlayPair, OverlayPair, OverlayTuple } from './types';
/**
* Overlay is a tree structure that represents all the intersections of slices
* in the text. It is used to quickly find all the slices that overlap a
* given point in the text. The overlay is a read-only structure, its state
* is changed only by calling the `refresh` method, which updates the overlay
* based on the current state of the text and slices.
*/
export declare class Overlay<T = string> implements Printable, Stateful {
protected readonly txt: Peritext<T>;
root: OverlayPoint<T> | undefined;
root2: MarkerOverlayPoint<T> | undefined;
/** A virtual absolute start point, used when the absolute start is missing. */
readonly START: OverlayPoint<T>;
/** A virtual absolute end point, used when the absolute end is missing. */
readonly END: OverlayPoint<T>;
constructor(txt: Peritext<T>);
private point;
private mPoint;
first(): OverlayPoint<T> | undefined;
last(): OverlayPoint<T> | undefined;
firstMarker(): MarkerOverlayPoint<T> | undefined;
lastMarker(): MarkerOverlayPoint<T> | undefined;
/**
* Retrieve overlay point or the previous one, measured in spacial dimension.
*/
getOrNextLower(point: Point<T>): OverlayPoint<T> | undefined;
/**
* Retrieve overlay point or the next one, measured in spacial dimension.
*/
getOrNextHigher(point: Point<T>): OverlayPoint<T> | undefined;
/**
* Retrieve a {@link MarkerOverlayPoint} at the specified point or the
* previous one, measured in spacial dimension.
*/
getOrNextLowerMarker(point: Point<T>): MarkerOverlayPoint<T> | undefined;
/** @todo Rename to `chunks()`. */
chunkSlices0(chunk: Chunk<T> | undefined, p1: Point<T>, p2: Point<T>, callback: (chunk: Chunk<T>, off: number, len: number) => boolean | void): Chunk<T> | undefined;
points0(after: undefined | OverlayPoint<T>, inclusive?: boolean): UndefIterator<OverlayPoint<T>>;
points(after?: undefined | OverlayPoint<T>, inclusive?: boolean): IterableIterator<OverlayPoint<T>>;
/**
* Returns all {@link MarkerOverlayPoint} instances in the overlay, starting
* from the given marker point, not including the marker point itself.
*
* If the `after` parameter is not provided, the iteration starts from the
* first marker point in the overlay.
*
* @param after The marker point after which to start the iteration.
* @returns All marker points in the overlay, starting from the given marker
* point.
*/
markers0(after: undefined | MarkerOverlayPoint<T>): UndefIterator<MarkerOverlayPoint<T>>;
markers(after?: undefined | MarkerOverlayPoint<T>): UndefEndIter<MarkerOverlayPoint<T>>;
/**
* Returns all {@link MarkerOverlayPoint} instances in the overlay, starting
* from a give {@link Point}, including any marker overlay points that are
* at the same position as the given point.
*
* @param point Point (inclusive) from which to return all markers.
* @returns All marker points in the overlay, starting from the given marker
* point.
*/
markersFrom0(point: Point<T>): UndefIterator<MarkerOverlayPoint<T>>;
/**
* Returns a pair of overlay marker points for each pair of adjacent marker
* points in the overlay, starting from a given point (which may not be a
* marker). The very first point in the first pair might be `undefined`, if
* the given point is not a marker. Similarly, the very last point in the last
* pair might be `undefined`, if the iteration end point is not a marker.
*
* @param start Start point of the iteration, inclusive.
* @param end End point of the iteration. If not provided, the iteration
* continues until the end of the overlay.
* @returns Iterator that returns pairs of overlay points.
*/
markerPairs0(start: Point<T>, end?: Point<T>): UndefIterator<MarkerOverlayPair<T>>;
pairs0(after: undefined | OverlayPoint<T>): UndefIterator<OverlayPair<T>>;
pairs(after?: undefined | OverlayPoint<T>): IterableIterator<OverlayPair<T>>;
tuples0(after: undefined | OverlayPoint<T>): UndefIterator<OverlayTuple<T>>;
tuples(after?: undefined | OverlayPoint<T>): IterableIterator<OverlayTuple<T>>;
/**
* Finds the first point that satisfies the given predicate function.
*
* @param predicate Predicate function to find the point, returns true if the
* point is found.
* @returns The first point that satisfies the predicate, or undefined if no
* point is found.
*/
find(predicate: (point: OverlayPoint<T>) => boolean): OverlayPoint<T> | undefined;
/**
* Finds all slices that are contained within the given range. A slice is
* considered contained if its start and end points are within the range,
* inclusive (uses {@link Range#contains} method to check containment).
*
* @param range The range to search for contained slices.
* @returns A set of slices that are contained within the given range.
*/
findContained(range: Range<T>): Set<Slice<T>>;
/**
* Finds all slices that overlap with the given range. A slice is considered
* overlapping if its start or end point is within the range, inclusive
* (uses {@link Range#containsPoint} method to check overlap).
*
* @param range The range to search for overlapping slices.
* @returns A set of slices that overlap with the given range.
*/
findOverlapping(range: Range<T>): Set<Slice<T>>;
/**
* Returns a summary of how different slice types overlap with the given range.
*
* @param range Range over which to search for slices.
* @param endOnMarker If set to a positive number, the search will stop after
* the given number of marker points have been observed.
* @returns Summary of the slices in this range. `complete` contains all
* "Overwrite" slice types, which overlay the full range, which have not
* been removed by "Erase" slice type. `partial` contains all "Overwrite"
* slice types, which mark a part of the range, and have not been removed
* by "Erase" slice type.
*/
stat(range: Range<T>, endOnMarker?: number): [complete: Set<SliceType>, partial: Set<SliceType>, markerCount: number];
/**
* Returns `true` if the current character is a marker sentinel.
*
* @param id ID of the point to check.
* @returns Whether the point is a marker point.
*/
isMarker(id: ITimestampStruct): boolean;
skipMarkers(point: Point<T>, direction: -1 | 1): boolean;
hash: number;
refresh(slicesOnly?: boolean): number;
readonly slices: Map<Slice<T>, [start: OverlayPoint<T>, end: OverlayPoint<T>]>;
private refreshSlices;
private insSlice;
private insMarker;
private delSlice;
/**
* Retrieve an existing {@link OverlayPoint} or create a new one, inserted
* in the tree, sorted by spatial dimension.
*/
private upsertPoint;
/**
* Inserts a point into the tree, sorted by spatial dimension.
* @param point Point to insert.
* @returns Returns the existing point if it was already in the tree.
*/
private insPoint;
private delPoint;
leadingTextHash: number;
protected refreshTextSlices(stateTotal: number): number;
toString(tab?: string): string;
}