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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 3283x 3283x 3283x 3283x 3283x 3283x 3283x 10588x 3283x 3283x 3283x 8982x 15920x 3097x 3097x 3097x 3064x 3528x 3523x 88x 88x 88x 88x 103x 103x 103x 103x 1675x 1653x 27570x 7x 7x 7x 7x 162x 7588x 7588x 564x 564x 564x 564x 564x 4650x 4650x 4650x 4650x 6293x 6293x 5421x 5421x 5421x 5421x 5421x 5421x 5421x 5421x 5421x 5421x 4662x 4662x 4662x 4662x 4662x 4662x 2362x 2362x 2362x 2362x 2x 2x 2x 2x 2x 2x 2x 3283x 1686x 1686x 1686x 1686x 1686x | import {printTree} from 'tree-dump/lib/printTree'; import {Anchor} from './rga/constants'; import {Point} from './rga/Point'; import {Range} from './rga/Range'; import {Editor} from './editor/Editor'; import {type ArrNode, StrNode} from '../../json-crdt/nodes'; import {Slices} from './slice/Slices'; import {LocalSlices} from './slice/LocalSlices'; import {Overlay} from './overlay/Overlay'; import {Chars} from './constants'; import {interval} from '../../json-crdt-patch/clock'; import {Model, type StrApi} from '../../json-crdt/model'; import {CONST, updateNum} from '../../json-hash'; import {SESSION} from '../../json-crdt-patch/constants'; import {s} from '../../json-crdt-patch'; import {ExtraSlices} from './slice/ExtraSlices'; import {Fragment} from './block/Fragment'; import {updateRga} from '../../json-crdt/hash'; import type {ITimestampStruct} from '../../json-crdt-patch/clock'; import type {Printable} from 'tree-dump/lib/types'; import type {MarkerSlice} from './slice/MarkerSlice'; import type {SliceSchema, SliceType} from './slice/types'; import type {SchemaToJsonNode} from '../../json-crdt/schema/types'; import type {AbstractRga} from '../../json-crdt/nodes/rga'; const EXTRA_SLICES_SCHEMA = s.vec(s.arr<SliceSchema>([])); type SlicesModel = Model<SchemaToJsonNode<typeof EXTRA_SLICES_SCHEMA>>; /** * Context for a Peritext instance. Contains all the data and methods needed to * interact with the text. */ export class Peritext<T = string> implements Printable { /** * *Slices* are rich-text annotations that appear in the text. The "saved" * slices are the ones that are persisted in the document. */ public readonly savedSlices: Slices<T>; /** * *Extra slices* are slices that are not persisted in the document. However, * they are still shared across users, i.e. they are ephemerally persisted * during the editing session. */ public readonly extraSlices: Slices<T>; /** * *Local slices* are slices that are not persisted in the document and are * not shared with other users. They are used only for local annotations for * the current user. */ public readonly localSlices: Slices<T>; public readonly editor: Editor<T>; public readonly overlay = new Overlay<T>(this); public readonly blocks: Fragment; /** * Creates a new Peritext context. * * @param model JSON CRDT model of the document where the text is stored. * @param str The {@link StrNode} where the text is stored. * @param slices The {@link ArrNode} where the slices are stored. * @param extraSlicesModel The JSON CRDT model for the extra slices, which are * not persisted in the main document, but are shared with other users. * @param localSlicesModel The JSON CRDT model for the local slices, which are * not persisted in the main document and are not shared with other * users. The local slices capture current-user-only annotations, such * as the current user's selection. */ constructor( public readonly model: Model, // TODO: Rename `str` to `rga`. public readonly str: AbstractRga<T>, slices: ArrNode, // TODO: Add test that verifies that SIDs are different across all three models. extraSlicesModel: SlicesModel = Model.create(EXTRA_SLICES_SCHEMA, model.clock.sid - 1), localSlicesModel: SlicesModel = Model.create(EXTRA_SLICES_SCHEMA, SESSION.LOCAL), ) { this.savedSlices = new Slices(this, slices); this.extraSlices = new ExtraSlices(this, extraSlicesModel.root.node().get(0)!); const localApi = localSlicesModel.api; localApi.onLocalChange.listen(() => { localApi.flush(); }); this.localSlices = new LocalSlices(this, localSlicesModel.root.node().get(0)!); this.editor = new Editor<T>(this); this.blocks = new Fragment(this as Peritext, this.pointAbsStart() as Point, this.pointAbsEnd() as Point); } public strApi(): StrApi { if (this.str instanceof StrNode) return this.model.api.wrap(this.str); throw new Error('INVALID_STR'); } /** Select a single character before a point. */ public findCharBefore(point: Point<T>): Range<T> | undefined { Iif (point.anchor === Anchor.After) { const chunk = point.chunk(); Iif (chunk && !chunk.del) return this.range(this.point(point.id, Anchor.Before), point); } const id = point.prevId(); Iif (!id) return; return this.range(this.point(id, Anchor.Before), this.point(id, Anchor.After)); } // ------------------------------------------------------------------- points /** * Creates a point at a character ID. * * @param id Character ID to which the point should be attached. * @param anchor Whether the point should be before or after the character. * @returns The point. */ public point(id: ITimestampStruct = this.str.id, anchor: Anchor = Anchor.After): Point<T> { return new Point<T>(this.str as unknown as AbstractRga<T>, id, anchor); } /** * Creates a point at a view position in the text. The `pos` argument * specifies the position of the character, not the gap between characters. * * @param pos Position of the character in the text. * @param anchor Whether the point should attach before or after a character. * Defaults to "before". * @returns The point. */ public pointAt(pos: number, anchor: Anchor = Anchor.Before): Point<T> { // TODO: Provide ability to attach to the beginning of the text? // TODO: Provide ability to attach to the end of the text? const str = this.str; const id = str.find(pos); if (!id) return this.point(str.id, pos ? Anchor.Before : Anchor.After); return this.point(id, anchor); } /** * Creates a point which is attached to the start of the text, before the * first character. * * @returns A point at the start of the text. */ public pointAbsStart(): Point<T> { return this.point(this.str.id, Anchor.After); } /** * Creates a point which is attached to the end of the text, after the last * character. * * @returns A point at the end of the text. */ public pointAbsEnd(): Point<T> { return this.point(this.str.id, Anchor.Before); } public pointStart(): Point<T> | undefined { Iif (!this.str.length()) return; const point = this.pointAbsStart(); point.refBefore(); return point; } public pointEnd(): Point<T> | undefined { Iif (!this.str.length()) return; const point = this.pointAbsEnd(); point.refAfter(); return point; } // ------------------------------------------------------------------- ranges /** * Creates a range from two points. The points can be in any order. * * @param p1 Point * @param p2 Point * @returns A range with points in correct order. */ public rangeFromPoints(p1: Point<T>, p2: Point<T>): Range<T> { return Range.from(this.str, p1, p2); } /** * Creates a range from two points, the points have to be in the correct * order. * * @param start Start point of the range, must be before or equal to end. * @param end End point of the range, must be after or equal to start. * @returns A range with the given start and end points. */ public range(start: Point<T>, end: Point<T> = start): Range<T> { return new Range(this.str, start, start === end ? end.clone() : end); } /** * A convenience method for creating a range from a view position and a * length. See {@link Range.at} for more information. * * @param start Position in the text. * @param length Length of the range. * @returns A range from the given position with the given length. */ public rangeAt(start: number, length: number = 0): Range<T> { return Range.at(this.str, start, length); } /** * Creates selection of relative start and end of the whole document. * * @returns Range, which selects the whole document, if any. */ public rangeAll(): Range<T> | undefined { const start = this.pointStart(); const end = this.pointEnd(); Iif (!start || !end) return; return this.range(start, end); } public fragment(range: Range): Fragment { return new Fragment(this as Peritext, range.start, range.end); } // ---------------------------------------------------------- text (& slices) /** * Insert plain text at a view position in the text. * * @param pos View position in the text. * @param text Text to insert. */ public insAt(pos: number, text: string): void { const str = this.strApi(); str.ins(pos, text); } /** * Insert plain text after a character referenced by its ID and return the * ID of the insertion operation. * * @param after Character ID after which the text should be inserted. * @param text Text to insert. * @returns ID of the insertion operation. */ public ins(after: ITimestampStruct, text: string): ITimestampStruct { Iif (!text) throw new Error('NO_TEXT'); const api = this.model.api; const textId = api.builder.insStr(this.str.id, after, text); api.apply(); return textId; } public delAt(pos: number, len: number): void { const range = this.rangeAt(pos, len); this.del(range); } public del(range: Range<T>): void { this.delSlices(range); this.delStr(range); } public delStr(range: Range<T>): boolean { const isCaret = range.isCollapsed(); if (isCaret) return false; const {start, end} = range; const delStartId = start.isAbsStart() ? this.point().refStart().id : start.anchor === Anchor.Before ? start.id : start.nextId(); const delEndId = end.isAbsEnd() ? this.point().refEnd().id : end.anchor === Anchor.After ? end.id : end.prevId(); Iif (!delStartId || !delEndId) throw new Error('INVALID_RANGE'); const rga = this.str; const spans = rga.findInterval2(delStartId, delEndId); const api = this.model.api; api.builder.del(rga.id, spans); api.apply(); return true; } public delSlices(range: Range<T>): boolean { // TODO: PERF: do we need this refresh? this.overlay.refresh(); range = range.range(); range.expand(); const slices = this.overlay.findContained(range); let deleted = false; if (!slices.size) return deleted; if (this.savedSlices.delSlices(slices)) deleted = true; if (this.extraSlices.delSlices(slices)) deleted = true; Iif (this.localSlices.delSlices(slices)) deleted = true; return deleted; } // ------------------------------------------------------------------ markers /** @deprecated Use the method in `Editor` and `Cursor` instead. */ public insMarker( after: ITimestampStruct, type: SliceType, data?: unknown, char: string = Chars.BlockSplitSentinel, ): MarkerSlice<T> { return this.savedSlices.insMarkerAfter(after, type, data, char); } /** @todo This can probably use .del() */ public delMarker(split: MarkerSlice<T>): void { const str = this.str; const api = this.model.api; const builder = api.builder; const strChunk = split.start.chunk(); if (strChunk) builder.del(str.id, [interval(strChunk.id, 0, 1)]); builder.del(this.savedSlices.set.id, [interval(split.id, 0, 1)]); api.apply(); } // ---------------------------------------------------------------- Printable public toString(tab: string = ''): string { const nl = () => ''; const {savedSlices, extraSlices, localSlices} = this; return ( 'Peritext' + printTree(tab, [ (tab) => this.str.toString(tab), nl, savedSlices.size() ? (tab) => savedSlices.toString(tab) : null, extraSlices.size() ? (tab) => extraSlices.toString(tab) : null, localSlices.size() ? (tab) => localSlices.toString(tab) : null, nl, (tab) => this.overlay.toString(tab), nl, (tab) => this.blocks.toString(tab), ]) ); } // ----------------------------------------------------------------- Stateful public hash: number = 0; public refresh(): number { let state: number = CONST.START_STATE; state = updateRga(state, this.str); state = updateNum(state, this.overlay.refresh()); state = updateNum(state, this.blocks.refresh()); return (this.hash = state); } } |