Better handle undo/interruption during behaviorDrawWay

This commit is contained in:
Bryan Housel
2019-02-14 17:24:09 -05:00
parent 1821c3e955
commit 078b19c828

View File

@@ -1,35 +1,34 @@
import { t } from '../util/locale';
import _isEmpty from 'lodash-es/isEmpty';
import {
event as d3_event,
select as d3_select
} from 'd3-selection';
import {
actionAddMidpoint,
actionMoveNode,
actionNoop
} from '../actions';
import { t } from '../util/locale';
import { actionAddMidpoint, actionMoveNode, actionNoop } from '../actions';
import { behaviorDraw } from './draw';
import { geoChooseEdge, geoHasSelfIntersections } from '../geo';
import { modeBrowse, modeSelect } from '../modes';
import { osmNode } from '../osm';
import { utilKeybinding } from '../util';
import _isEmpty from 'lodash-es/isEmpty';
export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
var origWay = context.entity(wayId);
export function behaviorDrawWay(context, wayID, index, mode, startGraph) {
var origWay = context.entity(wayID);
var annotation = t((origWay.isDegenerate() ?
'operations.start.annotation.' :
'operations.continue.annotation.') + context.geometry(wayId)
'operations.continue.annotation.') + context.geometry(wayID)
);
var behavior = behaviorDraw(context);
var _tempEdits = 0;
var end = osmNode({ loc: context.map().mouseCoordinates() });
if (context.graph() === startGraph) {
context.history().checkpoint('drawWay-initial');
}
// Push an annotated state for undo to return back to.
// We must make sure to remove this edit later.
context.perform(actionNoop(), annotation);
@@ -66,10 +65,12 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
}
}
function allowsVertex(d) {
return _isEmpty(d.tags) || context.presets().allowsVertex(d, context.graph());
}
// related code
// - `mode/drag_node.js` `doMode()`
// - `behavior/draw.js` `click()`
@@ -121,7 +122,7 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
for (var i = 0; i < parents.length; i++) {
var parent = parents[i];
var nodes = graph.childNodes(parent);
if (origWay.isClosed()) { // Check if Area
@@ -148,14 +149,16 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
function undone() {
// Undo popped the history back to the initial annotated no-op edit.
// Remove initial no-op edit and whatever edit happened immediately before it.
context.pop(2);
_tempEdits = 0;
_tempEdits = 0; // We will deal with the temp edits here
context.pop(1); // Remove initial no-op edit
if (context.hasEntity(wayId)) {
context.enter(mode);
if (context.graph() === startGraph) { // We've undone back to the beginning
context.history().reset('drawWay-initial');
context.enter(modeSelect(context, [wayID]));
} else {
context.enter(modeBrowse(context));
// Remove whatever segment was drawn previously and continue drawing
context.pop(1);
context.enter(mode);
}
}
@@ -198,10 +201,7 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
// This can happen if the user changes modes,
// clicks geolocate button, a hashchange event occurs, etc.
if (_tempEdits) {
context.pop(_tempEdits);
while (context.graph() !== startGraph) {
context.pop();
}
context.history().reset('drawWay-initial');
}
context.map()
@@ -313,7 +313,7 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
context.pop(_tempEdits);
_tempEdits = 0;
var way = context.hasEntity(wayId);
var way = context.hasEntity(wayID);
if (!way || way.isDegenerate()) {
drawWay.cancel();
return;
@@ -323,7 +323,7 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
context.map().dblclickEnable(true);
}, 1000);
var isNewFeature = !mode.isContinuing;
context.enter(modeSelect(context, [wayId]).newFeature(isNewFeature));
context.enter(modeSelect(context, [wayID]).newFeature(isNewFeature));
};