Skip to content

Commit

Permalink
fix(taro-h5): chooseImage 支持 sourceType
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuzhang committed Mar 5, 2021
1 parent f96a70a commit 9d02b8f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/taro-h5/src/api/image/chooseImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { shouleBeObject, getParameterError } from '../utils'
/**
* 从本地相册选择图片或使用相机拍照。
* @param {Object} object 参数
* @param {string[]} [object.sourceType=['album', 'camera']] 选择图片的来源(h5端未实现)
* @param {string[]} [object.sourceType=['album', 'camera']] 选择图片的来源,h5允许传入 `user/environment/camera/`
* @param {string[]} [object.sizeType=['original', 'compressed']] 所选的图片的尺寸(h5端未实现)
* @param {number} [object.count=9] 最多可以选择的图片张数
* @param {function} [object.success] 接口调用成功的回调函数
Expand All @@ -20,12 +20,14 @@ const 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 res = {
errMsg: 'chooseImage:ok',
tempFilePaths: [],
tempFiles: []
}
const sourceTypeString = sourceType && sourceType.toString()
const acceptableSourceType = ['user', 'environment', 'camera']

if (count && typeof count !== 'number') {
res.errMsg = getParameterError({
Expand All @@ -48,6 +50,9 @@ const 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 @@ -58,6 +63,11 @@ const chooseImage = function (options) {
} else {
taroChooseImageId.removeAttribute('multiple')
}
if (acceptableSourceType.indexOf(sourceTypeString) > -1) {
taroChooseImageId.setAttribute('capture', sourceTypeString)
} else {
taroChooseImageId.removeAttribute('capture')
}
}
let taroChooseImageCallback
const taroChooseImagePromise = new Promise(resolve => {
Expand Down

0 comments on commit 9d02b8f

Please sign in to comment.