In copy/paste, use the graph that entities originally came from

(fixes #2557)
This commit is contained in:
Bryan Housel
2015-03-18 10:08:36 -04:00
parent def08cab23
commit c69fbf3e3d
5 changed files with 54 additions and 18 deletions
+4 -2
View File
@@ -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]);
+1 -1
View File
@@ -61,7 +61,7 @@ iD.behavior.Copy = function(context) {
}
}
context.copiedIDs(canCopy);
context.copyIDs(canCopy);
}
function copy() {
+8 -6
View File
@@ -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,
+6 -4
View File
@@ -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;
};
+35 -5
View File
@@ -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);
});
});