Skip to content

Commit accc313

Browse files
authored
Set undoManager in constructor for cells in notebook (#321)
1 parent 6f4a7c3 commit accc313

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

javascript/src/ycell.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,12 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
171171
this._undoManager = null;
172172
if (options.notebook) {
173173
this._notebook = options.notebook as YNotebook;
174-
// We cannot create a undo manager with the cell not yet attached in the notebook
175-
// so we defer that to the notebook insertCell method
174+
if (this._notebook.disableDocumentWideUndoRedo) {
175+
this._undoManager = new Y.UndoManager([this.ymodel], {
176+
trackedOrigins: new Set([this]),
177+
doc: this._notebook.ydoc
178+
});
179+
}
176180
} else {
177181
// Standalone cell
178182
const doc = new Y.Doc();
@@ -187,7 +191,7 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
187191
}
188192

189193
/**
190-
* Cell notebook awareness or null if the cell is standalone.
194+
* Cell notebook awareness or null.
191195
*/
192196
get awareness(): Awareness | null {
193197
return this._awareness ?? this.notebook?.awareness ?? null;
@@ -288,22 +292,6 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
288292
: this.notebook.undoManager;
289293
}
290294

291-
/**
292-
* Defer setting the undo manager as it requires the
293-
* cell to be attached to the notebook Y document.
294-
*/
295-
setUndoManager(): void {
296-
if (this._undoManager) {
297-
throw new Error('The cell undo manager is already set.');
298-
}
299-
300-
if (this._notebook && this._notebook.disableDocumentWideUndoRedo) {
301-
this._undoManager = new Y.UndoManager([this.ymodel], {
302-
trackedOrigins: new Set([this])
303-
});
304-
}
305-
}
306-
307295
readonly ymodel: Y.Map<any>;
308296

309297
get ysource(): Y.Text {

javascript/src/ynotebook.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ export class YNotebook
227227
);
228228
});
229229

230-
yCells.forEach(c => {
231-
c.setUndoManager();
232-
});
233-
234230
return yCells;
235231
}
236232

@@ -592,7 +588,6 @@ export class YNotebook
592588
const type = (item.content as Y.ContentType).type as Y.Map<any>;
593589
if (!this._ycellMapping.has(type)) {
594590
const c = createCellModelFromSharedType(type, { notebook: this });
595-
c!.setUndoManager();
596591
this._ycellMapping.set(type, c);
597592
}
598593
});

0 commit comments

Comments
 (0)