Skip to content

Commit

Permalink
Allow canceling drag-to-zoom on Escape key (#672)
Browse files Browse the repository at this point in the history
This makes the behavior of drag-to-zoom closer to the browser's standard drag-and-drop behavior.
  • Loading branch information
joshkel authored Nov 23, 2022
1 parent fc69374 commit ddc717d
Showing 1 changed file with 15 additions and 0 deletions.
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 @@ -66,6 +78,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 @@ -201,6 +214,7 @@ export function addListeners(chart, options) {
removeHandler(chart, 'mousedown');
removeHandler(chart, 'mousemove');
removeHandler(chart, 'mouseup');
removeHandler(chart, 'keydown');
}
}

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

0 comments on commit ddc717d

Please sign in to comment.