-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-file.ts
37 lines (34 loc) · 943 Bytes
/
ui-file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
type FileListReturn = FileList | any[]
export function getUpload(op: {
accept: string,
multiple: boolean,
}) {
return new Promise<FileListReturn>((resolve) => {
let finished = false
const input = document.createElement('input')
input.type = 'file'
input.accept = op.accept
input.multiple = op.multiple
const finish = (result: FileListReturn) => {
if (finished) { return }
finished = true
resolve(result)
cleanup()
}
const handleInputChange = () => {
finish(input.files)
}
const handleWindowFocus = () => {
setTimeout(() => {
finish([])
}, 1000)
}
const cleanup = () => {
window.removeEventListener('focus', handleWindowFocus)
input.removeEventListener('change', handleInputChange)
}
window.addEventListener('focus', handleWindowFocus)
input.addEventListener('change', handleInputChange)
input.click()
})
}