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

react-native-image-viewing@0.2.2.patch

274 lines13.8 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@@ -31,7 +31,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
93 scrollEnabled: !isZoomed,
94 });
95 }
96- }, [imageContainer]);
97+ }, [imageContainer, onZoom]);
98 const onLongPressHandler = useCallback(() => {
99 onLongPress(imageSrc);
100 }, [imageSrc, onLongPress]);
101@@ -64,7 +64,7 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
102 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));
103 scrollValueY.setValue(offsetY);
104 };
105- return (<ScrollView ref={imageContainer} style={styles.listItem} pagingEnabled nestedScrollEnabled showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} contentContainerStyle={styles.imageScrollContainer} scrollEnabled={swipeToCloseEnabled} {...(swipeToCloseEnabled && {
106+ 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 && {
107 onScroll,
108 onScrollEndDrag,
109 })}>
110@@ -72,13 +72,4 @@ const ImageItem = ({ imageSrc, onZoom, onRequestClose, onLongPress, delayLongPre
111 {(!isLoaded || !imageDimensions) && <ImageLoading />}
112 </ScrollView>);
113 };
114-const styles = StyleSheet.create({
115- listItem: {
116- width: SCREEN_WIDTH,
117- height: SCREEN_HEIGHT,
118- },
119- imageScrollContainer: {
120- height: SCREEN_HEIGHT * 2,
121- },
122-});
123 export default React.memo(ImageItem);
124diff --git a/dist/components/ImageItem/ImageLoading.js b/dist/components/ImageItem/ImageLoading.js
125index 9770f7db158a2fb2b1c69a49e5fcbc89d8624603..995fab7524bb8673258d44e5eda361c62dae0324 100644
126--- a/dist/components/ImageItem/ImageLoading.js
127+++ b/dist/components/ImageItem/ImageLoading.js
128@@ -6,25 +6,17 @@
129 *
130 */
131 import React from "react";
132-import { ActivityIndicator, Dimensions, StyleSheet, View } from "react-native";
133+import { ActivityIndicator, Dimensions, Platform, StyleSheet, View } from "react-native";
134 const SCREEN = Dimensions.get("screen");
135-const SCREEN_WIDTH = SCREEN.width;
136-const SCREEN_HEIGHT = SCREEN.height;
137-export const ImageLoading = () => (<View style={styles.loading}>
138+export const ImageLoading = () => {
139+ const { width, height } = Platform.OS === "android" ? Dimensions.get("window") : SCREEN;
140+ return (<View style={[styles.loading, { width, height }]}>
141 <ActivityIndicator size="small" color="#FFF"/>
142 </View>);
143+};
144 const styles = StyleSheet.create({
145- listItem: {
146- width: SCREEN_WIDTH,
147- height: SCREEN_HEIGHT,
148- },
149 loading: {
150- width: SCREEN_WIDTH,
151- height: SCREEN_HEIGHT,
152 alignItems: "center",
153 justifyContent: "center",
154 },
155- imageScrollContainer: {
156- height: SCREEN_HEIGHT,
157- },
158 });
159diff --git a/dist/hooks/useAnimatedComponents.d.ts b/dist/hooks/useAnimatedComponents.d.ts
160index de5957458fea62f33743103acaa170a2c90d3638..dbafa8c5c5f3f86161e8520822cf10e19e88ea25 100644
161--- a/dist/hooks/useAnimatedComponents.d.ts
162+++ b/dist/hooks/useAnimatedComponents.d.ts
163@@ -6,7 +6,7 @@
164 *
165 */
166 import { Animated } from "react-native";
167-declare const useAnimatedComponents: () => readonly [{
168+declare const useAnimatedComponents: (resetKey?: string) => readonly [{
169 [key: string]: Animated.Value;
170 }[], {
171 [key: string]: Animated.Value;
172diff --git a/dist/hooks/useAnimatedComponents.js b/dist/hooks/useAnimatedComponents.js
173index 4730fe40735bd80afc97abdd14150a2fb4abf3e5..0ed2100744ccc9bcee7f1b15b0328a729d47bb16 100644
174--- a/dist/hooks/useAnimatedComponents.js
175+++ b/dist/hooks/useAnimatedComponents.js
176@@ -5,16 +5,19 @@
177 * LICENSE file in the root directory of this source tree.
178 *
179 */
180+import { useCallback, useMemo } from "react";
181 import { Animated } from "react-native";
182 const INITIAL_POSITION = { x: 0, y: 0 };
183 const ANIMATION_CONFIG = {
184 duration: 200,
185 useNativeDriver: true,
186 };
187-const useAnimatedComponents = () => {
188- const headerTranslate = new Animated.ValueXY(INITIAL_POSITION);
189- const footerTranslate = new Animated.ValueXY(INITIAL_POSITION);
190- const toggleVisible = (isVisible) => {
191+const useAnimatedComponents = (resetKey) => {
192+ const [headerTranslate, footerTranslate] = useMemo(() => [
193+ new Animated.ValueXY(INITIAL_POSITION),
194+ new Animated.ValueXY(INITIAL_POSITION),
195+ ], [resetKey]);
196+ const toggleVisible = useCallback((isVisible) => {
197 if (isVisible) {
198 Animated.parallel([
199 Animated.timing(headerTranslate.y, { ...ANIMATION_CONFIG, toValue: 0 }),
200@@ -33,7 +36,7 @@ const useAnimatedComponents = () => {
201 }),
202 ]).start();
203 }
204- };
205+ }, [headerTranslate, footerTranslate]);
206 const headerTransform = headerTranslate.getTranslateTransform();
207 const footerTransform = footerTranslate.getTranslateTransform();
208 return [headerTransform, footerTransform, toggleVisible];
209diff --git a/dist/hooks/useImageIndexChange.js b/dist/hooks/useImageIndexChange.js
210index cb5f0f798e5cfeedf459b7d22e92871db3092020..97ca2c9c326b1c53e6a5d90c3bb95d53342088d1 100644
211--- a/dist/hooks/useImageIndexChange.js
212+++ b/dist/hooks/useImageIndexChange.js
213@@ -5,16 +5,34 @@
214 * LICENSE file in the root directory of this source tree.
215 *
216 */
217-import { useState } from "react";
218+import { useCallback, useLayoutEffect, useRef, useState } from "react";
219+import { Platform } from "react-native";
220 const useImageIndexChange = (imageIndex, screen) => {
221 const [currentImageIndex, setImageIndex] = useState(imageIndex);
222- const onScroll = (event) => {
223+ const indexRef = useRef(imageIndex);
224+ const activeScroll = useRef(null);
225+ const onScroll = useCallback(function handleScroll(event) {
226+ if (Platform.OS === "android") {
227+ // Events at this width stay valid until commit; replaced or relaid-out lists do not.
228+ if (activeScroll.current !== handleScroll ||
229+ Math.abs(event.nativeEvent.layoutMeasurement.width - screen.width) > 1) {
230+ return;
231+ }
232+ }
233 const { nativeEvent: { contentOffset: { x: scrollX }, }, } = event;
234 if (screen.width) {
235 const nextIndex = Math.round(scrollX / screen.width);
236- setImageIndex(nextIndex < 0 ? 0 : nextIndex);
237+ const boundedIndex = nextIndex < 0 ? 0 : nextIndex;
238+ if (boundedIndex !== indexRef.current) {
239+ indexRef.current = boundedIndex;
240+ setImageIndex(boundedIndex);
241+ }
242 }
243- };
244+ }, [screen]);
245+ useLayoutEffect(() => {
246+ activeScroll.current = onScroll;
247+ return () => { activeScroll.current = null; };
248+ }, [onScroll]);
249 return [currentImageIndex, onScroll];
250 };
251 export default useImageIndexChange;
252diff --git a/dist/hooks/usePanResponder.js b/dist/hooks/usePanResponder.js
253index 99dcbc7efea4eba86a8bc2bad71dde73c8ebf1b4..3a374c0923879548ec3826a78be16f79c6e01432 100644
254--- a/dist/hooks/usePanResponder.js
255+++ b/dist/hooks/usePanResponder.js
256@@ -8,14 +8,14 @@
257 import { useMemo, useEffect } from "react";
258 import { Animated, Dimensions, } from "react-native";
259 import { createPanResponder, getDistanceBetweenTouches, getImageTranslate, getImageDimensionsByTranslate, } from "../utils";
260-const SCREEN = Dimensions.get("window");
261-const SCREEN_WIDTH = SCREEN.width;
262-const SCREEN_HEIGHT = SCREEN.height;
263-const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
264 const SCALE_MAX = 2;
265 const DOUBLE_TAP_DELAY = 300;
266 const OUT_BOUND_MULTIPLIER = 0.75;
267 const usePanResponder = ({ initialScale, initialTranslate, onZoom, doubleTapToZoomEnabled, onLongPress, delayLongPress, }) => {
268+ const SCREEN = Dimensions.get("window");
269+ const SCREEN_WIDTH = SCREEN.width;
270+ const SCREEN_HEIGHT = SCREEN.height;
271+ const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT);
272 let numberInitialTouches = 1;
273 let initialTouches = [];
274 let currentScale = initialScale;