forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
241 lines (214 loc) · 9 KB
/
index.tsx
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
import type {SyntheticEvent} from 'react';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import type {GestureResponderEvent, LayoutChangeEvent} from 'react-native';
import {View} from 'react-native';
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Image from '@components/Image';
import Lightbox from '@components/Lightbox';
import RESIZE_MODES from '@components/Image/resizeModes';
import type {ImageOnLoadEvent} from '@components/Image/types';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import CONST from '@src/CONST';
import viewRef from '@src/types/utils/viewRef';
import type ImageViewProps from './types';
type ZoomDelta = {offsetX: number; offsetY: number};
function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageViewProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const [isLoading, setIsLoading] = useState(true);
const [containerHeight, setContainerHeight] = useState(0);
const [containerWidth, setContainerWidth] = useState(0);
const [isZoomed, setIsZoomed] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const [isMouseDown, setIsMouseDown] = useState(false);
const [initialScrollLeft, setInitialScrollLeft] = useState(0);
const [initialScrollTop, setInitialScrollTop] = useState(0);
const [initialX, setInitialX] = useState(0);
const [initialY, setInitialY] = useState(0);
const [imgWidth, setImgWidth] = useState(0);
const [imgHeight, setImgHeight] = useState(0);
const [zoomScale, setZoomScale] = useState(0);
const [zoomDelta, setZoomDelta] = useState<ZoomDelta>();
const scrollableRef = useRef<HTMLDivElement>(null);
const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const setScale = (newContainerWidth: number, newContainerHeight: number, newImageWidth: number, newImageHeight: number) => {
if (!newContainerWidth || !newImageWidth || !newContainerHeight || !newImageHeight) {
return;
}
const newZoomScale = Math.min(newContainerWidth / newImageWidth, newContainerHeight / newImageHeight);
setZoomScale(newZoomScale);
};
const onContainerLayoutChanged = (e: LayoutChangeEvent) => {
const {width, height} = e.nativeEvent.layout;
setScale(width, height, imgWidth, imgHeight);
setContainerHeight(height);
setContainerWidth(width);
};
/**
* When open image, set image width, height.
*/
const setImageRegion = (imageWidth: number, imageHeight: number) => {
if (imageHeight <= 0) {
return;
}
setScale(containerWidth, containerHeight, imageWidth, imageHeight);
setImgWidth(imageWidth);
setImgHeight(imageHeight);
};
const imageLoadingStart = () => {
if (!isLoading) {
return;
}
setIsLoading(true);
setZoomScale(0);
setIsZoomed(false);
};
const imageLoad = ({nativeEvent}: ImageOnLoadEvent) => {
setImageRegion(nativeEvent.width, nativeEvent.height);
setIsLoading(false);
};
const onContainerPressIn = (e: GestureResponderEvent) => {
const {pageX, pageY} = e.nativeEvent;
setIsMouseDown(true);
setInitialX(pageX);
setInitialY(pageY);
setInitialScrollLeft(scrollableRef.current?.scrollLeft ?? 0);
setInitialScrollTop(scrollableRef.current?.scrollTop ?? 0);
};
/**
* Convert touch point to zoomed point
* @param x point when click zoom
* @param y point when click zoom
* @returns converted touch point
*/
const getScrollOffset = (x: number, y: number) => {
let offsetX = 0;
let offsetY = 0;
// Container size bigger than clicked position offset
if (x <= containerWidth / 2) {
offsetX = 0;
} else if (x > containerWidth / 2) {
// Minus half of container size because we want to be center clicked position
offsetX = x - containerWidth / 2;
}
if (y <= containerHeight / 2) {
offsetY = 0;
} else if (y > containerHeight / 2) {
// Minus half of container size because we want to be center clicked position
offsetY = y - containerHeight / 2;
}
return {offsetX, offsetY};
};
const onContainerPress = (e?: GestureResponderEvent | KeyboardEvent | SyntheticEvent<Element, PointerEvent>) => {
if (!isZoomed && !isDragging) {
if (e && 'nativeEvent' in e && e.nativeEvent instanceof PointerEvent) {
const {offsetX, offsetY} = e.nativeEvent;
// Dividing clicked positions by the zoom scale to get coordinates
// so that once we zoom we will scroll to the clicked location.
const delta = getScrollOffset(offsetX / zoomScale, offsetY / zoomScale);
setZoomDelta(delta);
} else {
setZoomDelta({offsetX: 0, offsetY: 0});
}
}
if (isZoomed && isDragging && isMouseDown) {
setIsDragging(false);
setIsMouseDown(false);
} else {
// We first zoom and once its done then we scroll to the location the user clicked.
setIsZoomed(!isZoomed);
setIsMouseDown(false);
}
};
const trackPointerPosition = useCallback(
(event: MouseEvent) => {
// Whether the pointer is released inside the ImageView
const isInsideImageView = scrollableRef.current?.contains(event.target as Node);
if (!isInsideImageView && isZoomed && isDragging && isMouseDown) {
setIsDragging(false);
setIsMouseDown(false);
}
},
[isDragging, isMouseDown, isZoomed],
);
const trackMovement = useCallback(
(event: MouseEvent) => {
if (!isZoomed) {
return;
}
if (isDragging && isMouseDown && scrollableRef.current) {
const x = event.x;
const y = event.y;
const moveX = initialX - x;
const moveY = initialY - y;
scrollableRef.current.scrollLeft = initialScrollLeft + moveX;
scrollableRef.current.scrollTop = initialScrollTop + moveY;
}
setIsDragging(isMouseDown);
},
[initialScrollLeft, initialScrollTop, initialX, initialY, isDragging, isMouseDown, isZoomed],
);
useEffect(() => {
if (!isZoomed || !zoomDelta || !scrollableRef.current) {
return;
}
scrollableRef.current.scrollLeft = zoomDelta.offsetX;
scrollableRef.current.scrollTop = zoomDelta.offsetY;
}, [zoomDelta, isZoomed]);
useEffect(() => {
if (canUseTouchScreen) {
return;
}
document.addEventListener('mousemove', trackMovement);
document.addEventListener('mouseup', trackPointerPosition);
return () => {
document.removeEventListener('mousemove', trackMovement);
document.removeEventListener('mouseup', trackPointerPosition);
};
}, [canUseTouchScreen, trackMovement, trackPointerPosition]);
if (canUseTouchScreen) {
return (
<Lightbox
uri={url}
isAuthTokenRequired={isAuthTokenRequired}
onError={onError}
/>
);
}
return (
<View
ref={viewRef(scrollableRef)}
onLayout={onContainerLayoutChanged}
style={[styles.imageViewContainer, styles.overflowAuto, styles.pRelative]}
>
<PressableWithoutFeedback
style={{
...StyleUtils.getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight, containerWidth, isLoading),
...StyleUtils.getZoomCursorStyle(isZoomed, isDragging),
...(isZoomed && zoomScale >= 1 ? styles.pRelative : styles.pAbsolute),
...styles.flex1,
}}
onPressIn={onContainerPressIn}
onPress={onContainerPress}
role={CONST.ROLE.IMG}
accessibilityLabel={fileName}
>
<Image
source={{uri: url}}
isAuthTokenRequired={isAuthTokenRequired}
style={[styles.h100, styles.w100]}
resizeMode={RESIZE_MODES.contain}
onLoadStart={imageLoadingStart}
onLoad={imageLoad}
onError={onError}
/>
</PressableWithoutFeedback>
{isLoading && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
);
}
ImageView.displayName = 'ImageView';
export default ImageView;