diff --git a/index.html b/index.html
index fae1fc940..5f62b0f8f 100644
--- a/index.html
+++ b/index.html
@@ -73,6 +73,7 @@
+
diff --git a/js/id/actions/add_midpoint.js b/js/id/actions/add_midpoint.js
new file mode 100644
index 000000000..c0cb97f59
--- /dev/null
+++ b/js/id/actions/add_midpoint.js
@@ -0,0 +1,11 @@
+iD.actions.AddMidpoint = function(midpoint, node) {
+ return function(graph) {
+ graph = graph.replace(node.move(midpoint.loc));
+
+ midpoint.ways.forEach(function(way) {
+ graph = graph.replace(graph.entity(way.id).addNode(node.id, way.index));
+ });
+
+ return graph;
+ };
+};
diff --git a/js/id/behavior/drag_midpoint.js b/js/id/behavior/drag_midpoint.js
index e0b0c7a1a..72691a512 100644
--- a/js/id/behavior/drag_midpoint.js
+++ b/js/id/behavior/drag_midpoint.js
@@ -8,14 +8,9 @@ iD.behavior.DragMidpoint = function(mode) {
return projection(d.loc);
})
.on('start', function(d) {
- var node = iD.Node({loc: d.loc});
+ var node = iD.Node();
- var args = [iD.actions.AddNode(node)];
- for (var i = 0; i < d.ways.length; i++) {
- args.push(iD.actions.AddWayNode(d.ways[i].id, node.id, d.ways[i].index));
- }
-
- history.perform.apply(history, args);
+ history.perform(iD.actions.AddMidpoint(d, node));
var vertex = d3.selectAll('.vertex')
.filter(function(data) { return data.id === node.id; });
diff --git a/test/index.html b/test/index.html
index 97cd63b9b..731800f62 100644
--- a/test/index.html
+++ b/test/index.html
@@ -68,6 +68,7 @@
+
@@ -137,6 +138,7 @@
+
diff --git a/test/index_packaged.html b/test/index_packaged.html
index 25093d5b3..aba99ba04 100644
--- a/test/index_packaged.html
+++ b/test/index_packaged.html
@@ -32,6 +32,7 @@
+
diff --git a/test/spec/actions/add_midpoint.js b/test/spec/actions/add_midpoint.js
new file mode 100644
index 000000000..1f749217e
--- /dev/null
+++ b/test/spec/actions/add_midpoint.js
@@ -0,0 +1,22 @@
+describe("iD.actions.AddMidpoint", function () {
+ it("adds the node at the midpoint location", function () {
+ var node = iD.Node(),
+ midpoint = {loc: [1, 2], ways: []},
+ graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph());
+
+ expect(graph.entity(node.id).loc).to.eql([1, 2]);
+ });
+
+ it("adds the node to all ways at the respective indexes", function () {
+ var node = iD.Node(),
+ a = iD.Node(),
+ b = iD.Node(),
+ w1 = iD.Way(),
+ w2 = iD.Way({nodes: [a.id, b.id]}),
+ midpoint = {loc: [1, 2], ways: [{id: w1.id, index: 0}, {id: w2.id, index: 1}]},
+ graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b, w1, w2]));
+
+ expect(graph.entity(w1.id).nodes).to.eql([node.id]);
+ expect(graph.entity(w2.id).nodes).to.eql([a.id, node.id, b.id]);
+ });
+});