element-plus
Version:
A Component Library for Vue 3
1 lines • 13.1 kB
Source Map (JSON)
{"version":3,"file":"upload.mjs","names":[],"sources":["../../../../../../packages/components/upload/src/upload.ts"],"sourcesContent":["import { NOOP, buildProps, definePropType, mutable } from '@element-plus/utils'\nimport { ajaxUpload } from './ajax'\n\nimport type { Awaitable, Mutable } from '@element-plus/utils'\nimport type { UploadAjaxError } from './ajax'\nimport type { ExtractPublicPropTypes } from 'vue'\nimport type Upload from './upload.vue'\n\n/**\n * @deprecated Removed after 3.0.0, Use `UploadProps` instead.\n */\nexport const uploadListTypes = ['text', 'picture', 'picture-card'] as const\n\nlet fileId = 1\nexport const genFileId = () => Date.now() + fileId++\n\nexport type UploadStatus = 'ready' | 'uploading' | 'success' | 'fail'\nexport interface UploadProgressEvent extends ProgressEvent {\n percent: number\n}\n\nexport interface UploadRequestOptions {\n action: string\n method: string\n data: Record<string, string | Blob | [string | Blob, string] | string[]>\n filename: string\n file: UploadRawFile\n headers: Headers | Record<string, string | number | null | undefined>\n onError: (evt: UploadAjaxError) => void\n onProgress: (evt: UploadProgressEvent) => void\n onSuccess: (response: any) => void\n withCredentials: boolean\n}\nexport interface UploadFile {\n name: string\n percentage?: number\n status: UploadStatus\n size?: number\n response?: unknown\n uid: number\n url?: string\n raw?: UploadRawFile\n}\nexport type UploadUserFile = Omit<UploadFile, 'status' | 'uid'> &\n Partial<Pick<UploadFile, 'status' | 'uid'>>\n\nexport type UploadFiles = UploadFile[]\nexport interface UploadRawFile extends File {\n uid: number\n isDirectory?: boolean\n}\nexport type UploadRequestHandler = (\n options: UploadRequestOptions\n) => XMLHttpRequest | Promise<unknown>\nexport interface UploadHooks {\n beforeUpload: (\n rawFile: UploadRawFile\n ) => Awaitable<void | undefined | null | boolean | File | Blob>\n beforeRemove: (\n uploadFile: UploadFile,\n uploadFiles: UploadFiles\n ) => Awaitable<boolean>\n onRemove: (uploadFile: UploadFile, uploadFiles: UploadFiles) => void\n onChange: (uploadFile: UploadFile, uploadFiles: UploadFiles) => void\n onPreview: (uploadFile: UploadFile) => void\n onSuccess: (\n response: any,\n uploadFile: UploadFile,\n uploadFiles: UploadFiles\n ) => void\n onProgress: (\n evt: UploadProgressEvent,\n uploadFile: UploadFile,\n uploadFiles: UploadFiles\n ) => void\n onError: (\n error: Error,\n uploadFile: UploadFile,\n uploadFiles: UploadFiles\n ) => void\n onExceed: (files: File[], uploadFiles: UploadUserFile[]) => void\n}\n\nexport type UploadData = Mutable<Record<string, any>>\n\nexport type ListType = 'text' | 'picture' | 'picture-card'\nexport type Crossorigin = 'anonymous' | 'use-credentials' | ''\n\nexport interface UploadBaseProps {\n /**\n * @description request URL\n */\n action?: string\n /**\n * @description request headers\n */\n headers?: Headers | Record<string, any>\n /**\n * @description set upload request method\n */\n method?: string\n /**\n * @description additions options of request\n */\n data?:\n | Awaitable<UploadData>\n | ((rawFile: UploadRawFile) => Awaitable<UploadData>)\n /**\n * @description whether uploading multiple files is permitted\n */\n multiple?: boolean\n /**\n * @description key name for uploaded file\n */\n name?: string\n /**\n * @description whether to activate drag and drop mode\n */\n drag?: boolean\n /**\n * @description whether cookies are sent\n */\n withCredentials?: boolean\n /**\n * @description whether to show the uploaded file list\n */\n showFileList?: boolean\n /**\n * @description accepted [file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept), will not work when `thumbnail-mode === true`\n */\n accept?: string\n /**\n * @description default uploaded files\n */\n fileList?: UploadUserFile[]\n /**\n * @description whether to auto upload file\n */\n autoUpload?: boolean\n /**\n * @description type of file list\n */\n listType?: ListType\n /**\n * @description override default xhr behavior, allowing you to implement your own upload-file's request\n */\n httpRequest?: UploadRequestHandler\n /**\n * @description whether to disable upload\n */\n disabled?: boolean\n /**\n * @description maximum number of uploads allowed\n */\n limit?: number\n /**\n * @description whether to support uploading directory\n */\n directory?: boolean\n}\n\nexport interface UploadProps extends UploadBaseProps {\n /**\n * @description hook function before uploading with the file to be uploaded as its parameter. If `false` is returned or a `Promise` is returned and then is rejected, uploading will be aborted\n */\n beforeUpload?: UploadHooks['beforeUpload']\n /**\n * @description hook function before removing a file with the file and file list as its parameters. If `false` is returned or a `Promise` is returned and then is rejected, removing will be aborted\n */\n beforeRemove?: UploadHooks['beforeRemove']\n /**\n * @description hook function when files are removed\n */\n onRemove?: UploadHooks['onRemove']\n /**\n * @description hook function when select file or upload file success or upload file fail\n */\n onChange?: UploadHooks['onChange']\n /**\n * @description hook function when clicking the uploaded files\n */\n onPreview?: UploadHooks['onPreview']\n /**\n * @description hook function when uploaded successfully\n */\n onSuccess?: UploadHooks['onSuccess']\n /**\n * @description hook function when some progress occurs\n */\n onProgress?: UploadHooks['onProgress']\n /**\n * @description hook function when some errors occurs\n */\n onError?: UploadHooks['onError']\n /**\n * @description hook function when limit is exceeded\n */\n onExceed?: UploadHooks['onExceed']\n /**\n * @description set HTML attribute: crossorigin.\n */\n crossorigin?: Crossorigin\n}\n\n/**\n * @deprecated Removed after 3.0.0, Use `UploadBaseProps` instead.\n */\nexport const uploadBaseProps = buildProps({\n /**\n * @description request URL\n */\n action: {\n type: String,\n default: '#',\n },\n /**\n * @description request headers\n */\n headers: {\n type: definePropType<Headers | Record<string, any>>(Object),\n },\n /**\n * @description set upload request method\n */\n method: {\n type: String,\n default: 'post',\n },\n /**\n * @description additions options of request\n */\n data: {\n type: definePropType<\n | Awaitable<UploadData>\n | ((rawFile: UploadRawFile) => Awaitable<UploadData>)\n >([Object, Function, Promise]),\n default: () => mutable({} as const),\n },\n /**\n * @description whether uploading multiple files is permitted\n */\n multiple: Boolean,\n /**\n * @description key name for uploaded file\n */\n name: {\n type: String,\n default: 'file',\n },\n /**\n * @description whether to activate drag and drop mode\n */\n drag: Boolean,\n /**\n * @description whether cookies are sent\n */\n withCredentials: Boolean,\n /**\n * @description whether to show the uploaded file list\n */\n showFileList: {\n type: Boolean,\n default: true,\n },\n /**\n * @description accepted [file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept), will not work when `thumbnail-mode === true`\n */\n accept: {\n type: String,\n default: '',\n },\n /**\n * @description default uploaded files\n */\n fileList: {\n type: definePropType<UploadUserFile[]>(Array),\n default: () => mutable([] as const),\n },\n /**\n * @description whether to auto upload file\n */\n autoUpload: {\n type: Boolean,\n default: true,\n },\n /**\n * @description type of file list\n */\n listType: {\n type: String,\n values: uploadListTypes,\n default: 'text',\n },\n /**\n * @description override default xhr behavior, allowing you to implement your own upload-file's request\n */\n httpRequest: {\n type: definePropType<UploadRequestHandler>(Function),\n default: ajaxUpload,\n },\n /**\n * @description whether to disable upload\n */\n disabled: {\n type: Boolean,\n default: undefined,\n },\n /**\n * @description maximum number of uploads allowed\n */\n limit: Number,\n /**\n * @description whether to support uploading directory\n */\n directory: Boolean,\n} as const)\n\n/**\n * @deprecated Removed after 3.0.0, Use `UploadProps` instead.\n */\nexport const uploadProps = buildProps({\n ...uploadBaseProps,\n /**\n * @description hook function before uploading with the file to be uploaded as its parameter. If `false` is returned or a `Promise` is returned and then is rejected, uploading will be aborted\n */\n beforeUpload: {\n type: definePropType<UploadHooks['beforeUpload']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function before removing a file with the file and file list as its parameters. If `false` is returned or a `Promise` is returned and then is rejected, removing will be aborted\n */\n beforeRemove: {\n type: definePropType<UploadHooks['beforeRemove']>(Function),\n },\n /**\n * @description hook function when files are removed\n */\n onRemove: {\n type: definePropType<UploadHooks['onRemove']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when select file or upload file success or upload file fail\n */\n onChange: {\n type: definePropType<UploadHooks['onChange']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when clicking the uploaded files\n */\n onPreview: {\n type: definePropType<UploadHooks['onPreview']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when uploaded successfully\n */\n onSuccess: {\n type: definePropType<UploadHooks['onSuccess']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when some progress occurs\n */\n onProgress: {\n type: definePropType<UploadHooks['onProgress']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when some errors occurs\n */\n onError: {\n type: definePropType<UploadHooks['onError']>(Function),\n default: NOOP,\n },\n /**\n * @description hook function when limit is exceeded\n */\n onExceed: {\n type: definePropType<UploadHooks['onExceed']>(Function),\n default: NOOP,\n },\n /**\n * @description set HTML attribute: crossorigin.\n */\n crossorigin: {\n type: definePropType<'anonymous' | 'use-credentials' | ''>(String),\n },\n} as const)\n\n/**\n * @deprecated Removed after 3.0.0, Use `UploadProps` instead.\n */\nexport type UploadPropsPublic = ExtractPublicPropTypes<typeof uploadProps>\n\nexport type UploadInstance = InstanceType<typeof Upload> & unknown\n\nexport const uploadBasePropsDefaults = {\n action: '#',\n method: 'post',\n data: () => mutable({}),\n name: 'file',\n showFileList: true,\n accept: '',\n fileList: () => mutable([]),\n autoUpload: true,\n listType: 'text',\n httpRequest: ajaxUpload,\n disabled: undefined,\n} as const\n\nexport const uploadPropsDefaults = {\n ...uploadBasePropsDefaults,\n beforeUpload: NOOP,\n onRemove: NOOP,\n onChange: NOOP,\n onPreview: NOOP,\n onSuccess: NOOP,\n onProgress: NOOP,\n onError: NOOP,\n onExceed: NOOP,\n} as const\n"],"mappings":";;;;;;;;;AAWA,MAAa,kBAAkB;CAAC;CAAQ;CAAW;CAAe;AAElE,IAAI,SAAS;AACb,MAAa,kBAAkB,KAAK,KAAK,GAAG;;;;AAiM5C,MAAa,kBAAkB,WAAW;CAIxC,QAAQ;EACN,MAAM;EACN,SAAS;EACV;CAID,SAAS,EACP,MAAM,eAA8C,OAAO,EAC5D;CAID,QAAQ;EACN,MAAM;EACN,SAAS;EACV;CAID,MAAM;EACJ,MAAM,eAGJ;GAAC;GAAQ;GAAU;GAAQ,CAAC;EAC9B,eAAe,QAAQ,EAAE,CAAU;EACpC;CAID,UAAU;CAIV,MAAM;EACJ,MAAM;EACN,SAAS;EACV;CAID,MAAM;CAIN,iBAAiB;CAIjB,cAAc;EACZ,MAAM;EACN,SAAS;EACV;CAID,QAAQ;EACN,MAAM;EACN,SAAS;E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