forked from react-component/image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreview.tsx
More file actions
319 lines (287 loc) · 9.57 KB
/
Preview.tsx
File metadata and controls
319 lines (287 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import React, { useState, useEffect, useCallback, useRef, useContext } from 'react';
import type { DialogProps as IDialogPropTypes } from 'rc-dialog';
import Dialog from 'rc-dialog';
import classnames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import KeyCode from 'rc-util/lib/KeyCode';
import { warning } from 'rc-util/lib/warning';
import { context } from './PreviewGroup';
import Operations from './Operations';
import useImageTransform from './hooks/useImageTransform';
import getFixScaleEleTransPosition from './getFixScaleEleTransPosition';
import { BASE_SCALE_RATIO, WHEEL_MAX_SCALE_RATIO } from './previewConfig';
export interface PreviewProps extends Omit<IDialogPropTypes, 'onClose'> {
onClose?: (e: React.SyntheticEvent<Element>) => void;
src?: string;
alt?: string;
rootClassName?: string;
icons?: {
rotateLeft?: React.ReactNode;
rotateRight?: React.ReactNode;
zoomIn?: React.ReactNode;
zoomOut?: React.ReactNode;
refresh?: React.ReactNode;
close?: React.ReactNode;
left?: React.ReactNode;
right?: React.ReactNode;
flipX?: React.ReactNode;
flipY?: React.ReactNode;
};
countRender?: (current: number, total: number) => string;
scaleStep?: number;
resetPositionAfterDrag?: boolean;
}
const Preview: React.FC<PreviewProps> = (props) => {
const {
prefixCls,
src,
alt,
onClose,
visible,
icons = {},
rootClassName,
getContainer,
countRender,
scaleStep = 0.5,
transitionName = 'zoom',
maskTransitionName = 'fade',
resetPositionAfterDrag = true,
...restProps
} = props;
const imgRef = useRef<HTMLImageElement>();
const downPositionRef = useRef({
deltaX: 0,
deltaY: 0,
transformX: 0,
transformY: 0,
});
const [isMoving, setMoving] = useState(false);
const { previewUrls, current, isPreviewGroup, setCurrent } = useContext(context);
const previewGroupCount = previewUrls.size;
const previewUrlsKeys = Array.from(previewUrls.keys());
const currentPreviewIndex = previewUrlsKeys.indexOf(current);
const combinationSrc = isPreviewGroup ? previewUrls.get(current) : src;
const showLeftOrRightSwitches = isPreviewGroup && previewGroupCount > 1;
const showOperationsProgress = isPreviewGroup && previewGroupCount >= 1;
const { transform, resetTransform, updateTransform, dispatchZoomChange } = useImageTransform(imgRef);
const { rotate, scale } = transform;
const wrapClassName = classnames({
[`${prefixCls}-moving`]: isMoving,
});
const onAfterClose = () => {
resetTransform();
};
const onRefresh = ()=> {
resetTransform();
}
const onZoomIn = () => {
dispatchZoomChange(BASE_SCALE_RATIO + scaleStep);
};
const onZoomOut = () => {
dispatchZoomChange(BASE_SCALE_RATIO - scaleStep);
};
const onRotateRight = () => {
updateTransform({ rotate: rotate + 90 });
};
const onRotateLeft = () => {
updateTransform({ rotate: rotate - 90 });
};
const onFlipX = () => {
updateTransform({flipX: !transform.flipX})
};
const onFlipY = () => {
updateTransform({flipY: !transform.flipY})
};
const onSwitchLeft: React.MouseEventHandler<HTMLDivElement> = (event) => {
event.preventDefault();
event.stopPropagation();
if (currentPreviewIndex > 0) {
setCurrent(previewUrlsKeys[currentPreviewIndex - 1]);
}
};
const onSwitchRight: React.MouseEventHandler<HTMLDivElement> = (event) => {
event.preventDefault();
event.stopPropagation();
if (currentPreviewIndex < previewGroupCount - 1) {
setCurrent(previewUrlsKeys[currentPreviewIndex + 1]);
}
};
const onMouseUp: React.MouseEventHandler<HTMLBodyElement> = () => {
if (visible && isMoving) {
setMoving(false);
if(!resetPositionAfterDrag){
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;
const hasChangedPosition = transform.x !== transformX && transform.y !== transformY;
if (!hasChangedPosition) {
return;
}
const width = imgRef.current.offsetWidth * scale;
const height = imgRef.current.offsetHeight * scale;
// eslint-disable-next-line @typescript-eslint/no-shadow
const { left, top } = imgRef.current.getBoundingClientRect();
const isRotate = rotate % 180 !== 0;
const fixState = getFixScaleEleTransPosition(
isRotate ? height : width,
isRotate ? width : height,
left,
top,
);
if (fixState) {
updateTransform({ ...fixState });
}
}
};
const onMouseDown: React.MouseEventHandler<HTMLDivElement> = (event) => {
// Only allow main button
if (event.button !== 0) return;
event.preventDefault();
event.stopPropagation();
downPositionRef.current = {
deltaX: event.pageX - transform.x,
deltaY: event.pageY - transform.y,
transformX: transform.x,
transformY: transform.y,
};
setMoving(true);
};
const onMouseMove: React.MouseEventHandler<HTMLBodyElement> = (event) => {
if (visible && isMoving) {
updateTransform({
x: event.pageX - downPositionRef.current.deltaX,
y: event.pageY - downPositionRef.current.deltaY,
});
}
};
const onWheel = (event: React.WheelEvent<HTMLImageElement>) => {
if (!visible || event.deltaY == 0) return;
// Scale ratio depends on the deltaY size
const scaleRatio = Math.abs(event.deltaY / 100);
// Limit the maximum scale ratio
const mergedScaleRatio = Math.min(scaleRatio, WHEEL_MAX_SCALE_RATIO);
// Scale the ratio each time
let ratio = BASE_SCALE_RATIO + (mergedScaleRatio * scaleStep);
if (event.deltaY > 0) {
ratio = BASE_SCALE_RATIO / ratio;
}
dispatchZoomChange(ratio, event.clientX, event.clientY);
};
const onKeyDown = useCallback(
(event: KeyboardEvent) => {
if (!visible || !showLeftOrRightSwitches) return;
if (event.keyCode === KeyCode.LEFT) {
if (currentPreviewIndex > 0) {
setCurrent(previewUrlsKeys[currentPreviewIndex - 1]);
}
} else if (event.keyCode === KeyCode.RIGHT) {
if (currentPreviewIndex < previewGroupCount - 1) {
setCurrent(previewUrlsKeys[currentPreviewIndex + 1]);
}
}
},
[
currentPreviewIndex,
previewGroupCount,
previewUrlsKeys,
setCurrent,
showLeftOrRightSwitches,
visible,
],
);
const onDoubleClick = (event: React.MouseEvent<HTMLImageElement, MouseEvent>) => {
if (visible) {
if (scale !== 1) {
updateTransform({ x: 0, y: 0, scale: 1 });
} else {
dispatchZoomChange(BASE_SCALE_RATIO + scaleStep, event.clientX, event.clientY);
}
}
};
useEffect(() => {
let onTopMouseUpListener;
let onTopMouseMoveListener;
const onMouseUpListener = addEventListener(window, 'mouseup', onMouseUp, false);
const onMouseMoveListener = addEventListener(window, 'mousemove', onMouseMove, false);
const onKeyDownListener = addEventListener(window, 'keydown', onKeyDown, false);
try {
// Resolve if in iframe lost event
/* istanbul ignore next */
if (window.top !== window.self) {
onTopMouseUpListener = addEventListener(window.top, 'mouseup', onMouseUp, false);
onTopMouseMoveListener = addEventListener(window.top, 'mousemove', onMouseMove, false);
}
} catch (error) {
/* istanbul ignore next */
warning(false, `[rc-image] ${error}`);
}
return () => {
onMouseUpListener.remove();
onMouseMoveListener.remove();
onKeyDownListener.remove();
/* istanbul ignore next */
onTopMouseUpListener?.remove();
/* istanbul ignore next */
onTopMouseMoveListener?.remove();
};
}, [visible, isMoving, onKeyDown]);
return (
<>
<Dialog
transitionName={transitionName}
maskTransitionName={maskTransitionName}
closable={false}
keyboard
prefixCls={prefixCls}
onClose={onClose}
visible={visible}
wrapClassName={wrapClassName}
rootClassName={rootClassName}
getContainer={getContainer}
{...restProps}
afterClose={onAfterClose}
>
<div className={`${prefixCls}-img-wrapper`}>
<img
width={props.width}
height={props.height}
onWheel={onWheel}
onMouseDown={onMouseDown}
onDoubleClick={onDoubleClick}
ref={imgRef}
className={`${prefixCls}-img`}
src={combinationSrc}
alt={alt}
style={{ transform: `translate3d(${transform.x}px, ${transform.y}px, 0) scale3d(${transform.flipX ? '-' : ''}${scale}, ${transform.flipY ? '-' : ''}${scale}, 1) rotate(${rotate}deg)` }}
/>
</div>
</Dialog>
<Operations
visible={visible}
maskTransitionName={maskTransitionName}
getContainer={getContainer}
prefixCls={prefixCls}
rootClassName={rootClassName}
icons={icons}
countRender={countRender}
showSwitch={showLeftOrRightSwitches}
showProgress={showOperationsProgress}
current={currentPreviewIndex}
count={previewGroupCount}
scale={scale}
onSwitchLeft={onSwitchLeft}
onSwitchRight={onSwitchRight}
onZoomIn={onZoomIn}
onZoomOut={onZoomOut}
onRotateRight={onRotateRight}
onRotateLeft={onRotateLeft}
onFlipX={onFlipX}
onFlipY={onFlipY}
onRefresh={onRefresh}
onClose={onClose}
/>
</>
);
};
export default Preview;