提交 79dd3b16 编写于 作者: D DCloud_LXH

feat(H5): judge user activation when choose file

上级 c45634db
......@@ -9,21 +9,17 @@ const passiveOptions = supportsPassive ? {
const vms = []
let userInteract = 0
let inited
function addInteractListener (vm) {
function addInteractListener (vm = {}) {
if (!inited) {
const eventNames = ['touchstart', 'touchmove', 'touchend', 'mousedown', 'mouseup']
eventNames.forEach(eventName => {
document.addEventListener(eventName, function () {
vms.forEach(vm => {
vm.userInteract = true
userInteract++
setTimeout(() => {
userInteract--
if (!userInteract) {
vm.userInteract = false
}
}, 0)
})
!userInteract && vms.forEach(vm => (vm.userInteract = true))
userInteract++
setTimeout(() => {
!--userInteract && vms.forEach(vm => (vm.userInteract = false))
}, 0)
}, passiveOptions)
})
inited = true
......@@ -51,5 +47,10 @@ export default {
},
beforeDestroy () {
removeInteractListener(this)
},
addInteractListener,
// true -> interact
getStatus () {
return !!userInteract
}
}
import { fileToUrl } from 'uni-platform/helpers/file'
import _createInput from './create_input'
import { interact } from 'uni-mixins'
const {
invokeCallbackHandler: invoke
......@@ -55,5 +56,11 @@ export function chooseFile ({
// TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。
})
fileInput.click()
if (interact.getStatus()) {
fileInput.click()
} else {
invoke(callbackId, {
errMsg: 'chooseFile:fail File chooser dialog can only be shown with a user activation.'
})
}
}
import { fileToUrl } from 'uni-platform/helpers/file'
import _createInput from './create_input'
import { interact } from 'uni-mixins'
const {
invokeCallbackHandler: invoke
......@@ -27,7 +28,6 @@ export function chooseImage ({
type: 'image'
})
document.body.appendChild(imageInput)
imageInput.addEventListener('change', function (event) {
const tempFiles = []
const fileCount = event.target.files.length
......@@ -54,5 +54,11 @@ export function chooseImage ({
// TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。
})
imageInput.click()
if (interact.getStatus()) {
imageInput.click()
} else {
invoke(callbackId, {
errMsg: 'chooseImage:fail File chooser dialog can only be shown with a user activation.'
})
}
}
import { fileToUrl, revokeObjectURL } from 'uni-platform/helpers/file'
import _createInput from './create_input'
import { interact } from 'uni-mixins'
const {
invokeCallbackHandler: invoke
......@@ -67,5 +68,11 @@ export function chooseVideo ({
// TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。
})
videoInput.click()
if (interact.getStatus()) {
videoInput.click()
} else {
invoke(callbackId, {
errMsg: 'chooseVideo:fail File chooser dialog can only be shown with a user activation.'
})
}
}
import { updateElementStyle } from 'uni-shared'
import MIMEType from './MIMEType'
import { interact } from 'uni-mixins'
interact.addInteractListener()
const ALL = '*'
function isWXEnv () {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册