All files / json-crdt-extensions/peritext/overlay OverlayPoint.ts

80% Statements 88/110
77.77% Branches 21/27
64.28% Functions 9/14
84.04% Lines 79/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 23657x 57x 57x 57x                     57x         34477x                 34477x                     17170x 17170x 17170x 5992x 5992x       11178x 11178x 11178x 11178x 10753x 10753x 425x 418x 134x 134x 134x 6x 6x 128x   410x                   37139x 37139x 37139x 46159x 16086x 16086x                     34477x                     967x 967x 967x 942x 942x       25x 25x 25x 25x 5x 5x 20x 10x 6x 6x 6x     6x   5x                   17006x 17006x 17006x 432x 370x 370x                   34477x               3x 3x                 3x 3x                 1x                   20282x 20282x 20282x 21781x 21781x 50x 50x 50x   21731x       20183x 20183x 20183x                                                             34477x 34477x 34477x    
import {Point} from '../rga/Point';
import {compare} from '../../../json-crdt-patch/clock';
import {type OverlayRef, OverlayRefSliceEnd, OverlayRefSliceStart} from './refs';
import {printTree} from 'tree-dump/lib/printTree';
import type {MarkerSlice} from '../slice/MarkerSlice';
import type {HeadlessNode} from 'sonic-forest/lib/types';
import type {PrintChild, Printable} from 'tree-dump/lib/types';
import type {Slice} from '../slice/types';
 
/**
 * A {@link Point} which is indexed in the {@link Overlay} tree. Represents
 * sparse locations in the string of the places where annotation slices start,
 * end, or are broken down by other intersecting slices.
 */
export class OverlayPoint<T = string> extends Point<T> implements Printable, HeadlessNode {
  /**
   * Hash of text contents until the next {@link OverlayPoint}. This field is
   * modified by the {@link Overlay} tree.
   */
  public hash: number = 0;
 
  // ------------------------------------------------------------------- layers
 
  /**
   * Sorted list of layers, contains the interval from this point to the next
   * one. A *layer* is a part of a slice from the current point to the next one.
   * This interval can contain many layers, as the slices can be overlapped.
   */
  public readonly layers: Slice<T>[] = [];
 
  /**
   * Inserts a slice to the list of layers which contains the area from this
   * point until the next one. The operation is idempotent, so inserting the
   * same slice twice will not change the state of the point. The layers are
   * sorted by the slice ID.
   *
   * @param slice Slice to add to the layer list.
   */
  public addLayer(slice: Slice<T>): void {
    const layers = this.layers;
    const length = layers.length;
    if (!length) {
      layers.push(slice);
      return;
    }
    // We attempt to insert from the end of the list, as it is the most likely
    // scenario. And `.push()` is more efficient than `.unshift()`.
    const lastSlice = layers[length - 1];
    const sliceId = slice.id;
    const cmp = compare(lastSlice.id, sliceId);
    if (cmp < 0) {
      layers.push(slice);
      return;
    } else if (!cmp) return;
    for (let i = length - 2; i >= 0; i--) {
      const currSlice = layers[i];
      const cmp = compare(currSlice.id, sliceId);
      if (cmp < 0) {
        layers.splice(i + 1, 0, slice);
        return;
      } else if (!cmp) return;
    }
    layers.unshift(slice);
  }
 
  /**
   * Removes a slice from the list of layers, which start from this overlay
   * point.
   *
   * @param slice Slice to remove from the layer list.
   */
  public removeLayer(slice: Slice<T>): void {
    const layers = this.layers;
    const length = layers.length;
    for (let i = 0; i < length; i++) {
      if (layers[i] === slice) {
        layers.splice(i, 1);
        return;
      }
    }
  }
 
  // ------------------------------------------------------------------ markers
 
  /**
   * Collapsed slices - markers (block splits), which represent a single point
   * in the text, even if the start and end of the slice are different.
   */
  public readonly markers: Slice<T>[] = [];
 
  /**
   * Inserts a slice to the list of markers which represent a single point in
   * the text, even if the start and end of the slice are different. The
   * operation is idempotent, so inserting the same slice twice will not change
   * the state of the point. The markers are sorted by the slice ID.
   *
   * @param slice Slice to add to the marker list.
   */
  public addMarker(slice: Slice<T>): void {
    const markers = this.markers;
    const length = markers.length;
    if (!length) {
      markers.push(slice);
      return;
    }
    // We attempt to insert from the end of the list, as it is the most likely
    // scenario. And `.push()` is more efficient than `.unshift()`.
    const lastSlice = markers[length - 1];
    const sliceId = slice.id;
    const cmp = compare(lastSlice.id, sliceId);
    if (cmp < 0) {
      markers.push(slice);
      return;
    } else if (!cmp) return;
    for (let i = length - 2; i >= 0; i--) {
      const currSlice = markers[i];
      const cmp = compare(currSlice.id, sliceId);
      Iif (cmp < 0) {
        markers.splice(i + 1, 0, slice);
        return;
      } else if (!cmp) return;
    }
    markers.unshift(slice);
  }
 
  /**
   * Removes a slice from the list of markers, which represent a single point in
   * the text, even if the start and end of the slice are different.
   *
   * @param slice Slice to remove from the marker list.
   */
  public removeMarker(slice: Slice<T>): void {
    const markers = this.markers;
    const length = markers.length;
    for (let i = 0; i < length; i++) {
      if (markers[i] === slice) {
        markers.splice(i, 1);
        return;
      }
    }
  }
 
  // --------------------------------------------------------------------- refs
 
  /**
   * Sorted list of all references to rich-text constructs.
   */
  public readonly refs: OverlayRef<T>[] = [];
 
  /**
   * Insert a reference to a marker.
   *
   * @param slice A marker (split slice).
   */
  public addMarkerRef(slice: MarkerSlice<T>): void {
    this.refs.push(slice);
    this.addMarker(slice);
  }
 
  /**
   * Insert a layer that starts at this point.
   *
   * @param slice A slice that starts at this point.
   */
  public addLayerStartRef(slice: Slice<T>): void {
    this.refs.push(new OverlayRefSliceStart<T>(slice));
    this.addLayer(slice);
  }
 
  /**
   * Insert a layer that ends at this point.
   *
   * @param slice A slice that ends at this point.
   */
  public addLayerEndRef(slice: Slice<T>): void {
    this.refs.push(new OverlayRefSliceEnd(slice));
  }
 
  /**
   * Removes a reference to a marker or a slice, and remove the corresponding
   * layer or marker.
   *
   * @param slice A slice to remove the reference to.
   */
  public removeRef(slice: Slice<T>): void {
    const refs = this.refs;
    const length = refs.length;
    for (let i = 0; i < length; i++) {
      const ref = refs[i];
      if (ref === slice) {
        refs.splice(i, 1);
        this.removeMarker(slice);
        return;
      }
      if (
        (ref instanceof OverlayRefSliceStart && ref.slice === slice) ||
        (ref instanceof OverlayRefSliceEnd && ref.slice === slice)
      ) {
        refs.splice(i, 1);
        this.removeLayer(slice);
        return;
      }
    }
  }
 
  // ---------------------------------------------------------------- Printable
 
  public toStringName(): string {
    return 'OverlayPoint';
  }
 
  public toStringHeader(tab: string = '', lite?: boolean): string {
    return super.toString(tab, lite);
  }
 
  public toString(tab: string = '', lite?: boolean): string {
    const refs = lite ? '' : `, refs = ${this.refs.length}`;
    const header = this.toStringHeader(tab, lite) + refs;
    Iif (lite) return header;
    const children: PrintChild[] = [];
    const layers = this.layers;
    const layerLength = layers.length;
    for (let i = 0; i < layerLength; i++) children.push((tab) => layers[i].toString(tab));
    const markers = this.markers;
    const markerLength = markers.length;
    for (let i = 0; i < markerLength; i++) children.push((tab) => markers[i].toString(tab));
    return header + printTree(tab, children);
  }
 
  // ------------------------------------------------------------- HeadlessNode
 
  public p: OverlayPoint<T> | undefined = undefined;
  public l: OverlayPoint<T> | undefined = undefined;
  public r: OverlayPoint<T> | undefined = undefined;
}