Skip to content

Commit

Permalink
feat: improve zoom tool (#1794)
Browse files Browse the repository at this point in the history
refactor: use global mouse event listener instead of component mouse listener 

close: #1783
  • Loading branch information
hamed-musallam committed Oct 5, 2022
1 parent 758e825 commit 9fe278f
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/component/EventsTrackers/BrushTracker.tsx
Expand Up @@ -69,6 +69,28 @@ export function BrushTracker({

setMouseDownTime(event.timeStamp);
}

function moveCallback(event) {
dispatch({
type: 'MOVE',
screenX: event.screenX,
screenY: event.screenY,
clientX: event.clientX,
clientY: event.clientY,
});
}

function mouseUpCallback() {
dispatch({
type: 'UP',
});
window.removeEventListener('mousemove', moveCallback);
window.removeEventListener('mouseup', mouseUpCallback);
}

window.addEventListener('mousemove', moveCallback);
window.addEventListener('mouseup', mouseUpCallback);

return false;
},
[noPropagation],
Expand Down Expand Up @@ -130,33 +152,12 @@ export function BrushTracker({
}
}, [onBrush, state]);

const moveCallback = useCallback((event) => {
dispatch({
type: 'MOVE',
screenX: event.screenX,
screenY: event.screenY,
clientX: event.clientX,
clientY: event.clientY,
});
}, []);

const upCallback = useCallback((event) => {
dispatch({
type: 'UP',
clientX: event.clientX,
clientY: event.clientY,
});
return false;
}, []);

return (
<BrushContext.Provider value={state}>
<div
className={className}
style={style}
onMouseDown={mouseDownHandler}
onMouseUp={upCallback}
onMouseMove={moveCallback}
onClick={clickHandler}
onWheel={handleMouseWheel}
>
Expand All @@ -170,13 +171,8 @@ function reducer(state, action) {
switch (action.type) {
case 'UP':
if (state.step === 'brushing' || state.step === 'start') {
const { clientX, clientY } = action;

return {
...state,
endX: clientX - state.boundingRect.x,
endY: clientY - state.boundingRect.y,

step: state.step === 'start' ? 'initial' : 'end',
};
}
Expand All @@ -194,7 +190,6 @@ function reducer(state, action) {
} = action;
const x = clientX - boundingRect.x;
const y = clientY - boundingRect.y;

return {
...state,
shiftKey,
Expand Down

0 comments on commit 9fe278f

Please sign in to comment.