Remove unused lightbox options (#1616)
* Inline lightbox helpers * Delete unused useImagePrefetch * Delete unused long press gesture * Always enable double tap * Always enable swipe to close * Remove unused onImageIndexChange * Inline custom Hooks into ImageViewing * Declare LightboxFooter outside Lightbox * Add more TODO comments * Inline useDoubleTapToZoom * Remove dead utils, move utils used only oncezio/stable
parent
eb7306b165
commit
260b03a05c
|
@ -36,23 +36,11 @@ type Props = {
|
||||||
imageSrc: ImageSource
|
imageSrc: ImageSource
|
||||||
onRequestClose: () => void
|
onRequestClose: () => void
|
||||||
onZoom: (isZoomed: boolean) => void
|
onZoom: (isZoomed: boolean) => void
|
||||||
onLongPress: (image: ImageSource) => void
|
|
||||||
delayLongPress: number
|
|
||||||
swipeToCloseEnabled?: boolean
|
|
||||||
doubleTapToZoomEnabled?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const AnimatedImage = Animated.createAnimatedComponent(Image)
|
const AnimatedImage = Animated.createAnimatedComponent(Image)
|
||||||
|
|
||||||
const ImageItem = ({
|
const ImageItem = ({imageSrc, onZoom, onRequestClose}: Props) => {
|
||||||
imageSrc,
|
|
||||||
onZoom,
|
|
||||||
onRequestClose,
|
|
||||||
onLongPress,
|
|
||||||
delayLongPress,
|
|
||||||
swipeToCloseEnabled = true,
|
|
||||||
doubleTapToZoomEnabled = true,
|
|
||||||
}: Props) => {
|
|
||||||
const imageContainer = useRef<ScrollView & NativeMethodsMixin>(null)
|
const imageContainer = useRef<ScrollView & NativeMethodsMixin>(null)
|
||||||
const imageDimensions = useImageDimensions(imageSrc)
|
const imageDimensions = useImageDimensions(imageSrc)
|
||||||
const [translate, scale] = getImageTransform(imageDimensions, SCREEN)
|
const [translate, scale] = getImageTransform(imageDimensions, SCREEN)
|
||||||
|
@ -72,17 +60,10 @@ const ImageItem = ({
|
||||||
[onZoom],
|
[onZoom],
|
||||||
)
|
)
|
||||||
|
|
||||||
const onLongPressHandler = useCallback(() => {
|
|
||||||
onLongPress(imageSrc)
|
|
||||||
}, [imageSrc, onLongPress])
|
|
||||||
|
|
||||||
const [panHandlers, scaleValue, translateValue] = usePanResponder({
|
const [panHandlers, scaleValue, translateValue] = usePanResponder({
|
||||||
initialScale: scale || 1,
|
initialScale: scale || 1,
|
||||||
initialTranslate: translate || {x: 0, y: 0},
|
initialTranslate: translate || {x: 0, y: 0},
|
||||||
onZoom: onZoomPerformed,
|
onZoom: onZoomPerformed,
|
||||||
doubleTapToZoomEnabled,
|
|
||||||
onLongPress: onLongPressHandler,
|
|
||||||
delayLongPress,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const imagesStyles = getImageStyles(
|
const imagesStyles = getImageStyles(
|
||||||
|
@ -126,11 +107,9 @@ const ImageItem = ({
|
||||||
showsHorizontalScrollIndicator={false}
|
showsHorizontalScrollIndicator={false}
|
||||||
showsVerticalScrollIndicator={false}
|
showsVerticalScrollIndicator={false}
|
||||||
contentContainerStyle={styles.imageScrollContainer}
|
contentContainerStyle={styles.imageScrollContainer}
|
||||||
scrollEnabled={swipeToCloseEnabled}
|
scrollEnabled={true}
|
||||||
{...(swipeToCloseEnabled && {
|
onScroll={onScroll}
|
||||||
onScroll,
|
onScrollEndDrag={onScrollEndDrag}>
|
||||||
onScrollEndDrag,
|
|
||||||
})}>
|
|
||||||
<AnimatedImage
|
<AnimatedImage
|
||||||
{...panHandlers}
|
{...panHandlers}
|
||||||
source={imageSrc}
|
source={imageSrc}
|
||||||
|
|
|
@ -16,57 +16,45 @@ import {
|
||||||
View,
|
View,
|
||||||
NativeScrollEvent,
|
NativeScrollEvent,
|
||||||
NativeSyntheticEvent,
|
NativeSyntheticEvent,
|
||||||
|
NativeTouchEvent,
|
||||||
TouchableWithoutFeedback,
|
TouchableWithoutFeedback,
|
||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
import {Image} from 'expo-image'
|
import {Image} from 'expo-image'
|
||||||
|
|
||||||
import useDoubleTapToZoom from '../../hooks/useDoubleTapToZoom'
|
|
||||||
import useImageDimensions from '../../hooks/useImageDimensions'
|
import useImageDimensions from '../../hooks/useImageDimensions'
|
||||||
|
|
||||||
import {getImageStyles, getImageTransform} from '../../utils'
|
import {getImageStyles, getImageTransform} from '../../utils'
|
||||||
import {ImageSource} from '../../@types'
|
import {ImageSource} from '../../@types'
|
||||||
import {ImageLoading} from './ImageLoading'
|
import {ImageLoading} from './ImageLoading'
|
||||||
|
|
||||||
|
const DOUBLE_TAP_DELAY = 300
|
||||||
const SWIPE_CLOSE_OFFSET = 75
|
const SWIPE_CLOSE_OFFSET = 75
|
||||||
const SWIPE_CLOSE_VELOCITY = 1
|
const SWIPE_CLOSE_VELOCITY = 1
|
||||||
const SCREEN = Dimensions.get('screen')
|
const SCREEN = Dimensions.get('screen')
|
||||||
const SCREEN_WIDTH = SCREEN.width
|
const SCREEN_WIDTH = SCREEN.width
|
||||||
const SCREEN_HEIGHT = SCREEN.height
|
const SCREEN_HEIGHT = SCREEN.height
|
||||||
|
const MIN_ZOOM = 2
|
||||||
const MAX_SCALE = 2
|
const MAX_SCALE = 2
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
imageSrc: ImageSource
|
imageSrc: ImageSource
|
||||||
onRequestClose: () => void
|
onRequestClose: () => void
|
||||||
onZoom: (scaled: boolean) => void
|
onZoom: (scaled: boolean) => void
|
||||||
onLongPress: (image: ImageSource) => void
|
|
||||||
delayLongPress: number
|
|
||||||
swipeToCloseEnabled?: boolean
|
|
||||||
doubleTapToZoomEnabled?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const AnimatedImage = Animated.createAnimatedComponent(Image)
|
const AnimatedImage = Animated.createAnimatedComponent(Image)
|
||||||
|
|
||||||
const ImageItem = ({
|
let lastTapTS: number | null = null
|
||||||
imageSrc,
|
|
||||||
onZoom,
|
const ImageItem = ({imageSrc, onZoom, onRequestClose}: Props) => {
|
||||||
onRequestClose,
|
|
||||||
onLongPress,
|
|
||||||
delayLongPress,
|
|
||||||
swipeToCloseEnabled = true,
|
|
||||||
doubleTapToZoomEnabled = true,
|
|
||||||
}: Props) => {
|
|
||||||
const scrollViewRef = useRef<ScrollView>(null)
|
const scrollViewRef = useRef<ScrollView>(null)
|
||||||
const [loaded, setLoaded] = useState(false)
|
const [loaded, setLoaded] = useState(false)
|
||||||
const [scaled, setScaled] = useState(false)
|
const [scaled, setScaled] = useState(false)
|
||||||
const imageDimensions = useImageDimensions(imageSrc)
|
const imageDimensions = useImageDimensions(imageSrc)
|
||||||
const handleDoubleTap = useDoubleTapToZoom(
|
|
||||||
scrollViewRef,
|
|
||||||
scaled,
|
|
||||||
SCREEN,
|
|
||||||
imageDimensions,
|
|
||||||
)
|
|
||||||
|
|
||||||
const [translate, scale] = getImageTransform(imageDimensions, SCREEN)
|
const [translate, scale] = getImageTransform(imageDimensions, SCREEN)
|
||||||
|
|
||||||
|
// TODO: It's not valid to reinitialize Animated values during render.
|
||||||
|
// This is a bug.
|
||||||
const scrollValueY = new Animated.Value(0)
|
const scrollValueY = new Animated.Value(0)
|
||||||
const scaleValue = new Animated.Value(scale || 1)
|
const scaleValue = new Animated.Value(scale || 1)
|
||||||
const translateValue = new Animated.ValueXY(translate)
|
const translateValue = new Animated.ValueXY(translate)
|
||||||
|
@ -91,15 +79,11 @@ const ImageItem = ({
|
||||||
onZoom(currentScaled)
|
onZoom(currentScaled)
|
||||||
setScaled(currentScaled)
|
setScaled(currentScaled)
|
||||||
|
|
||||||
if (
|
if (!currentScaled && Math.abs(velocityY) > SWIPE_CLOSE_VELOCITY) {
|
||||||
!currentScaled &&
|
|
||||||
swipeToCloseEnabled &&
|
|
||||||
Math.abs(velocityY) > SWIPE_CLOSE_VELOCITY
|
|
||||||
) {
|
|
||||||
onRequestClose()
|
onRequestClose()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[onRequestClose, onZoom, swipeToCloseEnabled],
|
[onRequestClose, onZoom],
|
||||||
)
|
)
|
||||||
|
|
||||||
const onScroll = ({nativeEvent}: NativeSyntheticEvent<NativeScrollEvent>) => {
|
const onScroll = ({nativeEvent}: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||||
|
@ -112,9 +96,40 @@ const ImageItem = ({
|
||||||
scrollValueY.setValue(offsetY)
|
scrollValueY.setValue(offsetY)
|
||||||
}
|
}
|
||||||
|
|
||||||
const onLongPressHandler = useCallback(() => {
|
const handleDoubleTap = useCallback(
|
||||||
onLongPress(imageSrc)
|
(event: NativeSyntheticEvent<NativeTouchEvent>) => {
|
||||||
}, [imageSrc, onLongPress])
|
const nowTS = new Date().getTime()
|
||||||
|
const scrollResponderRef = scrollViewRef?.current?.getScrollResponder()
|
||||||
|
|
||||||
|
if (lastTapTS && nowTS - lastTapTS < DOUBLE_TAP_DELAY) {
|
||||||
|
let nextZoomRect = {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: SCREEN.width,
|
||||||
|
height: SCREEN.height,
|
||||||
|
}
|
||||||
|
|
||||||
|
const willZoom = !scaled
|
||||||
|
if (willZoom) {
|
||||||
|
const {pageX, pageY} = event.nativeEvent
|
||||||
|
nextZoomRect = getZoomRectAfterDoubleTap(
|
||||||
|
imageDimensions,
|
||||||
|
pageX,
|
||||||
|
pageY,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
scrollResponderRef?.scrollResponderZoomTo({
|
||||||
|
...nextZoomRect, // This rect is in screen coordinates
|
||||||
|
animated: true,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
lastTapTS = nowTS
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[imageDimensions, scaled],
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View>
|
<View>
|
||||||
|
@ -126,17 +141,13 @@ const ImageItem = ({
|
||||||
showsVerticalScrollIndicator={false}
|
showsVerticalScrollIndicator={false}
|
||||||
maximumZoomScale={maxScrollViewZoom}
|
maximumZoomScale={maxScrollViewZoom}
|
||||||
contentContainerStyle={styles.imageScrollContainer}
|
contentContainerStyle={styles.imageScrollContainer}
|
||||||
scrollEnabled={swipeToCloseEnabled}
|
scrollEnabled={true}
|
||||||
|
onScroll={onScroll}
|
||||||
onScrollEndDrag={onScrollEndDrag}
|
onScrollEndDrag={onScrollEndDrag}
|
||||||
scrollEventThrottle={1}
|
scrollEventThrottle={1}>
|
||||||
{...(swipeToCloseEnabled && {
|
|
||||||
onScroll,
|
|
||||||
})}>
|
|
||||||
{(!loaded || !imageDimensions) && <ImageLoading />}
|
{(!loaded || !imageDimensions) && <ImageLoading />}
|
||||||
<TouchableWithoutFeedback
|
<TouchableWithoutFeedback
|
||||||
onPress={doubleTapToZoomEnabled ? handleDoubleTap : undefined}
|
onPress={handleDoubleTap}
|
||||||
onLongPress={onLongPressHandler}
|
|
||||||
delayLongPress={delayLongPress}
|
|
||||||
accessibilityRole="image"
|
accessibilityRole="image"
|
||||||
accessibilityLabel={imageSrc.alt}
|
accessibilityLabel={imageSrc.alt}
|
||||||
accessibilityHint="">
|
accessibilityHint="">
|
||||||
|
@ -161,4 +172,92 @@ const styles = StyleSheet.create({
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const getZoomRectAfterDoubleTap = (
|
||||||
|
imageDimensions: {width: number; height: number} | null,
|
||||||
|
touchX: number,
|
||||||
|
touchY: number,
|
||||||
|
): {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
} => {
|
||||||
|
if (!imageDimensions) {
|
||||||
|
return {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: SCREEN.width,
|
||||||
|
height: SCREEN.height,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// First, let's figure out how much we want to zoom in.
|
||||||
|
// We want to try to zoom in at least close enough to get rid of black bars.
|
||||||
|
const imageAspect = imageDimensions.width / imageDimensions.height
|
||||||
|
const screenAspect = SCREEN.width / SCREEN.height
|
||||||
|
const zoom = Math.max(
|
||||||
|
imageAspect / screenAspect,
|
||||||
|
screenAspect / imageAspect,
|
||||||
|
MIN_ZOOM,
|
||||||
|
)
|
||||||
|
// Unlike in the Android version, we don't constrain the *max* zoom level here.
|
||||||
|
// Instead, this is done in the ScrollView props so that it constraints pinch too.
|
||||||
|
|
||||||
|
// Next, we'll be calculating the rectangle to "zoom into" in screen coordinates.
|
||||||
|
// We already know the zoom level, so this gives us the rectangle size.
|
||||||
|
let rectWidth = SCREEN.width / zoom
|
||||||
|
let rectHeight = SCREEN.height / zoom
|
||||||
|
|
||||||
|
// Before we settle on the zoomed rect, figure out the safe area it has to be inside.
|
||||||
|
// We don't want to introduce new black bars or make existing black bars unbalanced.
|
||||||
|
let minX = 0
|
||||||
|
let minY = 0
|
||||||
|
let maxX = SCREEN.width - rectWidth
|
||||||
|
let maxY = SCREEN.height - rectHeight
|
||||||
|
if (imageAspect >= screenAspect) {
|
||||||
|
// The image has horizontal black bars. Exclude them from the safe area.
|
||||||
|
const renderedHeight = SCREEN.width / imageAspect
|
||||||
|
const horizontalBarHeight = (SCREEN.height - renderedHeight) / 2
|
||||||
|
minY += horizontalBarHeight
|
||||||
|
maxY -= horizontalBarHeight
|
||||||
|
} else {
|
||||||
|
// The image has vertical black bars. Exclude them from the safe area.
|
||||||
|
const renderedWidth = SCREEN.height * imageAspect
|
||||||
|
const verticalBarWidth = (SCREEN.width - renderedWidth) / 2
|
||||||
|
minX += verticalBarWidth
|
||||||
|
maxX -= verticalBarWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, we can position the rect according to its size and the safe area.
|
||||||
|
let rectX
|
||||||
|
if (maxX >= minX) {
|
||||||
|
// Content fills the screen horizontally so we have horizontal wiggle room.
|
||||||
|
// Try to keep the tapped point under the finger after zoom.
|
||||||
|
rectX = touchX - touchX / zoom
|
||||||
|
rectX = Math.min(rectX, maxX)
|
||||||
|
rectX = Math.max(rectX, minX)
|
||||||
|
} else {
|
||||||
|
// Keep the rect centered on the screen so that black bars are balanced.
|
||||||
|
rectX = SCREEN.width / 2 - rectWidth / 2
|
||||||
|
}
|
||||||
|
let rectY
|
||||||
|
if (maxY >= minY) {
|
||||||
|
// Content fills the screen vertically so we have vertical wiggle room.
|
||||||
|
// Try to keep the tapped point under the finger after zoom.
|
||||||
|
rectY = touchY - touchY / zoom
|
||||||
|
rectY = Math.min(rectY, maxY)
|
||||||
|
rectY = Math.max(rectY, minY)
|
||||||
|
} else {
|
||||||
|
// Keep the rect centered on the screen so that black bars are balanced.
|
||||||
|
rectY = SCREEN.height / 2 - rectHeight / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: rectX,
|
||||||
|
y: rectY,
|
||||||
|
height: rectHeight,
|
||||||
|
width: rectWidth,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default React.memo(ImageItem)
|
export default React.memo(ImageItem)
|
||||||
|
|
|
@ -8,10 +8,6 @@ type Props = {
|
||||||
imageSrc: ImageSource
|
imageSrc: ImageSource
|
||||||
onRequestClose: () => void
|
onRequestClose: () => void
|
||||||
onZoom: (scaled: boolean) => void
|
onZoom: (scaled: boolean) => void
|
||||||
onLongPress: (image: ImageSource) => void
|
|
||||||
delayLongPress: number
|
|
||||||
swipeToCloseEnabled?: boolean
|
|
||||||
doubleTapToZoomEnabled?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ImageItem = (_props: Props) => {
|
const ImageItem = (_props: Props) => {
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) JOB TODAY S.A. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {Animated} from 'react-native'
|
|
||||||
|
|
||||||
const INITIAL_POSITION = {x: 0, y: 0}
|
|
||||||
const ANIMATION_CONFIG = {
|
|
||||||
duration: 200,
|
|
||||||
useNativeDriver: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
const useAnimatedComponents = () => {
|
|
||||||
const headerTranslate = new Animated.ValueXY(INITIAL_POSITION)
|
|
||||||
const footerTranslate = new Animated.ValueXY(INITIAL_POSITION)
|
|
||||||
|
|
||||||
const toggleVisible = (isVisible: boolean) => {
|
|
||||||
if (isVisible) {
|
|
||||||
Animated.parallel([
|
|
||||||
Animated.timing(headerTranslate.y, {...ANIMATION_CONFIG, toValue: 0}),
|
|
||||||
Animated.timing(footerTranslate.y, {...ANIMATION_CONFIG, toValue: 0}),
|
|
||||||
]).start()
|
|
||||||
} else {
|
|
||||||
Animated.parallel([
|
|
||||||
Animated.timing(headerTranslate.y, {
|
|
||||||
...ANIMATION_CONFIG,
|
|
||||||
toValue: -300,
|
|
||||||
}),
|
|
||||||
Animated.timing(footerTranslate.y, {
|
|
||||||
...ANIMATION_CONFIG,
|
|
||||||
toValue: 300,
|
|
||||||
}),
|
|
||||||
]).start()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const headerTransform = headerTranslate.getTranslateTransform()
|
|
||||||
const footerTransform = footerTranslate.getTranslateTransform()
|
|
||||||
|
|
||||||
return [headerTransform, footerTransform, toggleVisible] as const
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useAnimatedComponents
|
|
|
@ -1,150 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) JOB TODAY S.A. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import React, {useCallback} from 'react'
|
|
||||||
import {ScrollView, NativeTouchEvent, NativeSyntheticEvent} from 'react-native'
|
|
||||||
|
|
||||||
import {Dimensions} from '../@types'
|
|
||||||
|
|
||||||
const DOUBLE_TAP_DELAY = 300
|
|
||||||
const MIN_ZOOM = 2
|
|
||||||
|
|
||||||
let lastTapTS: number | null = null
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is iOS only.
|
|
||||||
* Same functionality for Android implemented inside usePanResponder hook.
|
|
||||||
*/
|
|
||||||
function useDoubleTapToZoom(
|
|
||||||
scrollViewRef: React.RefObject<ScrollView>,
|
|
||||||
scaled: boolean,
|
|
||||||
screen: Dimensions,
|
|
||||||
imageDimensions: Dimensions | null,
|
|
||||||
) {
|
|
||||||
const handleDoubleTap = useCallback(
|
|
||||||
(event: NativeSyntheticEvent<NativeTouchEvent>) => {
|
|
||||||
const nowTS = new Date().getTime()
|
|
||||||
const scrollResponderRef = scrollViewRef?.current?.getScrollResponder()
|
|
||||||
|
|
||||||
const getZoomRectAfterDoubleTap = (
|
|
||||||
touchX: number,
|
|
||||||
touchY: number,
|
|
||||||
): {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
width: number
|
|
||||||
height: number
|
|
||||||
} => {
|
|
||||||
if (!imageDimensions) {
|
|
||||||
return {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: screen.width,
|
|
||||||
height: screen.height,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// First, let's figure out how much we want to zoom in.
|
|
||||||
// We want to try to zoom in at least close enough to get rid of black bars.
|
|
||||||
const imageAspect = imageDimensions.width / imageDimensions.height
|
|
||||||
const screenAspect = screen.width / screen.height
|
|
||||||
const zoom = Math.max(
|
|
||||||
imageAspect / screenAspect,
|
|
||||||
screenAspect / imageAspect,
|
|
||||||
MIN_ZOOM,
|
|
||||||
)
|
|
||||||
// Unlike in the Android version, we don't constrain the *max* zoom level here.
|
|
||||||
// Instead, this is done in the ScrollView props so that it constraints pinch too.
|
|
||||||
|
|
||||||
// Next, we'll be calculating the rectangle to "zoom into" in screen coordinates.
|
|
||||||
// We already know the zoom level, so this gives us the rectangle size.
|
|
||||||
let rectWidth = screen.width / zoom
|
|
||||||
let rectHeight = screen.height / zoom
|
|
||||||
|
|
||||||
// Before we settle on the zoomed rect, figure out the safe area it has to be inside.
|
|
||||||
// We don't want to introduce new black bars or make existing black bars unbalanced.
|
|
||||||
let minX = 0
|
|
||||||
let minY = 0
|
|
||||||
let maxX = screen.width - rectWidth
|
|
||||||
let maxY = screen.height - rectHeight
|
|
||||||
if (imageAspect >= screenAspect) {
|
|
||||||
// The image has horizontal black bars. Exclude them from the safe area.
|
|
||||||
const renderedHeight = screen.width / imageAspect
|
|
||||||
const horizontalBarHeight = (screen.height - renderedHeight) / 2
|
|
||||||
minY += horizontalBarHeight
|
|
||||||
maxY -= horizontalBarHeight
|
|
||||||
} else {
|
|
||||||
// The image has vertical black bars. Exclude them from the safe area.
|
|
||||||
const renderedWidth = screen.height * imageAspect
|
|
||||||
const verticalBarWidth = (screen.width - renderedWidth) / 2
|
|
||||||
minX += verticalBarWidth
|
|
||||||
maxX -= verticalBarWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally, we can position the rect according to its size and the safe area.
|
|
||||||
let rectX
|
|
||||||
if (maxX >= minX) {
|
|
||||||
// Content fills the screen horizontally so we have horizontal wiggle room.
|
|
||||||
// Try to keep the tapped point under the finger after zoom.
|
|
||||||
rectX = touchX - touchX / zoom
|
|
||||||
rectX = Math.min(rectX, maxX)
|
|
||||||
rectX = Math.max(rectX, minX)
|
|
||||||
} else {
|
|
||||||
// Keep the rect centered on the screen so that black bars are balanced.
|
|
||||||
rectX = screen.width / 2 - rectWidth / 2
|
|
||||||
}
|
|
||||||
let rectY
|
|
||||||
if (maxY >= minY) {
|
|
||||||
// Content fills the screen vertically so we have vertical wiggle room.
|
|
||||||
// Try to keep the tapped point under the finger after zoom.
|
|
||||||
rectY = touchY - touchY / zoom
|
|
||||||
rectY = Math.min(rectY, maxY)
|
|
||||||
rectY = Math.max(rectY, minY)
|
|
||||||
} else {
|
|
||||||
// Keep the rect centered on the screen so that black bars are balanced.
|
|
||||||
rectY = screen.height / 2 - rectHeight / 2
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: rectX,
|
|
||||||
y: rectY,
|
|
||||||
height: rectHeight,
|
|
||||||
width: rectWidth,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lastTapTS && nowTS - lastTapTS < DOUBLE_TAP_DELAY) {
|
|
||||||
let nextZoomRect = {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: screen.width,
|
|
||||||
height: screen.height,
|
|
||||||
}
|
|
||||||
|
|
||||||
const willZoom = !scaled
|
|
||||||
if (willZoom) {
|
|
||||||
const {pageX, pageY} = event.nativeEvent
|
|
||||||
nextZoomRect = getZoomRectAfterDoubleTap(pageX, pageY)
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
scrollResponderRef?.scrollResponderZoomTo({
|
|
||||||
...nextZoomRect, // This rect is in screen coordinates
|
|
||||||
animated: true,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
lastTapTS = nowTS
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[imageDimensions, scaled, screen.height, screen.width, scrollViewRef],
|
|
||||||
)
|
|
||||||
|
|
||||||
return handleDoubleTap
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useDoubleTapToZoom
|
|
|
@ -8,11 +8,29 @@
|
||||||
|
|
||||||
import {useEffect, useState} from 'react'
|
import {useEffect, useState} from 'react'
|
||||||
import {Image, ImageURISource} from 'react-native'
|
import {Image, ImageURISource} from 'react-native'
|
||||||
|
|
||||||
import {createCache} from '../utils'
|
|
||||||
import {Dimensions, ImageSource} from '../@types'
|
import {Dimensions, ImageSource} from '../@types'
|
||||||
|
|
||||||
const CACHE_SIZE = 50
|
const CACHE_SIZE = 50
|
||||||
|
|
||||||
|
type CacheStorageItem = {key: string; value: any}
|
||||||
|
|
||||||
|
const createCache = (cacheSize: number) => ({
|
||||||
|
_storage: [] as CacheStorageItem[],
|
||||||
|
get(key: string): any {
|
||||||
|
const {value} =
|
||||||
|
this._storage.find(({key: storageKey}) => storageKey === key) || {}
|
||||||
|
|
||||||
|
return value
|
||||||
|
},
|
||||||
|
set(key: string, value: any) {
|
||||||
|
if (this._storage.length >= cacheSize) {
|
||||||
|
this._storage.shift()
|
||||||
|
}
|
||||||
|
|
||||||
|
this._storage.push({key, value})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const imageDimensionsCache = createCache(CACHE_SIZE)
|
const imageDimensionsCache = createCache(CACHE_SIZE)
|
||||||
|
|
||||||
const useImageDimensions = (image: ImageSource): Dimensions | null => {
|
const useImageDimensions = (image: ImageSource): Dimensions | null => {
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) JOB TODAY S.A. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {useState} from 'react'
|
|
||||||
import {NativeSyntheticEvent, NativeScrollEvent} from 'react-native'
|
|
||||||
|
|
||||||
import {Dimensions} from '../@types'
|
|
||||||
|
|
||||||
const useImageIndexChange = (imageIndex: number, screen: Dimensions) => {
|
|
||||||
const [currentImageIndex, setImageIndex] = useState(imageIndex)
|
|
||||||
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
||||||
const {
|
|
||||||
nativeEvent: {
|
|
||||||
contentOffset: {x: scrollX},
|
|
||||||
},
|
|
||||||
} = event
|
|
||||||
|
|
||||||
if (screen.width) {
|
|
||||||
const nextIndex = Math.round(scrollX / screen.width)
|
|
||||||
setImageIndex(nextIndex < 0 ? 0 : nextIndex)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [currentImageIndex, onScroll] as const
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useImageIndexChange
|
|
|
@ -1,25 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) JOB TODAY S.A. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {useEffect} from 'react'
|
|
||||||
import {Image} from 'react-native'
|
|
||||||
import {ImageSource} from '../@types'
|
|
||||||
|
|
||||||
const useImagePrefetch = (images: ImageSource[]) => {
|
|
||||||
useEffect(() => {
|
|
||||||
images.forEach(image => {
|
|
||||||
//@ts-ignore
|
|
||||||
if (image.uri) {
|
|
||||||
//@ts-ignore
|
|
||||||
return Image.prefetch(image.uri)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}, [images])
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useImagePrefetch
|
|
|
@ -18,16 +18,11 @@ import {
|
||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
|
|
||||||
import {Position} from '../@types'
|
import {Position} from '../@types'
|
||||||
import {
|
import {getImageTranslate} from '../utils'
|
||||||
getDistanceBetweenTouches,
|
|
||||||
getImageTranslate,
|
|
||||||
getImageDimensionsByTranslate,
|
|
||||||
} from '../utils'
|
|
||||||
|
|
||||||
const SCREEN = Dimensions.get('window')
|
const SCREEN = Dimensions.get('window')
|
||||||
const SCREEN_WIDTH = SCREEN.width
|
const SCREEN_WIDTH = SCREEN.width
|
||||||
const SCREEN_HEIGHT = SCREEN.height
|
const SCREEN_HEIGHT = SCREEN.height
|
||||||
const MIN_DIMENSION = Math.min(SCREEN_WIDTH, SCREEN_HEIGHT)
|
|
||||||
const ANDROID_BAR_HEIGHT = 24
|
const ANDROID_BAR_HEIGHT = 24
|
||||||
|
|
||||||
const MIN_ZOOM = 2
|
const MIN_ZOOM = 2
|
||||||
|
@ -39,18 +34,12 @@ type Props = {
|
||||||
initialScale: number
|
initialScale: number
|
||||||
initialTranslate: Position
|
initialTranslate: Position
|
||||||
onZoom: (isZoomed: boolean) => void
|
onZoom: (isZoomed: boolean) => void
|
||||||
doubleTapToZoomEnabled: boolean
|
|
||||||
onLongPress: () => void
|
|
||||||
delayLongPress: number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const usePanResponder = ({
|
const usePanResponder = ({
|
||||||
initialScale,
|
initialScale,
|
||||||
initialTranslate,
|
initialTranslate,
|
||||||
onZoom,
|
onZoom,
|
||||||
doubleTapToZoomEnabled,
|
|
||||||
onLongPress,
|
|
||||||
delayLongPress,
|
|
||||||
}: Props): Readonly<
|
}: Props): Readonly<
|
||||||
[GestureResponderHandlers, Animated.Value, Animated.ValueXY]
|
[GestureResponderHandlers, Animated.Value, Animated.ValueXY]
|
||||||
> => {
|
> => {
|
||||||
|
@ -62,9 +51,9 @@ const usePanResponder = ({
|
||||||
let tmpTranslate: Position | null = null
|
let tmpTranslate: Position | null = null
|
||||||
let isDoubleTapPerformed = false
|
let isDoubleTapPerformed = false
|
||||||
let lastTapTS: number | null = null
|
let lastTapTS: number | null = null
|
||||||
let longPressHandlerRef: NodeJS.Timeout | null = null
|
|
||||||
|
|
||||||
const meaningfulShift = MIN_DIMENSION * 0.01
|
// TODO: It's not valid to reinitialize Animated values during render.
|
||||||
|
// This is a bug.
|
||||||
const scaleValue = new Animated.Value(initialScale)
|
const scaleValue = new Animated.Value(initialScale)
|
||||||
const translateValue = new Animated.ValueXY(initialTranslate)
|
const translateValue = new Animated.ValueXY(initialTranslate)
|
||||||
|
|
||||||
|
@ -155,10 +144,6 @@ const usePanResponder = ({
|
||||||
return () => scaleValue.removeAllListeners()
|
return () => scaleValue.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
const cancelLongPressHandle = () => {
|
|
||||||
longPressHandlerRef && clearTimeout(longPressHandlerRef)
|
|
||||||
}
|
|
||||||
|
|
||||||
const panResponder = PanResponder.create({
|
const panResponder = PanResponder.create({
|
||||||
onStartShouldSetPanResponder: () => true,
|
onStartShouldSetPanResponder: () => true,
|
||||||
onStartShouldSetPanResponderCapture: () => true,
|
onStartShouldSetPanResponderCapture: () => true,
|
||||||
|
@ -173,8 +158,6 @@ const usePanResponder = ({
|
||||||
if (gestureState.numberActiveTouches > 1) {
|
if (gestureState.numberActiveTouches > 1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
longPressHandlerRef = setTimeout(onLongPress, delayLongPress)
|
|
||||||
},
|
},
|
||||||
onPanResponderStart: (
|
onPanResponderStart: (
|
||||||
event: GestureResponderEvent,
|
event: GestureResponderEvent,
|
||||||
|
@ -194,7 +177,7 @@ const usePanResponder = ({
|
||||||
lastTapTS && tapTS - lastTapTS < DOUBLE_TAP_DELAY,
|
lastTapTS && tapTS - lastTapTS < DOUBLE_TAP_DELAY,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (doubleTapToZoomEnabled && isDoubleTapPerformed) {
|
if (isDoubleTapPerformed) {
|
||||||
let nextScale = initialScale
|
let nextScale = initialScale
|
||||||
let nextTranslate = initialTranslate
|
let nextTranslate = initialTranslate
|
||||||
|
|
||||||
|
@ -241,15 +224,8 @@ const usePanResponder = ({
|
||||||
event: GestureResponderEvent,
|
event: GestureResponderEvent,
|
||||||
gestureState: PanResponderGestureState,
|
gestureState: PanResponderGestureState,
|
||||||
) => {
|
) => {
|
||||||
const {dx, dy} = gestureState
|
|
||||||
|
|
||||||
if (Math.abs(dx) >= meaningfulShift || Math.abs(dy) >= meaningfulShift) {
|
|
||||||
cancelLongPressHandle()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't need to handle move because double tap in progress (was handled in onStart)
|
// Don't need to handle move because double tap in progress (was handled in onStart)
|
||||||
if (doubleTapToZoomEnabled && isDoubleTapPerformed) {
|
if (isDoubleTapPerformed) {
|
||||||
cancelLongPressHandle()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,8 +243,6 @@ const usePanResponder = ({
|
||||||
numberInitialTouches === 2 && gestureState.numberActiveTouches === 2
|
numberInitialTouches === 2 && gestureState.numberActiveTouches === 2
|
||||||
|
|
||||||
if (isPinchGesture) {
|
if (isPinchGesture) {
|
||||||
cancelLongPressHandle()
|
|
||||||
|
|
||||||
const initialDistance = getDistanceBetweenTouches(initialTouches)
|
const initialDistance = getDistanceBetweenTouches(initialTouches)
|
||||||
const currentDistance = getDistanceBetweenTouches(
|
const currentDistance = getDistanceBetweenTouches(
|
||||||
event.nativeEvent.touches,
|
event.nativeEvent.touches,
|
||||||
|
@ -315,7 +289,7 @@ const usePanResponder = ({
|
||||||
|
|
||||||
if (isTapGesture && currentScale > initialScale) {
|
if (isTapGesture && currentScale > initialScale) {
|
||||||
const {x, y} = currentTranslate
|
const {x, y} = currentTranslate
|
||||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
||||||
const {dx, dy} = gestureState
|
const {dx, dy} = gestureState
|
||||||
const [topBound, leftBound, bottomBound, rightBound] =
|
const [topBound, leftBound, bottomBound, rightBound] =
|
||||||
getBounds(currentScale)
|
getBounds(currentScale)
|
||||||
|
@ -360,8 +334,6 @@ const usePanResponder = ({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPanResponderRelease: () => {
|
onPanResponderRelease: () => {
|
||||||
cancelLongPressHandle()
|
|
||||||
|
|
||||||
if (isDoubleTapPerformed) {
|
if (isDoubleTapPerformed) {
|
||||||
isDoubleTapPerformed = false
|
isDoubleTapPerformed = false
|
||||||
}
|
}
|
||||||
|
@ -428,4 +400,24 @@ const usePanResponder = ({
|
||||||
return [panResponder.panHandlers, scaleValue, translateValue]
|
return [panResponder.panHandlers, scaleValue, translateValue]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getImageDimensionsByTranslate = (
|
||||||
|
translate: Position,
|
||||||
|
screen: {width: number; height: number},
|
||||||
|
): {width: number; height: number} => ({
|
||||||
|
width: screen.width - translate.x * 2,
|
||||||
|
height: screen.height - translate.y * 2,
|
||||||
|
})
|
||||||
|
|
||||||
|
const getDistanceBetweenTouches = (touches: NativeTouchEvent[]): number => {
|
||||||
|
const [a, b] = touches
|
||||||
|
|
||||||
|
if (a == null || b == null) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.sqrt(
|
||||||
|
Math.pow(a.pageX - b.pageX, 2) + Math.pow(a.pageY - b.pageY, 2),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default usePanResponder
|
export default usePanResponder
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) JOB TODAY S.A. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {useState} from 'react'
|
|
||||||
|
|
||||||
const useRequestClose = (onRequestClose: () => void) => {
|
|
||||||
const [opacity, setOpacity] = useState(1)
|
|
||||||
|
|
||||||
return [
|
|
||||||
opacity,
|
|
||||||
() => {
|
|
||||||
setOpacity(0)
|
|
||||||
onRequestClose()
|
|
||||||
setTimeout(() => setOpacity(1), 0)
|
|
||||||
},
|
|
||||||
] as const
|
|
||||||
}
|
|
||||||
|
|
||||||
export default useRequestClose
|
|
|
@ -12,12 +12,14 @@ import React, {
|
||||||
ComponentType,
|
ComponentType,
|
||||||
useCallback,
|
useCallback,
|
||||||
useRef,
|
useRef,
|
||||||
useEffect,
|
|
||||||
useMemo,
|
useMemo,
|
||||||
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {
|
import {
|
||||||
Animated,
|
Animated,
|
||||||
Dimensions,
|
Dimensions,
|
||||||
|
NativeSyntheticEvent,
|
||||||
|
NativeScrollEvent,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
View,
|
View,
|
||||||
VirtualizedList,
|
VirtualizedList,
|
||||||
|
@ -29,9 +31,6 @@ import {ModalsContainer} from '../../modals/Modal'
|
||||||
import ImageItem from './components/ImageItem/ImageItem'
|
import ImageItem from './components/ImageItem/ImageItem'
|
||||||
import ImageDefaultHeader from './components/ImageDefaultHeader'
|
import ImageDefaultHeader from './components/ImageDefaultHeader'
|
||||||
|
|
||||||
import useAnimatedComponents from './hooks/useAnimatedComponents'
|
|
||||||
import useImageIndexChange from './hooks/useImageIndexChange'
|
|
||||||
import useRequestClose from './hooks/useRequestClose'
|
|
||||||
import {ImageSource} from './@types'
|
import {ImageSource} from './@types'
|
||||||
import {Edge, SafeAreaView} from 'react-native-safe-area-context'
|
import {Edge, SafeAreaView} from 'react-native-safe-area-context'
|
||||||
|
|
||||||
|
@ -41,22 +40,21 @@ type Props = {
|
||||||
imageIndex: number
|
imageIndex: number
|
||||||
visible: boolean
|
visible: boolean
|
||||||
onRequestClose: () => void
|
onRequestClose: () => void
|
||||||
onLongPress?: (image: ImageSource) => void
|
|
||||||
onImageIndexChange?: (imageIndex: number) => void
|
|
||||||
presentationStyle?: ModalProps['presentationStyle']
|
presentationStyle?: ModalProps['presentationStyle']
|
||||||
animationType?: ModalProps['animationType']
|
animationType?: ModalProps['animationType']
|
||||||
backgroundColor?: string
|
backgroundColor?: string
|
||||||
swipeToCloseEnabled?: boolean
|
|
||||||
doubleTapToZoomEnabled?: boolean
|
|
||||||
delayLongPress?: number
|
|
||||||
HeaderComponent?: ComponentType<{imageIndex: number}>
|
HeaderComponent?: ComponentType<{imageIndex: number}>
|
||||||
FooterComponent?: ComponentType<{imageIndex: number}>
|
FooterComponent?: ComponentType<{imageIndex: number}>
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_BG_COLOR = '#000'
|
const DEFAULT_BG_COLOR = '#000'
|
||||||
const DEFAULT_DELAY_LONG_PRESS = 800
|
|
||||||
const SCREEN = Dimensions.get('screen')
|
const SCREEN = Dimensions.get('screen')
|
||||||
const SCREEN_WIDTH = SCREEN.width
|
const SCREEN_WIDTH = SCREEN.width
|
||||||
|
const INITIAL_POSITION = {x: 0, y: 0}
|
||||||
|
const ANIMATION_CONFIG = {
|
||||||
|
duration: 200,
|
||||||
|
useNativeDriver: true,
|
||||||
|
}
|
||||||
|
|
||||||
function ImageViewing({
|
function ImageViewing({
|
||||||
images,
|
images,
|
||||||
|
@ -64,35 +62,63 @@ function ImageViewing({
|
||||||
imageIndex,
|
imageIndex,
|
||||||
visible,
|
visible,
|
||||||
onRequestClose,
|
onRequestClose,
|
||||||
onLongPress = () => {},
|
|
||||||
onImageIndexChange,
|
|
||||||
backgroundColor = DEFAULT_BG_COLOR,
|
backgroundColor = DEFAULT_BG_COLOR,
|
||||||
swipeToCloseEnabled,
|
|
||||||
doubleTapToZoomEnabled,
|
|
||||||
delayLongPress = DEFAULT_DELAY_LONG_PRESS,
|
|
||||||
HeaderComponent,
|
HeaderComponent,
|
||||||
FooterComponent,
|
FooterComponent,
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const imageList = useRef<VirtualizedList<ImageSource>>(null)
|
const imageList = useRef<VirtualizedList<ImageSource>>(null)
|
||||||
const [opacity, onRequestCloseEnhanced] = useRequestClose(onRequestClose)
|
const [opacity, setOpacity] = useState(1)
|
||||||
const [currentImageIndex, onScroll] = useImageIndexChange(imageIndex, SCREEN)
|
const [currentImageIndex, setImageIndex] = useState(imageIndex)
|
||||||
const [headerTransform, footerTransform, toggleBarsVisible] =
|
|
||||||
useAnimatedComponents()
|
|
||||||
|
|
||||||
useEffect(() => {
|
// TODO: It's not valid to reinitialize Animated values during render.
|
||||||
if (onImageIndexChange) {
|
// This is a bug.
|
||||||
onImageIndexChange(currentImageIndex)
|
const headerTranslate = new Animated.ValueXY(INITIAL_POSITION)
|
||||||
|
const footerTranslate = new Animated.ValueXY(INITIAL_POSITION)
|
||||||
|
|
||||||
|
const toggleBarsVisible = (isVisible: boolean) => {
|
||||||
|
if (isVisible) {
|
||||||
|
Animated.parallel([
|
||||||
|
Animated.timing(headerTranslate.y, {...ANIMATION_CONFIG, toValue: 0}),
|
||||||
|
Animated.timing(footerTranslate.y, {...ANIMATION_CONFIG, toValue: 0}),
|
||||||
|
]).start()
|
||||||
|
} else {
|
||||||
|
Animated.parallel([
|
||||||
|
Animated.timing(headerTranslate.y, {
|
||||||
|
...ANIMATION_CONFIG,
|
||||||
|
toValue: -300,
|
||||||
|
}),
|
||||||
|
Animated.timing(footerTranslate.y, {
|
||||||
|
...ANIMATION_CONFIG,
|
||||||
|
toValue: 300,
|
||||||
|
}),
|
||||||
|
]).start()
|
||||||
}
|
}
|
||||||
}, [currentImageIndex, onImageIndexChange])
|
}
|
||||||
|
|
||||||
const onZoom = useCallback(
|
const onRequestCloseEnhanced = () => {
|
||||||
(isScaled: boolean) => {
|
setOpacity(0)
|
||||||
// @ts-ignore
|
onRequestClose()
|
||||||
imageList?.current?.setNativeProps({scrollEnabled: !isScaled})
|
setTimeout(() => setOpacity(1), 0)
|
||||||
toggleBarsVisible(!isScaled)
|
}
|
||||||
},
|
|
||||||
[toggleBarsVisible],
|
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
||||||
)
|
const {
|
||||||
|
nativeEvent: {
|
||||||
|
contentOffset: {x: scrollX},
|
||||||
|
},
|
||||||
|
} = event
|
||||||
|
|
||||||
|
if (SCREEN.width) {
|
||||||
|
const nextIndex = Math.round(scrollX / SCREEN.width)
|
||||||
|
setImageIndex(nextIndex < 0 ? 0 : nextIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onZoom = (isScaled: boolean) => {
|
||||||
|
// @ts-ignore
|
||||||
|
imageList?.current?.setNativeProps({scrollEnabled: !isScaled})
|
||||||
|
toggleBarsVisible(!isScaled)
|
||||||
|
}
|
||||||
|
|
||||||
const edges = useMemo(() => {
|
const edges = useMemo(() => {
|
||||||
if (Platform.OS === 'android') {
|
if (Platform.OS === 'android') {
|
||||||
|
@ -111,6 +137,8 @@ function ImageViewing({
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const headerTransform = headerTranslate.getTranslateTransform()
|
||||||
|
const footerTransform = footerTranslate.getTranslateTransform()
|
||||||
return (
|
return (
|
||||||
<SafeAreaView
|
<SafeAreaView
|
||||||
style={styles.screen}
|
style={styles.screen}
|
||||||
|
@ -148,10 +176,6 @@ function ImageViewing({
|
||||||
onZoom={onZoom}
|
onZoom={onZoom}
|
||||||
imageSrc={imageSrc}
|
imageSrc={imageSrc}
|
||||||
onRequestClose={onRequestCloseEnhanced}
|
onRequestClose={onRequestCloseEnhanced}
|
||||||
onLongPress={onLongPress}
|
|
||||||
delayLongPress={delayLongPress}
|
|
||||||
swipeToCloseEnabled={swipeToCloseEnabled}
|
|
||||||
doubleTapToZoomEnabled={doubleTapToZoomEnabled}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
onMomentumScrollEnd={onScroll}
|
onMomentumScrollEnd={onScroll}
|
||||||
|
|
|
@ -6,42 +6,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Animated, NativeTouchEvent} from 'react-native'
|
import {Animated} from 'react-native'
|
||||||
import {Dimensions, Position} from './@types'
|
import {Dimensions, Position} from './@types'
|
||||||
|
|
||||||
type CacheStorageItem = {key: string; value: any}
|
|
||||||
|
|
||||||
export const createCache = (cacheSize: number) => ({
|
|
||||||
_storage: [] as CacheStorageItem[],
|
|
||||||
get(key: string): any {
|
|
||||||
const {value} =
|
|
||||||
this._storage.find(({key: storageKey}) => storageKey === key) || {}
|
|
||||||
|
|
||||||
return value
|
|
||||||
},
|
|
||||||
set(key: string, value: any) {
|
|
||||||
if (this._storage.length >= cacheSize) {
|
|
||||||
this._storage.shift()
|
|
||||||
}
|
|
||||||
|
|
||||||
this._storage.push({key, value})
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const splitArrayIntoBatches = (arr: any[], batchSize: number): any[] =>
|
|
||||||
arr.reduce((result, item) => {
|
|
||||||
const batch = result.pop() || []
|
|
||||||
|
|
||||||
if (batch.length < batchSize) {
|
|
||||||
batch.push(item)
|
|
||||||
result.push(batch)
|
|
||||||
} else {
|
|
||||||
result.push(batch, [item])
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
export const getImageTransform = (
|
export const getImageTransform = (
|
||||||
image: Dimensions | null,
|
image: Dimensions | null,
|
||||||
screen: Dimensions,
|
screen: Dimensions,
|
||||||
|
@ -97,43 +64,3 @@ export const getImageTranslate = (
|
||||||
y: getTranslateForAxis('y'),
|
y: getTranslateForAxis('y'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getImageDimensionsByTranslate = (
|
|
||||||
translate: Position,
|
|
||||||
screen: Dimensions,
|
|
||||||
): Dimensions => ({
|
|
||||||
width: screen.width - translate.x * 2,
|
|
||||||
height: screen.height - translate.y * 2,
|
|
||||||
})
|
|
||||||
|
|
||||||
export const getImageTranslateForScale = (
|
|
||||||
currentTranslate: Position,
|
|
||||||
targetScale: number,
|
|
||||||
screen: Dimensions,
|
|
||||||
): Position => {
|
|
||||||
const {width, height} = getImageDimensionsByTranslate(
|
|
||||||
currentTranslate,
|
|
||||||
screen,
|
|
||||||
)
|
|
||||||
|
|
||||||
const targetImageDimensions = {
|
|
||||||
width: width * targetScale,
|
|
||||||
height: height * targetScale,
|
|
||||||
}
|
|
||||||
|
|
||||||
return getImageTranslate(targetImageDimensions, screen)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getDistanceBetweenTouches = (
|
|
||||||
touches: NativeTouchEvent[],
|
|
||||||
): number => {
|
|
||||||
const [a, b] = touches
|
|
||||||
|
|
||||||
if (a == null || b == null) {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return Math.sqrt(
|
|
||||||
Math.pow(a.pageX - b.pageX, 2) + Math.pow(a.pageY - b.pageY, 2),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,94 +15,10 @@ import * as MediaLibrary from 'expo-media-library'
|
||||||
|
|
||||||
export const Lightbox = observer(function Lightbox() {
|
export const Lightbox = observer(function Lightbox() {
|
||||||
const store = useStores()
|
const store = useStores()
|
||||||
const [isAltExpanded, setAltExpanded] = React.useState(false)
|
|
||||||
const [permissionResponse, requestPermission] = MediaLibrary.usePermissions()
|
|
||||||
|
|
||||||
const onClose = React.useCallback(() => {
|
const onClose = React.useCallback(() => {
|
||||||
store.shell.closeLightbox()
|
store.shell.closeLightbox()
|
||||||
}, [store])
|
}, [store])
|
||||||
|
|
||||||
const saveImageToAlbumWithToasts = React.useCallback(
|
|
||||||
async (uri: string) => {
|
|
||||||
if (!permissionResponse || permissionResponse.granted === false) {
|
|
||||||
Toast.show('Permission to access camera roll is required.')
|
|
||||||
if (permissionResponse?.canAskAgain) {
|
|
||||||
requestPermission()
|
|
||||||
} else {
|
|
||||||
Toast.show(
|
|
||||||
'Permission to access camera roll was denied. Please enable it in your system settings.',
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await saveImageToMediaLibrary({uri})
|
|
||||||
Toast.show('Saved to your camera roll.')
|
|
||||||
} catch (e: any) {
|
|
||||||
Toast.show(`Failed to save image: ${String(e)}`)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[permissionResponse, requestPermission],
|
|
||||||
)
|
|
||||||
|
|
||||||
const LightboxFooter = React.useCallback(
|
|
||||||
({imageIndex}: {imageIndex: number}) => {
|
|
||||||
const lightbox = store.shell.activeLightbox
|
|
||||||
if (!lightbox) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
let altText = ''
|
|
||||||
let uri = ''
|
|
||||||
if (lightbox.name === 'images') {
|
|
||||||
const opts = lightbox as models.ImagesLightbox
|
|
||||||
uri = opts.images[imageIndex].uri
|
|
||||||
altText = opts.images[imageIndex].alt || ''
|
|
||||||
} else if (lightbox.name === 'profile-image') {
|
|
||||||
const opts = lightbox as models.ProfileImageLightbox
|
|
||||||
uri = opts.profileView.avatar || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<View style={[styles.footer]}>
|
|
||||||
{altText ? (
|
|
||||||
<Pressable
|
|
||||||
onPress={() => setAltExpanded(!isAltExpanded)}
|
|
||||||
accessibilityRole="button">
|
|
||||||
<Text
|
|
||||||
style={[s.gray3, styles.footerText]}
|
|
||||||
numberOfLines={isAltExpanded ? undefined : 3}>
|
|
||||||
{altText}
|
|
||||||
</Text>
|
|
||||||
</Pressable>
|
|
||||||
) : null}
|
|
||||||
<View style={styles.footerBtns}>
|
|
||||||
<Button
|
|
||||||
type="primary-outline"
|
|
||||||
style={styles.footerBtn}
|
|
||||||
onPress={() => saveImageToAlbumWithToasts(uri)}>
|
|
||||||
<FontAwesomeIcon icon={['far', 'floppy-disk']} style={s.white} />
|
|
||||||
<Text type="xl" style={s.white}>
|
|
||||||
Save
|
|
||||||
</Text>
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="primary-outline"
|
|
||||||
style={styles.footerBtn}
|
|
||||||
onPress={() => shareImageModal({uri})}>
|
|
||||||
<FontAwesomeIcon icon="arrow-up-from-bracket" style={s.white} />
|
|
||||||
<Text type="xl" style={s.white}>
|
|
||||||
Share
|
|
||||||
</Text>
|
|
||||||
</Button>
|
|
||||||
</View>
|
|
||||||
</View>
|
|
||||||
)
|
|
||||||
},
|
|
||||||
[store.shell.activeLightbox, isAltExpanded, saveImageToAlbumWithToasts],
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!store.shell.activeLightbox) {
|
if (!store.shell.activeLightbox) {
|
||||||
return null
|
return null
|
||||||
} else if (store.shell.activeLightbox.name === 'profile-image') {
|
} else if (store.shell.activeLightbox.name === 'profile-image') {
|
||||||
|
@ -132,6 +48,92 @@ export const Lightbox = observer(function Lightbox() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const LightboxFooter = observer(function LightboxFooter({
|
||||||
|
imageIndex,
|
||||||
|
}: {
|
||||||
|
imageIndex: number
|
||||||
|
}) {
|
||||||
|
const store = useStores()
|
||||||
|
const [isAltExpanded, setAltExpanded] = React.useState(false)
|
||||||
|
const [permissionResponse, requestPermission] = MediaLibrary.usePermissions()
|
||||||
|
|
||||||
|
const saveImageToAlbumWithToasts = React.useCallback(
|
||||||
|
async (uri: string) => {
|
||||||
|
if (!permissionResponse || permissionResponse.granted === false) {
|
||||||
|
Toast.show('Permission to access camera roll is required.')
|
||||||
|
if (permissionResponse?.canAskAgain) {
|
||||||
|
requestPermission()
|
||||||
|
} else {
|
||||||
|
Toast.show(
|
||||||
|
'Permission to access camera roll was denied. Please enable it in your system settings.',
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await saveImageToMediaLibrary({uri})
|
||||||
|
Toast.show('Saved to your camera roll.')
|
||||||
|
} catch (e: any) {
|
||||||
|
Toast.show(`Failed to save image: ${String(e)}`)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[permissionResponse, requestPermission],
|
||||||
|
)
|
||||||
|
|
||||||
|
const lightbox = store.shell.activeLightbox
|
||||||
|
if (!lightbox) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
let altText = ''
|
||||||
|
let uri = ''
|
||||||
|
if (lightbox.name === 'images') {
|
||||||
|
const opts = lightbox as models.ImagesLightbox
|
||||||
|
uri = opts.images[imageIndex].uri
|
||||||
|
altText = opts.images[imageIndex].alt || ''
|
||||||
|
} else if (lightbox.name === 'profile-image') {
|
||||||
|
const opts = lightbox as models.ProfileImageLightbox
|
||||||
|
uri = opts.profileView.avatar || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={[styles.footer]}>
|
||||||
|
{altText ? (
|
||||||
|
<Pressable
|
||||||
|
onPress={() => setAltExpanded(!isAltExpanded)}
|
||||||
|
accessibilityRole="button">
|
||||||
|
<Text
|
||||||
|
style={[s.gray3, styles.footerText]}
|
||||||
|
numberOfLines={isAltExpanded ? undefined : 3}>
|
||||||
|
{altText}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
) : null}
|
||||||
|
<View style={styles.footerBtns}>
|
||||||
|
<Button
|
||||||
|
type="primary-outline"
|
||||||
|
style={styles.footerBtn}
|
||||||
|
onPress={() => saveImageToAlbumWithToasts(uri)}>
|
||||||
|
<FontAwesomeIcon icon={['far', 'floppy-disk']} style={s.white} />
|
||||||
|
<Text type="xl" style={s.white}>
|
||||||
|
Save
|
||||||
|
</Text>
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="primary-outline"
|
||||||
|
style={styles.footerBtn}
|
||||||
|
onPress={() => shareImageModal({uri})}>
|
||||||
|
<FontAwesomeIcon icon="arrow-up-from-bracket" style={s.white} />
|
||||||
|
<Text type="xl" style={s.white}>
|
||||||
|
Share
|
||||||
|
</Text>
|
||||||
|
</Button>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
footer: {
|
footer: {
|
||||||
paddingTop: 16,
|
paddingTop: 16,
|
||||||
|
|
Loading…
Reference in New Issue