react-native-gesture-image-viewer 1.5.1 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -48,3 +48,56 @@ export const createBoundsConstraint =
48
48
  translateY: Math.max(-maxTranslateY, Math.min(maxTranslateY, translateY)),
49
49
  };
50
50
  };
51
+
52
+ export const createLoopData = <T>(dataRef: React.RefObject<T[]>, enableLoop: boolean): T[] => {
53
+ const data = dataRef.current;
54
+
55
+ if (!enableLoop || !data?.length || data.length <= 1) {
56
+ return data;
57
+ }
58
+
59
+ const lastItem = data[data.length - 1];
60
+ const firstItem = data[0];
61
+
62
+ if (lastItem === undefined || firstItem === undefined) {
63
+ return data;
64
+ }
65
+
66
+ return [lastItem, ...data, firstItem];
67
+ };
68
+
69
+ export const getLoopAdjustedIndex = (
70
+ scrollIndex: number,
71
+ originalDataLength: number,
72
+ enableLoop: boolean,
73
+ ): { realIndex: number; needsJump: boolean; jumpToIndex?: number } => {
74
+ if (!enableLoop || originalDataLength <= 1) {
75
+ return { realIndex: scrollIndex, needsJump: false };
76
+ }
77
+
78
+ if (scrollIndex === 0) {
79
+ return {
80
+ realIndex: originalDataLength - 1,
81
+ needsJump: true,
82
+ jumpToIndex: originalDataLength,
83
+ };
84
+ } else if (scrollIndex === originalDataLength + 1) {
85
+ return {
86
+ realIndex: 0,
87
+ needsJump: true,
88
+ jumpToIndex: 1,
89
+ };
90
+ }
91
+
92
+ return { realIndex: scrollIndex - 1, needsJump: false };
93
+ };
94
+
95
+ export const createScrollAction = (listRef: any, width: number) => ({
96
+ scrollTo: (index: number, animated: boolean) => {
97
+ if (listRef?.scrollToIndex) {
98
+ listRef.scrollToIndex({ index, animated });
99
+ } else if (listRef?.scrollTo) {
100
+ listRef.scrollTo({ x: index * width, animated });
101
+ }
102
+ },
103
+ });