@@ -13,8 +13,19 @@ import type { DocumentChange, ISharedDocument, StateChange } from './api.js';
13
13
* Generic shareable document.
14
14
*/
15
15
export class YDocument < T extends DocumentChange > implements ISharedDocument {
16
- constructor ( ) {
17
- this . ystate . observe ( this . onStateChanged ) ;
16
+ constructor ( options ?: YDocument . IOptions ) {
17
+ this . _ydoc = options ?. ydoc ?? new Y . Doc ( ) ;
18
+
19
+ this . _ystate = this . _ydoc . getMap ( 'state' ) ;
20
+
21
+ this . _undoManager = new Y . UndoManager ( [ ] , {
22
+ trackedOrigins : new Set ( [ this ] ) ,
23
+ doc : this . _ydoc
24
+ } ) ;
25
+
26
+ this . _awareness = new Awareness ( this . _ydoc ) ;
27
+
28
+ this . _ystate . observe ( this . onStateChanged ) ;
18
29
}
19
30
20
31
/**
@@ -25,27 +36,32 @@ export class YDocument<T extends DocumentChange> implements ISharedDocument {
25
36
}
26
37
27
38
/**
28
- * YJS document
39
+ * YJS document.
29
40
*/
30
- readonly ydoc = new Y . Doc ( ) ;
41
+ get ydoc ( ) : Y . Doc {
42
+ return this . _ydoc ;
43
+ }
31
44
32
45
/**
33
46
* Shared state
34
47
*/
35
- readonly ystate : Y . Map < any > = this . ydoc . getMap ( 'state' ) ;
48
+ get ystate ( ) : Y . Map < any > {
49
+ return this . _ystate ;
50
+ }
36
51
37
52
/**
38
53
* YJS document undo manager
39
54
*/
40
- readonly undoManager = new Y . UndoManager ( [ ] , {
41
- trackedOrigins : new Set ( [ this ] ) ,
42
- doc : this . ydoc
43
- } ) ;
55
+ get undoManager ( ) : Y . UndoManager {
56
+ return this . _undoManager ;
57
+ }
44
58
45
59
/**
46
60
* Shared awareness
47
61
*/
48
- readonly awareness = new Awareness ( this . ydoc ) ;
62
+ get awareness ( ) : Awareness {
63
+ return this . _awareness ;
64
+ }
49
65
50
66
/**
51
67
* The changed signal.
@@ -178,6 +194,19 @@ export class YDocument<T extends DocumentChange> implements ISharedDocument {
178
194
} ;
179
195
180
196
protected _changed = new Signal < this, T > ( this ) ;
197
+ private _ydoc : Y . Doc ;
198
+ private _ystate : Y . Map < any > ;
199
+ private _undoManager : Y . UndoManager ;
200
+ private _awareness : Awareness ;
181
201
private _isDisposed = false ;
182
202
private _disposed = new Signal < this, void > ( this ) ;
183
203
}
204
+
205
+ namespace YDocument {
206
+ export interface IOptions {
207
+ /**
208
+ * The optional YJS document for YDocument.
209
+ */
210
+ ydoc ?: Y . Doc ;
211
+ }
212
+ }
0 commit comments