@momo-kits/camerakit 0.0.51-rc.2 → 0.0.51

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.
@@ -0,0 +1,52 @@
1
+ import React, {forwardRef, useImperativeHandle, useRef} from 'react';
2
+
3
+ import {
4
+ findNodeHandle,
5
+ NativeModules,
6
+ requireNativeComponent,
7
+ } from 'react-native';
8
+ import {CameraApi} from './type';
9
+
10
+ let Module: any;
11
+ let NativeCamera: any;
12
+
13
+ if (NativeModules?.RNCameraKitModule) {
14
+ Module = NativeModules.RNCameraKitModule;
15
+ NativeCamera = requireNativeComponent('RNCameraManager');
16
+ } else {
17
+ Module = NativeModules.CameraKitModule;
18
+ NativeCamera = requireNativeComponent('CKCameraManager');
19
+ }
20
+
21
+ const Camera = forwardRef((props: any, ref) => {
22
+ const nativeRef = useRef();
23
+
24
+ useImperativeHandle<any, CameraApi>(ref, () => ({
25
+ capture: async (options = {}) => {
26
+ return await Module.capture(
27
+ options,
28
+ findNodeHandle(nativeRef.current ?? null)
29
+ );
30
+ },
31
+ startCamera: () => {
32
+ Module.startCamera(findNodeHandle(nativeRef.current ?? null));
33
+ },
34
+ stopCamera: () => {
35
+ Module.stopCamera(findNodeHandle(nativeRef.current ?? null));
36
+ },
37
+ readImageQRCode: (base64: string) => {
38
+ Module.readImageQRCode(base64, findNodeHandle(nativeRef.current ?? null));
39
+ },
40
+ }));
41
+
42
+ return (
43
+ <NativeCamera
44
+ style={{minWidth: 100, minHeight: 100}}
45
+ ref={nativeRef}
46
+ onOpenCamera={() => {}}
47
+ {...props}
48
+ />
49
+ );
50
+ });
51
+
52
+ export default Camera;
package/Camera.ios.tsx ADDED
@@ -0,0 +1,33 @@
1
+ import React from 'react';
2
+ import {NativeModules, requireNativeComponent} from 'react-native';
3
+ import {CameraApi} from './type';
4
+
5
+ let Module: any;
6
+ let NativeCamera: any;
7
+
8
+ if (NativeModules?.RNCameraKitManager) {
9
+ Module = NativeModules.RNCameraKitManager;
10
+ NativeCamera = requireNativeComponent('RNCameraKit');
11
+ } else {
12
+ Module = NativeModules.CameraKitManager;
13
+ NativeCamera = requireNativeComponent('CameraKit');
14
+ }
15
+
16
+ const Camera = React.forwardRef((props: any, ref: any) => {
17
+ React.useImperativeHandle<any, CameraApi>(ref, () => ({
18
+ capture: Module.capture,
19
+ startCamera: Module.startCamera,
20
+ stopCamera: Module.stopCamera,
21
+ readImageQRCode: Module.readImageQRCode,
22
+ }));
23
+
24
+ return (
25
+ <NativeCamera
26
+ style={{minWidth: 100, minHeight: 100}}
27
+ onOpenCamera={() => {}}
28
+ {...props}
29
+ />
30
+ );
31
+ });
32
+
33
+ export default Camera;
package/index.tsx ADDED
@@ -0,0 +1,96 @@
1
+ import React, {forwardRef, useImperativeHandle, useRef} from 'react';
2
+ import {Platform, StyleSheet, View} from 'react-native';
3
+ import styles from './styles';
4
+ import CameraIOS from './Camera.ios';
5
+ import CameraAndroid from './Camera.android';
6
+ import {CameraApi, CameraKitProps} from './type';
7
+
8
+ const Camera = Platform.OS === 'ios' ? CameraIOS : CameraAndroid;
9
+
10
+ const ThrottleEvent = 500;
11
+
12
+ const CameraKit = forwardRef(
13
+ (
14
+ {
15
+ onBarCodeRead,
16
+ onMRZ,
17
+ onImageCaptured,
18
+ type = 'back',
19
+ torchMode = 'off',
20
+ customFinder,
21
+ style,
22
+ }: CameraKitProps,
23
+ ref,
24
+ ) => {
25
+ const cameraRef = useRef<any>(null);
26
+ const lastEvent = useRef<any>(null);
27
+ const lastTime = useRef<number>(0);
28
+
29
+ useImperativeHandle<any, CameraApi>(ref, () => ({
30
+ capture,
31
+ startCamera,
32
+ stopCamera,
33
+ readImageQRCode,
34
+ }));
35
+
36
+ const readImageQRCode = (base64: string) => {
37
+ cameraRef.current?.readImageQRCode(base64);
38
+ };
39
+
40
+ const startCamera = () => {
41
+ cameraRef.current?.startCamera();
42
+ };
43
+
44
+ const stopCamera = () => {
45
+ cameraRef.current?.stopCamera();
46
+ };
47
+
48
+ const capture = async (options = {}) => {
49
+ let data;
50
+ try {
51
+ data = await cameraRef.current?.capture(options);
52
+ onImageCaptured?.({...data, url: data.uri});
53
+ } catch (e: any) {
54
+ onImageCaptured?.({error: e.message});
55
+ }
56
+ return data;
57
+ };
58
+
59
+ const onBarCodeReadHandler = ({nativeEvent}: any) => {
60
+ const value = JSON.stringify(nativeEvent);
61
+ const now = Date.now();
62
+ const outdated = !lastTime || now - lastTime.current > ThrottleEvent;
63
+ if (value && value === lastEvent.current && !outdated) {
64
+ return;
65
+ }
66
+ onBarCodeRead?.({data: nativeEvent?.codeStringValue});
67
+ lastEvent.current = value;
68
+ lastTime.current = Date.now();
69
+ };
70
+
71
+ const onMRZHandler = ({nativeEvent}: any) => {
72
+ onMRZ?.({data: nativeEvent?.docMRZ});
73
+ };
74
+
75
+ return (
76
+ <View style={[styles.container, style]}>
77
+ <Camera
78
+ ref={cameraRef}
79
+ style={styles.camera}
80
+ cameraType={type}
81
+ torchMode={torchMode}
82
+ focusMode={torchMode}
83
+ scanBarcode={!!onBarCodeRead}
84
+ scanMRZ={!!onMRZ}
85
+ onReadCode={onBarCodeReadHandler}
86
+ onMRZ={onMRZHandler}
87
+ />
88
+ {customFinder && (
89
+ <View style={StyleSheet.absoluteFill}>{customFinder()}</View>
90
+ )}
91
+ </View>
92
+ );
93
+ },
94
+ );
95
+
96
+ export {CameraKit};
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@momo-kits/camerakit",
3
- "version": "0.0.51-rc.2",
3
+ "version": "0.0.51",
4
4
  "private": false,
5
- "main": "index.js",
6
- "dependencies": {
7
- "@momo-platform/versions": "4.0.2"
8
- },
5
+ "main": "index.tsx",
9
6
  "peerDependencies": {
10
7
  "react": "16.9.0",
11
- "react-native": ">=0.55"
8
+ "react-native": ">=0.55",
9
+ "prop-types": "^15.7.2"
10
+ },
11
+ "devDependencies": {
12
+ "@momo-platform/versions": "4.1.11"
12
13
  },
13
- "devDependencies": {},
14
- "license": "MoMo"
15
- }
14
+ "license": "MoMo",
15
+ "dependencies": {}
16
+ }
package/publish.sh CHANGED
@@ -1,28 +1,23 @@
1
1
  #!/bin/bash
2
+
3
+ # Prepare dist files
2
4
  rm -rf dist
3
5
  mkdir dist
4
-
5
- cp . ./dist
6
-
7
- # GET VERSION from mck_package.json
8
- VERSIONSTRING=( v$(jq .version package.json) )
9
- VERSION=(${VERSIONSTRING//[\"]/})
10
- echo VERSION: $VERSION
11
-
12
- rsync -r --verbose --exclude '*.mdx' --exclude '*Demo.js' --exclude 'props-type.js' --exclude 'prop-types.js' ./* dist
13
-
14
- # #babel component to dist
15
- #babel ./dist -d dist --copy-files
16
-
17
- #copy option
18
- #cp -r ./src/ dist
19
-
20
-
21
- #npm login
22
- #publish dist to npm
6
+ rsync -r --exclude=/dist ./* dist
23
7
  cd dist
24
- npm publish --tag beta --access=public
8
+
9
+ if [ "$1" == "beta" ]; then # Publish beta
10
+ npm version $(npm view @momo-kits/camerakit@beta version)
11
+ npm version prepatch --preid=beta
12
+ npm publish --tag beta --access=public
13
+ else # Publish latest
14
+ npm version $(npm view @momo-kits/camerakit version)
15
+ npm version patch
16
+ npm publish --tag latest --access=public
17
+ fi
18
+ PACKAGE_NAME=$(npm pkg get name)
19
+ NEW_PACKAGE_VERSION=$(npm pkg get version)
20
+
21
+ # Clean up
25
22
  cd ..
26
23
  rm -rf dist
27
-
28
- #curl -X POST -H 'Content-Type: application/json' 'https://chat.googleapis.com/v1/spaces/AAAAbP8987c/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=UGSFRvk_oYb9uGsAgs31bVvMm6jDkmD8zihGm3eyaQA%3D&threadKey=JoaXTEYaNNkl' -d '{"text": "@momo-kits/camerakit new version release: '*"$VERSION"*' https://www.npmjs.com/package/@momo-kits/camerakit"}'
package/styles.ts ADDED
@@ -0,0 +1,12 @@
1
+ import {StyleSheet} from 'react-native';
2
+
3
+ export default StyleSheet.create({
4
+ container: {
5
+ overflow: 'hidden',
6
+ flex: 1,
7
+ },
8
+ camera: {
9
+ flex: 1,
10
+ backgroundColor: 'black',
11
+ },
12
+ });
package/type.ts ADDED
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+
3
+ export interface CameraKitProps {
4
+ onBarCodeRead?: (data: any) => void;
5
+ onMRZ?: (data: any) => void;
6
+ onImageCaptured?: (data: any) => void;
7
+ type?: 'front' | 'back';
8
+ torchMode?: 'on' | 'off' | 'auto';
9
+ zoomMode?: 'on' | 'off';
10
+ zoomRate?: number;
11
+ onOpenCamera?: (event: any) => void;
12
+ customFinder?: () => React.ReactNode;
13
+ style?: any;
14
+ }
15
+
16
+ export type CameraApi = {
17
+ capture: (option: any) => Promise<any>;
18
+ stopCamera: () => void;
19
+ startCamera: () => void;
20
+ readImageQRCode: (base64: string) => void;
21
+ };
22
+
23
+ export type CameraType = 'font' | 'back';
package/CameraKit.js DELETED
@@ -1,111 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { StyleSheet, View } from 'react-native';
4
- import { Camera } from 'react-native-camera-kit';
5
-
6
- const styles = StyleSheet.create({
7
- container: {
8
- overflow: 'hidden',
9
- },
10
- camera: {
11
- flex: 1,
12
- },
13
- });
14
-
15
- const EventThrottleMs = 250;
16
-
17
- class CameraKit extends React.Component {
18
- constructor(props) {
19
- super(props);
20
- this.camera = null;
21
- }
22
-
23
- readImageQRCode = (base64) => {
24
- this.camera?.readImageQRCode(base64);
25
- };
26
-
27
- startCamera = () => {
28
- this.camera?.startCamera();
29
- };
30
-
31
- stopCamera = () => {
32
- this.camera?.stopCamera();
33
- };
34
-
35
- async capture(options) {
36
- const data = await this.camera?.capture(options);
37
- const { onImageCaptured } = this.props;
38
- if (typeof onImageCaptured === 'function') {
39
- onImageCaptured({ ...data, url: data.uri });
40
- }
41
- }
42
-
43
- onBarCodeRead = ({ nativeEvent }) => {
44
- const value = JSON.stringify(nativeEvent);
45
- const outdated = new Date() - this.lastEventTime > EventThrottleMs;
46
- if (value && value === this.lastEvent && !outdated) {
47
- return;
48
- }
49
- const { onBarCodeRead } = this.props;
50
- if (typeof onBarCodeRead === 'function') {
51
- onBarCodeRead({ data: nativeEvent?.codeStringValue });
52
- this.lastEvent = value;
53
- this.lastEventTime = new Date();
54
- }
55
- };
56
-
57
- onOpenCamera = ({ nativeEvent }) => {
58
- const { onOpenCamera } = this.props;
59
- if (typeof onOpenCamera === 'function') {
60
- onOpenCamera(nativeEvent);
61
- }
62
- };
63
-
64
- render() {
65
- const {
66
- customFinder,
67
- style,
68
- torchMode,
69
- type,
70
- onBarCodeRead,
71
- zoomMode,
72
- } = this.props;
73
-
74
- return (
75
- <View style={[styles.container, style]}>
76
- <Camera
77
- ref={(cam) => (this.camera = cam)}
78
- style={styles.camera}
79
- cameraType={type}
80
- torchMode={torchMode}
81
- focusMode={torchMode}
82
- zoomMode={zoomMode}
83
- scanBarcode={!!onBarCodeRead}
84
- onReadCode={this.onBarCodeRead}
85
- onOpenCamera={this.onOpenCamera}
86
- />
87
- <View style={StyleSheet.absoluteFill}>
88
- {customFinder && customFinder()}
89
- </View>
90
- </View>
91
- );
92
- }
93
- }
94
-
95
- CameraKit.propTypes = {
96
- onBarCodeRead: PropTypes.func,
97
- onImageCaptured: PropTypes.func,
98
- type: PropTypes.oneOf(['front', 'back']),
99
- torchMode: PropTypes.oneOf(['on', 'off', 'auto']),
100
- zoomMode: PropTypes.oneOf(['on', 'off']),
101
- onOpenCamera: PropTypes.func,
102
- };
103
-
104
- CameraKit.defaultProps = {
105
- type: 'back',
106
- torchMode: 'off',
107
- zoomMode: 'off',
108
- onOpenCamera: () => {},
109
- };
110
-
111
- export default CameraKit;
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- import CameraKit from './CameraKit';
2
-
3
- export { CameraKit };