11import {
2- YFile as DefaultYFile
3- // YNotebook as DefaultYNotebook
2+ YFile as DefaultYFile ,
3+ YNotebook as DefaultYNotebook ,
4+ ISharedNotebook
45} from '@jupyter/ydoc' ;
56import * as Y from 'yjs' ;
67import { Awareness } from 'y-protocols/awareness' ;
@@ -34,14 +35,14 @@ export class YFile extends DefaultYFile {
3435 ( this as any ) . _ydoc = new Y . Doc ( ) ;
3536
3637 // Reset all properties derived from `this._ydoc`
37- ( this as any ) . ysource = ( this as any ) . _ydoc . getText ( 'source' ) ;
38- ( this as any ) . _ystate = ( this as any ) . _ydoc . getMap ( 'state' ) ;
38+ ( this as any ) . ysource = this . ydoc . getText ( 'source' ) ;
39+ ( this as any ) . _ystate = this . ydoc . getMap ( 'state' ) ;
3940 ( this as any ) . _undoManager = new Y . UndoManager ( [ ] , {
4041 trackedOrigins : new Set ( [ this ] ) ,
4142 doc : ( this as any ) . _ydoc
4243 } ) ;
4344 ( this as any ) . _undoManager . addToScope ( this . ysource ) ;
44- ( this as any ) . _awareness = new Awareness ( ( this as any ) . _ydoc ) ;
45+ ( this as any ) . _awareness = new Awareness ( this . ydoc ) ;
4546
4647 // Emit to `this.resetSignal` to inform consumers immediately
4748 this . _resetSignal . emit ( null ) ;
@@ -64,3 +65,52 @@ export class YFile extends DefaultYFile {
6465
6566 _resetSignal : Signal < this, null > ;
6667}
68+
69+ export class YNotebook extends DefaultYNotebook {
70+ constructor ( options ?: Omit < ISharedNotebook . IOptions , 'data' > ) {
71+ super ( options ) ;
72+ this . _resetSignal = new Signal ( this ) ;
73+ }
74+
75+ /**
76+ * See `YFile.reset()`.
77+ */
78+ reset ( ) {
79+ // Remove default observers
80+ this . _ycells . unobserve ( ( this as any ) . _onYCellsChanged ) ;
81+ this . ymeta . unobserveDeep ( ( this as any ) . _onMetaChanged ) ;
82+ ( this as any ) . _ystate . unobserve ( this . onStateChanged ) ;
83+
84+ // Reset `this._ydoc` to an empty state
85+ ( this as any ) . _ydoc = new Y . Doc ( ) ;
86+
87+ // Reset all properties derived from `this._ydoc`
88+ ( this as any ) . _ystate = this . ydoc . getMap ( 'state' ) ;
89+ ( this as any ) . _ycells = this . ydoc . getArray ( 'cells' ) ;
90+ ( this as any ) . cells = [ ] ;
91+ ( this as any ) . ymeta = this . ydoc . getMap ( 'meta' ) ;
92+ ( this as any ) . _undoManager = new Y . UndoManager ( [ ] , {
93+ trackedOrigins : new Set ( [ this ] ) ,
94+ doc : ( this as any ) . _ydoc
95+ } ) ;
96+ ( this as any ) . _undoManager . addToScope ( this . _ycells ) ;
97+ ( this as any ) . _awareness = new Awareness ( this . ydoc ) ;
98+
99+ // Emit to `this.resetSignal` to inform consumers immediately
100+ this . _resetSignal . emit ( null ) ;
101+
102+ // Add back default observers
103+ this . _ycells . observe ( ( this as any ) . _onYCellsChanged ) ;
104+ this . ymeta . observeDeep ( ( this as any ) . _onMetaChanged ) ;
105+ ( this as any ) . _ystate . observe ( this . onStateChanged ) ;
106+ }
107+
108+ /**
109+ * See `YFile.resetSignal`.
110+ */
111+ get resetSignal ( ) : ISignal < this, null > {
112+ return this . _resetSignal ;
113+ }
114+
115+ _resetSignal : Signal < this, null > ;
116+ }
0 commit comments