Escape to cancel halfway through dragging a box?
tenznhok opened this issue · 1 comments
tenznhok commented
Is there a way I can escape/ cancel the drawing using the ESC key while I am half way dragging a rectangle/ circle?
bytesnz commented
Yes there is
const cancelDrawing = (event) => {
if (map.editTools.drawing()) {
map.editTools.stopDrawing();
// TODO Remove the component
map.editTools._drawingEditor.feature.remove();
if (event) {
event.preventDefault();
}
}
};
document.addEventListener('keyup', (event) => {
if (event.key === 'Escape') {
cancelDrawing();
}
});
Note map.editTools._drawingEditor.feature.remove();
is a little bit of a hack as it is accessing an "internal" object (starting with a _
), so it may break. Calling the associate start
edit function will return the feature being drawn, so you could store that in a variable to track what feature is being drawn.
feature = map.editTools.startPolyline();
const cancelDrawing = (event) => {
if (map.editTools.drawing()) {
map.editTools.stopDrawing();
// TODO Remove the component
feature.remove();
feature = null;
if (event) {
event.preventDefault();
}
}
};