Browse Source

Add getStackGroup method to UndoManager

pull/2732/head
Artur Arseniev 7 years ago
parent
commit
936e30c434
  1. 25
      src/undo_manager/index.js
  2. 23
      test/specs/editor/index.js

25
src/undo_manager/index.js

@ -251,6 +251,31 @@ export default () => {
return um.stack;
},
/**
* Get grouped undo manager stack.
* The difference between `getStack` is when you do multiple operations at a time,
* like appending multiple components:
* `editor.getWrapper().append(`<div>C1</div><div>C2</div>`);`
* `getStack` will return a collection length of 2.
* `getStackGroup` instead will group them as a single operation (the first
* inserted component will be returned in the list) by returning an array length of 1.
* @return {Array}
*/
getStackGroup() {
const result = [];
const inserted = [];
this.getStack().forEach(item => {
const index = item.get('magicFusionIndex');
if (inserted.indexOf(index) < 0) {
inserted.push(index);
result.push(item);
}
});
return result;
},
/**
* Clear the stack
* @return {this}

23
test/specs/editor/index.js

@ -51,10 +51,9 @@ describe('Editor', () => {
expect(keys(all).length).toBe(1 + initComps);
});
test.only('Components are correctly tracked on add and remove', () => {
test('Components are correctly tracked on add and remove', () => {
const all = editor.Components.allById();
const wrapper = editor.getWrapper();
console.log(keys(all));
const added = wrapper.append(`
<div>Component 1</div>
<div></div>
@ -66,15 +65,23 @@ describe('Editor', () => {
<div>Component 3</div>
`);
expect(keys(all).length).toBe(4 + initComps);
console.log(keys(all));
wrapper.empty();
console.log(keys(all));
expect(wrapper.components().length).toBe(0);
expect(keys(all).length).toBe(initComps);
});
// test('New component correctly added', () => {
// const all = editor.Css.getAll();
// expect(all.length).toBe(0);
// });
test.only('Components are correctly tracked with UndoManager', () => {
editor.Components.postLoad(); // Init UndoManager
const all = editor.Components.allById();
const um = editor.UndoManager;
const umStack = um.getStack();
const wrapper = editor.getWrapper();
expect(umStack.length).toBe(0);
wrapper.append(`<div>Component 1</div><div>Component 2</div>`);
expect(umStack.length).toBe(1);
wrapper.empty();
console.log('Post reset', umStack);
expect(umStack.length).toBe(2);
expect(keys(all).length).toBe(initComps);
});
});

Loading…
Cancel
Save