Skip to content

Commit

Permalink
Avoid dispatching extra change events, don't mutate childNodes
Browse files Browse the repository at this point in the history
(closes #5996, related #5941)
  • Loading branch information
bhousel committed Mar 6, 2019
1 parent 6da2ba7 commit e216d5b
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions modules/behavior/draw_way.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _clone from 'lodash-es/clone';

import {
event as d3_event,
select as d3_select
Expand Down Expand Up @@ -30,13 +32,15 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin

// Push an annotated state for undo to return back to.
// We must make sure to remove this edit later.
context.pauseChangeDispatch();
context.perform(actionNoop(), annotation);
_tempEdits++;

// Add the drawing node to the graph.
// We must make sure to remove this edit later.
context.perform(_actionAddDrawNode());
_tempEdits++;
context.resumeChangeDispatch();


function keydown() {
Expand Down Expand Up @@ -122,7 +126,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
for (var i = 0; i < parents.length; i++) {
var parent = parents[i];

var nodes = graph.childNodes(parent);
var nodes = _clone(graph.childNodes(parent));

if (origWay.isClosed()) { // Check if Area
if (finishDraw) {
Expand All @@ -149,17 +153,20 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin


function undone() {
context.pauseChangeDispatch();
// Undo popped the history back to the initial annotated no-op edit.
_tempEdits = 0; // We will deal with the temp edits here
context.pop(1); // Remove initial no-op edit

if (context.graph() === baselineGraph) { // We've undone back to the beginning
// baselineGraph may be behind startGraph if this way was added rather than continued
resetContextGraphToStartGraph();
resetToStartGraph();
context.resumeChangeDispatch();
context.enter(modeSelect(context, [wayID]));
} else {
// Remove whatever segment was drawn previously and continue drawing
context.pop(1);
context.resumeChangeDispatch();
context.enter(mode);
}
}
Expand All @@ -171,6 +178,13 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
}


function resetToStartGraph() {
while (context.graph() !== startGraph) {
context.pop();
}
}


var drawWay = function(surface) {
behavior
.on('move', move)
Expand All @@ -197,19 +211,16 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
.on('undone.draw', undone);
};

function resetContextGraphToStartGraph() {
while (context.graph() !== startGraph) {
context.pop();
}
}

drawWay.off = function(surface) {
// Drawing was interrupted unexpectedly.
// This can happen if the user changes modes,
// clicks geolocate button, a hashchange event occurs, etc.
if (_tempEdits) {
context.pauseChangeDispatch();
context.pop(_tempEdits);
resetContextGraphToStartGraph();
resetToStartGraph();
context.resumeChangeDispatch();
}

context.map()
Expand Down Expand Up @@ -257,6 +268,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
return; // can't click here
}

context.pauseChangeDispatch();
context.pop(_tempEdits);
_tempEdits = 0;

Expand All @@ -265,6 +277,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
annotation
);

context.resumeChangeDispatch();
checkGeometry(false); // finishDraw = false
context.enter(mode);
};
Expand All @@ -276,6 +289,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
return; // can't click here
}

context.pauseChangeDispatch();
context.pop(_tempEdits);
_tempEdits = 0;

Expand All @@ -285,6 +299,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
annotation
);

context.resumeChangeDispatch();
checkGeometry(false); // finishDraw = false
context.enter(mode);
};
Expand All @@ -296,6 +311,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
return; // can't click here
}

context.pauseChangeDispatch();
context.pop(_tempEdits);
_tempEdits = 0;

Expand All @@ -304,6 +320,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
annotation
);

context.resumeChangeDispatch();
checkGeometry(false); // finishDraw = false
context.enter(mode);
};
Expand All @@ -318,6 +335,7 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
return; // can't click here
}

context.pauseChangeDispatch();
context.pop(_tempEdits);
_tempEdits = 0;

Expand All @@ -327,6 +345,8 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin
return;
}

context.resumeChangeDispatch();

window.setTimeout(function() {
context.map().dblclickEnable(true);
}, 1000);
Expand All @@ -340,10 +360,12 @@ export function behaviorDrawWay(context, wayID, index, mode, startGraph, baselin

// Cancel the draw operation, delete everything, and return to browse mode.
drawWay.cancel = function() {
context.pauseChangeDispatch();
context.pop(_tempEdits);
_tempEdits = 0;

resetContextGraphToStartGraph();
resetToStartGraph();
context.resumeChangeDispatch();

window.setTimeout(function() {
context.map().dblclickEnable(true);
Expand Down

0 comments on commit e216d5b

Please sign in to comment.