@@ -13,10 +13,16 @@ import {Model} from '../../json-crdt/model';
13
13
import { CONST , updateNum } from '../../json-hash' ;
14
14
import { SESSION } from '../../json-crdt-patch/constants' ;
15
15
import { s } from '../../json-crdt-patch' ;
16
+ import { ExtraSlices } from './slice/ExtraSlices' ;
16
17
import type { ITimestampStruct } from '../../json-crdt-patch/clock' ;
17
18
import type { Printable } from 'tree-dump/lib/types' ;
18
- import type { SliceType } from './types' ;
19
19
import 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 > > ;
20
26
21
27
/**
22
28
* Context for a Peritext instance. Contains all the data and methods needed to
@@ -46,27 +52,33 @@ export class Peritext implements Printable {
46
52
public readonly editor : Editor ;
47
53
public readonly overlay = new Overlay ( this ) ;
48
54
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
+ */
49
68
constructor (
50
69
public readonly model : Model ,
51
70
public readonly str : StrNode ,
52
71
slices : ArrNode ,
72
+ extraSlicesModel : SlicesModel = Model . create ( EXTRA_SLICES_SCHEMA , model . clock . sid - 1 ) ,
73
+ localSlicesModel : SlicesModel = Model . create ( EXTRA_SLICES_SCHEMA , SESSION . LOCAL ) ,
53
74
) {
54
75
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 ;
65
78
localApi . onLocalChange . listen ( ( ) => {
66
79
localApi . flush ( ) ;
67
80
} ) ;
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 ) ;
70
82
this . editor = new Editor ( this , this . localSlices ) ;
71
83
}
72
84
@@ -99,8 +111,8 @@ export class Peritext implements Printable {
99
111
}
100
112
101
113
/**
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.
104
116
*
105
117
* @param pos Position of the character in the text.
106
118
* @param anchor Whether the point should attach before or after a character.
@@ -150,7 +162,8 @@ export class Peritext implements Printable {
150
162
}
151
163
152
164
/**
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.
154
167
*
155
168
* @param start Start point of the range, must be before or equal to end.
156
169
* @param end End point of the range, must be after or equal to start.
@@ -161,8 +174,8 @@ export class Peritext implements Printable {
161
174
}
162
175
163
176
/**
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.
166
179
*
167
180
* @param start Position in the text.
168
181
* @param length Length of the range.
@@ -238,14 +251,15 @@ export class Peritext implements Printable {
238
251
239
252
public toString ( tab : string = '' ) : string {
240
253
const nl = ( ) => '' ;
254
+ const { savedSlices, extraSlices, localSlices} = this ;
241
255
return (
242
256
this . constructor . name +
243
257
printTree ( tab , [
244
- ( tab ) => this . editor . cursor . toString ( tab ) ,
245
- nl ,
246
258
( tab ) => this . str . toString ( tab ) ,
247
259
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 ,
249
263
nl ,
250
264
( tab ) => this . overlay . toString ( tab ) ,
251
265
] )
0 commit comments