Skip to content

Commit

Permalink
feat(useUpload): add clear and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Feb 14, 2024
1 parent 0856f32 commit 23f0d4b
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions packages/core/useUpload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ref } from 'vue'
* - [ ] 允许并发上传数量,默认1
* - [ ] 允许设置失败重传次数,默认0
* - [ ] 限制可上传的文件大小,默认Infinity表示不限制
* - [ ] 清空文件列表 delete(index: number)
*/

interface UseUploadOptions {
Expand All @@ -25,13 +26,17 @@ interface UseUploadFile {
interface UseUploadReturn {
upload: () => void
append: (file: File | File[]) => void
clearFiles: () => boolean
remove: (index: number[]) => boolean
files: Ref<UseUploadFile[]>
}

interface UploadOptions {
url: string
headers?: []
method?: string
maxCount: number // default Infinity
maxSize: number // default Infinity
}

// 文件类型默认是 formData 格式,如果是其他的格式需要根据 ContentType 进行处理
Expand All @@ -48,7 +53,8 @@ const uploadFile = (options: UploadOptions, file: File) => {

type Base64 = string

const useFileReader = (file: File): Promise<Base64> => new Promise((resolve) => {
const useFileReader = (file: File): Promise<Base64> =>
new Promise((resolve) => {
const reader = new FileReader()
reader.onload = (e) => {
resolve(e.target.result)
Expand All @@ -57,13 +63,13 @@ const useFileReader = (file: File): Promise<Base64> => new Promise((resolve) =>
})

const genUploadFile = async (file: File): UseUploadFile => ({
file,
name: file.name.split('.')[0],
ext: file.name.split('.')[1],
data: await useFileReader(file),
url: '',
status: 'ready',
})
file,
name: file.name.split('.')[0],
ext: file.name.split('.')[1],
data: await useFileReader(file),
url: '',
status: 'ready',
})

export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
const { url, multiple = false, accept = '' } = options
Expand All @@ -72,8 +78,7 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {

const upload = () => {
files.value.forEach((file, index) => {
uploadFile({ url }, file)
.then((_result) => {
uploadFile({ url }, file).then((_result) => {
files.value[index].status = 'success'
})
})
Expand Down Expand Up @@ -104,7 +109,7 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
}
files.value = [
...files.value,
...await (file as File[]).map(async (f) => genUploadFile(f)),
...(await (file as File[]).map(async (f) => genUploadFile(f))),
]
} else {
if (!validFileType(file as File)) {
Expand All @@ -114,9 +119,29 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
}
}

const remove = (index: number[] | number): UseUploadFile[] => {
if (index >= files.value.length) {
console.warn('cannot find file by index.')
return false
}
if (Array.isArray(index)) {
const removeFiles = files.value.filter((_, i) => index.includes(i))
files.value = files.value.filter((_, i) => !index.includes(i))
return removeFiles
}
return files.value.splice(index, 1)
}

const clear = (): boolean => {
files.value = []
return true
}

return {
upload,
append,
files,
append,
remove,
clear,
}
}

0 comments on commit 23f0d4b

Please sign in to comment.