Skip to content

Allow canceling drag-to-zoom on Escape key #672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ export function mouseMove(chart, event) {
}
}

function keyDown(chart, event) {
const state = getState(chart);
if (!state.dragStart || event.key !== 'Escape') {
return;
}

removeHandler(chart, 'keydown');
state.dragging = false;
state.dragStart = state.dragEnd = null;
chart.update('none');
}

function zoomStart(chart, event, zoomOptions) {
const {onZoomStart, onZoomRejected} = zoomOptions;
if (onZoomStart) {
Expand Down Expand Up @@ -62,6 +74,7 @@ export function mouseDown(chart, event) {
state.dragStart = event;

addHandler(chart, chart.canvas, 'mousemove', mouseMove);
addHandler(chart, window.document, 'keydown', keyDown);
}

export function computeDragRect(chart, mode, beginPoint, endPoint) {
Expand Down Expand Up @@ -197,6 +210,7 @@ export function addListeners(chart, options) {
removeHandler(chart, 'mousedown');
removeHandler(chart, 'mousemove');
removeHandler(chart, 'mouseup');
removeHandler(chart, 'keydown');
}
}

Expand All @@ -206,4 +220,5 @@ export function removeListeners(chart) {
removeHandler(chart, 'mouseup');
removeHandler(chart, 'wheel');
removeHandler(chart, 'click');
removeHandler(chart, 'keydown');
}