From d649fbe64a6687f4cea08e50a64060389ee39984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Fri, 29 Nov 2024 11:29:58 +0100 Subject: [PATCH] Backport PR #246: Add optional origin to transaction, filter out 'modeldb' origin (#291) Co-authored-by: Zachary Sailer Co-authored-by: David Brochart --- javascript/src/api.ts | 3 ++- javascript/src/ycell.ts | 30 +++++++++++++++++++++--------- javascript/src/ydocument.ts | 8 ++++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/javascript/src/api.ts b/javascript/src/api.ts index 2269f44..ca08026 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -77,8 +77,9 @@ export interface ISharedBase extends IObservableDisposable { * * @param f Transaction to execute * @param undoable Whether to track the change in the action history or not (default `true`) + * @param origin Transaction origin */ - transact(f: () => void, undoable?: boolean): void; + transact(f: () => void, undoable?: boolean, origin?: any): void; } /** diff --git a/javascript/src/ycell.ts b/javascript/src/ycell.ts index ccafbf8..2e63499 100644 --- a/javascript/src/ycell.ts +++ b/javascript/src/ycell.ts @@ -563,12 +563,14 @@ export class YBaseCell * * @param f Transaction to execute * @param undoable Whether to track the change in the action history or not (default `true`) + * @param origin Transaction origin; if set to 'silent-change' and {@link undoable} is false, + * it won't emit model {@link changed}. */ - transact(f: () => void, undoable = true): void { + transact(f: () => void, undoable = true, origin: any = null): void { !this.notebook || this.notebook.disableDocumentWideUndoRedo ? this.ymodel.doc == null ? f() - : this.ymodel.doc.transact(f, undoable ? this : null) + : this.ymodel.doc.transact(f, undoable ? this : origin) : this.notebook.transact(f, undoable); } @@ -656,8 +658,13 @@ export class YBaseCell /** * Handle a change to the ymodel. */ - private _modelObserver = (events: Y.YEvent[]) => { - this._changed.emit(this.getChanges(events)); + private _modelObserver = ( + events: Y.YEvent[], + transaction: Y.Transaction + ) => { + if (transaction.origin !== 'silent-change') { + this._changed.emit(this.getChanges(events)); + } }; protected _metadataChanged = new Signal(this); @@ -783,14 +790,19 @@ export class YCodeCell updateOutputs( start: number, end: number, - outputs: Array = [] + outputs: Array = [], + origin: any = null ): void { const fin = end < this._youtputs.length ? end - start : this._youtputs.length - start; - this.transact(() => { - this._youtputs.delete(start, fin); - this._youtputs.insert(start, outputs); - }, false); + this.transact( + () => { + this._youtputs.delete(start, fin); + this._youtputs.insert(start, outputs); + }, + false, + origin + ); } /** diff --git a/javascript/src/ydocument.ts b/javascript/src/ydocument.ts index c923c12..ee01f35 100644 --- a/javascript/src/ydocument.ts +++ b/javascript/src/ydocument.ts @@ -169,9 +169,13 @@ export abstract class YDocument /** * Perform a transaction. While the function f is called, all changes to the shared * document are bundled into a single event. + * + * @param f Transaction to execute + * @param undoable Whether to track the change in the action history or not (default `true`) + * @param origin Transaction origin */ - transact(f: () => void, undoable = true): void { - this.ydoc.transact(f, undoable ? this : null); + transact(f: () => void, undoable = true, origin: any = null): void { + this.ydoc.transact(f, undoable ? this : origin); } /**