Skip to content
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

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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/handlers.js
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');
}