Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(taro-h5): chooseImage 支持 sourceType #8815

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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