From 5d3922cb646ac2ce8d659d95cdf264827363b305 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Tue, 30 Jan 2018 15:51:54 -0800 Subject: [PATCH] fix undo-redo --- src/Struct/Item.js | 16 +++++++++------- src/Struct/ItemJSON.js | 4 ++-- src/Struct/ItemString.js | 4 ++-- src/Struct/Type.js | 11 ++++++----- src/Type/y-xml/YXmlElement.js | 4 ++-- src/Type/y-xml/YXmlFragment.js | 24 +++++++++++++----------- src/Type/y-xml/YXmlHook.js | 5 +++++ src/Util/UndoManager.js | 2 +- 8 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/Struct/Item.js b/src/Struct/Item.js index d872482b..27f4224b 100644 --- a/src/Struct/Item.js +++ b/src/Struct/Item.js @@ -55,14 +55,16 @@ export default class Item { /** * Copy the effect of struct */ - _copy () { + _copy (undeleteChildren, copyPosition) { let struct = new this.constructor() - struct._origin = this._left - struct._left = this._left - struct._right = this - struct._right_origin = this - struct._parent = this._parent - struct._parentSub = this._parentSub + if (copyPosition) { + struct._origin = this._left + struct._left = this._left + struct._right = this + struct._right_origin = this + struct._parent = this._parent + struct._parentSub = this._parentSub + } return struct } get _lastId () { diff --git a/src/Struct/ItemJSON.js b/src/Struct/ItemJSON.js index f8d22dd4..f2d773d1 100644 --- a/src/Struct/ItemJSON.js +++ b/src/Struct/ItemJSON.js @@ -6,8 +6,8 @@ export default class ItemJSON extends Item { super() this._content = null } - _copy () { - let struct = super._copy() + _copy (undeleteChildren, copyPosition) { + let struct = super._copy(undeleteChildren, copyPosition) struct._content = this._content return struct } diff --git a/src/Struct/ItemString.js b/src/Struct/ItemString.js index a5a0b90c..7b888b64 100644 --- a/src/Struct/ItemString.js +++ b/src/Struct/ItemString.js @@ -6,8 +6,8 @@ export default class ItemString extends Item { super() this._content = null } - _copy () { - let struct = super._copy() + _copy (undeleteChildren, copyPosition) { + let struct = super._copy(undeleteChildren, copyPosition) struct._content = this._content return struct } diff --git a/src/Struct/Type.js b/src/Struct/Type.js index 2f02184e..734dcdcf 100644 --- a/src/Struct/Type.js +++ b/src/Struct/Type.js @@ -79,15 +79,16 @@ export default class Type extends Item { type = type._parent } } - _copy (undeleteChildren) { - let copy = super._copy() + _copy (undeleteChildren, copyPosition) { + let copy = super._copy(undeleteChildren, copyPosition) let map = new Map() copy._map = map for (let [key, value] of this._map) { if (undeleteChildren.has(value) || !value.deleted) { - let _item = value._copy(undeleteChildren) + let _item = value._copy(undeleteChildren, false) _item._parent = copy - map.set(key, value._copy(undeleteChildren)) + _item._parentSub = key + map.set(key, _item) } } let prevUndeleted = null @@ -95,7 +96,7 @@ export default class Type extends Item { let item = this._start while (item !== null) { if (undeleteChildren.has(item) || !item.deleted) { - let _item = item._copy(undeleteChildren) + let _item = item._copy(undeleteChildren, false) _item._left = prevUndeleted _item._origin = prevUndeleted _item._right = null diff --git a/src/Type/y-xml/YXmlElement.js b/src/Type/y-xml/YXmlElement.js index 261ceb45..39c4a3ae 100644 --- a/src/Type/y-xml/YXmlElement.js +++ b/src/Type/y-xml/YXmlElement.js @@ -20,8 +20,8 @@ export default class YXmlElement extends YXmlFragment { this._domFilter = arg2 } } - _copy (undeleteChildren) { - let struct = super._copy(undeleteChildren) + _copy (undeleteChildren, copyPosition) { + let struct = super._copy(undeleteChildren, copyPosition) struct.nodeName = this.nodeName return struct } diff --git a/src/Type/y-xml/YXmlFragment.js b/src/Type/y-xml/YXmlFragment.js index d8429cdc..ace6b680 100644 --- a/src/Type/y-xml/YXmlFragment.js +++ b/src/Type/y-xml/YXmlFragment.js @@ -152,18 +152,20 @@ export default class YXmlFragment extends YArray { attributes.set(key, attrs[key]) } } - let result = this._domFilter(this.nodeName, new Map(attributes)) - if (result === null) { - this._delete(this._y) - } else { - attributes.forEach((value, key) => { - if (!result.has(key)) { - this.removeAttribute(key) - } + this._y.transact(() => { + let result = this._domFilter(this.nodeName, new Map(attributes)) + if (result === null) { + this._delete(this._y) + } else { + attributes.forEach((value, key) => { + if (!result.has(key)) { + this.removeAttribute(key) + } + }) + } + this.forEach(xml => { + xml.setDomFilter(f) }) - } - this.forEach(xml => { - xml.setDomFilter(f) }) } _callObserver (transaction, parentSubs, remote) { diff --git a/src/Type/y-xml/YXmlHook.js b/src/Type/y-xml/YXmlHook.js index adef4368..60926148 100644 --- a/src/Type/y-xml/YXmlHook.js +++ b/src/Type/y-xml/YXmlHook.js @@ -14,6 +14,11 @@ export default class YXmlHook extends YMap { getHook(hookName).fillType(dom, this) } } + _copy (undeleteChildren, copyPosition) { + const struct = super._copy(undeleteChildren, copyPosition) + struct.hookName = this.hookName + return struct + } getDom (_document) { _document = _document || document if (this._dom === null) { diff --git a/src/Util/UndoManager.js b/src/Util/UndoManager.js index a7ad9820..9d8a16ef 100644 --- a/src/Util/UndoManager.js +++ b/src/Util/UndoManager.js @@ -50,7 +50,7 @@ function applyReverseOperation (y, scope, reverseBuffer) { ) ) { performedUndo = true - op = op._copy(undoOp.deletedStructs) + op = op._copy(undoOp.deletedStructs, true) op._integrate(y) } }