diff --git a/js/id/core/history.js b/js/id/core/history.js index 7325dbbb0..f71365d37 100644 --- a/js/id/core/history.js +++ b/js/id/core/history.js @@ -174,7 +174,9 @@ iD.History = function(context) { toJSON: function() { if (stack.length <= 1) return; - var allEntities = {}; + var allEntities = {}, + baseEntities = {}, + base = stack[0]; var s = stack.map(function(i) { var modified = [], deleted = []; @@ -187,6 +189,10 @@ iD.History = function(context) { } else { deleted.push(id); } + // make sure that the originals of changed or deleted entities get merged + // into the base of the stack after restoring the data from JSON. + if (id in base.graph.entities && !base.graph.entities.hasOwnProperty(id)) + baseEntities[id] = base.graph.entities[id]; }); var x = {}; @@ -199,15 +205,6 @@ iD.History = function(context) { return x; }); - // make sure that the originals of changed entities get merged into - // the base of the stack after restoring the data from JSON. - var base = stack[0], - baseEntities = {}; - _.forEach(allEntities, function(entity) { - if (entity.id in base.graph.entities && !base.graph.entities.hasOwnProperty(entity.id)) - baseEntities[entity.id] = base.graph.entities[entity.id]; - }); - return JSON.stringify({ version: 3, entities: _.values(allEntities), @@ -235,7 +232,9 @@ iD.History = function(context) { // this merges originals for changed entities into the base of // the stack even if the current stack doesn't have them (for // example when iD has been restarted in a different region) - stack[0].graph.rebase(h.baseEntities, _.pluck(stack, 'graph')); + var baseEntities = h.baseEntities.map(iD.Entity); + stack[0].graph.rebase(baseEntities, _.pluck(stack, 'graph')); + tree.rebase(baseEntities); } stack = h.stack.map(function(d) { diff --git a/test/spec/core/history.js b/test/spec/core/history.js index bbffe247e..064e286ec 100644 --- a/test/spec/core/history.js +++ b/test/spec/core/history.js @@ -230,12 +230,18 @@ describe("iD.History", function () { describe("#toJSON", function() { it("generates v3 JSON", function() { - var node = iD.Node({id: 'n-1'}); - history.merge([iD.Node({id: 'n1'})]); - history.perform(iD.actions.AddEntity(node)); + var node_1 = iD.Node({id: 'n-1'}), + node1 = iD.Node({id: 'n1'}), + node2 = iD.Node({id: 'n2'}), + node3 = iD.Node({id: 'n3'}); + history.merge([node1, node2, node3]); + history.perform(iD.actions.AddEntity(node_1)); // addition + history.perform(iD.actions.ChangeTags('n2', {k: 'v'})); // modification + history.perform(iD.actions.DeleteNode('n3')); // deletion var json = JSON.parse(history.toJSON()); expect(json.version).to.eql(3); - expect(json.entities).to.eql([node]); + expect(json.entities).to.eql([node_1, node2.update({tags: {k: 'v'}})]); + expect(json.baseEntities).to.eql([node2, node3]); }); }); @@ -401,7 +407,7 @@ describe("iD.History", function () { var json = { "version": 3, "entities": [], - "baseEntities": [], + "baseEntities": [{"loc": [1, 2], "id": "n1"}], "stack": [ {}, {"deleted": ["n1"], "imageryUsed": ["Bing"], "annotation": "Deleted a point."} @@ -410,7 +416,6 @@ describe("iD.History", function () { "index": 1 }; history.fromJSON(JSON.stringify(json)); - history.merge([iD.Node({id: 'n1'})]); expect(history.graph().hasEntity('n1')).to.be.undefined; expect(history.undoAnnotation()).to.eql("Deleted a point."); expect(history.imageryUsed()).to.eql(["Bing"]);