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

feat: support preview - dragRebound control and add refresh operation… #217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions docs/examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default function Base() {
style={{
marginRight: 24,
}}
preview={{
dragRebound: false,
mask: 'close drag rebound'
}}
/>
<Image
src="https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*NZuwQp_vcIQAAAAAAAAAAABkARQnAQ"
Expand Down
3 changes: 3 additions & 0 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ImagePreviewType
maskClassName?: string;
icons?: PreviewProps['icons'];
scaleStep?: number;
dragRebound?: boolean;
}

let uuid = 0;
Expand Down Expand Up @@ -101,6 +102,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = ({
maskClassName,
icons,
scaleStep,
dragRebound,
...dialogProps
}: ImagePreviewType = typeof preview === 'object' ? preview : {};
const src = previewSrc ?? imgSrc;
Expand Down Expand Up @@ -282,6 +284,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = ({
mousePosition={mousePosition}
src={mergedSrc}
alt={alt}
dragRebound={dragRebound}
getContainer={getPreviewContainer}
icons={icons}
scaleStep={scaleStep}
Expand Down
9 changes: 8 additions & 1 deletion src/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface OperationsProps
scale: number;
onSwitchLeft: React.MouseEventHandler<HTMLDivElement>;
onSwitchRight: React.MouseEventHandler<HTMLDivElement>;
onRefresh: () => void;
onZoomIn: () => void;
onZoomOut: () => void;
onRotateRight: () => void;
Expand All @@ -49,14 +50,15 @@ const Operations: React.FC<OperationsProps> = (props) => {
onSwitchLeft,
onSwitchRight,
onClose,
onRefresh,
onZoomIn,
onZoomOut,
onRotateRight,
onRotateLeft,
onFlipX,
onFlipY
} = props;
const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right, flipX, flipY } = icons;
const { rotateLeft, rotateRight, refresh, zoomIn, zoomOut, close, left, right, flipX, flipY } = icons;
const toolClassName = `${prefixCls}-operations-operation`;
const iconClassName = `${prefixCls}-operations-icon`;
const tools = [
Expand All @@ -65,6 +67,11 @@ const Operations: React.FC<OperationsProps> = (props) => {
onClick: onClose,
type: 'close',
},
{
icon: refresh,
onClick: onRefresh,
type: 'refresh',
},
{
icon: zoomIn,
onClick: onZoomIn,
Expand Down
11 changes: 11 additions & 0 deletions src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface PreviewProps extends Omit<IDialogPropTypes, 'onClose'> {
rotateRight?: React.ReactNode;
zoomIn?: React.ReactNode;
zoomOut?: React.ReactNode;
refresh?: React.ReactNode;
close?: React.ReactNode;
left?: React.ReactNode;
right?: React.ReactNode;
Expand All @@ -29,6 +30,7 @@ export interface PreviewProps extends Omit<IDialogPropTypes, 'onClose'> {
};
countRender?: (current: number, total: number) => string;
scaleStep?: number;
dragRebound?: boolean;
}

const Preview: React.FC<PreviewProps> = (props) => {
Expand All @@ -45,6 +47,7 @@ const Preview: React.FC<PreviewProps> = (props) => {
scaleStep = 0.5,
transitionName = 'zoom',
maskTransitionName = 'fade',
dragRebound = true,
...restProps
} = props;

Expand Down Expand Up @@ -74,6 +77,10 @@ const Preview: React.FC<PreviewProps> = (props) => {
resetTransform();
};

const onRefresh = ()=> {
resetTransform();
};

const onZoomIn = () => {
dispatchZoomChange(BASE_SCALE_RATIO + scaleStep);
};
Expand Down Expand Up @@ -117,6 +124,9 @@ const Preview: React.FC<PreviewProps> = (props) => {
const onMouseUp: React.MouseEventHandler<HTMLBodyElement> = () => {
if (visible && isMoving) {
setMoving(false);
if (!dragRebound) {
return;
}

/** No need to restore the position when the picture is not moved, So as not to interfere with the click */
const { transformX, transformY } = downPositionRef.current;
Expand Down Expand Up @@ -298,6 +308,7 @@ const Preview: React.FC<PreviewProps> = (props) => {
onRotateLeft={onRotateLeft}
onFlipX={onFlipX}
onFlipY={onFlipY}
onRefresh={onRefresh}
onClose={onClose}
/>
</>
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/preview.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ exports[`Preview add rootClassName should be correct when open preview 1`] = `
<li
class="rc-image-preview-operations-operation rc-image-preview-operations-operation-close"
/>
<li
class="rc-image-preview-operations-operation rc-image-preview-operations-operation-refresh"
/>
<li
class="rc-image-preview-operations-operation rc-image-preview-operations-operation-zoomIn"
/>
Expand Down
28 changes: 15 additions & 13 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ZoomOutOutlined from '@ant-design/icons/ZoomOutOutlined';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import LeftOutlined from '@ant-design/icons/LeftOutlined';
import RightOutlined from '@ant-design/icons/RightOutlined';
import ReloadOutlined from '@ant-design/icons/ReloadOutlined';

jest.mock('../src/Preview', () => {
const MockPreview = (props: any) => {
Expand Down Expand Up @@ -118,15 +119,15 @@ describe('Preview', () => {
jest.runAllTimers();
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[3]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[4]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, 1, 1) rotate(90deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[4]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[5]);
act(() => {
jest.runAllTimers();
});
Expand All @@ -145,31 +146,31 @@ describe('Preview', () => {
jest.runAllTimers();
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[5]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[6]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(-1, 1, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[6]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[7]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(-1, -1, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[5]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[6]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, -1, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[6]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[7]);
act(() => {
jest.runAllTimers();
});
Expand All @@ -188,23 +189,23 @@ describe('Preview', () => {
jest.runAllTimers();
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[3]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, 1, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[1]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(-256px, -192px, 0) scale3d(1.5, 1.5, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[3]);
act(() => {
jest.runAllTimers();
});
Expand Down Expand Up @@ -261,23 +262,23 @@ describe('Preview', () => {
jest.runAllTimers();
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[3]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, 1, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[1]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(-512px, -384px, 0) scale3d(2, 2, 1) rotate(0deg)',
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[3]);
act(() => {
jest.runAllTimers();
});
Expand Down Expand Up @@ -317,7 +318,7 @@ describe('Preview', () => {
jest.runAllTimers();
});

fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[1]);
fireEvent.click(document.querySelectorAll('.rc-image-preview-operations-operation')[2]);
act(() => {
jest.runAllTimers();
});
Expand Down Expand Up @@ -572,6 +573,7 @@ describe('Preview', () => {
zoomIn: <ZoomInOutlined />,
zoomOut: <ZoomOutOutlined />,
close: <CloseOutlined />,
refresh: <ReloadOutlined />,
left: <LeftOutlined />,
right: <RightOutlined />,
}}
Expand Down