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

Added functionality for zoom in\out by sliding the wheel #52

Merged
Show file tree
Hide file tree
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
25 changes: 23 additions & 2 deletions src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import useFrameSetState from './hooks/useFrameSetState';
import getFixScaleEleTransPosition from './getFixScaleEleTransPosition';
import { context } from './PreviewGroup';

const { useState } = React;
const { useState, useEffect } = React;

interface PreviewProps extends Omit<IDialogPropTypes, 'onClose'> {
onClose?: (e: React.SyntheticEvent<Element>) => void;
Expand Down Expand Up @@ -55,6 +55,7 @@ const Preview: React.FC<PreviewProps> = props => {
const currentPreviewIndex = previewUrlsKeys.indexOf(current);
const combinationSrc = isPreviewGroup ? previewUrls.get(current) : src;
const showLeftOrRightSwitches = isPreviewGroup && previewGroupCount > 1;
const [lastWheelZoomDirection, setLastWheelZoomDirection] = React.useState({ wheelDirection: 0 });

const onAfterClose = () => {
setScale(1);
Expand Down Expand Up @@ -176,12 +177,31 @@ const Preview: React.FC<PreviewProps> = props => {
}
};

React.useEffect(() => {
const onWheelMove: React.WheelEventHandler<HTMLBodyElement> = event => {
shaodahong marked this conversation as resolved.
Show resolved Hide resolved
if (!visible) return;
event.preventDefault();
const wheelDirection = event.deltaY;
setLastWheelZoomDirection({ wheelDirection });
};

useEffect(() => {
const { wheelDirection } = lastWheelZoomDirection;
if (wheelDirection > 0) {
onZoomOut();
} else if (wheelDirection < 0) {
onZoomIn();
}
}, [lastWheelZoomDirection]);

useEffect(() => {
let onTopMouseUpListener;
let onTopMouseMoveListener;

const onMouseUpListener = addEventListener(window, 'mouseup', onMouseUp, false);
const onMouseMoveListener = addEventListener(window, 'mousemove', onMouseMove, false);
const onScrollWheelListener = addEventListener(window, 'wheel', onWheelMove, {
passive: false,
});

try {
// Resolve if in iframe lost event
Expand All @@ -198,6 +218,7 @@ const Preview: React.FC<PreviewProps> = props => {
return () => {
onMouseUpListener.remove();
onMouseMoveListener.remove();
onScrollWheelListener.remove();

/* istanbul ignore next */
if (onTopMouseUpListener) onTopMouseUpListener.remove();
Expand Down
22 changes: 22 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ describe('Preview', () => {
expect(wrapper.find('.rc-image-preview-img').prop('style')).toMatchObject({
transform: 'scale3d(1, 1, 1) rotate(0deg)',
});

act(() => {
const wheelEvent = new Event('wheel');
wheelEvent.deltaY = -50;
global.dispatchEvent(wheelEvent);
jest.runAllTimers();
wrapper.update();
});
expect(wrapper.find('.rc-image-preview-img').prop('style')).toMatchObject({
transform: 'scale3d(2, 2, 1) rotate(0deg)',
});

act(() => {
const wheelEvent = new Event('wheel');
wheelEvent.deltaY = 50;
global.dispatchEvent(wheelEvent);
jest.runAllTimers();
wrapper.update();
});
expect(wrapper.find('.rc-image-preview-img').prop('style')).toMatchObject({
transform: 'scale3d(1, 1, 1) rotate(0deg)',
});
});

it('Mouse Event', () => {
Expand Down