react-native-gesture-image-viewer 2.0.0-beta.2 → 2.0.0-beta.3
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';
|
|
4
4
|
import { registry } from "./GestureViewerRegistry.js";
|
|
5
5
|
/**
|
|
6
6
|
* Hook to control the gesture viewer programmatically.
|
|
@@ -55,42 +55,72 @@ import { registry } from "./GestureViewerRegistry.js";
|
|
|
55
55
|
* ```
|
|
56
56
|
*/
|
|
57
57
|
export const useGestureViewerController = (id = 'default') => {
|
|
58
|
-
const
|
|
58
|
+
const managerRef = useRef(null);
|
|
59
|
+
const stateRef = useRef({
|
|
59
60
|
currentIndex: 0,
|
|
60
61
|
totalCount: 0
|
|
61
62
|
});
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
const updateState = useCallback((newState, onStoreChange) => {
|
|
64
|
+
if (stateRef.current.currentIndex !== newState.currentIndex || stateRef.current.totalCount !== newState.totalCount) {
|
|
65
|
+
stateRef.current = {
|
|
66
|
+
currentIndex: newState.currentIndex,
|
|
67
|
+
totalCount: newState.totalCount
|
|
68
|
+
};
|
|
69
|
+
onStoreChange();
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
const subscribe = useCallback(onStoreChange => {
|
|
73
|
+
let unsubscribeFromManager = null;
|
|
74
|
+
const unsubscribeFromRegistry = registry.subscribeToManager(id, newManager => {
|
|
75
|
+
unsubscribeFromManager?.();
|
|
76
|
+
unsubscribeFromManager = null;
|
|
77
|
+
managerRef.current = newManager;
|
|
69
78
|
if (newManager) {
|
|
70
|
-
|
|
71
|
-
|
|
79
|
+
unsubscribeFromManager = newManager.subscribe(newState => {
|
|
80
|
+
updateState(newState, onStoreChange);
|
|
81
|
+
});
|
|
82
|
+
updateState(newManager.getState(), onStoreChange);
|
|
72
83
|
return;
|
|
73
84
|
}
|
|
74
|
-
|
|
85
|
+
updateState({
|
|
75
86
|
currentIndex: 0,
|
|
76
87
|
totalCount: 0
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
const unsubscribeFromRegistry = registry.subscribeToManager(id, handleManagerChange);
|
|
88
|
+
}, onStoreChange);
|
|
89
|
+
});
|
|
80
90
|
return () => {
|
|
81
91
|
unsubscribeFromRegistry();
|
|
82
|
-
|
|
92
|
+
unsubscribeFromManager?.();
|
|
83
93
|
};
|
|
84
|
-
}, [id]);
|
|
85
|
-
const
|
|
94
|
+
}, [id, updateState]);
|
|
95
|
+
const getSnapshot = useCallback(() => {
|
|
96
|
+
return stateRef.current;
|
|
97
|
+
}, []);
|
|
98
|
+
const state = useSyncExternalStore(subscribe, getSnapshot);
|
|
99
|
+
const controller = useMemo(() => ({
|
|
100
|
+
goToIndex: index => {
|
|
101
|
+
managerRef.current?.goToIndex(index);
|
|
102
|
+
},
|
|
103
|
+
goToPrevious: () => {
|
|
104
|
+
managerRef.current?.goToPrevious();
|
|
105
|
+
},
|
|
106
|
+
goToNext: () => {
|
|
107
|
+
managerRef.current?.goToNext();
|
|
108
|
+
},
|
|
109
|
+
zoomIn: multiplier => {
|
|
110
|
+
managerRef.current?.zoomIn(multiplier);
|
|
111
|
+
},
|
|
112
|
+
zoomOut: multiplier => {
|
|
113
|
+
managerRef.current?.zoomOut(multiplier);
|
|
114
|
+
},
|
|
115
|
+
resetZoom: scale => {
|
|
116
|
+
managerRef.current?.resetZoom(scale);
|
|
117
|
+
},
|
|
118
|
+
rotate: (angle, clockwise) => {
|
|
119
|
+
managerRef.current?.rotate(angle, clockwise);
|
|
120
|
+
}
|
|
121
|
+
}), []);
|
|
86
122
|
return {
|
|
87
|
-
|
|
88
|
-
goToPrevious: manager?.goToPrevious || noopFunction,
|
|
89
|
-
goToNext: manager?.goToNext || noopFunction,
|
|
90
|
-
zoomIn: manager?.zoomIn || noopFunction,
|
|
91
|
-
zoomOut: manager?.zoomOut || noopFunction,
|
|
92
|
-
resetZoom: manager?.resetZoom || noopFunction,
|
|
93
|
-
rotate: manager?.rotate || noopFunction,
|
|
123
|
+
...controller,
|
|
94
124
|
...state
|
|
95
125
|
};
|
|
96
126
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["useCallback","useMemo","useRef","useSyncExternalStore","registry","useGestureViewerController","id","managerRef","stateRef","currentIndex","totalCount","updateState","newState","onStoreChange","current","subscribe","unsubscribeFromManager","unsubscribeFromRegistry","subscribeToManager","newManager","getState","getSnapshot","state","controller","goToIndex","index","goToPrevious","goToNext","zoomIn","multiplier","zoomOut","resetZoom","scale","rotate","angle","clockwise"],"sourceRoot":"../../src","sources":["useGestureViewerController.ts"],"mappings":";;AAAA,SAASA,WAAW,EAAEC,OAAO,EAAEC,MAAM,EAAEC,oBAAoB,QAAQ,OAAO;AAE1E,SAASC,QAAQ,QAAQ,4BAAyB;AAGlD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,0BAA0B,GAAGA,CAACC,EAAE,GAAG,SAAS,KAA8B;EACrF,MAAMC,UAAU,GAAGL,MAAM,CAA8B,IAAI,CAAC;EAC5D,MAAMM,QAAQ,GAAGN,MAAM,CAA+B;IAAEO,YAAY,EAAE,CAAC;IAAEC,UAAU,EAAE;EAAE,CAAC,CAAC;EAEzF,MAAMC,WAAW,GAAGX,WAAW,CAAC,CAACY,QAAsC,EAAEC,aA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AACY,SAAS,EAAEM,WAAW,CAAC;EAE1D,MAAME,UAAU,GAAGtB,OAAO,CACxB,OAAO;IACLuB,SAAS,EAAGC,KAAK,IAAK;MACpBlB,UAAU,CAACO,OAAO,EAAEU,SAAS,CAACC,KAAK,CAAC;IACtC,CAAC;IACDC,YAAY,EAAEA,CAAA,KAAM;MAClBnB,UAAU,CAACO,OAAO,EAAEY,YAAY,CAAC,CAAC;IACpC,CAAC;IACDC,QAAQ,EAAEA,CAAA,KAAM;MACdpB,UAAU,CAACO,OAAO,EAAEa,QAAQ,CAAC,CAAC;IAChC,CAAC;IACDC,MAAM,EAAGC,UAAU,IAAK;MACtBtB,UAAU,CAACO,OAAO,EAAEc,MAAM,CAACC,UAAU,CAAC;IACxC,CAAC;IACDC,OAAO,EAAGD,UAAU,IAAK;MACvBtB,UAAU,CAACO,OAAO,EAAEgB,OAAO,CAACD,UAAU,CAAC;IACzC,CAAC;IACDE,SAAS,EAAGC,KAAK,IAAK;MACpBzB,UAAU,CAACO,OAAO,EAAEiB,SAAS,CAACC,KAAK,CAAC;IACtC,CAAC;IACDC,MAAM,EAAEA,CAACC,KAAK,EAAEC,SAAS,KAAK;MAC5B5B,UAAU,CAACO,OAAO,EAAEmB,MAAM,CAACC,KAAK,EAAEC,SAAS,CAAC;IAC9C;EACF,CAAC,CAAC,EACF,EACF,CAAC;EAED,OAAO;IACL,GAAGZ,UAAU;IACb,GAAGD;EACL,CAAC;AACH,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGestureViewerController.d.ts","sourceRoot":"","sources":["../../../src/useGestureViewerController.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAgC,MAAM,SAAS,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,eAAO,MAAM,0BAA0B,GAAI,WAAc,KAAG,
|
|
1
|
+
{"version":3,"file":"useGestureViewerController.d.ts","sourceRoot":"","sources":["../../../src/useGestureViewerController.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uBAAuB,EAAgC,MAAM,SAAS,CAAC;AAErF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,eAAO,MAAM,0BAA0B,GAAI,WAAc,KAAG,uBAmF3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-gesture-image-viewer",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
4
|
"description": "🖼️ A highly customizable and easy-to-use React Native image viewer with gesture support and external controls",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useMemo, useRef, useSyncExternalStore } from 'react';
|
|
2
2
|
import type GestureViewerManager from './GestureViewerManager';
|
|
3
3
|
import { registry } from './GestureViewerRegistry';
|
|
4
4
|
import type { GestureViewerController, GestureViewerControllerState } from './types';
|
|
@@ -56,48 +56,86 @@ import type { GestureViewerController, GestureViewerControllerState } from './ty
|
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
58
|
export const useGestureViewerController = (id = 'default'): GestureViewerController => {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
totalCount: 0,
|
|
62
|
-
});
|
|
59
|
+
const managerRef = useRef<GestureViewerManager | null>(null);
|
|
60
|
+
const stateRef = useRef<GestureViewerControllerState>({ currentIndex: 0, totalCount: 0 });
|
|
63
61
|
|
|
64
|
-
const
|
|
65
|
-
|
|
62
|
+
const updateState = useCallback((newState: GestureViewerControllerState, onStoreChange: () => void) => {
|
|
63
|
+
if (
|
|
64
|
+
stateRef.current.currentIndex !== newState.currentIndex ||
|
|
65
|
+
stateRef.current.totalCount !== newState.totalCount
|
|
66
|
+
) {
|
|
67
|
+
stateRef.current = {
|
|
68
|
+
currentIndex: newState.currentIndex,
|
|
69
|
+
totalCount: newState.totalCount,
|
|
70
|
+
};
|
|
71
|
+
onStoreChange();
|
|
72
|
+
}
|
|
73
|
+
}, []);
|
|
66
74
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
unsubscribeRef.current = null;
|
|
75
|
+
const subscribe = useCallback(
|
|
76
|
+
(onStoreChange: () => void) => {
|
|
77
|
+
let unsubscribeFromManager: (() => void) | null = null;
|
|
71
78
|
|
|
72
|
-
|
|
79
|
+
const unsubscribeFromRegistry = registry.subscribeToManager(id, (newManager) => {
|
|
80
|
+
unsubscribeFromManager?.();
|
|
81
|
+
unsubscribeFromManager = null;
|
|
82
|
+
managerRef.current = newManager;
|
|
73
83
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
84
|
+
if (newManager) {
|
|
85
|
+
unsubscribeFromManager = newManager.subscribe((newState) => {
|
|
86
|
+
updateState(newState, onStoreChange);
|
|
87
|
+
});
|
|
79
88
|
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
updateState(newManager.getState(), onStoreChange);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
82
92
|
|
|
83
|
-
|
|
93
|
+
updateState({ currentIndex: 0, totalCount: 0 }, onStoreChange);
|
|
94
|
+
});
|
|
84
95
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
96
|
+
return () => {
|
|
97
|
+
unsubscribeFromRegistry();
|
|
98
|
+
unsubscribeFromManager?.();
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
[id, updateState],
|
|
102
|
+
);
|
|
90
103
|
|
|
91
|
-
const
|
|
104
|
+
const getSnapshot = useCallback(() => {
|
|
105
|
+
return stateRef.current;
|
|
106
|
+
}, []);
|
|
107
|
+
|
|
108
|
+
const state = useSyncExternalStore(subscribe, getSnapshot);
|
|
109
|
+
|
|
110
|
+
const controller = useMemo<Omit<GestureViewerController, 'currentIndex' | 'totalCount'>>(
|
|
111
|
+
() => ({
|
|
112
|
+
goToIndex: (index) => {
|
|
113
|
+
managerRef.current?.goToIndex(index);
|
|
114
|
+
},
|
|
115
|
+
goToPrevious: () => {
|
|
116
|
+
managerRef.current?.goToPrevious();
|
|
117
|
+
},
|
|
118
|
+
goToNext: () => {
|
|
119
|
+
managerRef.current?.goToNext();
|
|
120
|
+
},
|
|
121
|
+
zoomIn: (multiplier) => {
|
|
122
|
+
managerRef.current?.zoomIn(multiplier);
|
|
123
|
+
},
|
|
124
|
+
zoomOut: (multiplier) => {
|
|
125
|
+
managerRef.current?.zoomOut(multiplier);
|
|
126
|
+
},
|
|
127
|
+
resetZoom: (scale) => {
|
|
128
|
+
managerRef.current?.resetZoom(scale);
|
|
129
|
+
},
|
|
130
|
+
rotate: (angle, clockwise) => {
|
|
131
|
+
managerRef.current?.rotate(angle, clockwise);
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
[],
|
|
135
|
+
);
|
|
92
136
|
|
|
93
137
|
return {
|
|
94
|
-
|
|
95
|
-
goToPrevious: manager?.goToPrevious || noopFunction,
|
|
96
|
-
goToNext: manager?.goToNext || noopFunction,
|
|
97
|
-
zoomIn: manager?.zoomIn || noopFunction,
|
|
98
|
-
zoomOut: manager?.zoomOut || noopFunction,
|
|
99
|
-
resetZoom: manager?.resetZoom || noopFunction,
|
|
100
|
-
rotate: manager?.rotate || noopFunction,
|
|
138
|
+
...controller,
|
|
101
139
|
...state,
|
|
102
140
|
};
|
|
103
141
|
};
|