Skip to content

Set undoManager in constructor for cells in notebook #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions javascript/src/ycell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
this._undoManager = null;
if (options.notebook) {
this._notebook = options.notebook as YNotebook;
// We cannot create a undo manager with the cell not yet attached in the notebook
// so we defer that to the notebook insertCell method
if (this._notebook.disableDocumentWideUndoRedo) {
this._undoManager = new Y.UndoManager([this.ymodel], {
trackedOrigins: new Set([this]),
doc: this._notebook.ydoc
});
}
} else {
// Standalone cell
const doc = new Y.Doc();
Expand All @@ -187,7 +191,7 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
}

/**
* Cell notebook awareness or null if the cell is standalone.
* Cell notebook awareness or null.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but I spotted that if the cell is standalone then this._awareness is defined in line 184 (old line 180) so it definitely is not null.

*/
get awareness(): Awareness | null {
return this._awareness ?? this.notebook?.awareness ?? null;
Expand Down Expand Up @@ -288,22 +292,6 @@ export class YBaseCell<Metadata extends nbformat.IBaseCellMetadata>
: this.notebook.undoManager;
}

/**
* Defer setting the undo manager as it requires the
* cell to be attached to the notebook Y document.
*/
setUndoManager(): void {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove it as it is not part of the public API defined in api.ts and was only used internally in ynotebook.ts, or should we keep it and deprecate, making it a no-op?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not part of the public API, I'd say we should remove it.

if (this._undoManager) {
throw new Error('The cell undo manager is already set.');
}

if (this._notebook && this._notebook.disableDocumentWideUndoRedo) {
this._undoManager = new Y.UndoManager([this.ymodel], {
trackedOrigins: new Set([this])
});
}
}

readonly ymodel: Y.Map<any>;

get ysource(): Y.Text {
Expand Down
5 changes: 0 additions & 5 deletions javascript/src/ynotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ export class YNotebook
);
});

yCells.forEach(c => {
c.setUndoManager();
});

return yCells;
}

Expand Down Expand Up @@ -592,7 +588,6 @@ export class YNotebook
const type = (item.content as Y.ContentType).type as Y.Map<any>;
if (!this._ycellMapping.has(type)) {
const c = createCellModelFromSharedType(type, { notebook: this });
c!.setUndoManager();
this._ycellMapping.set(type, c);
}
});
Expand Down
Loading