All the complicated code in drawWay can be removed

Now that we can target specific segments along a line, we don't need
temporary drawing objects or extra code to check for self intersections
This commit is contained in:
Bryan Housel
2017-12-21 23:48:52 -05:00
parent 5d9b051f84
commit 2be62fffe5
+25 -123
View File
@@ -1,64 +1,36 @@
import _clone from 'lodash-es/clone';
import { t } from '../util/locale';
import {
actionAddEntity,
actionAddMidpoint,
actionMoveNode,
actionNoop
} from '../actions';
import { behaviorDraw } from './draw';
import { geoChooseEdge, geoEdgeEqual } from '../geo';
import { geoChooseEdge } from '../geo';
import { modeBrowse, modeSelect } from '../modes';
import { osmNode, osmWay } from '../osm';
import { osmNode } from '../osm';
export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
var origWay = context.entity(wayId);
var isArea = context.geometry(wayId) === 'area';
var annotation = t((origWay.isDegenerate() ?
'operations.start.annotation.' :
'operations.continue.annotation.') + context.geometry(wayId)
);
var behavior = behaviorDraw(context);
var _tempEdits = 0;
var _startIndex;
var start;
var end;
var segment;
// initialize the temporary drawing entities
if (!isArea) {
_startIndex = (typeof index === 'undefined' ? origWay.nodes.length - 1 : 0);
start = osmNode({
id: 'nStart',
loc: context.entity(origWay.nodes[_startIndex]).loc
});
end = osmNode({
id: 'nEnd',
loc: context.map().mouseCoordinates()
});
segment = osmWay({
id: 'wTemp',
nodes: typeof index === 'undefined' ? [start.id, end.id] : [end.id, start.id],
tags: _clone(origWay.tags)
});
} else {
end = osmNode({ loc: context.map().mouseCoordinates() });
}
var end = osmNode({ loc: context.map().mouseCoordinates() });
// Push an annotated state for undo to return back to.
// We must make sure to remove this edit later.
context.perform(actionNoop(), annotation);
_tempEdits++;
// Add the temporary drawing entities to the graph.
// Add the drawing node to the graph.
// We must make sure to remove this edit later.
context.perform(AddDrawEntities());
context.perform(_actionAddDrawNode());
_tempEdits++;
@@ -155,75 +127,35 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
};
function AddDrawEntities() {
function _actionAddDrawNode() {
return function(graph) {
if (isArea) {
// For area drawing, there is no need for a temporary node.
// `end` gets inserted into the way as the penultimate node.
return graph
.replace(end)
.replace(origWay.addNode(end.id));
} else {
// For line drawing, add a temporary start, end, and segment to the graph.
// This allows us to class the new segment as `active`, but still
// connect it back to parts of the way that have already been drawn.
return graph
.replace(start)
.replace(end)
.replace(segment);
}
return graph
.replace(end)
.replace(origWay.addNode(end.id));
};
}
function ReplaceDrawEntities(newNode) {
function _actionReplaceDrawNode(newNode) {
return function(graph) {
if (isArea) {
// For area drawing, we didn't create a temporary node.
// `newNode` gets inserted into the _original_ way as the penultimate node.
return graph
.replace(origWay.addNode(newNode.id))
.remove(end);
} else {
// For line drawing, add the `newNode` to the way at specified index,
// and remove the temporary start, end, and segment.
return graph
.replace(origWay.addNode(newNode.id, index))
.remove(end)
.remove(segment)
.remove(start);
}
return graph
.replace(origWay.addNode(newNode.id))
.remove(end);
};
}
// Accept the current position of the temporary node and continue drawing.
// Accept the current position of the drawing node and continue drawing.
drawWay.add = function(loc, datum) {
// shouldn't happen now?
// prevent duplicate nodes
// var last = context.hasEntity(origWay.nodes[origWay.nodes.length - (isArea ? 2 : 1)]);
// if (last && last.loc[0] === loc[0] && last.loc[1] === loc[1]) return;
if (datum && datum.id && /-nope/.test(datum.id)) { // can't click here
return;
}
if (datum && datum.id && /-nope/.test(datum.id)) return; // can't click here
context.pop(_tempEdits);
_tempEdits = 0;
if (isArea) {
context.perform(
AddDrawEntities(),
annotation
);
} else {
var newNode = osmNode({loc: loc});
context.perform(
actionAddEntity(newNode),
ReplaceDrawEntities(newNode),
annotation
);
}
context.perform(
_actionAddDrawNode(),
annotation
);
context.enter(mode);
};
@@ -234,29 +166,11 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
context.pop(_tempEdits);
_tempEdits = 0;
if (isArea) {
context.perform(
AddDrawEntities(),
actionAddMidpoint({ loc: loc, edge: edge}, end),
annotation
);
} else {
// shouldn't happen now?
// var previousEdge = _startIndex ?
// [origWay.nodes[_startIndex], origWay.nodes[_startIndex - 1]] :
// [origWay.nodes[0], origWay.nodes[1]];
// // Avoid creating duplicate segments
// if (geoEdgeEqual(edge, previousEdge))
// return;
var newNode = osmNode({ loc: loc });
context.perform(
actionAddMidpoint({ loc: loc, edge: edge}, newNode),
ReplaceDrawEntities(newNode),
annotation
);
}
context.perform(
_actionAddDrawNode(),
actionAddMidpoint({ loc: loc, edge: edge }, end),
annotation
);
context.enter(mode);
};
@@ -264,23 +178,11 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
// Connect the way to an existing node and continue drawing.
drawWay.addNode = function(node) {
// Avoid creating duplicate segments
// shouldn't happen now?
// if (origWay.areAdjacent(node.id, origWay.nodes[origWay.nodes.length - 1])) return;
// Clicks should not occur on the drawing node, however a space keypress can
// sometimes grab that node's datum (before it gets classed as `active`?) #4016
// shouldn't happen now?
// if (node.id === end.id) {
// drawWay.add(node.loc);
// return;
// }
context.pop(_tempEdits);
_tempEdits = 0;
context.perform(
ReplaceDrawEntities(node),
_actionReplaceDrawNode(node),
annotation
);