Browse Source

Adjust UndoManager for symbols

pull/3426/head
Artur Arseniev 5 years ago
parent
commit
957c46dd13
  1. 15
      src/dom_components/model/Component.js
  2. 7
      src/undo_manager/index.js
  3. 17
      test/specs/dom_components/model/Symbols.js

15
src/dom_components/model/Component.js

@ -738,7 +738,12 @@ const Component = Backbone.Model.extend(Styleable).extend(
const { em } = this;
const symbEnabled = em && em.get('symbols');
if (opts.fromInstance || opts.noPropagate || !symbEnabled) {
if (
opts.fromInstance ||
opts.noPropagate ||
opts.fromUndo ||
!symbEnabled
) {
return result;
}
@ -781,7 +786,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
},
__upSymbCls(m, c, opts = {}) {
const toUp = this.__getSymbToUp();
const toUp = this.__getSymbToUp(opts);
this.__logSymbol('classes', toUp, { opts });
toUp.forEach(child => {
// This will propagate the change up to __upSymbProps
@ -792,8 +797,8 @@ const Component = Backbone.Model.extend(Styleable).extend(
__upSymbComps(m, c, o) {
const optUp = o || c || {};
const { fromInstance } = optUp;
const toUpOpts = { fromInstance };
const { fromInstance, fromUndo } = optUp;
const toUpOpts = { fromInstance, fromUndo };
const isTemp = m.opt.temporary;
if (!o) {
@ -1424,7 +1429,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
if (!opts.fromUndo) {
if (obj[keySymbols]) {
obj[keySymbols] = this.__getSymbToUp().map(i => i.getId());
obj[keySymbols] = (this.__getSymbols() || []).map(i => i.getId());
}
if (obj[keySymbol] && !isString(obj[keySymbol])) {
obj[keySymbol] = obj[keySymbol].getId();

7
src/undo_manager/index.js

@ -51,6 +51,7 @@ export default () => {
config = { ...opts, ...configDef };
em = config.em;
this.em = em;
const fromUndo = true;
um = new UndoManager({ track: true, register: [], ...config });
um.changeUndoType('change', {
condition: object => {
@ -78,7 +79,7 @@ export default () => {
const result = {
object,
before: beforeCache,
after: object.toJSON({ fromUndo: 1 })
after: object.toJSON({ fromUndo })
};
beforeCache = null;
return result;
@ -92,7 +93,7 @@ export default () => {
object: collection,
before: undefined,
after: model,
options: { ...options }
options: { ...options, fromUndo }
};
}
});
@ -103,7 +104,7 @@ export default () => {
object: collection,
before: model,
after: undefined,
options: { ...options }
options: { ...options, fromUndo }
};
}
});

17
test/specs/dom_components/model/Symbols.js

@ -27,6 +27,7 @@ describe('Symbols', () => {
let allInst, all, comp, symbol, compInitChild;
let secComp, secSymbol;
const getUm = cmp => cmp.em.get('UndoManager');
const getInnerComp = (cmp, i = 0) => cmp.components().at(i);
const getFirstInnSymbol = cmp => getInnerComp(cmp).__getSymbol();
const getInnSymbol = (cmp, i = 0) => getInnerComp(cmp, i).__getSymbol();
@ -249,6 +250,22 @@ describe('Symbols', () => {
allInst.forEach(cmp => expect(getFirstInnSymbol(cmp)).toBe(addSymb));
});
test('Adding a new component to an instance of the symbol, works correctly with Undo Manager', () => {
const added = comp.append(simpleComp, { at: 0 })[0];
const um = getUm(added);
um.undo();
all.forEach(cmp => expect(cmp.components().length).toBe(compInitChild));
um.redo();
um.undo();
um.redo(); // check multiple undo/redo
all.forEach(cmp =>
expect(cmp.components().length).toBe(compInitChild + 1)
);
// Check symbol references
const addSymbs = added.__getSymbol().__getSymbols();
expect(addSymbs.length).toBe(allInst.length);
});
test('Moving a new added component in the instance, will propagate the action in all symbols', () => {
const added = comp.append(simpleComp)[0];
expect(added.index()).toBe(compInitChild);

Loading…
Cancel
Save