Skip to content

Commit

Permalink
fix: drag moving offset value should be exclude window
Browse files Browse the repository at this point in the history
  • Loading branch information
shaodahong committed Feb 2, 2021
1 parent 2b3d49b commit 9ceb215
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react';
import Dialog, { DialogProps as IDialogPropTypes } from 'rc-dialog';
import classnames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import { getOffset } from 'rc-util/lib/Dom/css';
import { warning } from 'rc-util/lib/warning';
import useFrameSetState from './hooks/useFrameSetState';
import getFixScaleEleTransPosition from './getFixScaleEleTransPosition';
Expand Down Expand Up @@ -31,16 +30,7 @@ const initialPosition = {
};

const Preview: React.FC<PreviewProps> = props => {
const {
prefixCls,
src,
alt,
onClose,
afterClose,
visible,
icons = {},
...restProps
} = props;
const { prefixCls, src, alt, onClose, afterClose, visible, icons = {}, ...restProps } = props;
const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right } = icons;
const [scale, setScale] = useState(1);
const [rotate, setRotate] = useState(0);
Expand Down Expand Up @@ -75,17 +65,17 @@ const Preview: React.FC<PreviewProps> = props => {
setPosition(initialPosition);
};

const onZoomIn = () => {
const onZoomIn = useCallback(() => {
setScale(value => value + 1);
setPosition(initialPosition);
};
});

const onZoomOut = () => {
const onZoomOut = useCallback(() => {
if (scale > 1) {
setScale(value => value - 1);
}
setPosition(initialPosition);
};
});

const onRotateRight = () => {
setRotate(value => value + 90);
Expand Down Expand Up @@ -152,7 +142,7 @@ const Preview: React.FC<PreviewProps> = props => {
const width = imgRef.current.offsetWidth * scale;
const height = imgRef.current.offsetHeight * scale;
// eslint-disable-next-line @typescript-eslint/no-shadow
const { left, top } = getOffset(imgRef.current);
const { left, top } = imgRef.current.getBoundingClientRect();
const isRotate = rotate % 180 !== 0;

setMoving(false);
Expand Down Expand Up @@ -206,7 +196,7 @@ const Preview: React.FC<PreviewProps> = props => {
} else if (wheelDirection < 0) {
onZoomIn();
}
}, [lastWheelZoomDirection]);
}, [lastWheelZoomDirection, onZoomIn, onZoomOut]);

useEffect(() => {
let onTopMouseUpListener;
Expand Down Expand Up @@ -240,7 +230,7 @@ const Preview: React.FC<PreviewProps> = props => {
/* istanbul ignore next */
if (onTopMouseMoveListener) onTopMouseMoveListener.remove();
};
}, [visible, isMoving]);
}, [visible, isMoving, onMouseUp, onMouseMove, onWheelMove]);

return (
<Dialog
Expand Down

0 comments on commit 9ceb215

Please sign in to comment.