diff --git a/demo/lib/App.tsx b/demo/lib/App.tsx index c787f118b..fa618c704 100644 --- a/demo/lib/App.tsx +++ b/demo/lib/App.tsx @@ -219,7 +219,7 @@ export default class App extends React.Component { /> - this.setState({ zoom })} /> + this.setState({ zoom })} /> this.setState({ search: { query } })} /> @@ -302,16 +302,16 @@ const ViewerTypeInput = ({ setType }: { setType: (viewType: string) => void }) = ); -const LinearZoomInput = ({ setZoom }: { setZoom: (zoom: number) => void }) => ( +const LinearZoomInput = ({ zoom, setZoom }: { zoom: number; setZoom: (zoom: number) => void }) => (
Zoom { setZoom(parseInt(e.target.value)); }} diff --git a/src/EventHandler.tsx b/src/EventHandler.tsx index aff47a38f..a4533d9fb 100644 --- a/src/EventHandler.tsx +++ b/src/EventHandler.tsx @@ -12,7 +12,9 @@ export interface EventsHandlerProps { selectAllEvent: (e: React.KeyboardEvent) => boolean; selection: Selection; seq: string; + setLinearZoom: (zoom: number) => void; setSelection: (selection: Selection) => void; + zoom: number; } /** @@ -26,6 +28,20 @@ export class EventHandler extends React.PureComponent { clickedOnce: EventTarget | null = null; clickedTwice: EventTarget | null = null; + containerRef: React.RefObject = React.createRef(); + + componentDidMount = () => { + /* + Because React uses passive event handlers by default with wheel event which prevents using preventDefault(), + so using refs is the solution for this issue to make event handler non-passive by adding event handler manually. + */ + this.containerRef.current?.addEventListener("wheel", this.handleMouseWheel, { passive: false }); + }; + + componentWillUnmount(): void { + this.containerRef.current?.removeEventListener("wheel", this.handleMouseWheel); + } + /** * action handler for a keyboard keypresses. */ @@ -247,8 +263,33 @@ export class EventHandler extends React.PureComponent { } }; + updateLinearZoom(zoomLevel) { + const { setLinearZoom } = this.props; + setLinearZoom(Math.min(Math.max(zoomLevel, 11), 100)); + } + + zoomIn() { + this.updateLinearZoom(this.props.zoom + 20); + } + + zoomOut() { + this.updateLinearZoom(this.props.zoom - 20); + } + + handleMouseWheel = event => { + event.preventDefault(); + if (event.ctrlKey) { + if (event.wheelDelta > 0) { + this.zoomIn(); + } else if (event.wheelDelta < 0) { + this.zoomOut(); + } + } + }; + render = () => (