Browse Source

Fix undo manager double types

pull/3411/head
Artur Arseniev 5 years ago
parent
commit
1ed66a5a5c
  1. 3
      src/dom_components/model/Component.js
  2. 27
      src/undo_manager/index.js
  3. 14
      test/specs/editor/index.js

3
src/dom_components/model/Component.js

@ -220,7 +220,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
const um = em && em.get('UndoManager'); const um = em && em.get('UndoManager');
const comps = this.components(); const comps = this.components();
if (um && !this.__hasUm) { if (um && !this.__hasUm) {
um.add(this); // um.add(this);
um.add(comps); um.add(comps);
this.__hasUm = 1; this.__hasUm = 1;
} }
@ -865,6 +865,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
const addChild = !this.opt.avoidChildren; const addChild = !this.opt.avoidChildren;
this.set('components', comps); this.set('components', comps);
addChild && addChild &&
components &&
comps.add( comps.add(
isFunction(components) ? components(this) : components, isFunction(components) ? components(this) : components,
this.opt this.opt

27
src/undo_manager/index.js

@ -106,34 +106,7 @@ export default () => {
}; };
} }
}); });
const customUndoType = {
on(object, value, opt = {}) {
!beforeCache && (beforeCache = object.previousAttributes());
if (hasSkip(opt)) {
return;
} else {
const result = {
object,
before: beforeCache,
after: object.toJSON({ keepSymbols: 1 })
};
beforeCache = null;
return result;
}
},
undo(model, bf, af, opt) {
model.set(bf);
},
redo(model, bf, af, opt) {
model.set(af);
}
};
const events = ['style', 'attributes', 'content', 'src'];
events.forEach(ev => um.addUndoType(`change:${ev}`, customUndoType));
um.on('undo redo', () => um.on('undo redo', () =>
em.trigger('component:toggled change:canvasOffset') em.trigger('component:toggled change:canvasOffset')
); );

14
test/specs/editor/index.js

@ -9,6 +9,7 @@ describe('Editor', () => {
beforeEach(() => { beforeEach(() => {
editor = new Editor(); editor = new Editor();
editor.init(); editor.init();
editor.getModel().loadOnStart();
}); });
afterEach(() => { afterEach(() => {
@ -24,7 +25,7 @@ describe('Editor', () => {
const allKeys = keys(all); const allKeys = keys(all);
// By default 1 wrapper components is created // By default 1 wrapper components is created
expect(allKeys.length).toBe(initComps); expect(allKeys.length).toBe(initComps);
expect(allKeys[0]).toBe('wrapper'); expect(all[allKeys[0]].get('type')).toBe('wrapper');
}); });
test('Has no CSS rules', () => { test('Has no CSS rules', () => {
@ -41,7 +42,7 @@ describe('Editor', () => {
const wrapper = editor.getWrapper(); const wrapper = editor.getWrapper();
const style = editor.getStyle(); const style = editor.getStyle();
const frame = editor.Canvas.getFrame(); const frame = editor.Canvas.getFrame();
expect(wrapper).toBe(frame.get('root')); expect(wrapper).toBe(frame.getComponent());
expect(style).toBe(frame.get('styles')); expect(style).toBe(frame.get('styles'));
}); });
@ -72,7 +73,6 @@ describe('Editor', () => {
}); });
test('Components are correctly tracked with UndoManager', () => { test('Components are correctly tracked with UndoManager', () => {
editor.Components.postLoad(); // Init UndoManager
const all = editor.Components.allById(); const all = editor.Components.allById();
const um = editor.UndoManager; const um = editor.UndoManager;
const umStack = um.getStack(); const umStack = um.getStack();
@ -88,7 +88,6 @@ describe('Editor', () => {
}); });
test('Components are correctly tracked with UndoManager and mutiple operations', () => { test('Components are correctly tracked with UndoManager and mutiple operations', () => {
editor.Components.postLoad(); // Init UndoManager
const all = editor.Components.allById(); const all = editor.Components.allById();
const um = editor.UndoManager; const um = editor.UndoManager;
const umStack = um.getStack(); const umStack = um.getStack();
@ -106,12 +105,11 @@ describe('Editor', () => {
.components() .components()
.at(0) .at(0)
.remove(); // Remove 1 component .remove(); // Remove 1 component
// UM registers 2 identical remove undoTypes as Backbone triggers remove from the
// collection and the model expect(umStack.length).toBe(2);
expect(umStack.length).toBe(3);
expect(keys(all).length).toBe(3 + initComps); expect(keys(all).length).toBe(3 + initComps);
wrapper.empty(); wrapper.empty();
expect(umStack.length).toBe(4); expect(umStack.length).toBe(3);
expect(keys(all).length).toBe(initComps); expect(keys(all).length).toBe(initComps);
}); });
}); });

Loading…
Cancel
Save