All files / utils chooseFile.ts

100% Statements 21/21
100% Branches 3/3
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 38 39 40 41 42                              4x 4x 4x 4x 4x 4x 4x   4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x      
import { bindEvent } from './bindEvent'
import { LiteralUnion } from '../types'
import { toArray } from 'lodash-uni'
 
/**
 * 选择文件。
 *
 * @param accept 接受的文件类型
 * @param multiple 是否多选
 * @returns 返回选中的文件列表
 */
export function chooseFile(
  accept: LiteralUnion<'image', string>,
  multiple = false,
): Promise<ReadonlyArray<File>> {
  return new Promise(resolve => {
    let input = document.createElement('input')
    input.style.all = 'unset'
    input.style.position = 'fixed'
    input.style.top = '0px'
    input.style.clip = 'rect(0, 0, 0, 0)'
    input.style.webkitUserSelect = 'text'
    // @ts-ignore
    input.style.MozUserSelect = 'text'
    // @ts-ignore
    input.style.msUserSelect = 'text'
    input.style.userSelect = 'text'
    input.type = 'file'
    input.accept = accept === 'image' ? '.jpg,.jpeg,.png,.gif' : accept
    input.multiple = multiple
    document.body.appendChild(input)
    const unbindChange = bindEvent(input)('change', () => {
      const files = input.files!
      unbindChange()
      document.body.removeChild(input)
      input = null as any
      resolve(Object.freeze(toArray(files)))
    })
    input.click()
  })
}