@tarojs/taro-rn 4.0.0-beta.0 → 4.0.0-beta.2
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/LICENSE +14 -0
- package/dist/lib/chooseImage/index.js +36 -146
- package/dist/lib/chooseImage/index.js.map +1 -1
- package/dist/lib/chooseVideo/index.js +32 -47
- package/dist/lib/chooseVideo/index.js.map +1 -1
- package/dist/lib/media.js +9 -12
- package/dist/lib/media.js.map +1 -1
- package/dist/lib/showActionSheet/index.js +10 -24
- package/dist/lib/showActionSheet/index.js.map +1 -1
- package/dist/setup.js +13 -30
- package/dist/setup.js.map +1 -1
- package/package.json +45 -48
- package/src/__tests__/__mock__/mockRNCCameraRoll.js +3 -12
- package/src/__tests__/__snapshots__/scanCode.test.tsx.snap +11 -16
- package/src/__tests__/clipboard.test.ts +2 -2
- package/src/lib/chooseImage/index.ts +24 -148
- package/src/lib/chooseVideo/index.ts +23 -48
- package/src/lib/file.ts +1 -1
- package/src/lib/media.ts +17 -13
- package/src/lib/showActionSheet/index.tsx +10 -36
- package/src/setup.ts +13 -33
- package/src/__tests__/__mock__/mockAsyncStorage.js +0 -47
- package/src/__tests__/__mock__/mockClipboard.js +0 -15
- package/src/__tests__/__mock__/mockNavigator.js +0 -20
- package/src/__tests__/__mock__/mockNetwork.js +0 -59
- package/src/__tests__/__mock__/mockVibrate.js +0 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/taro-rn",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.2",
|
|
4
4
|
"description": "Taro RN framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "types/index.d.ts",
|
|
@@ -21,73 +21,70 @@
|
|
|
21
21
|
"author": "O2Team",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@bam.tech/react-native-image-resizer": "~3.0.
|
|
25
|
-
"@react-native-async-storage/async-storage": "~1.
|
|
26
|
-
"@react-native-camera-roll/camera-roll": "~
|
|
27
|
-
"@react-native-clipboard/clipboard": "~1.
|
|
28
|
-
"@react-native-community/geolocation": "~3.0
|
|
29
|
-
"@react-native-community/netinfo": "~
|
|
30
|
-
"babel-preset-expo": "~
|
|
24
|
+
"@bam.tech/react-native-image-resizer": "~3.0.5",
|
|
25
|
+
"@react-native-async-storage/async-storage": "~1.21.0",
|
|
26
|
+
"@react-native-camera-roll/camera-roll": "~7.2.0",
|
|
27
|
+
"@react-native-clipboard/clipboard": "~1.13.0",
|
|
28
|
+
"@react-native-community/geolocation": "~3.1.0",
|
|
29
|
+
"@react-native-community/netinfo": "~11.2.0",
|
|
30
|
+
"babel-preset-expo": "~10.0.0",
|
|
31
31
|
"base64-js": "^1.3.0",
|
|
32
32
|
"deprecated-react-native-prop-types": "^2.3.0",
|
|
33
|
-
"expo-av": "~13.
|
|
34
|
-
"expo-barcode-scanner": "~12.
|
|
35
|
-
"expo-brightness": "~11.
|
|
36
|
-
"expo-camera": "~
|
|
37
|
-
"expo-file-system": "~
|
|
38
|
-
"expo-image-picker": "~14.
|
|
39
|
-
"expo-keep-awake": "~12.0
|
|
40
|
-
"expo-location": "~
|
|
41
|
-
"expo-sensors": "~12.
|
|
33
|
+
"expo-av": "~13.10.0",
|
|
34
|
+
"expo-barcode-scanner": "~12.9.0",
|
|
35
|
+
"expo-brightness": "~11.8.0",
|
|
36
|
+
"expo-camera": "~14.0.0",
|
|
37
|
+
"expo-file-system": "~16.0.0",
|
|
38
|
+
"expo-image-picker": "~14.7.0",
|
|
39
|
+
"expo-keep-awake": "~12.8.0",
|
|
40
|
+
"expo-location": "~16.5.1",
|
|
41
|
+
"expo-sensors": "~12.9.0",
|
|
42
42
|
"nullthrows": "^1.1.1",
|
|
43
43
|
"prop-types": "^15.8.1",
|
|
44
|
-
"react-native-device-info": "~10.
|
|
44
|
+
"react-native-device-info": "~10.12.0",
|
|
45
45
|
"react-native-image-zoom-viewer": "^3.0.1",
|
|
46
|
-
"react-native-root-siblings": "^
|
|
47
|
-
"react-native-root-toast": "^3.
|
|
48
|
-
"react-native-safe-area-context": "~4.
|
|
46
|
+
"react-native-root-siblings": "^5.0.1",
|
|
47
|
+
"react-native-root-toast": "^3.5.1",
|
|
48
|
+
"react-native-safe-area-context": "~4.8.0",
|
|
49
49
|
"react-native-stylekit": "^1.0.0",
|
|
50
|
-
"
|
|
51
|
-
"@tarojs/runtime-rn": "4.0.0-beta.0"
|
|
50
|
+
"@tarojs/runtime-rn": "4.0.0-beta.2"
|
|
52
51
|
},
|
|
53
52
|
"devDependencies": {
|
|
54
53
|
"@babel/core": "^7.14.5",
|
|
55
54
|
"@babel/preset-env": "^7.14.5",
|
|
56
|
-
"@
|
|
55
|
+
"@react-native/babel-preset": "^0.73.18",
|
|
57
56
|
"@testing-library/jest-native": "^5.4.2",
|
|
58
|
-
"@testing-library/react-native": "^12.
|
|
57
|
+
"@testing-library/react-native": "^12.4.1",
|
|
59
58
|
"@types/jest": "^27.4.1",
|
|
60
|
-
"expo": "~
|
|
59
|
+
"expo": "~50.0.0-preview.7",
|
|
61
60
|
"fbjs-scripts": "^3.0.1",
|
|
62
61
|
"jest": "^29.3.1",
|
|
63
62
|
"jest-cli": "^29.3.1",
|
|
64
63
|
"jest-environment-node": "^29.5.0",
|
|
65
|
-
"jest-expo": "^
|
|
66
|
-
"metro-react-native-babel-preset": "^0.73.7",
|
|
64
|
+
"jest-expo": "^50.0.0",
|
|
67
65
|
"mock-socket": "^7.1.0",
|
|
68
66
|
"react": "18.2.0",
|
|
69
|
-
"react-native": "^0.
|
|
67
|
+
"react-native": "^0.73.1",
|
|
70
68
|
"react-test-renderer": "18.2.0"
|
|
71
69
|
},
|
|
72
70
|
"peerDependencies": {
|
|
73
|
-
"@bam.tech/react-native-image-resizer": "~3.0.
|
|
74
|
-
"@react-native-async-storage/async-storage": "~1.
|
|
75
|
-
"@react-native-camera-roll/camera-roll": "~
|
|
76
|
-
"@react-native-clipboard/clipboard": "~1.
|
|
77
|
-
"@react-native-community/geolocation": "~3.0
|
|
78
|
-
"@react-native-community/netinfo": "~
|
|
79
|
-
"expo-av": "~13.
|
|
80
|
-
"expo-barcode-scanner": "~12.
|
|
81
|
-
"expo-brightness": "~11.
|
|
82
|
-
"expo-camera": "~
|
|
83
|
-
"expo-file-system": "~
|
|
84
|
-
"expo-image-picker": "~14.
|
|
85
|
-
"expo-keep-awake": "~12.0
|
|
86
|
-
"expo-location": "~
|
|
87
|
-
"expo-sensors": "~12.
|
|
88
|
-
"react-native-device-info": "~10.
|
|
89
|
-
"react-native-safe-area-context": "~4.
|
|
90
|
-
"react-native-syan-image-picker": "0.5.3"
|
|
71
|
+
"@bam.tech/react-native-image-resizer": "~3.0.5",
|
|
72
|
+
"@react-native-async-storage/async-storage": "~1.21.0",
|
|
73
|
+
"@react-native-camera-roll/camera-roll": "~7.2.0",
|
|
74
|
+
"@react-native-clipboard/clipboard": "~1.13.0",
|
|
75
|
+
"@react-native-community/geolocation": "~3.1.0",
|
|
76
|
+
"@react-native-community/netinfo": "~11.2.0",
|
|
77
|
+
"expo-av": "~13.10.0",
|
|
78
|
+
"expo-barcode-scanner": "~12.9.0",
|
|
79
|
+
"expo-brightness": "~11.8.0",
|
|
80
|
+
"expo-camera": "~14.0.0",
|
|
81
|
+
"expo-file-system": "~16.0.0",
|
|
82
|
+
"expo-image-picker": "~14.7.0",
|
|
83
|
+
"expo-keep-awake": "~12.8.0",
|
|
84
|
+
"expo-location": "~16.5.1",
|
|
85
|
+
"expo-sensors": "~12.9.0",
|
|
86
|
+
"react-native-device-info": "~10.12.0",
|
|
87
|
+
"react-native-safe-area-context": "~4.8.0"
|
|
91
88
|
},
|
|
92
89
|
"scripts": {
|
|
93
90
|
"assets": "cpy 'src/**/*.png' '!src/__tests__/*' dist",
|
|
@@ -95,7 +92,7 @@
|
|
|
95
92
|
"dev": "pnpm run assets && tsc --watch",
|
|
96
93
|
"lint": "eslint .",
|
|
97
94
|
"test": "jest --silent",
|
|
98
|
-
"test:ci": "jest --ci -i --
|
|
95
|
+
"test:ci": "jest --ci -i --coverage --silent",
|
|
99
96
|
"test:coverage": "jest --coverage",
|
|
100
97
|
"test:dev": "jest --watch",
|
|
101
98
|
"script": "node ./script"
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
saveToCameraRoll: jest.fn((url, { type }) => Promise.resolve(`${type}://${url}`)),
|
|
4
|
-
getPhotos: jest.fn(() => Promise.resolve()),
|
|
5
|
-
}
|
|
6
|
-
const RNCCameraRollPermissionModule = {
|
|
7
|
-
checkPermission: jest.fn(),
|
|
8
|
-
requestReadWritePermission: jest.fn(),
|
|
9
|
-
requestAddOnlyPermission: jest.fn(),
|
|
10
|
-
refreshPhotoSelection: jest.fn(),
|
|
1
|
+
const CameraRoll = {
|
|
2
|
+
save: jest.fn((url, { type }) => Promise.resolve(`${type}://${url}`)),
|
|
11
3
|
}
|
|
12
4
|
|
|
13
5
|
export default {
|
|
14
|
-
|
|
15
|
-
RNCCameraRollPermissionModule,
|
|
6
|
+
CameraRoll,
|
|
16
7
|
}
|
|
@@ -15,33 +15,28 @@ exports[`scanCode should render scanCode success 1`] = `
|
|
|
15
15
|
]
|
|
16
16
|
}
|
|
17
17
|
>
|
|
18
|
-
<
|
|
18
|
+
<ViewManagerAdapter_ExpoCamera
|
|
19
|
+
barCodeScannerEnabled={true}
|
|
20
|
+
barCodeScannerSettings={
|
|
21
|
+
{
|
|
22
|
+
"barCodeTypes": [],
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
faceDetectorSettings={{}}
|
|
26
|
+
focusDepth={0}
|
|
19
27
|
onBarCodeScanned={[Function]}
|
|
20
28
|
onCameraReady={[Function]}
|
|
21
29
|
onFacesDetected={[Function]}
|
|
22
30
|
onMountError={[Function]}
|
|
23
31
|
onPictureSaved={[Function]}
|
|
24
|
-
|
|
25
|
-
{
|
|
26
|
-
"autoFocus": undefined,
|
|
27
|
-
"barCodeScannerEnabled": true,
|
|
28
|
-
"barCodeScannerSettings": {
|
|
29
|
-
"barCodeTypes": [],
|
|
30
|
-
},
|
|
31
|
-
"faceDetectorSettings": {},
|
|
32
|
-
"flashMode": undefined,
|
|
33
|
-
"focusDepth": 0,
|
|
34
|
-
"type": undefined,
|
|
35
|
-
"whiteBalance": undefined,
|
|
36
|
-
"zoom": 0,
|
|
37
|
-
}
|
|
38
|
-
}
|
|
32
|
+
onResponsiveOrientationChanged={[Function]}
|
|
39
33
|
style={
|
|
40
34
|
{
|
|
41
35
|
"height": 1334,
|
|
42
36
|
"width": 750,
|
|
43
37
|
}
|
|
44
38
|
}
|
|
39
|
+
zoom={0}
|
|
45
40
|
/>
|
|
46
41
|
<View
|
|
47
42
|
accessibilityLabel="Close"
|
|
@@ -12,7 +12,7 @@ const Taro = {
|
|
|
12
12
|
describe('clipboard', () => {
|
|
13
13
|
describe('setClipboardData', () => {
|
|
14
14
|
test('should set value into Clipboard', async () => {
|
|
15
|
-
const data = '
|
|
15
|
+
const data = 'mockString'
|
|
16
16
|
const success = jest.fn()
|
|
17
17
|
const fail = jest.fn()
|
|
18
18
|
const complete = jest.fn()
|
|
@@ -63,7 +63,7 @@ describe('clipboard', () => {
|
|
|
63
63
|
|
|
64
64
|
describe('getClipboardData', () => {
|
|
65
65
|
test('可以获得正确的剪切板数据', async () => {
|
|
66
|
-
const data = '
|
|
66
|
+
const data = 'mockString'
|
|
67
67
|
const success = jest.fn()
|
|
68
68
|
const fail = jest.fn()
|
|
69
69
|
const complete = jest.fn()
|
|
@@ -1,157 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { shouldBeObject } from '../../utils'
|
|
1
|
+
import { chooseMedia, MEDIA_TYPE } from '../media'
|
|
4
2
|
import { showActionSheet } from '../showActionSheet'
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
sourceType
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
images.forEach((item: any) => {
|
|
17
|
-
tempFilePaths.push(item.uri)
|
|
18
|
-
tempFiles.push({
|
|
19
|
-
path: item.uri,
|
|
20
|
-
size: item.size
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
return { tempFilePaths, tempFiles }
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function openCamera(options: Taro.chooseImage.Option): Promise<Taro.chooseImage.SuccessCallbackResult> {
|
|
27
|
-
const { success, fail, complete } = options
|
|
28
|
-
return new Promise((resolve, reject) => {
|
|
29
|
-
SYImagePicker.openCamera({
|
|
30
|
-
compressFocusAlpha: true
|
|
31
|
-
}, (err, photo: any) => {
|
|
32
|
-
if (err) {
|
|
33
|
-
const res = {
|
|
34
|
-
errMsg: err
|
|
35
|
-
}
|
|
36
|
-
fail?.(res)
|
|
37
|
-
complete?.(res)
|
|
38
|
-
reject(res)
|
|
39
|
-
} else {
|
|
40
|
-
try {
|
|
41
|
-
const data = Object.assign({}, getRes(photo), res)
|
|
42
|
-
success?.(data)
|
|
43
|
-
complete?.(data)
|
|
44
|
-
resolve(data)
|
|
45
|
-
} catch (error) {
|
|
46
|
-
// 错误
|
|
47
|
-
reject(error)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// NOTE: 调用此函数会改变图片的像素尺寸
|
|
55
|
-
function openPicker(options: Taro.chooseImage.Option): Promise<Taro.chooseImage.SuccessCallbackResult> {
|
|
56
|
-
const { count: imageCount, sizeType = [],success, complete, fail } = options
|
|
57
|
-
return new Promise((resolve, reject) => {
|
|
58
|
-
// NOTE:react-native-syan-image-picker 暂不支持 Android 端压缩
|
|
59
|
-
SYImagePicker.showImagePicker({
|
|
60
|
-
compressFocusAlpha: true,
|
|
61
|
-
imageCount,
|
|
62
|
-
quality: sizeType[0] === 'compressed' ? 70 : 90,
|
|
63
|
-
}, (err, photos: any) => {
|
|
64
|
-
if (err) {
|
|
65
|
-
const res = {
|
|
66
|
-
errMsg: err
|
|
67
|
-
}
|
|
68
|
-
fail?.(res)
|
|
69
|
-
complete?.(res)
|
|
70
|
-
reject(res)
|
|
71
|
-
} else {
|
|
72
|
-
try {
|
|
73
|
-
const data = Object.assign({}, getRes(photos), res)
|
|
74
|
-
success?.(data)
|
|
75
|
-
complete?.(data)
|
|
76
|
-
resolve(data)
|
|
77
|
-
} catch (error) {
|
|
78
|
-
reject(error)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
})
|
|
4
|
+
export function chooseImage(opts: Taro.chooseImage.Option): Promise<any> {
|
|
5
|
+
const {
|
|
6
|
+
sourceType = ['album', 'camera'],
|
|
7
|
+
} = opts
|
|
8
|
+
if(sourceType?.includes('camera') && sourceType?.includes('album')) {
|
|
9
|
+
return showImagePicker(opts)
|
|
10
|
+
} else if (sourceType?.includes('camera')) {
|
|
11
|
+
return chooseMedia({...opts, sourceType: ['camera']}, MEDIA_TYPE.IMAGES)
|
|
12
|
+
}
|
|
13
|
+
return chooseMedia({...opts, sourceType: ['album']}, MEDIA_TYPE.IMAGES)
|
|
83
14
|
}
|
|
84
15
|
|
|
85
|
-
|
|
86
|
-
* @description Display the image picker.
|
|
87
|
-
* @param options
|
|
88
|
-
* @returns {Promise}
|
|
89
|
-
*/
|
|
90
|
-
function showImagePicker(options: Taro.chooseImage.Option):Promise<Taro.chooseImage.SuccessCallbackResult> {
|
|
16
|
+
async function showImagePicker(options: Taro.chooseImage.Option):Promise<any> {
|
|
91
17
|
const { fail, complete } = options
|
|
92
|
-
|
|
93
|
-
showActionSheet({
|
|
18
|
+
try {
|
|
19
|
+
const res = await showActionSheet({
|
|
94
20
|
itemList: ['拍照', '从手机相册选择'],
|
|
95
|
-
success(res) {
|
|
96
|
-
console.log(res.tapIndex)
|
|
97
|
-
if (res.tapIndex === 0) {
|
|
98
|
-
// 不添加 setTimeout 会有一定概率卡死,肯能是与 sibling.destroy() 冲突
|
|
99
|
-
setTimeout(() => {
|
|
100
|
-
openCamera(options).then(res => {
|
|
101
|
-
resolve(res)
|
|
102
|
-
}).catch(e => {
|
|
103
|
-
fail?.({ errMsg: `chooseImage:${e.message}` })
|
|
104
|
-
complete?.({ errMsg: `chooseImage:${e.message}` })
|
|
105
|
-
reject(new Error(`chooseImage:${e.message}`))
|
|
106
|
-
})
|
|
107
|
-
}, 100)
|
|
108
|
-
}
|
|
109
|
-
if (res.tapIndex === 1) {
|
|
110
|
-
setTimeout(() => {
|
|
111
|
-
openPicker(options).then(res => {
|
|
112
|
-
resolve(res)
|
|
113
|
-
}).catch(e => {
|
|
114
|
-
fail?.({ errMsg: `chooseImage:${e.message}` })
|
|
115
|
-
complete?.({ errMsg: `chooseImage:${e.message}` })
|
|
116
|
-
reject(new Error(`chooseImage:${e.message}`))
|
|
117
|
-
})
|
|
118
|
-
}, 100)
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
fail(res) {
|
|
122
|
-
console.log('error: ', res.errMsg)
|
|
123
|
-
}
|
|
124
21
|
})
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
export function chooseImage(options: Taro.chooseImage.Option): Promise<Taro.chooseImage.SuccessCallbackResult> {
|
|
136
|
-
// options must be an Object
|
|
137
|
-
const isObject = shouldBeObject(options)
|
|
138
|
-
if (!isObject.res) {
|
|
139
|
-
const res = { errMsg: `chooseImage${isObject.msg}` }
|
|
140
|
-
console.error(res.errMsg)
|
|
141
|
-
return Promise.reject(res)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
options = Object.assign({}, defaultOptions, options)
|
|
145
|
-
const { sourceType } = options
|
|
146
|
-
|
|
147
|
-
switch (JSON.stringify(sourceType)) {
|
|
148
|
-
case '[\'camera\']':
|
|
149
|
-
case '["camera"]':
|
|
150
|
-
return openCamera(options)
|
|
151
|
-
case '[\'album\']':
|
|
152
|
-
case '["album"]':
|
|
153
|
-
return openPicker(options)
|
|
154
|
-
default:
|
|
155
|
-
return showImagePicker(options)
|
|
22
|
+
if (res.tapIndex === 0) {
|
|
23
|
+
return chooseMedia({...options, sourceType: ['camera']}, MEDIA_TYPE.IMAGES)
|
|
24
|
+
}
|
|
25
|
+
if (res.tapIndex === 1) {
|
|
26
|
+
return chooseMedia({...options, sourceType: ['album']}, MEDIA_TYPE.IMAGES)
|
|
27
|
+
}
|
|
28
|
+
} catch (err) {
|
|
29
|
+
const res = { errMsg: 'chooseImage: fail' }
|
|
30
|
+
fail?.(res)
|
|
31
|
+
complete?.(res)
|
|
156
32
|
}
|
|
157
33
|
}
|
|
@@ -1,60 +1,35 @@
|
|
|
1
1
|
import { chooseMedia, MEDIA_TYPE } from '../media'
|
|
2
2
|
import { showActionSheet } from '../showActionSheet'
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* 拍摄视频或从手机相册中选视频。
|
|
6
|
-
* @param {Object} opts
|
|
7
|
-
* @param {Array} [opts.sourceType=['album', 'camera']] ✔
|
|
8
|
-
* @param {boolean} [opts.compressed=true] ✔
|
|
9
|
-
* @param {number} [opts.maxDuration=60] - 拍摄视频最长拍摄时间,单位秒 ✖
|
|
10
|
-
* @param {string} [opts.camera='back'] - 默认拉起的是前置或者后置摄像头。✖
|
|
11
|
-
* @returns {Promise<*>}
|
|
12
|
-
*/
|
|
13
4
|
export function chooseVideo(opts: Taro.chooseVideo.Option): Promise<any> {
|
|
14
|
-
const {
|
|
5
|
+
const {
|
|
6
|
+
sourceType = ['album', 'camera'],
|
|
7
|
+
compressed
|
|
8
|
+
} = opts
|
|
15
9
|
opts = Object.assign(opts, { sizeType: compressed ? ['compressed'] : [] })
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
case '[\'album\']':
|
|
21
|
-
case '["album"]':
|
|
22
|
-
return chooseMedia({...opts, sourceType: ['album']}, MEDIA_TYPE.VIDEOS)
|
|
23
|
-
default:
|
|
24
|
-
return showVideoPicker(opts)
|
|
10
|
+
if(sourceType?.includes('camera') && sourceType?.includes('album')) {
|
|
11
|
+
return showVideoPicker(opts)
|
|
12
|
+
} else if (sourceType?.includes('camera')) {
|
|
13
|
+
return chooseMedia({...opts, sourceType: ['camera']}, MEDIA_TYPE.VIDEOS)
|
|
25
14
|
}
|
|
15
|
+
return chooseMedia({...opts, sourceType: ['album']}, MEDIA_TYPE.VIDEOS)
|
|
26
16
|
}
|
|
27
17
|
|
|
28
|
-
|
|
29
|
-
* 视频选择器
|
|
30
|
-
* @param options
|
|
31
|
-
* @returns {Promise}
|
|
32
|
-
*/
|
|
33
|
-
function showVideoPicker(options: Taro.chooseVideo.Option):any {
|
|
18
|
+
async function showVideoPicker(options: Taro.chooseVideo.Option):Promise<any> {
|
|
34
19
|
const { fail, complete } = options
|
|
35
|
-
|
|
36
|
-
showActionSheet({
|
|
20
|
+
try {
|
|
21
|
+
const res = await showActionSheet({
|
|
37
22
|
itemList: ['拍摄', '从手机相册选择'],
|
|
38
|
-
success: function (res) {
|
|
39
|
-
if (res.tapIndex === 0) {
|
|
40
|
-
resolve(chooseMedia({...options, sourceType: ['camera']}, MEDIA_TYPE.VIDEOS))
|
|
41
|
-
}
|
|
42
|
-
if (res.tapIndex === 1) {
|
|
43
|
-
resolve(chooseMedia({...options, sourceType: ['album']}, MEDIA_TYPE.VIDEOS))
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
fail: function () {
|
|
47
|
-
const res = { errMsg: 'chooseVideo:fail cancel' }
|
|
48
|
-
reject(res)
|
|
49
|
-
}
|
|
50
|
-
}).catch(err => {
|
|
51
|
-
console.log('error: ', err.errMsg)
|
|
52
|
-
if (/showActionSheet:fail cancel/.test(err.errMsg)) {
|
|
53
|
-
const res = { errMsg: 'chooseVideo:fail cancel' }
|
|
54
|
-
fail?.(res)
|
|
55
|
-
complete?.(res)
|
|
56
|
-
reject(res)
|
|
57
|
-
}
|
|
58
23
|
})
|
|
59
|
-
|
|
24
|
+
if (res.tapIndex === 0) {
|
|
25
|
+
return chooseMedia({...options, sourceType: ['camera']}, MEDIA_TYPE.VIDEOS)
|
|
26
|
+
}
|
|
27
|
+
if (res.tapIndex === 1) {
|
|
28
|
+
return chooseMedia({...options, sourceType: ['album']}, MEDIA_TYPE.VIDEOS)
|
|
29
|
+
}
|
|
30
|
+
} catch (err) {
|
|
31
|
+
const res = { errMsg: 'chooseVideo: fail' }
|
|
32
|
+
fail?.(res)
|
|
33
|
+
complete?.(res)
|
|
34
|
+
}
|
|
60
35
|
}
|
package/src/lib/file.ts
CHANGED
package/src/lib/media.ts
CHANGED
|
@@ -28,15 +28,17 @@ export async function saveMedia(opts: Taro.saveImageToPhotosAlbum.Option | Taro.
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export async function chooseMedia(opts: Taro.chooseImage.Option | Taro.chooseVideo.Option, mediaTypes:
|
|
31
|
+
export async function chooseMedia(opts: Taro.chooseImage.Option | Taro.chooseVideo.Option, mediaTypes: any): Promise<TaroGeneral.CallbackResult> {
|
|
32
32
|
if (!opts || typeof opts !== 'object') {
|
|
33
33
|
opts = {}
|
|
34
34
|
}
|
|
35
|
-
const { sizeType = [], sourceType = [], success, fail, complete, maxDuration } = opts as any
|
|
35
|
+
const { sizeType = [], sourceType = [], success, fail, complete, maxDuration, count } = opts as any
|
|
36
36
|
const options = {
|
|
37
37
|
mediaTypes,
|
|
38
38
|
quality: sizeType[0] === 'compressed' ? 0.7 : 1,
|
|
39
|
-
videoMaxDuration: maxDuration
|
|
39
|
+
videoMaxDuration: maxDuration,
|
|
40
|
+
allowsMultipleSelection: (mediaTypes === MEDIA_TYPE.IMAGES && count > 1) ? true : false,
|
|
41
|
+
selectionLimit: mediaTypes === MEDIA_TYPE.VIDEOS ? 1 : (count || 9),
|
|
40
42
|
}
|
|
41
43
|
const isCamera = sourceType[0] === 'camera'
|
|
42
44
|
const { granted } = isCamera ? await requestCameraPermissionsAsync() : await ImagePicker.requestMediaLibraryPermissionsAsync()
|
|
@@ -47,22 +49,24 @@ export async function chooseMedia(opts: Taro.chooseImage.Option | Taro.chooseVid
|
|
|
47
49
|
|
|
48
50
|
let launchMediaAsync = isCamera ? ImagePicker.launchCameraAsync : ImagePicker.launchImageLibraryAsync
|
|
49
51
|
try {
|
|
50
|
-
const resp
|
|
51
|
-
const { uri, duration, width, height } = resp
|
|
52
|
-
resp.path = uri
|
|
53
|
-
|
|
52
|
+
const resp = await launchMediaAsync(options)
|
|
54
53
|
let res: any = {}
|
|
55
54
|
if (mediaTypes === MEDIA_TYPE.VIDEOS) {
|
|
55
|
+
const asset = resp.assets?.[0]
|
|
56
56
|
res = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
height
|
|
57
|
+
...asset,
|
|
58
|
+
tempFilePath: asset?.uri,
|
|
59
|
+
size: asset?.fileSize,
|
|
61
60
|
}
|
|
62
61
|
} else {
|
|
63
62
|
res = {
|
|
64
|
-
tempFilePaths:
|
|
65
|
-
tempFiles:
|
|
63
|
+
tempFilePaths: resp.assets?.map((item) => item.uri),
|
|
64
|
+
tempFiles: resp.assets?.map((item) => ({
|
|
65
|
+
...item,
|
|
66
|
+
path: item.uri,
|
|
67
|
+
size: item.fileSize,
|
|
68
|
+
type: item.mimeType,
|
|
69
|
+
}))
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
if (res.tempFilePath || (!!res.tempFilePaths && res.tempFilePaths.length > 0)) {
|
|
@@ -20,9 +20,13 @@ function showActionSheet (obj: Taro.showActionSheet.Option): Promise<Taro.showAc
|
|
|
20
20
|
sibling && sibling.destroy()
|
|
21
21
|
sibling = undefined
|
|
22
22
|
const res = { tapIndex, errMsg: 'showActionSheet:ok' }
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
// fix: iOS 无法打开相册
|
|
24
|
+
// https://github.com/expo/expo/issues/25705
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
success?.(res)
|
|
27
|
+
complete?.(res)
|
|
28
|
+
resolve(res)
|
|
29
|
+
}, 1)
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
function onFail () {
|
|
@@ -38,7 +42,7 @@ function showActionSheet (obj: Taro.showActionSheet.Option): Promise<Taro.showAc
|
|
|
38
42
|
<ActionSheet
|
|
39
43
|
autoDectect={autoDectect}
|
|
40
44
|
type={type}
|
|
41
|
-
visible={
|
|
45
|
+
visible={true}
|
|
42
46
|
onClose={onFail}
|
|
43
47
|
menus={
|
|
44
48
|
itemList.map((item, index) => {
|
|
@@ -46,7 +50,7 @@ function showActionSheet (obj: Taro.showActionSheet.Option): Promise<Taro.showAc
|
|
|
46
50
|
type: 'default',
|
|
47
51
|
label: item,
|
|
48
52
|
textStyle: { color: itemColor },
|
|
49
|
-
onPress: onSuccess
|
|
53
|
+
onPress: () => onSuccess(index)
|
|
50
54
|
}
|
|
51
55
|
}) as any
|
|
52
56
|
}
|
|
@@ -55,41 +59,11 @@ function showActionSheet (obj: Taro.showActionSheet.Option): Promise<Taro.showAc
|
|
|
55
59
|
type: 'default',
|
|
56
60
|
label: '取消',
|
|
57
61
|
textStyle: { color: itemColor },
|
|
58
|
-
onPress: onFail
|
|
62
|
+
onPress: onFail
|
|
59
63
|
}
|
|
60
64
|
] as any}
|
|
61
65
|
/>
|
|
62
66
|
)
|
|
63
|
-
|
|
64
|
-
// hack 的做法。不推荐
|
|
65
|
-
setTimeout(() => {
|
|
66
|
-
sibling.update(
|
|
67
|
-
<ActionSheet
|
|
68
|
-
autoDectect={autoDectect}
|
|
69
|
-
type={type}
|
|
70
|
-
visible
|
|
71
|
-
onClose={onFail}
|
|
72
|
-
menus={
|
|
73
|
-
itemList.map((item, index) => {
|
|
74
|
-
return {
|
|
75
|
-
type: 'default',
|
|
76
|
-
label: item,
|
|
77
|
-
textStyle: { color: itemColor },
|
|
78
|
-
onPress: onSuccess.bind(this, index)
|
|
79
|
-
}
|
|
80
|
-
}) as any
|
|
81
|
-
}
|
|
82
|
-
actions={[
|
|
83
|
-
{
|
|
84
|
-
type: 'default',
|
|
85
|
-
label: '取消',
|
|
86
|
-
textStyle: { color: itemColor },
|
|
87
|
-
onPress: onFail.bind(this)
|
|
88
|
-
}
|
|
89
|
-
] as any}
|
|
90
|
-
/>
|
|
91
|
-
)
|
|
92
|
-
}, 100)
|
|
93
67
|
})
|
|
94
68
|
}
|
|
95
69
|
|