Browse Source

Update symbol refs before remove

pull/3428/head
Artur Arseniev 6 years ago
parent
commit
cab183a40b
  1. 28
      src/dom_components/model/Component.js
  2. 2
      src/undo_manager/index.js
  3. 11
      test/specs/dom_components/model/Symbols.js

28
src/dom_components/model/Component.js

@ -839,6 +839,15 @@ const Component = Backbone.Model.extend(Styleable).extend(
symb.append(toAppend, { fromInstance: this, ...o }); symb.append(toAppend, { fromInstance: this, ...o });
}); });
} else { } else {
// Remove instance reference from the symbol
const symb = m.__getSymbol();
symb &&
!o.temporary &&
symb.set(
keySymbols,
symb.__getSymbols().filter(i => i !== m)
);
// Propagate remove only if the component is an inner symbol // Propagate remove only if the component is an inner symbol
if (!m.__isSymbolTop()) { if (!m.__isSymbolTop()) {
const { index } = o; const { index } = o;
@ -864,15 +873,6 @@ const Component = Backbone.Model.extend(Styleable).extend(
}); });
toUp.forEach(toUpFn); toUp.forEach(toUpFn);
} }
// Remove instance reference from the symbol
const symb = m.__getSymbol();
symb &&
!o.temporary &&
symb.set(
keySymbols,
symb.__getSymbols().filter(i => i !== m)
);
} }
this.__changesUp(optUp); this.__changesUp(optUp);
@ -1428,11 +1428,13 @@ const Component = Backbone.Model.extend(Styleable).extend(
delete obj.open; // used in Layers delete obj.open; // used in Layers
if (!opts.fromUndo) { if (!opts.fromUndo) {
if (obj[keySymbols]) { const symbol = obj[keySymbol];
obj[keySymbols] = (this.__getSymbols() || []).map(i => i.getId()); const symbols = obj[keySymbols];
if (symbols && isArray(symbols)) {
obj[keySymbols] = symbols.map(i => (i.getId ? i.getId() : i));
} }
if (obj[keySymbol] && !isString(obj[keySymbol])) { if (symbol && !isString(symbol)) {
obj[keySymbol] = obj[keySymbol].getId(); obj[keySymbol] = symbol.getId();
} }
} }

2
src/undo_manager/index.js

@ -48,7 +48,7 @@ export default () => {
* @private * @private
*/ */
init(opts = {}) { init(opts = {}) {
config = { ...opts, ...configDef }; config = { ...configDef, ...opts };
em = config.em; em = config.em;
this.em = em; this.em = em;
const fromUndo = true; const fromUndo = true;

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

@ -229,6 +229,17 @@ describe('Symbols', () => {
expect(symbol.__getSymbols().length).toBe(allInst.length - 1); expect(symbol.__getSymbols().length).toBe(allInst.length - 1);
}); });
test('Removing one instance, works with UndoManager', done => {
setTimeout(() => {
// This will commit the undo
const um = getUm(comp);
allInst[0].remove();
um.undo();
expect(symbol.__getSymbols().length).toBe(allInst.length);
done();
});
});
test('Adding a new component to a symbol, it will be propogated to all instances', () => { test('Adding a new component to a symbol, it will be propogated to all instances', () => {
const added = symbol.append(simpleComp, { at: 0 })[0]; const added = symbol.append(simpleComp, { at: 0 })[0];
all.forEach(cmp => all.forEach(cmp =>

Loading…
Cancel
Save