mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-14 13:18:15 +02:00
Allow history to set checkpoints and reset to them
This commit is contained in:
+22
-4
@@ -15,6 +15,7 @@ export function coreHistory(context) {
|
||||
dispatch = d3.dispatch('change', 'undone', 'redone'),
|
||||
lock = utilSessionMutex('lock'),
|
||||
duration = 150,
|
||||
checkpoints = {},
|
||||
stack, index, tree;
|
||||
|
||||
|
||||
@@ -278,10 +279,27 @@ export function coreHistory(context) {
|
||||
},
|
||||
|
||||
|
||||
reset: function() {
|
||||
stack = [{graph: coreGraph()}];
|
||||
index = 0;
|
||||
tree = coreTree(stack[0].graph);
|
||||
// save the current history state
|
||||
checkpoint: function(key) {
|
||||
checkpoints[key] = {
|
||||
stack: _.cloneDeep(stack),
|
||||
index: index
|
||||
};
|
||||
return history;
|
||||
},
|
||||
|
||||
|
||||
// restore history state to a given checkpoint or reset completely
|
||||
reset: function(key) {
|
||||
if (key !== undefined) {
|
||||
stack = _.cloneDeep(checkpoints[key].stack);
|
||||
index = checkpoints[key].index;
|
||||
} else {
|
||||
stack = [{graph: coreGraph()}];
|
||||
index = 0;
|
||||
tree = coreTree(stack[0].graph);
|
||||
checkpoints = {};
|
||||
}
|
||||
dispatch.call('change');
|
||||
return history;
|
||||
},
|
||||
|
||||
@@ -283,6 +283,36 @@ describe('iD.History', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#checkpoint', function () {
|
||||
it('saves and resets to checkpoints', function () {
|
||||
history.perform(action, 'annotation1');
|
||||
history.perform(action, 'annotation2');
|
||||
history.perform(action, 'annotation3');
|
||||
history.checkpoint('check1');
|
||||
history.perform(action, 'annotation4');
|
||||
history.perform(action, 'annotation5');
|
||||
history.checkpoint('check2');
|
||||
history.perform(action, 'annotation6');
|
||||
history.perform(action, 'annotation7');
|
||||
history.perform(action, 'annotation8');
|
||||
|
||||
history.reset('check1');
|
||||
expect(history.undoAnnotation()).to.equal('annotation3');
|
||||
|
||||
history.reset('check2');
|
||||
expect(history.undoAnnotation()).to.equal('annotation5');
|
||||
|
||||
history.reset('check1');
|
||||
expect(history.undoAnnotation()).to.equal('annotation3');
|
||||
});
|
||||
|
||||
it('emits a change event', function () {
|
||||
history.on('change', spy);
|
||||
history.reset();
|
||||
expect(spy).to.have.been.called;
|
||||
});
|
||||
});
|
||||
|
||||
describe('#toJSON', function() {
|
||||
it('doesn\'t generate unsaveable changes', function() {
|
||||
var node_1 = iD.Node({id: 'n-1'});
|
||||
|
||||
Reference in New Issue
Block a user