Skip to content

Commit

Permalink
feat: support preview - dragRebound control and add refresh operation (
Browse files Browse the repository at this point in the history
  • Loading branch information
suyi660 committed Apr 7, 2023
1 parent 38d02b2 commit 670d175
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
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
17 changes: 17 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,16 @@ const Preview: React.FC<PreviewProps> = (props) => {
resetTransform();
};

const onRefresh = ()=> {
console.log('refresh');
updateTransform({
rotate: 0,
x: 0,
y: 0,
scale: 1,
})
}

const onZoomIn = () => {
dispatchZoomChange(BASE_SCALE_RATIO + scaleStep);
};
Expand Down Expand Up @@ -117,6 +130,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 +314,7 @@ const Preview: React.FC<PreviewProps> = (props) => {
onRotateLeft={onRotateLeft}
onFlipX={onFlipX}
onFlipY={onFlipY}
onRefresh={onRefresh}
onClose={onClose}
/>
</>
Expand Down

0 comments on commit 670d175

Please sign in to comment.