diff --git a/js/id/actions/copy_entity.js b/js/id/actions/copy_entity.js index af4a9138a..33a1cd000 100644 --- a/js/id/actions/copy_entity.js +++ b/js/id/actions/copy_entity.js @@ -1,8 +1,10 @@ -iD.actions.CopyEntity = function(entity, deep) { +iD.actions.CopyEntity = function(id, fromGraph, deep) { var newEntities = []; var action = function(graph) { - newEntities = entity.copy(deep, graph); + var entity = fromGraph.entity(id); + + newEntities = entity.copy(deep, fromGraph); for (var i = 0; i < newEntities.length; i++) { graph = graph.replace(newEntities[i]); diff --git a/js/id/behavior/copy.js b/js/id/behavior/copy.js index cc4647708..f2483ebda 100644 --- a/js/id/behavior/copy.js +++ b/js/id/behavior/copy.js @@ -61,7 +61,7 @@ iD.behavior.Copy = function(context) { } } - context.copiedIDs(canCopy); + context.copyIDs(canCopy); } function copy() { diff --git a/js/id/behavior/paste.js b/js/id/behavior/paste.js index 92bf92d31..7956e26b4 100644 --- a/js/id/behavior/paste.js +++ b/js/id/behavior/paste.js @@ -26,18 +26,20 @@ iD.behavior.Paste = function(context) { if (!iD.geo.pointInPolygon(mouse, viewport)) return; - var graph = context.graph(), - extent = iD.geo.Extent(), - oldIDs = context.copiedIDs(), + var extent = iD.geo.Extent(), + oldIDs = context.copyIDs(), + oldGraph = context.copyGraph(), newIDs = [], i, j; + if (!oldIDs.length) return; + for (i = 0; i < oldIDs.length; i++) { - var oldEntity = graph.entity(oldIDs[i]), - action = iD.actions.CopyEntity(oldEntity, true), + var oldEntity = oldGraph.entity(oldIDs[i]), + action = iD.actions.CopyEntity(oldEntity.id, oldGraph, true), newEntities; - extent._extend(oldEntity.extent(graph)); + extent._extend(oldEntity.extent(oldGraph)); context.perform(action); // First element in `newEntities` contains the copied Entity, diff --git a/js/id/id.js b/js/id/id.js index acd000a9c..58972fc2a 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -221,10 +221,12 @@ window.iD = function () { }; /* Copy/Paste */ - var copiedIDs = []; - context.copiedIDs = function(_) { - if (!arguments.length) return copiedIDs; - copiedIDs = _; + var copyIDs = [], copyGraph; + context.copyGraph = function() { return copyGraph; }; + context.copyIDs = function(_) { + if (!arguments.length) return copyIDs; + copyIDs = _; + copyGraph = history.graph(); return context; }; diff --git a/test/spec/actions/copy_entity.js b/test/spec/actions/copy_entity.js index 15f0497a3..3b85f3ba9 100644 --- a/test/spec/actions/copy_entity.js +++ b/test/spec/actions/copy_entity.js @@ -2,7 +2,7 @@ describe("iD.actions.CopyEntity", function () { it("copies a Node and adds it to the graph", function () { var a = iD.Node({id: 'a'}), base = iD.Graph([a]), - head = iD.actions.CopyEntity(a)(base), + head = iD.actions.CopyEntity('a', base, false)(base), diff = iD.Difference(base, head), created = diff.created(); @@ -16,7 +16,7 @@ describe("iD.actions.CopyEntity", function () { b = iD.Node({id: 'b'}), w = iD.Way({id: 'w', nodes: ['a', 'b']}), base = iD.Graph([a, b, w]), - head = iD.actions.CopyEntity(w)(base), + head = iD.actions.CopyEntity('w', base, false)(base), diff = iD.Difference(base, head), created = diff.created(); @@ -30,7 +30,7 @@ describe("iD.actions.CopyEntity", function () { b = iD.Node({id: 'b'}), w = iD.Way({id: 'w', nodes: ['a', 'b']}), base = iD.Graph([a, b, w]), - head = iD.actions.CopyEntity(w, true)(base), + head = iD.actions.CopyEntity('w', base, true)(base), diff = iD.Difference(base, head), created = diff.created(); @@ -47,7 +47,7 @@ describe("iD.actions.CopyEntity", function () { w = iD.Way({id: 'w', nodes: ['a', 'b']}), r = iD.Relation({id: 'r', members: [{id: 'w'}]}), base = iD.Graph([a, b, w, r]), - head = iD.actions.CopyEntity(r)(base), + head = iD.actions.CopyEntity('r', base, false)(base), diff = iD.Difference(base, head), created = diff.created(); @@ -62,7 +62,7 @@ describe("iD.actions.CopyEntity", function () { // w = iD.Way({id: 'w', nodes: ['a', 'b']}), // r = iD.Relation({id: 'r', members: [{id: 'w'}]}), // base = iD.Graph([a, b, w, r]), - // head = iD.actions.CopyEntity(r, true)(base), + // head = iD.actions.CopyEntity('r', base, true)(base), // diff = iD.Difference(base, head), // created = diff.created(); @@ -73,4 +73,34 @@ describe("iD.actions.CopyEntity", function () { // expect(created[2]).to.be.an.instanceof(iD.Node); // expect(created[3]).to.be.an.instanceof(iD.Node); // }); + + it("shallow copies from one graph to another", function () { + var a = iD.Node({id: 'a'}), + b = iD.Node({id: 'b'}), + w = iD.Way({id: 'w', nodes: ['a', 'b']}), + source = iD.Graph([a, b, w]), + base = iD.Graph(), + head = iD.actions.CopyEntity('w', source, false)(base), + diff = iD.Difference(base, head), + created = diff.created(); + + expect(created).to.have.length(1); + expect(created[0]).to.be.an.instanceof(iD.Way); + }); + + it("deep copies from one graph to another", function () { + var a = iD.Node({id: 'a'}), + b = iD.Node({id: 'b'}), + w = iD.Way({id: 'w', nodes: ['a', 'b']}), + source = iD.Graph([a, b, w]), + base = iD.Graph(), + head = iD.actions.CopyEntity('w', source, true)(base), + diff = iD.Difference(base, head), + created = diff.created(); + + expect(created).to.have.length(3); + expect(created[0]).to.be.an.instanceof(iD.Way); + expect(created[1]).to.be.an.instanceof(iD.Node); + expect(created[2]).to.be.an.instanceof(iD.Node); + }); });