From 132d9d0672cd86c8a8518093776e69cb3d7b86ec Mon Sep 17 00:00:00 2001 From: Duc Trung Le Date: Thu, 16 Feb 2023 13:51:06 +0100 Subject: [PATCH] Update `YDocument` constructor --- javascript/src/ydocument.ts | 49 +++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/javascript/src/ydocument.ts b/javascript/src/ydocument.ts index 5f4c9d1..77ea2f2 100644 --- a/javascript/src/ydocument.ts +++ b/javascript/src/ydocument.ts @@ -13,8 +13,19 @@ import type { DocumentChange, ISharedDocument, StateChange } from './api.js'; * Generic shareable document. */ export class YDocument implements ISharedDocument { - constructor() { - this.ystate.observe(this.onStateChanged); + constructor(options?: YDocument.IOptions) { + this._ydoc = options?.ydoc ?? new Y.Doc(); + + 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); } /** @@ -25,27 +36,32 @@ export class YDocument implements ISharedDocument { } /** - * YJS document + * YJS document. */ - readonly ydoc = new Y.Doc(); + get ydoc(): Y.Doc { + return this._ydoc; + } /** * Shared state */ - readonly ystate: Y.Map = this.ydoc.getMap('state'); + get ystate(): Y.Map { + return this._ystate; + } /** * YJS document undo manager */ - readonly undoManager = new Y.UndoManager([], { - trackedOrigins: new Set([this]), - doc: this.ydoc - }); + get undoManager(): Y.UndoManager { + return this._undoManager; + } /** * Shared awareness */ - readonly awareness = new Awareness(this.ydoc); + get awareness(): Awareness { + return this._awareness; + } /** * The changed signal. @@ -178,6 +194,19 @@ export class YDocument implements ISharedDocument { }; protected _changed = new Signal(this); + private _ydoc: Y.Doc; + private _ystate: Y.Map; + private _undoManager: Y.UndoManager; + private _awareness: Awareness; private _isDisposed = false; private _disposed = new Signal(this); } + +namespace YDocument { + export interface IOptions { + /** + * The optional YJS document for YDocument. + */ + ydoc?: Y.Doc; + } +}