From 6fa8778fc76aa1ec928e6e04e4485eb75062a42e Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Fri, 22 Jul 2022 14:22:46 +0200 Subject: [PATCH] add doc parameter to UndoManager --- src/utils/UndoManager.js | 6 ++++-- tests/undo-redo.tests.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/utils/UndoManager.js b/src/utils/UndoManager.js index 25e68c63..c53924a1 100644 --- a/src/utils/UndoManager.js +++ b/src/utils/UndoManager.js @@ -139,6 +139,7 @@ const popStackItem = (undoManager, stack, eventType) => { * undo/redo scope. * @property {Set} [UndoManagerOptions.trackedOrigins=new Set([null])] * @property {boolean} [ignoreRemoteMapChanges] Experimental. By default, the UndoManager will never overwrite remote changes. Enable this property to enable overwriting remote changes on key-value changes (Y.Map, properties on Y.Xml, etc..). + * @property {Doc} [doc] The document that this UndoManager operates on. Only needed if typeScope is empty. */ /** @@ -160,7 +161,8 @@ export class UndoManager extends Observable { captureTransaction = tr => true, deleteFilter = () => true, trackedOrigins = new Set([null]), - ignoreRemoteMapChanges = false + ignoreRemoteMapChanges = false, + doc = /** @type {Doc} */ (array.isArray(typeScope) ? typeScope[0].doc : typeScope.doc) } = {}) { super() /** @@ -187,7 +189,7 @@ export class UndoManager extends Observable { */ this.undoing = false this.redoing = false - this.doc = /** @type {Doc} */ (this.scope[0].doc) + this.doc = doc this.lastChange = 0 this.ignoreRemoteMapChanges = ignoreRemoteMapChanges /** diff --git a/tests/undo-redo.tests.js b/tests/undo-redo.tests.js index c1889d9f..b5bbeba8 100644 --- a/tests/undo-redo.tests.js +++ b/tests/undo-redo.tests.js @@ -49,6 +49,20 @@ export const testUndoText = tc => { t.compare(text0.toDelta(), [{ insert: 'b' }, { insert: 'cxy', attributes: { bold: true } }, { insert: 'z' }]) } +/** + * Test case to fix #241 + * @param {t.TestCase} tc + */ +export const testEmptyTypeScope = tc => { + const ydoc = new Y.Doc() + const um = new Y.UndoManager([], { doc: ydoc }) + const yarray = ydoc.getArray() + um.addToScope(yarray) + yarray.insert(0, [1]) + um.undo() + t.assert(yarray.length === 0) +} + /** * Test case to fix #241 * @param {t.TestCase} tc