{"version":3,"file":"use-handlers.js","sources":["../../../../../../packages/components/upload/src/use-handlers.ts"],"sourcesContent":["import { watch } from 'vue'\nimport { isNil } from 'lodash-unified'\nimport { useVModel } from '@vueuse/core'\nimport { debugWarn, throwError } from '@element-plus/utils'\nimport { genFileId } from './upload'\nimport type { ShallowRef } from 'vue'\nimport type {\n UploadContentInstance,\n UploadContentProps,\n} from './upload-content'\nimport type {\n UploadFile,\n UploadFiles,\n UploadProps,\n UploadRawFile,\n UploadStatus,\n} from './upload'\n\nconst SCOPE = 'ElUpload'\n\nconst revokeFileObjectURL = (file: UploadFile) => {\n if (file.url?.startsWith('blob:')) {\n URL.revokeObjectURL(file.url)\n }\n}\n\nexport const useHandlers = (\n props: UploadProps,\n uploadRef: ShallowRef\n) => {\n const uploadFiles = useVModel(\n props as Omit & { fileList: UploadFiles },\n 'fileList',\n undefined,\n { passive: true }\n )\n\n const getFile = (rawFile: UploadRawFile) =>\n uploadFiles.value.find((file) => file.uid === rawFile.uid)\n\n function abort(file: UploadFile) {\n uploadRef.value?.abort(file)\n }\n\n function clearFiles(\n /** @default ['ready', 'uploading', 'success', 'fail'] */\n states: UploadStatus[] = ['ready', 'uploading', 'success', 'fail']\n ) {\n uploadFiles.value = uploadFiles.value.filter(\n (row) => !states.includes(row.status)\n )\n }\n\n function removeFile(file: UploadFile) {\n uploadFiles.value = uploadFiles.value.filter(\n (uploadFile) => uploadFile !== file\n )\n }\n\n const handleError: UploadContentProps['onError'] = (err, rawFile) => {\n const file = getFile(rawFile)\n if (!file) return\n\n console.error(err)\n file.status = 'fail'\n removeFile(file)\n props.onError(err, file, uploadFiles.value)\n props.onChange(file, uploadFiles.value)\n }\n\n const handleProgress: UploadContentProps['onProgress'] = (evt, rawFile) => {\n const file = getFile(rawFile)\n if (!file) return\n\n props.onProgress(evt, file, uploadFiles.value)\n file.status = 'uploading'\n file.percentage = Math.round(evt.percent)\n }\n\n const handleSuccess: UploadContentProps['onSuccess'] = (\n response,\n rawFile\n ) => {\n const file = getFile(rawFile)\n if (!file) return\n\n file.status = 'success'\n file.response = response\n props.onSuccess(response, file, uploadFiles.value)\n props.onChange(file, uploadFiles.value)\n }\n\n const handleStart: UploadContentProps['onStart'] = (file) => {\n if (isNil(file.uid)) file.uid = genFileId()\n const uploadFile: UploadFile = {\n name: file.name,\n percentage: 0,\n status: 'ready',\n size: file.size,\n raw: file,\n uid: file.uid,\n }\n if (props.listType === 'picture-card' || props.listType === 'picture') {\n try {\n uploadFile.url = URL.createObjectURL(file)\n } catch (err: unknown) {\n debugWarn(SCOPE, (err as Error).message)\n props.onError(err as Error, uploadFile, uploadFiles.value)\n }\n }\n uploadFiles.value = [...uploadFiles.value, uploadFile]\n props.onChange(uploadFile, uploadFiles.value)\n }\n\n const handleRemove: UploadContentProps['onRemove'] = async (\n file\n ): Promise => {\n const uploadFile = file instanceof File ? getFile(file) : file\n if (!uploadFile) throwError(SCOPE, 'file to be removed not found')\n\n const doRemove = (file: UploadFile) => {\n abort(file)\n removeFile(file)\n props.onRemove(file, uploadFiles.value)\n revokeFileObjectURL(file)\n }\n\n if (props.beforeRemove) {\n const before = await props.beforeRemove(uploadFile, uploadFiles.value)\n if (before !== false) doRemove(uploadFile)\n } else {\n doRemove(uploadFile)\n }\n }\n\n function submit() {\n uploadFiles.value\n .filter(({ status }) => status === 'ready')\n .forEach(({ raw }) => raw && uploadRef.value?.upload(raw))\n }\n\n watch(\n () => props.listType,\n (val) => {\n if (val !== 'picture-card' && val !== 'picture') {\n return\n }\n\n uploadFiles.value = uploadFiles.value.map((file) => {\n const { raw, url } = file\n if (!url && raw) {\n try {\n file.url = URL.createObjectURL(raw)\n } catch (err: unknown) {\n props.onError(err as Error, file, uploadFiles.value)\n }\n }\n return file\n })\n }\n )\n\n watch(\n uploadFiles,\n (files) => {\n for (const file of files) {\n file.uid ||= genFileId()\n file.status ||= 'success'\n }\n },\n { immediate: true, deep: true }\n )\n\n return {\n /** @description two-way binding ref from props `fileList` */\n uploadFiles,\n abort,\n clearFiles,\n handleError,\n handleProgress,\n handleStart,\n handleSuccess,\n handleRemove,\n submit,\n revokeFileObjectURL,\n }\n}\n"],"names":["useVModel","isNil","genFileId","debugWarn","throwError","watch"],"mappings":";;;;;;;;;;;AAKA,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,mBAAmB,GAAG,CAAC,IAAI,KAAK;AACtC,EAAE,IAAI,EAAE,CAAC;AACT,EAAE,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,KAAK,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;AACjE,IAAI,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClC,GAAG;AACH,CAAC,CAAC;AACU,MAAC,WAAW,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK;AACjD,EAAE,MAAM,WAAW,GAAGA,cAAS,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9E,EAAE,MAAM,OAAO,GAAG,CAAC,OAAO,KAAK,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAC1F,EAAE,SAAS,KAAK,CAAC,IAAI,EAAE;AACvB,IAAI,IAAI,EAAE,CAAC;AACX,IAAI,CAAC,EAAE,GAAG,SAAS,CAAC,KAAK,KAAK,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAC7D,GAAG;AACH,EAAE,SAAS,UAAU,CAAC,MAAM,GAAG,CAAC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE;AAC1E,IAAI,WAAW,CAAC,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC,M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