-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathydocument.ts
285 lines (248 loc) · 5.82 KB
/
ydocument.ts
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
/* -----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { JSONExt, JSONObject, JSONValue } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import { Awareness } from 'y-protocols/awareness';
import * as Y from 'yjs';
import type {
DocumentChange,
IDocumentProvider,
ISharedDocument,
StateChange
} from './api.js';
/**
* Generic shareable document.
*/
export abstract class YDocument<T extends DocumentChange>
implements ISharedDocument
{
constructor(options?: YDocument.IOptions) {
this._ydoc = options?.ydoc ?? new Y.Doc();
this.rootRoomId = options?.rootRoomId ?? '';
this.currentRoomId = options?.currentRoomId ?? '';
this._ystate = this._ydoc.getMap('state');
this._undoManager = new Y.UndoManager([], {
trackedOrigins: new Set([this]),
doc: this._ydoc
});
this._awareness = new Awareness(this._ydoc);
this._ystate.observe(this.onStateChanged);
}
/**
* Document version
*/
abstract readonly version: string;
addFork(forkId: string) {
this.ystate.set(`fork_${forkId}`, 'new');
}
get provider(): IDocumentProvider {
if (this._provider === undefined) {
throw new Error('YDocument has no provider');
}
return this._provider;
}
set provider(provider: IDocumentProvider) {
this._provider = provider;
}
/**
* YJS document.
*/
get ydoc(): Y.Doc {
return this._ydoc;
}
/**
* Shared state
*/
get ystate(): Y.Map<any> {
return this._ystate;
}
/**
* YJS document undo manager
*/
get undoManager(): Y.UndoManager {
return this._undoManager;
}
/**
* Shared awareness
*/
get awareness(): Awareness {
return this._awareness;
}
/**
* The changed signal.
*/
get changed(): ISignal<this, T> {
return this._changed;
}
/**
* A signal emitted when the document is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Whether the document is disposed or not.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Document state
*/
get state(): JSONObject {
return JSONExt.deepCopy(this.ystate.toJSON());
}
/**
* Whether the object can undo changes.
*/
canUndo(): boolean {
return this.undoManager.undoStack.length > 0;
}
/**
* Whether the object can redo changes.
*/
canRedo(): boolean {
return this.undoManager.redoStack.length > 0;
}
/**
* Dispose of the resources.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.ystate.unobserve(this.onStateChanged);
this.awareness.destroy();
this.undoManager.destroy();
this.ydoc.destroy();
this._disposed.emit();
Signal.clearData(this);
}
/**
* Get the value for a state attribute
*
* @param key Key to get
*/
getState(key: string): JSONValue | undefined {
const value = this.ystate.get(key);
return typeof value === 'undefined'
? value
: (JSONExt.deepCopy(value) as unknown as JSONValue);
}
/**
* Set the value of a state attribute
*
* @param key Key to set
* @param value New attribute value
*/
setState(key: string, value: JSONValue): void {
if (!JSONExt.deepEqual(this.ystate.get(key), value)) {
this.ystate.set(key, value);
}
}
/**
* Get the document source
*
* @returns The source
*/
get source(): JSONValue | string {
return this.getSource();
}
/**
* Set the document source
*
* @param value The source to set
*/
set source(value: JSONValue | string) {
this.setSource(value);
}
/**
* Get the document source
*
* @returns The source
*/
abstract getSource(): JSONValue | string;
/**
* Set the document source
*
* @param value The source to set
*/
abstract setSource(value: JSONValue | string): void;
/**
* Undo an operation.
*/
undo(): void {
this.undoManager.undo();
}
/**
* Redo an operation.
*/
redo(): void {
this.undoManager.redo();
}
/**
* Clear the change stack.
*/
clearUndoHistory(): void {
this.undoManager.clear();
}
/**
* Perform a transaction. While the function f is called, all changes to the shared
* document are bundled into a single event.
*/
transact(f: () => void, undoable = true, origin: any = null): void {
this.ydoc.transact(f, undoable ? this : origin);
}
/**
* Handle a change to the ystate.
*/
protected onStateChanged = (event: Y.YMapEvent<any>): void => {
const stateChange = new Array<StateChange<any>>();
event.keysChanged.forEach(key => {
const change = event.changes.keys.get(key);
if (change) {
stateChange.push({
name: key,
oldValue: change.oldValue,
newValue: this.ystate.get(key)
});
}
});
this._changed.emit({ stateChange } as any);
};
protected _changed = new Signal<this, T>(this);
private _ydoc: Y.Doc;
private _ystate: Y.Map<any>;
private _undoManager: Y.UndoManager;
private _awareness: Awareness;
private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
private _provider: IDocumentProvider;
public rootRoomId: string;
public currentRoomId: string;
}
/**
* YDocument namespace
*/
export namespace YDocument {
/**
* YDocument constructor options
*/
export interface IOptions {
/**
* The optional YJS document for YDocument.
*/
ydoc?: Y.Doc;
/**
* The document root room ID, defaults to ''.
*/
rootRoomId?: string;
/**
* The document current room ID, defaults to ''.
*/
currentRoomId?: string;
}
}