@@ -13,10 +13,16 @@ import {Model} from '../../json-crdt/model';
1313import { CONST , updateNum } from '../../json-hash' ;
1414import { SESSION } from '../../json-crdt-patch/constants' ;
1515import { s } from '../../json-crdt-patch' ;
16+ import { ExtraSlices } from './slice/ExtraSlices' ;
1617import type { ITimestampStruct } from '../../json-crdt-patch/clock' ;
1718import type { Printable } from 'tree-dump/lib/types' ;
18- import type { SliceType } from './types' ;
1919import type { MarkerSlice } from './slice/MarkerSlice' ;
20+ import type { SliceSchema , SliceType } from './slice/types' ;
21+ import type { SchemaToJsonNode } from '../../json-crdt/schema/types' ;
22+
23+ const EXTRA_SLICES_SCHEMA = s . vec ( s . arr < SliceSchema > ( [ ] ) ) ;
24+
25+ type SlicesModel = Model < SchemaToJsonNode < typeof EXTRA_SLICES_SCHEMA > > ;
2026
2127/**
2228 * Context for a Peritext instance. Contains all the data and methods needed to
@@ -46,27 +52,33 @@ export class Peritext implements Printable {
4652 public readonly editor : Editor ;
4753 public readonly overlay = new Overlay ( this ) ;
4854
55+ /**
56+ * Creates a new Peritext context.
57+ *
58+ * @param model JSON CRDT model of the document where the text is stored.
59+ * @param str The {@link StrNode} where the text is stored.
60+ * @param slices The {@link ArrNode} where the slices are stored.
61+ * @param extraSlicesModel The JSON CRDT model for the extra slices, which are
62+ * not persisted in the main document, but are shared with other users.
63+ * @param localSlicesModel The JSON CRDT model for the local slices, which are
64+ * not persisted in the main document and are not shared with other
65+ * users. The local slices capture current-user-only annotations, such
66+ * as the current user's selection.
67+ */
4968 constructor (
5069 public readonly model : Model ,
5170 public readonly str : StrNode ,
5271 slices : ArrNode ,
72+ extraSlicesModel : SlicesModel = Model . create ( EXTRA_SLICES_SCHEMA , model . clock . sid - 1 ) ,
73+ localSlicesModel : SlicesModel = Model . create ( EXTRA_SLICES_SCHEMA , SESSION . LOCAL ) ,
5374 ) {
5475 this . savedSlices = new Slices ( this . model , slices , this . str ) ;
55-
56- const extraModel = Model . withLogicalClock ( SESSION . GLOBAL )
57- . setSchema ( s . vec ( s . arr ( [ ] ) ) )
58- . fork ( this . model . clock . sid + 1 ) ;
59- this . extraSlices = new Slices ( extraModel , extraModel . root . node ( ) . get ( 0 ) ! , this . str ) ;
60-
61- // TODO: flush patches
62- // TODO: remove `arr` tombstones
63- const localModel = Model . withLogicalClock ( SESSION . LOCAL ) . setSchema ( s . vec ( s . arr ( [ ] ) ) ) ;
64- const localApi = localModel . api ;
76+ this . extraSlices = new ExtraSlices ( extraSlicesModel , extraSlicesModel . root . node ( ) . get ( 0 ) ! , this . str ) ;
77+ const localApi = localSlicesModel . api ;
6578 localApi . onLocalChange . listen ( ( ) => {
6679 localApi . flush ( ) ;
6780 } ) ;
68- this . localSlices = new LocalSlices ( localModel , localModel . root . node ( ) . get ( 0 ) ! , this . str ) ;
69-
81+ this . localSlices = new LocalSlices ( localSlicesModel , localSlicesModel . root . node ( ) . get ( 0 ) ! , this . str ) ;
7082 this . editor = new Editor ( this , this . localSlices ) ;
7183 }
7284
@@ -99,8 +111,8 @@ export class Peritext implements Printable {
99111 }
100112
101113 /**
102- * Creates a point at a view position in the text. The `pos` argument specifies
103- * the position of the character, not the gap between characters.
114+ * Creates a point at a view position in the text. The `pos` argument
115+ * specifies the position of the character, not the gap between characters.
104116 *
105117 * @param pos Position of the character in the text.
106118 * @param anchor Whether the point should attach before or after a character.
@@ -150,7 +162,8 @@ export class Peritext implements Printable {
150162 }
151163
152164 /**
153- * Creates a range from two points, the points have to be in the correct order.
165+ * Creates a range from two points, the points have to be in the correct
166+ * order.
154167 *
155168 * @param start Start point of the range, must be before or equal to end.
156169 * @param end End point of the range, must be after or equal to start.
@@ -161,8 +174,8 @@ export class Peritext implements Printable {
161174 }
162175
163176 /**
164- * A convenience method for creating a range from a view position and a length.
165- * See {@link Range.at} for more information.
177+ * A convenience method for creating a range from a view position and a
178+ * length. See {@link Range.at} for more information.
166179 *
167180 * @param start Position in the text.
168181 * @param length Length of the range.
@@ -238,14 +251,15 @@ export class Peritext implements Printable {
238251
239252 public toString ( tab : string = '' ) : string {
240253 const nl = ( ) => '' ;
254+ const { savedSlices, extraSlices, localSlices} = this ;
241255 return (
242256 this . constructor . name +
243257 printTree ( tab , [
244- ( tab ) => this . editor . cursor . toString ( tab ) ,
245- nl ,
246258 ( tab ) => this . str . toString ( tab ) ,
247259 nl ,
248- ( tab ) => this . savedSlices . toString ( tab ) ,
260+ savedSlices . size ( ) ? ( tab ) => savedSlices . toString ( tab ) : null ,
261+ extraSlices . size ( ) ? ( tab ) => extraSlices . toString ( tab ) : null ,
262+ localSlices . size ( ) ? ( tab ) => localSlices . toString ( tab ) : null ,
249263 nl ,
250264 ( tab ) => this . overlay . toString ( tab ) ,
251265 ] )
0 commit comments