Skip to content

Commit

Permalink
fix(chooseFile): 选择取消监听优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 29, 2024
1 parent f1727a9 commit 3afd571
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/utils/chooseFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { toArray } from 'lodash-uni'
import { LiteralUnion } from '../types'
import { bindEvent } from './bindEvent'
import { wait } from './wait'

/**
* 选择文件。
Expand Down Expand Up @@ -29,9 +30,12 @@ export function chooseFile(
input.accept = accept === 'image' ? '.jpg,.jpeg,.png,.gif' : accept
input.multiple = multiple
document.body.appendChild(input)

const handleChange = () => {
unbindChange()
unbindBlur()
unbindCancel()
unbindFocus()
unbindTouchend()
if (input) {
const files = input.files || []
document.body.removeChild(input)
Expand All @@ -40,7 +44,20 @@ export function chooseFile(
}
}
const unbindChange = bindEvent(input)('change', handleChange)
const unbindBlur = bindEvent(input)('blur', handleChange)

// 标准取消监听 但有兼容问题
// https://caniuse.com/?search=HTMLInputElement%20cancel
const unbindCancel = bindEvent(input)('cancel', handleChange)

// 取消监听 hack
// https://stackoverflow.com/a/67603015
const unbindFocus = bindEvent(window)('focus', () =>
wait(1000).then(handleChange),
)
const unbindTouchend = bindEvent(window)('touchend', () =>
wait(1000).then(handleChange),
)

input.click()
})
}

0 comments on commit 3afd571

Please sign in to comment.