fast-element-plus 1.0.0-alpha.9 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.full.js +3180 -4581
- package/dist/index.full.js.map +1 -1
- package/dist/index.full.min.js +5 -5
- package/dist/index.full.min.js.map +1 -1
- package/dist/index.full.min.mjs +5 -5
- package/dist/index.full.min.mjs.map +1 -1
- package/dist/index.full.mjs +3180 -4581
- package/dist/index.full.mjs.map +1 -1
- package/dist/styles/index.css +2 -2
- package/es/components/avatar/index.d.ts +1 -1
- package/es/components/avatar/index.mjs.map +1 -1
- package/es/components/avatar/src/avatar.d.ts +18 -102
- package/es/components/avatar/src/avatar.mjs +1 -2
- package/es/components/avatar/src/avatar.mjs.map +1 -1
- package/es/components/button/index.d.ts +1 -1
- package/es/components/button/index.mjs.map +1 -1
- package/es/components/button/src/button.d.ts +35 -180
- package/es/components/button/src/button.mjs +14 -4
- package/es/components/button/src/button.mjs.map +1 -1
- package/es/components/carNumber/src/carNumber.d.ts +58 -194
- package/es/components/contextMenu/src/contextMenu.mjs +1 -1
- package/es/components/contextMenu/src/contextMenu.mjs.map +1 -1
- package/es/components/dialog/src/dialog.d.ts +45 -297
- package/es/components/drawer/src/drawer.d.ts +72 -450
- package/es/components/form/src/form.d.ts +28 -133
- package/es/components/form/src/formItem.d.ts +32 -137
- package/es/components/iconSelector/src/iconSelector.mjs +2 -2
- package/es/components/iconSelector/src/iconSelector.mjs.map +1 -1
- package/es/components/image/src/image.d.ts +43 -253
- package/es/components/select/src/select.d.ts +25 -81
- package/es/components/select/src/select.mjs +1 -0
- package/es/components/select/src/select.mjs.map +1 -1
- package/es/components/selectPage/src/selectPage.d.ts +19 -61
- package/es/components/selectV2/src/selectV2.d.ts +25 -81
- package/es/components/selectV2/src/selectV2.mjs +1 -0
- package/es/components/selectV2/src/selectV2.mjs.map +1 -1
- package/es/components/table/src/table.d.ts +5 -12
- package/es/components/table/src/table.mjs +4 -6
- package/es/components/table/src/table.mjs.map +1 -1
- package/es/components/table/src/table.state.d.ts +1 -1
- package/es/components/table/src/table.type.d.ts +4 -1
- package/es/components/table/src/table.type.mjs.map +1 -1
- package/es/components/table/src/tableColumn.d.ts +1 -1
- package/es/components/table/src/tableColumn.mjs +7 -7
- package/es/components/table/src/tableColumn.mjs.map +1 -1
- package/es/components/table/src/tableSearchForm.d.ts +6 -4
- package/es/components/table/src/tableSearchForm.mjs +2 -2
- package/es/components/table/src/tableSearchForm.mjs.map +1 -1
- package/es/components/table/src/useTable.mjs +3 -3
- package/es/components/table/src/useTable.mjs.map +1 -1
- package/es/components/tree/src/tree.d.ts +23 -653
- package/es/components/tree/src/tree.mjs +1 -1
- package/es/components/tree/src/tree.mjs.map +1 -1
- package/es/components/tree/src/tree.type.d.ts +1 -1
- package/es/components/treeSelect/src/treeSelect.d.ts +20 -62
- package/es/components/treeSelect/src/treeSelect.mjs +1 -0
- package/es/components/treeSelect/src/treeSelect.mjs.map +1 -1
- package/es/components/upload/src/upload.d.ts +298 -460
- package/es/components/upload/src/useUpload.mjs +2 -2
- package/es/components/upload/src/useUpload.mjs.map +1 -1
- package/es/components/uploadImage/src/uploadImage.d.ts +295 -394
- package/es/components/uploadImage/src/uploadImage.mjs +1 -1
- package/es/components/uploadImage/src/uploadImage.mjs.map +1 -1
- package/es/components/uploadImages/src/uploadImages.d.ts +300 -420
- package/es/components/uploadImages/src/uploadImages.mjs +4 -4
- package/es/components/uploadImages/src/uploadImages.mjs.map +1 -1
- package/es/constants/regex.mjs +13 -13
- package/es/constants/regex.mjs.map +1 -1
- package/es/directive.d.ts +1 -1
- package/es/directives/click-copy/index.d.ts +1 -1
- package/es/directives/click-debounce/index.d.ts +1 -1
- package/es/directives/click-draggable/index.d.ts +1 -1
- package/es/directives/click-icon-copy/index.d.ts +1 -1
- package/es/directives/click-longpress/index.d.ts +1 -1
- package/es/directives/click-throttle/index.d.ts +1 -1
- package/es/element-plus.mjs +1 -1
- package/es/element-plus.mjs.map +1 -1
- package/es/make-installer.mjs +4 -0
- package/es/make-installer.mjs.map +1 -1
- package/es/version.d.ts +1 -1
- package/es/version.mjs +1 -1
- package/es/version.mjs.map +1 -1
- package/lib/components/avatar/index.d.ts +1 -1
- package/lib/components/avatar/index.js.map +1 -1
- package/lib/components/avatar/src/avatar.d.ts +18 -102
- package/lib/components/avatar/src/avatar.js +1 -1
- package/lib/components/avatar/src/avatar.js.map +1 -1
- package/lib/components/button/index.d.ts +1 -1
- package/lib/components/button/index.js.map +1 -1
- package/lib/components/button/src/button.d.ts +35 -180
- package/lib/components/button/src/button.js +1 -1
- package/lib/components/button/src/button.js.map +1 -1
- package/lib/components/carNumber/src/carNumber.d.ts +58 -194
- package/lib/components/contextMenu/src/contextMenu.js +1 -1
- package/lib/components/contextMenu/src/contextMenu.js.map +1 -1
- package/lib/components/dialog/src/dialog.d.ts +45 -297
- package/lib/components/drawer/src/drawer.d.ts +72 -450
- package/lib/components/form/src/form.d.ts +28 -133
- package/lib/components/form/src/formItem.d.ts +32 -137
- package/lib/components/image/src/image.d.ts +43 -253
- package/lib/components/select/src/select.d.ts +25 -81
- package/lib/components/select/src/select.js +1 -1
- package/lib/components/select/src/select.js.map +1 -1
- package/lib/components/selectPage/src/selectPage.d.ts +19 -61
- package/lib/components/selectV2/src/selectV2.d.ts +25 -81
- package/lib/components/selectV2/src/selectV2.js +1 -1
- package/lib/components/selectV2/src/selectV2.js.map +1 -1
- package/lib/components/table/src/table.d.ts +5 -12
- package/lib/components/table/src/table.js +1 -1
- package/lib/components/table/src/table.js.map +1 -1
- package/lib/components/table/src/table.state.d.ts +1 -1
- package/lib/components/table/src/table.type.d.ts +4 -1
- package/lib/components/table/src/table.type.js.map +1 -1
- package/lib/components/table/src/tableColumn.d.ts +1 -1
- package/lib/components/table/src/tableColumn.js +1 -1
- package/lib/components/table/src/tableColumn.js.map +1 -1
- package/lib/components/table/src/tableSearchForm.d.ts +6 -4
- package/lib/components/table/src/tableSearchForm.js +1 -1
- package/lib/components/table/src/tableSearchForm.js.map +1 -1
- package/lib/components/table/src/useTable.js +1 -1
- package/lib/components/table/src/useTable.js.map +1 -1
- package/lib/components/tree/src/tree.d.ts +23 -653
- package/lib/components/tree/src/tree.js +1 -1
- package/lib/components/tree/src/tree.js.map +1 -1
- package/lib/components/tree/src/tree.type.d.ts +1 -1
- package/lib/components/treeSelect/src/treeSelect.d.ts +20 -62
- package/lib/components/treeSelect/src/treeSelect.js +1 -1
- package/lib/components/treeSelect/src/treeSelect.js.map +1 -1
- package/lib/components/upload/src/upload.d.ts +298 -460
- package/lib/components/upload/src/useUpload.js +1 -1
- package/lib/components/upload/src/useUpload.js.map +1 -1
- package/lib/components/uploadImage/src/uploadImage.d.ts +295 -394
- package/lib/components/uploadImage/src/uploadImage.js +1 -1
- package/lib/components/uploadImage/src/uploadImage.js.map +1 -1
- package/lib/components/uploadImages/src/uploadImages.d.ts +300 -420
- package/lib/components/uploadImages/src/uploadImages.js +1 -1
- package/lib/components/uploadImages/src/uploadImages.js.map +1 -1
- package/lib/constants/regex.js +1 -1
- package/lib/constants/regex.js.map +1 -1
- package/lib/directive.d.ts +1 -1
- package/lib/directives/click-copy/index.d.ts +1 -1
- package/lib/directives/click-debounce/index.d.ts +1 -1
- package/lib/directives/click-draggable/index.d.ts +1 -1
- package/lib/directives/click-icon-copy/index.d.ts +1 -1
- package/lib/directives/click-longpress/index.d.ts +1 -1
- package/lib/directives/click-throttle/index.d.ts +1 -1
- package/lib/element-plus.js +1 -1
- package/lib/element-plus.js.map +1 -1
- package/lib/make-installer.js +1 -1
- package/lib/make-installer.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/lib/version.js.map +1 -1
- package/package.json +8 -8
- package/styles/components/contextMenu.scss +2 -2
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),l=require("element-plus"),a=require("@fast-china/utils"),i=require("@vueuse/core"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("vue"),l=require("element-plus"),a=require("@fast-china/utils"),i=require("@vueuse/core"),u=require("decimal.js"),r=require("lodash-unified"),t=require("../utils/upload.js");exports.useUpload=(o,n,s,d,p)=>{const c=i.useVModel(s,"fileList",d,{passive:!0}),m=e.ref(!1),f=e.inject(l.formContextKey,void 0),g=e.inject(l.formItemContextKey,void 0),v=new u.Decimal(1024),h=new u.Decimal(r.isNumber(p?.maxSize)?p?.maxSize:Number(p?.maxSize)),$=h.div(v);e.onMounted(()=>{!s.autoUpload||p.uploadApi||p.uploadUrl||a.consoleWarn(o,"['uploadApi', 'uploadUrl'] 属性必须二选一。")});const y=()=>{if(c.value.length>0)if(r.isString(s.modelValue))d("update:modelValue",c.value[0].url),d("change",c.value[0].url);else if(s.multiple){const e=c.value.map(e=>e.url);d("update:modelValue",e),d("change",e)}else d("update:modelValue",c.value[0].url),d("change",c.value[0].url);else d("update:modelValue",null),d("change",null)};return e.watch(()=>s.modelValue,e=>{if(e)if(r.isArray(e))c.value=e.map(e=>{const a=c.value.find(l=>l.url===e);return{name:"",status:"success",uid:a?.uid??l.genFileId(),url:e}});else{const a=c.value.find(l=>l.url===e);c.value=[{name:"",status:"success",uid:a?.uid??l.genFileId(),url:e}]}},{immediate:!0}),{fileList:c,loading:m,formContext:f,formItemContext:g,maxSizeMB:$,handleValue:y,handleHttpRequest:async e=>{let i;if(s.data&&(i=t.uploadUtil.getPropsData(e.file,s.data)),!p?.uploadApi&&!p?.uploadUrl)return l.ElMessage.error(`上传${n}Api或地址不能为空`),void a.consoleError(o,`上传${n}接口 “uploadApi” 或地址 “uploadUrl” 不能为空`);m.value=!0;try{let l;l=p.uploadApi?await t.uploadUtil.uploadFileByApi(p.uploadApi,e.file,e.filename,i):await t.uploadUtil.uploadFile(p.uploadUrl,e.file,e.filename,i),e.onSuccess(l)}finally{m.value=!1}},handleOnSuccess:(e,a,i)=>{e&&(!s.multiple&&i.length>1&&i.shift(),a.url=e,y(),g?.prop&&f?.validateField([g.prop]),l.ElMessage.success("上传成功"),s.onSuccess&&s.onSuccess(e,a,i))},handleOnError:(e,a,i)=>{l.ElNotification({message:`【${a.name}】${n}上传失败,请您重新上传`,type:"error"}),s.onError&&s.onError(e,a,i)},handleOnExceed:(e,a)=>{l.ElMessage.warning(`最多只能上传 ${s.limit} 个${n},请移除后再进行上传`),s.onExceed&&s.onExceed(e,a)},handleOnUpload:e=>{if(new u.Decimal(e.size).div(v).greaterThan(h))return a.consoleWarn(o,`【${e.name}】${n}上传大小不能超过 ${$.toString()}MB`),l.ElMessage.warning(`【${e.name}】${n}上传大小不能超过 ${$.toString()}MB`),!1;const i="type"in e?e.type:e.raw.type;if(s.accept&&s.accept.split(",").every(e=>e!==i)){const e=t.uploadUtil.detectFileType(s.accept);return a.consoleError(o,`只允许上传【${e}】格式的${n}`),l.ElMessage.error(`只允许上传【${e}】格式的${n}`),!1}return!0}}};
|
|
2
2
|
//# sourceMappingURL=useUpload.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUpload.js","sources":["../../../../../packages/components/upload/src/useUpload.ts"],"sourcesContent":["import { inject, onMounted, ref, watch } from \"vue\";\nimport { ElMessage, ElNotification, formContextKey, formItemContextKey, genFileId } from \"element-plus\";\nimport { consoleError, consoleWarn } from \"@fast-china/utils\";\nimport { useVModel } from \"@vueuse/core\";\nimport { Decimal } from \"decimal.js\";\nimport { isArray, isNumber, isString } from \"lodash-unified\";\nimport { uploadUtil } from \"../utils/upload\";\nimport type { UploadFile, UploadFiles, UploadProps, UploadRawFile, UploadRequestOptions, UploadUserFile } from \"element-plus\";\n\nexport const useUpload = <T extends string | string[]>(\n\tcomponentName: string,\n\tfileTypeName: string,\n\tprops: UploadProps & {\n\t\tmodelValue: T;\n\t},\n\temit: ((event: \"update:fileList\", value: UploadUserFile[]) => void) &\n\t\t((event: \"update:modelValue\", value: T) => void) &\n\t\t((event: \"change\", value: T) => void),\n\tdata?: {\n\t\tmaxSize?: string | number;\n\t\tuploadApi?: (formData: FormData) => Promise<string>;\n\t\tuploadUrl?: string;\n\t}\n\t// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types\n) => {\n\tconst fileList = useVModel(props, \"fileList\", emit, { passive: true });\n\n\tconst loading = ref(false);\n\n\t// 获取 el-form 组件上下文\n\tconst formContext = inject(formContextKey, undefined);\n\t// 获取 el-form-item 组件上下文\n\tconst formItemContext = inject(formItemContextKey, undefined);\n\n\tconst mbNum = new Decimal(1024);\n\tconst maxSizeKB = new Decimal(isNumber(data?.maxSize) ? data?.maxSize : Number(data?.maxSize));\n\tconst maxSizeMB = maxSizeKB.div(mbNum);\n\n\tonMounted(() => {\n\t\tif (!data.uploadApi && !data.uploadUrl) {\n\t\t\tconsoleWarn(componentName, \"['uploadApi', 'uploadUrl'] 属性必须二选一。\");\n\t\t}\n\t});\n\n\tconst handleValue = (): void => {\n\t\tif (fileList.value.length > 0) {\n\t\t\tif (isString(props.modelValue)) {\n\t\t\t\temit(\"update:modelValue\", fileList.value[0].url as T);\n\t\t\t\temit(\"change\", fileList.value[0].url as T);\n\t\t\t} else {\n\t\t\t\tif (props.multiple) {\n\t\t\t\t\tconst value = fileList.value.map((m) => m.url);\n\t\t\t\t\temit(\"update:modelValue\", value as T);\n\t\t\t\t\temit(\"change\", value as T);\n\t\t\t\t} else {\n\t\t\t\t\temit(\"update:modelValue\", fileList.value[0].url as T);\n\t\t\t\t\temit(\"change\", fileList.value[0].url as T);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\temit(\"update:modelValue\", null);\n\t\t\temit(\"change\", null);\n\t\t}\n\t};\n\n\tconst handleHttpRequest = async (options: UploadRequestOptions): Promise<void> => {\n\t\tlet propsData;\n\t\tif (props.data) {\n\t\t\tpropsData = uploadUtil.getPropsData(options.file, props.data);\n\t\t}\n\t\tif (!data?.uploadUrl && !data?.uploadUrl) {\n\t\t\tElMessage.error(`上传${fileTypeName}Api或地址不能为空`);\n\t\t\tconsoleError(componentName, `上传${fileTypeName}接口 “uploadApi” 或地址 “uploadUrl” 不能为空`);\n\t\t\treturn;\n\t\t}\n\t\tloading.value = true;\n\t\ttry {\n\t\t\tlet fileUrl: string;\n\t\t\tif (data.uploadApi) {\n\t\t\t\tfileUrl = await uploadUtil.uploadFileByApi(data.uploadApi, options.file, options.filename, propsData);\n\t\t\t} else {\n\t\t\t\tfileUrl = await uploadUtil.uploadFile(data.uploadUrl, options.file, options.filename, propsData);\n\t\t\t}\n\t\t\toptions.onSuccess(fileUrl);\n\t\t} finally {\n\t\t\tloading.value = false;\n\t\t}\n\t};\n\n\tconst handleOnSuccess = (fileUrl: string, uploadFile: UploadFile, uploadFiles: UploadFiles): void => {\n\t\tif (!fileUrl) return;\n\t\tif (!props.multiple && uploadFiles.length > 1) {\n\t\t\tuploadFiles.shift();\n\t\t}\n\t\tuploadFile.url = fileUrl;\n\t\thandleValue();\n\t\t// 调用 el-form 内部的校验方法(可自动校验)\n\t\tformItemContext?.prop && formContext?.validateField([formItemContext.prop as string]);\n\t\tElMessage.success(\"上传成功\");\n\t\tprops.onSuccess && props.onSuccess(fileUrl, uploadFile, uploadFiles);\n\t};\n\n\tconst handleOnError = (error: Error, uploadFile: UploadFile, uploadFiles: UploadFiles): void => {\n\t\tElNotification({\n\t\t\tmessage: `【${uploadFile.name}】${fileTypeName}上传失败,请您重新上传`,\n\t\t\ttype: \"error\",\n\t\t});\n\t\tprops.onError && props.onError(error, uploadFile, uploadFiles);\n\t};\n\n\tconst handleOnExceed = (files: File[], uploadFiles: UploadUserFile[]): void => {\n\t\tElMessage.warning(`最多只能上传 ${props.limit} 个${fileTypeName},请移除后再进行上传`);\n\t\tprops.onExceed && props.onExceed(files, uploadFiles);\n\t};\n\n\tconst handleOnUpload = (file: UploadFile | UploadRawFile): boolean => {\n\t\tconst fileSizeKB = new Decimal(file.size).div(mbNum);\n\n\t\tif (fileSizeKB.greaterThan(maxSizeKB)) {\n\t\t\tconsoleWarn(componentName, `【${file.name}】${fileTypeName}上传大小不能超过 ${maxSizeMB.toString()}MB`);\n\t\t\tElMessage.warning(`【${file.name}】${fileTypeName}上传大小不能超过 ${maxSizeMB.toString()}MB`);\n\t\t\treturn false;\n\t\t}\n\n\t\tconst fileType = \"type\" in file ? file.type : file.raw.type;\n\n\t\tif (props.accept && props.accept.split(\",\").every((e) => e !== fileType)) {\n\t\t\tconst uploadFileNames = uploadUtil.detectFileType(props.accept);\n\t\t\tconsoleError(componentName, `只允许上传【${uploadFileNames}】格式的${fileTypeName}`);\n\t\t\tElMessage.error(`只允许上传【${uploadFileNames}】格式的${fileTypeName}`);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t};\n\n\t/**\n\t * 监听 v-model 绑定数据\n\t */\n\twatch(\n\t\t() => props.modelValue,\n\t\t(newValue) => {\n\t\t\tif (newValue) {\n\t\t\t\tif (isArray(newValue)) {\n\t\t\t\t\tfileList.value = newValue.map((m) => {\n\t\t\t\t\t\tconst find = fileList.value.find((f) => f.url === m);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tname: \"\",\n\t\t\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\t\t\tuid: find?.uid ?? genFileId(),\n\t\t\t\t\t\t\turl: m,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tconst find = fileList.value.find((f) => f.url === newValue);\n\t\t\t\t\tfileList.value = [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"\",\n\t\t\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\t\t\tuid: find?.uid ?? genFileId(),\n\t\t\t\t\t\t\turl: newValue,\n\t\t\t\t\t\t},\n\t\t\t\t\t];\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\timmediate: true,\n\t\t}\n\t);\n\n\treturn {\n\t\tfileList,\n\t\tloading,\n\t\tformContext,\n\t\tformItemContext,\n\t\tmaxSizeMB,\n\t\thandleValue,\n\t\thandleHttpRequest,\n\t\thandleOnSuccess,\n\t\thandleOnError,\n\t\thandleOnExceed,\n\t\thandleOnUpload,\n\t};\n};\n"],"names":["componentName","fileTypeName","props","emit","data","fileList","useVModel","passive","loading","ref","formContext","inject","formContextKey","formItemContext","formItemContextKey","mbNum","Decimal","maxSizeKB","isNumber","maxSize","Number","maxSizeMB","div","onMounted","uploadApi","uploadUrl","consoleWarn","handleValue","value","length","isString","modelValue","url","multiple","map","m","watch","newValue","isArray","find","f","name","status","uid","genFileId","immediate","handleHttpRequest","async","options","propsData","uploadUtil","getPropsData","file","ElMessage","error","consoleError","fileUrl","uploadFileByApi","filename","uploadFile","onSuccess","handleOnSuccess","uploadFiles","shift","prop","validateField","success","handleOnError","ElNotification","message","type","onError","handleOnExceed","files","warning","limit","onExceed","handleOnUpload","size","greaterThan","toString","fileType","raw","accept","split","every","e","uploadFileNames","detectFileType"],"mappings":"gSASyB,CACxBA,EACAC,EACAC,EAGAC,EAGAC,KAOA,MAAMC,EAAWC,EAAAA,UAAUJ,EAAO,WAAYC,EAAM,CAAEI,SAAS,IAEzDC,EAAUC,EAAAA,KAAI,GAGdC,EAAcC,EAAAA,OAAOC,EAAAA,oBAAgB,GAErCC,EAAkBF,EAAAA,OAAOG,EAAAA,wBAAoB,GAE7CC,EAAQ,IAAIC,EAAAA,QAAQ,MACpBC,EAAY,IAAID,UAAQE,EAAAA,SAASd,GAAMe,SAAWf,GAAMe,QAAUC,OAAOhB,GAAMe,UAC/EE,EAAYJ,EAAUK,IAAIP,GAEhCQ,EAAAA,UAAU,KACJnB,EAAKoB,WAAcpB,EAAKqB,WAC5BC,EAAAA,YAAY1B,EAAe,yCAI7B,MAAM2B,EAAc,KACnB,GAAItB,EAASuB,MAAMC,OAAS,EAC3B,GAAIC,EAAAA,SAAS5B,EAAM6B,YAClB5B,EAAK,oBAAqBE,EAASuB,MAAM,GAAGI,KAC5C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iC,QAAsBtF,MACxC,CACR,CAEA,OAAO"}
|
|
1
|
+
{"version":3,"file":"useUpload.js","sources":["../../../../../packages/components/upload/src/useUpload.ts"],"sourcesContent":["import { inject, onMounted, ref, watch } from \"vue\";\nimport { ElMessage, ElNotification, formContextKey, formItemContextKey, genFileId } from \"element-plus\";\nimport { consoleError, consoleWarn } from \"@fast-china/utils\";\nimport { useVModel } from \"@vueuse/core\";\nimport { Decimal } from \"decimal.js\";\nimport { isArray, isNumber, isString } from \"lodash-unified\";\nimport { uploadUtil } from \"../utils/upload\";\nimport type { UploadFile, UploadFiles, UploadProps, UploadRawFile, UploadRequestOptions, UploadUserFile } from \"element-plus\";\n\nexport const useUpload = <T extends string | string[]>(\n\tcomponentName: string,\n\tfileTypeName: string,\n\tprops: UploadProps & {\n\t\tmodelValue: T;\n\t},\n\temit: ((event: \"update:fileList\", value: UploadUserFile[]) => void) &\n\t\t((event: \"update:modelValue\", value: T) => void) &\n\t\t((event: \"change\", value: T) => void),\n\tdata?: {\n\t\tmaxSize?: string | number;\n\t\tuploadApi?: (formData: FormData) => Promise<string>;\n\t\tuploadUrl?: string;\n\t}\n\t// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types\n) => {\n\tconst fileList = useVModel(props, \"fileList\", emit, { passive: true });\n\n\tconst loading = ref(false);\n\n\t// 获取 el-form 组件上下文\n\tconst formContext = inject(formContextKey, undefined);\n\t// 获取 el-form-item 组件上下文\n\tconst formItemContext = inject(formItemContextKey, undefined);\n\n\tconst mbNum = new Decimal(1024);\n\tconst maxSizeKB = new Decimal(isNumber(data?.maxSize) ? data?.maxSize : Number(data?.maxSize));\n\tconst maxSizeMB = maxSizeKB.div(mbNum);\n\n\tonMounted(() => {\n\t\tif (props.autoUpload && !data.uploadApi && !data.uploadUrl) {\n\t\t\tconsoleWarn(componentName, \"['uploadApi', 'uploadUrl'] 属性必须二选一。\");\n\t\t}\n\t});\n\n\tconst handleValue = (): void => {\n\t\tif (fileList.value.length > 0) {\n\t\t\tif (isString(props.modelValue)) {\n\t\t\t\temit(\"update:modelValue\", fileList.value[0].url as T);\n\t\t\t\temit(\"change\", fileList.value[0].url as T);\n\t\t\t} else {\n\t\t\t\tif (props.multiple) {\n\t\t\t\t\tconst value = fileList.value.map((m) => m.url);\n\t\t\t\t\temit(\"update:modelValue\", value as T);\n\t\t\t\t\temit(\"change\", value as T);\n\t\t\t\t} else {\n\t\t\t\t\temit(\"update:modelValue\", fileList.value[0].url as T);\n\t\t\t\t\temit(\"change\", fileList.value[0].url as T);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\temit(\"update:modelValue\", null);\n\t\t\temit(\"change\", null);\n\t\t}\n\t};\n\n\tconst handleHttpRequest = async (options: UploadRequestOptions): Promise<void> => {\n\t\tlet propsData;\n\t\tif (props.data) {\n\t\t\tpropsData = uploadUtil.getPropsData(options.file, props.data);\n\t\t}\n\t\tif (!data?.uploadApi && !data?.uploadUrl) {\n\t\t\tElMessage.error(`上传${fileTypeName}Api或地址不能为空`);\n\t\t\tconsoleError(componentName, `上传${fileTypeName}接口 “uploadApi” 或地址 “uploadUrl” 不能为空`);\n\t\t\treturn;\n\t\t}\n\t\tloading.value = true;\n\t\ttry {\n\t\t\tlet fileUrl: string;\n\t\t\tif (data.uploadApi) {\n\t\t\t\tfileUrl = await uploadUtil.uploadFileByApi(data.uploadApi, options.file, options.filename, propsData);\n\t\t\t} else {\n\t\t\t\tfileUrl = await uploadUtil.uploadFile(data.uploadUrl, options.file, options.filename, propsData);\n\t\t\t}\n\t\t\toptions.onSuccess(fileUrl);\n\t\t} finally {\n\t\t\tloading.value = false;\n\t\t}\n\t};\n\n\tconst handleOnSuccess = (fileUrl: string, uploadFile: UploadFile, uploadFiles: UploadFiles): void => {\n\t\tif (!fileUrl) return;\n\t\tif (!props.multiple && uploadFiles.length > 1) {\n\t\t\tuploadFiles.shift();\n\t\t}\n\t\tuploadFile.url = fileUrl;\n\t\thandleValue();\n\t\t// 调用 el-form 内部的校验方法(可自动校验)\n\t\tformItemContext?.prop && formContext?.validateField([formItemContext.prop as string]);\n\t\tElMessage.success(\"上传成功\");\n\t\tprops.onSuccess && props.onSuccess(fileUrl, uploadFile, uploadFiles);\n\t};\n\n\tconst handleOnError = (error: Error, uploadFile: UploadFile, uploadFiles: UploadFiles): void => {\n\t\tElNotification({\n\t\t\tmessage: `【${uploadFile.name}】${fileTypeName}上传失败,请您重新上传`,\n\t\t\ttype: \"error\",\n\t\t});\n\t\tprops.onError && props.onError(error, uploadFile, uploadFiles);\n\t};\n\n\tconst handleOnExceed = (files: File[], uploadFiles: UploadUserFile[]): void => {\n\t\tElMessage.warning(`最多只能上传 ${props.limit} 个${fileTypeName},请移除后再进行上传`);\n\t\tprops.onExceed && props.onExceed(files, uploadFiles);\n\t};\n\n\tconst handleOnUpload = (file: UploadFile | UploadRawFile): boolean => {\n\t\tconst fileSizeKB = new Decimal(file.size).div(mbNum);\n\n\t\tif (fileSizeKB.greaterThan(maxSizeKB)) {\n\t\t\tconsoleWarn(componentName, `【${file.name}】${fileTypeName}上传大小不能超过 ${maxSizeMB.toString()}MB`);\n\t\t\tElMessage.warning(`【${file.name}】${fileTypeName}上传大小不能超过 ${maxSizeMB.toString()}MB`);\n\t\t\treturn false;\n\t\t}\n\n\t\tconst fileType = \"type\" in file ? file.type : file.raw.type;\n\n\t\tif (props.accept && props.accept.split(\",\").every((e) => e !== fileType)) {\n\t\t\tconst uploadFileNames = uploadUtil.detectFileType(props.accept);\n\t\t\tconsoleError(componentName, `只允许上传【${uploadFileNames}】格式的${fileTypeName}`);\n\t\t\tElMessage.error(`只允许上传【${uploadFileNames}】格式的${fileTypeName}`);\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t};\n\n\t/**\n\t * 监听 v-model 绑定数据\n\t */\n\twatch(\n\t\t() => props.modelValue,\n\t\t(newValue) => {\n\t\t\tif (newValue) {\n\t\t\t\tif (isArray(newValue)) {\n\t\t\t\t\tfileList.value = newValue.map((m) => {\n\t\t\t\t\t\tconst find = fileList.value.find((f) => f.url === m);\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tname: \"\",\n\t\t\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\t\t\tuid: find?.uid ?? genFileId(),\n\t\t\t\t\t\t\turl: m,\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tconst find = fileList.value.find((f) => f.url === newValue);\n\t\t\t\t\tfileList.value = [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"\",\n\t\t\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\t\t\tuid: find?.uid ?? genFileId(),\n\t\t\t\t\t\t\turl: newValue,\n\t\t\t\t\t\t},\n\t\t\t\t\t];\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\timmediate: true,\n\t\t}\n\t);\n\n\treturn {\n\t\tfileList,\n\t\tloading,\n\t\tformContext,\n\t\tformItemContext,\n\t\tmaxSizeMB,\n\t\thandleValue,\n\t\thandleHttpRequest,\n\t\thandleOnSuccess,\n\t\thandleOnError,\n\t\thandleOnExceed,\n\t\thandleOnUpload,\n\t};\n};\n"],"names":["componentName","fileTypeName","props","emit","data","fileList","useVModel","passive","loading","ref","formContext","inject","formContextKey","formItemContext","formItemContextKey","mbNum","Decimal","maxSizeKB","isNumber","maxSize","Number","maxSizeMB","div","onMounted","autoUpload","uploadApi","uploadUrl","consoleWarn","handleValue","value","length","isString","modelValue","url","multiple","map","m","watch","newValue","isArray","find","f","name","status","uid","genFileId","immediate","handleHttpRequest","async","options","propsData","uploadUtil","getPropsData","file","ElMessage","error","consoleError","fileUrl","uploadFileByApi","filename","uploadFile","onSuccess","handleOnSuccess","uploadFiles","shift","prop","validateField","success","handleOnError","ElNotification","message","type","onError","handleOnExceed","files","warning","limit","onExceed","handleOnUpload","size","greaterThan","toString","fileType","raw","accept","split","every","e","uploadFileNames","detectFileType"],"mappings":"g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