2022-03-05 23:09:31 +08:00
|
|
|
import { NOOP } from '@vue/shared'
|
|
|
|
import { buildProps, definePropType, mutable } from '@element-plus/utils'
|
|
|
|
import { ajaxUpload } from './ajax'
|
|
|
|
|
|
|
|
import type { UploadAjaxError } from './ajax'
|
|
|
|
import type { Awaitable } from '@element-plus/utils'
|
|
|
|
import type { ExtractPropTypes } from 'vue'
|
|
|
|
import type Upload from './upload.vue'
|
|
|
|
|
|
|
|
export const uploadListTypes = ['text', 'picture', 'picture-card'] as const
|
|
|
|
|
|
|
|
let fileId = 1
|
|
|
|
export const genFileId = () => Date.now() + fileId++
|
|
|
|
|
|
|
|
export type UploadStatus = 'ready' | 'uploading' | 'success' | 'fail'
|
|
|
|
export interface UploadProgressEvent extends ProgressEvent {
|
|
|
|
percent: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UploadRequestOptions {
|
|
|
|
action: string
|
|
|
|
method: string
|
|
|
|
data: Record<string, string | Blob | [string | Blob, string]>
|
|
|
|
filename: string
|
|
|
|
file: File
|
|
|
|
headers: Headers | Record<string, string | number | null | undefined>
|
|
|
|
onError: (evt: UploadAjaxError) => void
|
|
|
|
onProgress: (evt: UploadProgressEvent) => void
|
|
|
|
onSuccess: (response: any) => void
|
|
|
|
withCredentials: boolean
|
|
|
|
}
|
|
|
|
export interface UploadFile {
|
|
|
|
name: string
|
|
|
|
percentage?: number
|
|
|
|
status: UploadStatus
|
2022-03-10 23:19:44 +08:00
|
|
|
size?: number
|
2022-03-05 23:09:31 +08:00
|
|
|
response?: unknown
|
|
|
|
uid: number
|
|
|
|
url?: string
|
2022-03-10 23:19:44 +08:00
|
|
|
raw?: UploadRawFile
|
2022-03-05 23:09:31 +08:00
|
|
|
}
|
2022-03-10 23:19:44 +08:00
|
|
|
export type UploadUserFile = Omit<UploadFile, 'status' | 'uid'> &
|
|
|
|
Partial<Pick<UploadFile, 'status' | 'uid'>>
|
|
|
|
|
2022-03-05 23:09:31 +08:00
|
|
|
export type UploadFiles = UploadFile[]
|
|
|
|
export interface UploadRawFile extends File {
|
|
|
|
uid: number
|
|
|
|
}
|
|
|
|
export type UploadRequestHandler = (
|
|
|
|
options: UploadRequestOptions
|
|
|
|
) => XMLHttpRequest | Promise<unknown>
|
|
|
|
export interface UploadHooks {
|
|
|
|
beforeUpload: (
|
|
|
|
rawFile: UploadRawFile
|
|
|
|
) => Awaitable<void | undefined | null | boolean | File | Blob>
|
|
|
|
beforeRemove: (
|
|
|
|
uploadFile: UploadFile,
|
|
|
|
uploadFiles: UploadFiles
|
|
|
|
) => Awaitable<boolean>
|
|
|
|
onRemove: (uploadFile: UploadFile, uploadFiles: UploadFiles) => void
|
|
|
|
onChange: (uploadFile: UploadFile, uploadFiles: UploadFiles) => void
|
|
|
|
onPreview: (uploadFile: UploadFile) => void
|
|
|
|
onSuccess: (
|
|
|
|
response: any,
|
|
|
|
uploadFile: UploadFile,
|
|
|
|
uploadFiles: UploadFiles
|
|
|
|
) => void
|
|
|
|
onProgress: (
|
|
|
|
evt: UploadProgressEvent,
|
|
|
|
uploadFile: UploadFile,
|
|
|
|
uploadFiles: UploadFiles
|
|
|
|
) => void
|
|
|
|
onError: (
|
|
|
|
error: Error,
|
|
|
|
uploadFile: UploadFile,
|
|
|
|
uploadFiles: UploadFiles
|
|
|
|
) => void
|
2022-03-23 18:36:31 +08:00
|
|
|
onExceed: (files: File[], uploadFiles: UploadUserFile[]) => void
|
2022-03-05 23:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const uploadBaseProps = buildProps({
|
|
|
|
action: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
type: definePropType<Headers | Record<string, any>>(Object),
|
|
|
|
},
|
|
|
|
method: {
|
|
|
|
type: String,
|
|
|
|
default: 'post',
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
type: Object,
|
|
|
|
default: () => mutable({} as const),
|
|
|
|
},
|
|
|
|
multiple: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
default: 'file',
|
|
|
|
},
|
|
|
|
drag: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
withCredentials: Boolean,
|
|
|
|
showFileList: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
accept: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
default: 'select',
|
|
|
|
},
|
|
|
|
fileList: {
|
2022-03-10 23:19:44 +08:00
|
|
|
type: definePropType<UploadUserFile[]>(Array),
|
2022-03-05 23:09:31 +08:00
|
|
|
default: () => mutable([] as const),
|
|
|
|
},
|
|
|
|
autoUpload: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
listType: {
|
|
|
|
type: String,
|
|
|
|
values: uploadListTypes,
|
|
|
|
default: 'text',
|
|
|
|
},
|
|
|
|
httpRequest: {
|
|
|
|
type: definePropType<UploadRequestHandler>(Function),
|
|
|
|
default: ajaxUpload,
|
|
|
|
},
|
|
|
|
disabled: Boolean,
|
|
|
|
limit: Number,
|
|
|
|
} as const)
|
|
|
|
|
|
|
|
export const uploadProps = buildProps({
|
|
|
|
...uploadBaseProps,
|
|
|
|
beforeUpload: {
|
|
|
|
type: definePropType<UploadHooks['beforeUpload']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
beforeRemove: {
|
|
|
|
type: definePropType<UploadHooks['beforeRemove']>(Function),
|
|
|
|
},
|
|
|
|
onRemove: {
|
|
|
|
type: definePropType<UploadHooks['onRemove']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onChange: {
|
|
|
|
type: definePropType<UploadHooks['onChange']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onPreview: {
|
|
|
|
type: definePropType<UploadHooks['onPreview']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onSuccess: {
|
|
|
|
type: definePropType<UploadHooks['onSuccess']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onProgress: {
|
|
|
|
type: definePropType<UploadHooks['onProgress']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onError: {
|
|
|
|
type: definePropType<UploadHooks['onError']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
onExceed: {
|
|
|
|
type: definePropType<UploadHooks['onExceed']>(Function),
|
|
|
|
default: NOOP,
|
|
|
|
},
|
|
|
|
} as const)
|
|
|
|
|
|
|
|
export type UploadProps = ExtractPropTypes<typeof uploadProps>
|
|
|
|
|
|
|
|
export type UploadInstance = InstanceType<typeof Upload>
|