Skip to content

Commit

Permalink
fix(taro-h5): chooseImage 支持 sourceType (#8815)
Browse files Browse the repository at this point in the history
Co-authored-by: Zakary <zakarycode@gmail.com>
  • Loading branch information
liuchuzhang and ZakaryCode authored Apr 6, 2022
1 parent 0879585 commit 9e43aca
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/taro-h5/src/api/media/image/chooseImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
return Promise.reject(res)
}

const { count = 1, success, fail, complete, imageId = 'taroChooseImage' } = options
const {
count = 1,
success,
fail,
complete,
imageId = 'taroChooseImage',
sourceType = ['album', 'camera']
} = options
const handle = new MethodHandler({ name: 'chooseImage', success, fail, complete })
const res: Partial<Taro.chooseImage.SuccessCallbackResult> = {
tempFilePaths: [],
tempFiles: []
}
const sourceTypeString = sourceType && sourceType.toString()
const acceptableSourceType = ['user', 'environment', 'camera']

if (count && typeof count !== 'number') {
res.errMsg = getParameterError({
Expand All @@ -39,6 +48,9 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
if (count > 1) {
obj.setAttribute('multiple', 'multiple')
}
if (acceptableSourceType.indexOf(sourceTypeString) > -1) {
obj.setAttribute('capture', sourceTypeString)
}
obj.setAttribute('accept', 'image/*')
obj.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;')
document.body.appendChild(obj)
Expand All @@ -49,6 +61,11 @@ export const chooseImage: typeof Taro.chooseImage = function (options) {
} else {
el.removeAttribute('multiple')
}
if (acceptableSourceType.indexOf(sourceTypeString) > -1) {
el.setAttribute('capture', sourceTypeString)
} else {
el.removeAttribute('capture')
}
}

return new Promise(resolve => {
Expand Down

0 comments on commit 9e43aca

Please sign in to comment.