UNPKG

vue-amazing-ui

Version:

An Amazing Vue3 UI Components Library, Using TypeScript.

2 lines (1 loc) 9.57 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),H=require("../spin/index.cjs"),$=require("../message/index.cjs"),S=require("../image/index.cjs"),I=require("../space/index.cjs"),_=require("../utils/index.cjs"),T=["onDrop","onClick"],j=["accept","multiple","onChange"],A={class:"upload-tip"},O={class:"file-uploading"},W={key:0,class:"file-preview"},G={key:1,class:"file-svg",focusable:"false","data-icon":"file-pdf",width:"1em",height:"1em",fill:"currentColor","aria-hidden":"true",viewBox:"64 64 896 896"},J={key:2,class:"file-svg",focusable:"false","data-icon":"file",width:"1em",height:"1em",fill:"currentColor","aria-hidden":"true",viewBox:"64 64 896 896"},K={class:"file-mask"},Q=["onClick"],X=["onClick"],Y=e.defineComponent({__name:"Upload",props:{accept:{default:"*"},multiple:{type:Boolean,default:!1},maxCount:{default:void 0},tip:{default:"Upload"},fit:{default:"contain"},draggable:{type:Boolean,default:!0},disabled:{type:Boolean,default:!1},spaceProps:{default:()=>({})},spinProps:{default:()=>({})},imageProps:{default:()=>({})},messageProps:{default:()=>({})},actionMessage:{default:()=>({upload:"上传成功",remove:"删除成功"})},beforeUpload:{type:Function,default:()=>!0},uploadMode:{default:"base64"},customRequest:{type:Function,default:()=>{}},fileList:{default:()=>[]}},emits:["update:fileList","drop","change","preview","remove"],setup(w,{expose:k,emit:y}){const i=w,n=e.ref([]),f=e.ref(1),p=e.ref([]),v=e.ref(),g=e.ref(),u=e.ref(),{colorPalettes:V}=_.useInject("Upload"),c=y,d=e.computed(()=>i.maxCount===void 0?1/0:i.maxCount);e.watchEffect(()=>{b()});function b(){n.value=[...i.fileList],n.value.length>d.value&&n.value.splice(d.value),i.disabled?f.value=n.value.length:n.value.length<d.value?f.value=i.fileList.length+1:f.value=d.value}function m(t){const o=/\.(jpg|jpeg|png|gif)$/i,l=/^data:image/;return o.test(t)||l.test(t)}function M(t){const o=/\.pdf$/i,l=/^data:application\/pdf/;return o.test(t)||l.test(t)}function C(t,o){var a;const l=(a=t.dataTransfer)==null?void 0:a.files;if(l!=null&&l.length){const s=l.length;for(let r=0;r<s&&o+r<=d.value;r++)h(l[r],o+r);v.value[o].value=""}c("drop",t)}function E(t){v.value[t].click()}function z(t,o){const l=t.target.files;if(l!=null&&l.length){const a=l.length;for(let s=0;s<a&&o+s<d.value;s++)h(l[s],o+s);v.value[o].value=""}}const h=async(t,o)=>{new Promise((a,s)=>{try{const r=i.beforeUpload(t);r instanceof Promise?r.then(a,s):typeof r=="boolean"?r?a(r):s(new Error("Function returned false")):a(r)}catch(r){s(r)}}).then(()=>{d.value>f.value&&f.value++,i.uploadMode==="base64"&&(p.value[o]=!0,N(t,o)),i.uploadMode==="custom"&&(p.value[o]=!0,B(t,o))}).catch(a=>{console.log("beforeUpload error:",a)})};function N(t,o){var l=new FileReader;l.readAsDataURL(t),l.onloadstart=function(a){},l.onabort=function(a){},l.onerror=function(a){},l.onprogress=function(a){a.loaded===a.total&&(p.value[o]=!1)},l.onload=function(a){var s;n.value.push({name:t.name,url:(s=a.target)==null?void 0:s.result}),i.actionMessage.upload&&u.value.success(i.actionMessage.upload),c("update:fileList",n.value),c("change",n.value)},l.onloadend=function(a){}}function B(t,o){i.customRequest(t).then(l=>{n.value.push(l),i.actionMessage.upload&&u.value.success(i.actionMessage.upload),c("update:fileList",n.value),c("change",n.value)}).catch(l=>{d.value>1&&(f.value=n.value.length+1),u.value.error(l)}).finally(()=>{p.value[o]=!1})}function P(t,o){if(m(o)){const l=n.value.slice(0,t).filter(a=>!m(a.url));g.value[t-l.length].preview(0)}else window.open(o);c("preview",n.value[t])}function q(t){n.value.length<d.value&&f.value--;const o=n.value.splice(t,1);i.actionMessage.remove&&u.value.success(i.actionMessage.remove),c("remove",o[0]),c("update:fileList",n.value),c("change",n.value)}function R(t){u.value.info(t)}function L(t){u.value.success(t)}function U(t){u.value.error(t)}function F(t){u.value.warning(t)}function D(t){u.value.loading(t)}return k({info:R,success:L,error:U,warning:F,loading:D}),(t,o)=>(e.openBlock(),e.createElementBlock("div",{class:"m-upload-wrap",style:e.normalizeStyle(`--upload-primary-color: ${e.unref(V)[5]};`)},[e.createVNode(e.unref(I.default),e.mergeProps({gap:"small"},t.spaceProps),{default:e.withCtx(()=>[(e.openBlock(!0),e.createElementBlock(e.Fragment,null,e.renderList(f.value,l=>(e.openBlock(),e.createElementBlock("div",{class:"upload-item-panel",key:l},[e.withDirectives(e.createElementVNode("div",{class:e.normalizeClass(["upload-item",{"upload-disabled":t.disabled}]),onDragenter:o[1]||(o[1]=e.withModifiers(()=>{},["stop","prevent"])),onDragover:o[2]||(o[2]=e.withModifiers(()=>{},["stop","prevent"])),onDrop:e.withModifiers(a=>t.draggable&&!t.disabled?C(a,l-1):()=>!1,["stop","prevent"]),onClick:a=>t.disabled?()=>!1:E(l-1)},[e.createElementVNode("input",{ref_for:!0,ref_key:"uploadInputRef",ref:v,type:"file",onClick:o[0]||(o[0]=e.withModifiers(()=>{},["stop"])),accept:t.accept,multiple:t.multiple,onChange:a=>z(a,l-1),style:{display:"none"}},null,40,j),e.createElementVNode("div",null,[o[3]||(o[3]=e.createElementVNode("svg",{focusable:"false",class:"plus-svg","data-icon":"plus",width:"1em",height:"1em",fill:"currentColor","aria-hidden":"true",viewBox:"64 64 896 896"},[e.createElementVNode("defs"),e.createElementVNode("path",{d:"M482 152h60q8 0 8 8v704q0 8-8 8h-60q-8 0-8-8V160q0-8 8-8z"}),e.createElementVNode("path",{d:"M176 474h672q8 0 8 8v60q0 8-8 8H176q-8 0-8-8v-60q0-8 8-8z"})],-1)),e.createElementVNode("p",A,[e.renderSlot(t.$slots,"tip",{},()=>[e.createTextVNode(e.toDisplayString(t.tip),1)],!0)])])],42,T),[[e.vShow,!p.value[l-1]&&!n.value[l-1]]]),e.withDirectives(e.createElementVNode("div",O,[e.createVNode(e.unref(H.default),e.mergeProps({class:"spin-uploading",tip:"uploading",size:"small",indicator:"spin-line",color:"var(--upload-primary-color)",ref_for:!0},t.spinProps),null,16)],512),[[e.vShow,p.value[l-1]]]),n.value[l-1]?(e.openBlock(),e.createElementBlock("div",W,[m(n.value[l-1].url)?(e.openBlock(),e.createBlock(e.unref(S.default),e.mergeProps({key:0,ref_for:!0,ref_key:"imageRef",ref:g,style:{"--image-primary-color":"var(--upload-primary-color)"},bordered:!1,width:82,height:82,fit:t.fit,src:n.value[l-1].url,name:n.value[l-1].name},t.imageProps),null,16,["fit","src","name"])):M(n.value[l-1].url)?(e.openBlock(),e.createElementBlock("svg",G,o[4]||(o[4]=[e.createElementVNode("path",{d:"M531.3 574.4l.3-1.4c5.8-23.9 13.1-53.7 7.4-80.7-3.8-21.3-19.5-29.6-32.9-30.2-15.8-.7-29.9 8.3-33.4 21.4-6.6 24-.7 56.8 10.1 98.6-13.6 32.4-35.3 79.5-51.2 107.5-29.6 15.3-69.3 38.9-75.2 68.7-1.2 5.5.2 12.5 3.5 18.8 3.7 7 9.6 12.4 16.5 15 3 1.1 6.6 2 10.8 2 17.6 0 46.1-14.2 84.1-79.4 5.8-1.9 11.8-3.9 17.6-5.9 27.2-9.2 55.4-18.8 80.9-23.1 28.2 15.1 60.3 24.8 82.1 24.8 21.6 0 30.1-12.8 33.3-20.5 5.6-13.5 2.9-30.5-6.2-39.6-13.2-13-45.3-16.4-95.3-10.2-24.6-15-40.7-35.4-52.4-65.8zM421.6 726.3c-13.9 20.2-24.4 30.3-30.1 34.7 6.7-12.3 19.8-25.3 30.1-34.7zm87.6-235.5c5.2 8.9 4.5 35.8.5 49.4-4.9-19.9-5.6-48.1-2.7-51.4.8.1 1.5.7 2.2 2zm-1.6 120.5c10.7 18.5 24.2 34.4 39.1 46.2-21.6 4.9-41.3 13-58.9 20.2-4.2 1.7-8.3 3.4-12.3 5 13.3-24.1 24.4-51.4 32.1-71.4zm155.6 65.5c.1.2.2.5-.4.9h-.2l-.2.3c-.8.5-9 5.3-44.3-8.6 40.6-1.9 45 7.3 45.1 7.4zm191.4-388.2L639.4 73.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.7-9.4-22.7zM790.2 326H602V137.8L790.2 326zm1.8 562H232V136h302v216a42 42 0 0042 42h216v494z"},null,-1)]))):(e.openBlock(),e.createElementBlock("svg",J,o[5]||(o[5]=[e.createElementVNode("path",{d:"M854.6 288.6L639.4 73.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.7-9.4-22.7zM790.2 326H602V137.8L790.2 326zm1.8 562H232V136h302v216a42 42 0 0042 42h216v494z"},null,-1)]))),e.createElementVNode("div",K,[e.createElementVNode("a",{class:"file-icon",title:"预览",onClick:a=>P(l-1,n.value[l-1].url)},o[6]||(o[6]=[e.createElementVNode("svg",{class:"icon-svg",focusable:"false","data-icon":"eye",width:"1em",height:"1em",fill:"currentColor","aria-hidden":"true",viewBox:"64 64 896 896"},[e.createElementVNode("path",{d:"M942.2 486.2C847.4 286.5 704.1 186 512 186c-192.2 0-335.4 100.5-430.2 300.3a60.3 60.3 0 000 51.5C176.6 737.5 319.9 838 512 838c192.2 0 335.4-100.5 430.2-300.3 7.7-16.2 7.7-35 0-51.5zM512 766c-161.3 0-279.4-81.8-362.7-254C232.6 339.8 350.7 258 512 258c161.3 0 279.4 81.8 362.7 254C791.5 684.2 673.4 766 512 766zm-4-430c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176zm0 288c-61.9 0-112-50.1-112-112s50.1-112 112-112 112 50.1 112 112-50.1 112-112 112z"})],-1)]),8,Q),e.withDirectives(e.createElementVNode("a",{class:"file-icon",title:"删除",onClick:e.withModifiers(a=>q(l-1),["prevent","stop"])},o[7]||(o[7]=[e.createElementVNode("svg",{class:"icon-svg",focusable:"false","data-icon":"delete",width:"1em",height:"1em",fill:"currentColor","aria-hidden":"true",viewBox:"64 64 896 896"},[e.createElementVNode("path",{d:"M360 184h-8c4.4 0 8-3.6 8-8v8h304v-8c0 4.4 3.6 8 8 8h-8v72h72v-80c0-35.3-28.7-64-64-64H352c-35.3 0-64 28.7-64 64v80h72v-72zm504 72H160c-17.7 0-32 14.3-32 32v32c0 4.4 3.6 8 8 8h60.4l24.7 523c1.6 34.1 29.8 61 63.9 61h454c34.2 0 62.3-26.8 63.9-61l24.7-523H888c4.4 0 8-3.6 8-8v-32c0-17.7-14.3-32-32-32zM731.3 840H292.7l-24.2-512h487l-24.2 512z"})],-1)]),8,X),[[e.vShow,!t.disabled]])])])):e.createCommentVNode("",!0)]))),128))]),_:3},16),e.createVNode(e.unref($.default),e.mergeProps({ref_key:"messageRef",ref:u},t.messageProps),null,16)],4))}});exports.default=Y;