UNPKG

react-native-gesture-image-viewer

Version:

🖼️ React Native Image Viewer - Reanimated-powered image gestures with full control

100 lines (98 loc) 2.61 kB
"use strict"; import { FlatList as RNFlatList, ScrollView as RNScrollView } from 'react-native'; import { FlatList as GestureFlatList, ScrollView as GestureScrollView } from 'react-native-gesture-handler'; export const isScrollViewLike = component => { return component === RNScrollView || component === GestureScrollView; }; export const isFlatListLike = component => { if (component === RNFlatList || component === GestureFlatList || isFlashListLike(component)) { return true; } return false; }; export const isFlashListLike = component => { try { const FlashList = require('@shopify/flash-list')?.FlashList; if (FlashList && component === FlashList) { return true; } } catch { // do nothing } return component?.displayName === 'FlashList' || component?.name === 'FlashList'; }; export const createBoundsConstraint = ({ width, height }) => ({ scale, translateX, translateY }) => { 'worklet'; if (scale <= 1) { return { translateX, translateY }; } const maxTranslateX = (width * scale - width) / 2; const maxTranslateY = (height * scale - height) / 2; return { translateX: Math.max(-maxTranslateX, Math.min(maxTranslateX, translateX)), translateY: Math.max(-maxTranslateY, Math.min(maxTranslateY, translateY)) }; }; export const createLoopData = (dataRef, enableLoop) => { const data = dataRef.current; if (!enableLoop || !data?.length || data.length <= 1) { return data; } const lastItem = data[data.length - 1]; const firstItem = data[0]; if (lastItem === undefined || firstItem === undefined) { return data; } return [lastItem, ...data, firstItem]; }; export const getLoopAdjustedIndex = (scrollIndex, originalDataLength, enableLoop) => { if (!enableLoop || originalDataLength <= 1) { return { realIndex: scrollIndex, needsJump: false }; } if (scrollIndex === 0) { return { realIndex: originalDataLength - 1, needsJump: true, jumpToIndex: originalDataLength }; } else if (scrollIndex === originalDataLength + 1) { return { realIndex: 0, needsJump: true, jumpToIndex: 1 }; } return { realIndex: scrollIndex - 1, needsJump: false }; }; export const createScrollAction = (listRef, width) => ({ scrollTo: (index, animated) => { if (listRef?.scrollToIndex) { listRef.scrollToIndex({ index, animated }); } else if (listRef?.scrollTo) { listRef.scrollTo({ x: index * width, animated }); } } }); //# sourceMappingURL=utils.js.map