Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

react-native-image-viewing@0.2.2.patch

288 lines14.6 KB

1diff --git a/dist/ImageViewing.js b/dist/ImageViewing.js
2index 3d7641a9a94864415d207c678b71f8c592c90aa1..2b9ef77c3eb58cd284152ad22b2e711a91d50e7d 100644
3--- a/dist/ImageViewing.js
4+++ b/dist/ImageViewing.js
5@@ -5,8 +5,8 @@
6 * LICENSE file in the root directory of this source tree.
7 *
8 */
9-import React, { useCallback, useRef, useEffect } from "react";
10-import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, } from "react-native";
11+import React, { useCallback, useRef, useEffect, useLayoutEffect } from "react";
12+import { Animated, Dimensions, StyleSheet, View, VirtualizedList, Modal, Platform, useWindowDimensions, } from "react-native";
13 import ImageItem from "./components/ImageItem/ImageItem";
14 import ImageDefaultHeader from "./components/ImageDefaultHeader";
15 import StatusBarManager from "./components/StatusBarManager";
16@@ -17,23 +17,34 @@ const DEFAULT_ANIMATION_TYPE = "fade";
17 const DEFAULT_BG_COLOR = "#000";
18 const DEFAULT_DELAY_LONG_PRESS = 800;
19 const SCREEN = Dimensions.get("screen");
20-const SCREEN_WIDTH = SCREEN.width;
21+const useViewingDimensions = Platform.OS === "android" ? useWindowDimensions : () => SCREEN;
22 function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClose, onLongPress = () => { }, onImageIndexChange, animationType = DEFAULT_ANIMATION_TYPE, backgroundColor = DEFAULT_BG_COLOR, presentationStyle, swipeToCloseEnabled, doubleTapToZoomEnabled, delayLongPress = DEFAULT_DELAY_LONG_PRESS, HeaderComponent, FooterComponent, }) {
23+ const viewport = useViewingDimensions();
24+ const SCREEN_WIDTH = viewport.width;
25+ const viewportKey = Platform.OS === "android" ? `${viewport.width}:${viewport.height}` : undefined;
26 const imageList = useRef(null);
27 const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose);
28- const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN);
29- const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents();
30+ const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, viewport);
31+ const [headerTransform, footerTransform, toggleBarsVisible] = useAnimatedComponents(viewportKey);
32 useEffect(() => {
33 if (onImageIndexChange) {
34 onImageIndexChange(currentImageIndex);
35 }
36 }, [currentImageIndex]);
37- const onZoom = useCallback((isScaled) => {
38+ const activeZoom = useRef(null);
39+ const onZoom = useCallback(function handleZoom(isScaled) {
40+ if (Platform.OS === "android" && activeZoom.current !== handleZoom) {
41+ return;
42+ }
43 var _a, _b;
44 // @ts-ignore
45 (_b = (_a = imageList) === null || _a === void 0 ? void 0 : _a.current) === null || _b === void 0 ? void 0 : _b.setNativeProps({ scrollEnabled: !isScaled });
46 toggleBarsVisible(!isScaled);
47- }, [imageList]);
48+ }, [imageList, toggleBarsVisible]);
49+ useLayoutEffect(() => {
50+ activeZoom.current = onZoom;
51+ return () => { activeZoom.current = null; };
52+ }, [onZoom]);
53 if (!visible) {
54 return null;
55 }
56@@ -45,7 +56,8 @@ function ImageViewing({ images, keyExtractor, imageIndex, visible, onRequestClos
57 imageIndex: currentImageIndex,
58 })) : (<ImageDefaultHeader onRequestClose={onRequestCloseEnhanced}/>)}
59 </Animated.View>
60- <VirtualizedList ref={imageList} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={imageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
61+ {/* Reset Android image gestures on resize while preserving custom header state. */}
62+ <VirtualizedList key={viewportKey} ref={imageList} onScroll={Platform.OS === "android" ? onScroll : undefined} scrollEventThrottle={Platform.OS === "android" ? 16 : undefined} data={images} horizontal pagingEnabled windowSize={2} initialNumToRender={1} maxToRenderPerBatch={1} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} initialScrollIndex={currentImageIndex} getItem={(_, index) => images[index]} getItemCount={() => images.length} getItemLayout={(_, index) => ({
63 length: SCREEN_WIDTH,
64 offset: SCREEN_WIDTH * index,
65 index,
66diff --git a/dist/components/ImageItem/ImageItem.android.js b/dist/components/ImageItem/ImageItem.android.js
67index 5fcaa9f9a7d893817fee89b85186ee3936118e69..3effe838fa54495d165b81cc7501e72d51fbee1b 100644
68--- a/dist/components/ImageItem/ImageItem.android.js
69+++ b/dist/components/ImageItem/ImageItem.android.js
70@@ -6,17 +6,17 @@
71 *
72 */
73 import React, { useCallback, useRef, useState } from "react";
74-import { Animated, ScrollView, Dimensions, StyleSheet, } from "react-native";
75+import { Animated, ScrollView, Dimensions, } from "react-native";
76 import useImageDimensions from "../../hooks/useImageDimensions";
77 import usePanResponder from "../../hooks/usePanResponder";
78 import { getImageStyles, getImageTransform } from "../../utils";
79 import { ImageLoading } from "./ImageLoading";
80 const SWIPE_CLOSE_OFFSET = 75;
81 const SWIPE_CLOSE_VELOCITY = 1.75;
82-const SCREEN = Dimensions.get("window");
83-const SCREEN_WIDTH = SCREEN.width;
84-const SCREEN_HEIGHT = SCREEN.height;
85 const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPress, swipeToCloseEnabled = true, doubleTapToZoomEnabled = true, }) => {
86+ const SCREEN = Dimensions.get("window");
87+ const SCREEN_WIDTH = SCREEN.width;
88+ const SCREEN_HEIGHT = SCREEN.height;
89 const imageContainer = useRef(null);
90 const imageDimensions = useImageDimensions(imageSrc);
91 const [translate, scale] = getImageTransform(imageDimensions, SCREEN);
92@@ -28,10 +28,10 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
93 onZoom(isZoomed);
94 if ((_a = imageContainer) === null || _a === void 0 ? void 0 : _a.current) {
95 imageContainer.current.setNativeProps({
96- scrollEnabled: !isZoomed,
97+ scrollEnabled: swipeToCloseEnabled && !isZoomed,
98 });
99 }
100- }, [imageContainer]);
101+ }, [imageContainer, onZoom, swipeToCloseEnabled]);
102 const onLongPressHandler = useCallback(() => {
103 onLongPress(imageSrc);
104 }, [imageSrc, onLongPress]);
105@@ -64,7 +64,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
106 const offsetY = (_c = (_b = (_a = nativeEvent) === null || _a === void 0 ? void 0 : _a.contentOffset) === null || _b === void 0 ? void 0 : _b.y, (_c !== null && _c !== void 0 ? _c : 0));
107 scrollValueY.setValue(offsetY);
108 };
109- return (<ScrollView ref={imageContainer} style={styles.listItem} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={styles.imageScrollContainer} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
110+ return (<ScrollView ref={imageContainer} style={{ width: SCREEN_WIDTH, height: SCREEN_HEIGHT }} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={{ height: SCREEN_HEIGHT * 2 }} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
111 onScroll,
112 onScrollEndDrag,
113 })}>
114@@ -72,13 +72,4 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
115 {(!isLoaded || !imageDimensions) && <ImageLoading />}
116 </ScrollView>);
117 };
118-const styles = StyleSheet.create({
119- listItem: {
120- width: SCREEN_WIDTH,
121- height: SCREEN_HEIGHT,
122- },
123- imageScrollContainer: {
124- height: SCREEN_HEIGHT * 2,
125- },
126-});
127 export default React.memo(ImageItem);
128diff --git a/dist/components/ImageItem/ImageLoading.js b/dist/components/ImageItem/ImageLoading.js
129index 9770f7db158a2fb2b1c69a49e5fcbc89d8624603..995fab7524bb8673258d44e5eda361c62dae0324 100644
130--- a/dist/components/ImageItem/ImageLoading.js
131+++ b/dist/components/ImageItem/ImageLoading.js
132@@ -6,25 +6,17 @@
133 *
134 */
135 import React from "react";
136-import { ActivityIndicator, Dimensions, StyleSheet, View } from "react-native";
137+import { ActivityIndicator, Dimensions, Platform, StyleSheet, View } from "react-native";
138 const SCREEN = Dimensions.get("screen");
139-const SCREEN_WIDTH = SCREEN.width;
140-const SCREEN_HEIGHT = SCREEN.height;
141-export const ImageLoading = () => (<View style={styles.loading}>
142+export const ImageLoading = () => {
143+ const { width, height } = Platform.OS === "android" ? Dimensions.get("window") : SCREEN;
144+ return (<View style={[styles.loading, { width, height }]}>
145 <ActivityIndicator size="small" color="#FFF"/>
146 </View>);
147+};
148 const styles = StyleSheet.create({
149- listItem: {
150- width: SCREEN_WIDTH,
151- height: SCREEN_HEIGHT,
152- },
153 loading: {
154- width: SCREEN_WIDTH,
155- height: SCREEN_HEIGHT,
156 alignItems: "center",
157 justifyContent: "center",
158 },
159- imageScrollContainer: {
160- height: SCREEN_HEIGHT,
161- },
162 });
163diff --git a/dist/hooks/useAnimatedComponents.d.ts b/dist/hooks/useAnimatedComponents.d.ts
164index de5957458fea62f33743103acaa170a2c90d3638..dbafa8c5c5f3f86161e8520822cf10e19e88ea25 100644
165--- a/dist/hooks/useAnimatedComponents.d.ts
166+++ b/dist/hooks/useAnimatedComponents.d.ts
167@@ -6,7 +6,7 @@
168 *
169 */
170 import { Animated } from "react-native";
171-declare const useAnimatedComponents: () => readonly [{
172+declare const useAnimatedComponents: (resetKey?: string) => readonly [{
173 [key: string]: Animated.Value;
174 }[], {
175 [key: string]: Animated.Value;
176diff --git a/dist/hooks/useAnimatedComponents.js b/dist/hooks/useAnimatedComponents.js
177index 4730fe40735bd80afc97abdd14150a2fb4abf3e5..0ed2100744ccc9bcee7f1b15b0328a729d47bb16 100644
178--- a/dist/hooks/useAnimatedComponents.js
179+++ b/dist/hooks/useAnimatedComponents.js
180@@ -5,16 +5,19 @@
181 * LICENSE file in the root directory of this source tree.
182 *
183 */
184+import { useCallback, useMemo } from "react";
185 import { Animated } from "react-native";
186 const INITIAL_POSITION = { x: 0, y: 0 };
187 const ANIMATION_CONFIG = {
188 duration: 200,
189 useNativeDriver: true,
190 };
191-const useAnimatedComponents = () => {
192- const headerTranslate = new Animated.ValueXY(INITIAL_POSITION);
193- const footerTranslate = new Animated.ValueXY(INITIAL_POSITION);
194- const toggleVisible = (isVisible) => {
195+const useAnimatedComponents = (resetKey) => {
196+ const [headerTranslate, footerTranslate] = useMemo(() => [
197+ new Animated.ValueXY(INITIAL_POSITION),
198+ new Animated.ValueXY(INITIAL_POSITION),
199+ ], [resetKey]);
200+ const toggleVisible = useCallback((isVisible) => {
201 if (isVisible) {
202 Animated.parallel([
203 Animated.timing(headerTranslate.y, { ...ANIMATION_CONFIG, toValue: 0 }),
204@@ -33,7 +36,7 @@ const useAnimatedComponents = () => {
205 }),
206 ]).start();
207 }
208- };
209+ }, [headerTranslate, footerTranslate]);
210 const headerTransform = headerTranslate.getTranslateTransform();
211 const footerTransform = footerTranslate.getTranslateTransform();
212 return [headerTransform, footerTransform, toggleVisible];
213diff --git a/dist/hooks/useImageIndexChange.js b/dist/hooks/useImageIndexChange.js
214index cb5f0f798e5cfeedf459b7d22e92871db3092020..97ca2c9c326b1c53e6a5d90c3bb95d53342088d1 100644
215--- a/dist/hooks/useImageIndexChange.js
216+++ b/dist/hooks/useImageIndexChange.js
217@@ -5,16 +5,34 @@
218 * LICENSE file in the root directory of this source tree.
219 *
220 */
221-import { useState } from "react";
222+import { useCallback, useLayoutEffect, useRef, useState } from "react";
223+import { Platform } from "react-native";
224 const useImageIndexChange = (imageIndex, screen) => {
225 const [currentImageIndex, setImageIndex] = useState(imageIndex);
226- const onScroll = (event) => {
227+ const indexRef = useRef(imageIndex);
228+ const activeScroll = useRef(null);
229+ const onScroll = useCallback(function handleScroll(event) {
230+ if (Platform.OS === "android") {
231+ // Events at this width stay valid until commit; replaced or relaid-out lists do not.
232+ if (activeScroll.current !== handleScroll ||
233+ Math.abs(event.nativeEvent.layoutMeasurement.width - screen.width) > 1) {
234+ return;
235+ }
236+ }
237 const { nativeEvent: { contentOffset: { x: scrollX }, }, } = event;
238 if (screen.width) {
239 const nextIndex = Math.round(scrollX / screen.width);
240- setImageIndex(nextIndex < 0 ? 0 : nextIndex);
241+ const boundedIndex = nextIndex < 0 ? 0 : nextIndex;
242+ if (boundedIndex !== indexRef.current) {
243+ indexRef.current = boundedIndex;
244+ setImageIndex(boundedIndex);
245+ }
246 }
247- };
248+ }, [screen]);
249+ useLayoutEffect(() => {
250+ activeScroll.current = onScroll;
251+ return () => { activeScroll.current = null; };
252+ }, [onScroll]);
253 return [currentImageIndex, onScroll];
254 };
255 export default useImageIndexChange;
256diff --git a/dist/hooks/usePanResponder.js b/dist/hooks/usePanResponder.js
257index 99dcbc7efea4eba86a8bc2bad71dde73c8ebf1b4..3a374c0923879548ec3826a78be16f79c6e01432 100644
258--- a/dist/hooks/usePanResponder.js
259+++ b/dist/hooks/usePanResponder.js
260@@ -8,14 +8,14 @@
261 import { useMemo, useEffect } from "react";
262 import { Animated, Dimensions, } from "react-native";
263 import { createPanResponder, getDistanceBetweenTouches, getImageTranslate, getImageDimensionsByTranslate, } from "../utils";
264-const SCREEN = Dimensions.get("window");
265-const SCREEN_WIDTH = SCREEN.width;
266-const SCREEN_HEIGHT = SCREEN.height;
267-const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
268 const SCALE_MAX = 2;
269 const DOUBLE_TAP_DELAY = 300;
270 const OUT_BOUND_MULTIPLIER = 0.75;
271 const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }) => {
272+ const SCREEN = Dimensions.get("window");
273+ const SCREEN_WIDTH = SCREEN.width;
274+ const SCREEN_HEIGHT = SCREEN.height;
275+ const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
276 let numberInitialTouches = 1;
277 let initialTouches = [];
278 let currentScale = initialScale;
279@@ -266,6 +266,9 @@ const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZo
280 tmpTranslate = null;
281 }
282 },
283+ // A gesture taken over mid-pinch must commit like a release, or pans stay
284+ // gated on the pre-pinch scale while the image still shows zoomed.
285+ onTerminate: () => handlers.onRelease(),
286 };
287 const panResponder = useMemo(() => createPanResponder(handlers), [handlers]);
288 return [panResponder.panHandlers, scaleValue, translateValue];