2 lines
5.2 KiB
JavaScript
2 lines
5.2 KiB
JavaScript
import{j as t,T as O,U as I,B as ee,d as te,e as re,f as se}from"./semi-ui-fcB3D8nU.js";import{r as x}from"./react-core-C8o2IdY8.js";import{s as F,A as oe,aM as ne}from"./index-DApEyau7.js";import{u as le}from"./i18n-DJWjvQyM.js";function ae(e){const[o,a]=x.useState(null),g=x.useRef(null),u=x.useRef(0),r=x.useRef(null);return g.current=e,x.useEffect(()=>{if(e==null){r.current!=null&&(cancelAnimationFrame(r.current),r.current=null),u.current=0,a(null);return}let j=!1;const b=()=>{if(j)return;const n=g.current;if(n==null)return;const p=u.current;if(p<n-.01){const y=Math.min(n,p+Math.max(.55,(n-p)*.2));u.current=y,a(Math.round(y)),r.current=requestAnimationFrame(b)}else p>n+.01?(u.current=n,a(Math.round(n)),r.current=requestAnimationFrame(b)):(u.current=n,a(Math.round(n)),r.current=null)};return r.current=requestAnimationFrame(b),()=>{j=!0,r.current!=null&&(cancelAnimationFrame(r.current),r.current=null)}},[e]),o}const{Text:S}=O;function ie({children:e,className:o=""}){return t.jsxs(S,{strong:!0,className:`text-[var(--semi-color-text-0)] ${o}`.trim(),children:[t.jsx("span",{className:"text-[var(--semi-color-danger)] mr-1 font-normal","aria-hidden":!0,children:"*"}),e]})}function ce(e){return/\.pdf(\?|$)/i.test(e||"")}function C(e){const o=(e==null?void 0:e.type)||"",a=((e==null?void 0:e.name)||"").toLowerCase();return o==="image/jpeg"||o==="image/png"||/\.(jpe?g|png)$/.test(a)}function ue(e){const o=(e==null?void 0:e.type)||"",a=((e==null?void 0:e.name)||"").toLowerCase();return o==="application/pdf"||a.endsWith(".pdf")}const pe="absolute -right-1.5 -top-1.5 z-10 inline-flex h-[18px] w-[18px] shrink-0 cursor-pointer items-center justify-center rounded-full border-0 bg-[var(--semi-color-danger)] p-0 text-white opacity-100 shadow-[0_1px_4px_rgba(0,0,0,0.25)] transition-colors hover:brightness-90 focus:outline-none focus-visible:ring-2 focus-visible:ring-[var(--semi-color-danger)] [&_.semi-icon]:!m-0";function E({onClick:e,label:o}){return t.jsx("button",{type:"button","aria-label":o,className:pe,onClick:e,children:t.jsx(se,{size:"small",className:"!text-[10px]"})})}function ge({label:e,required:o=!1,url:a="",onUrlChange:g,urls:u,onUrlsChange:r,maxCount:j=1,multiple:b,hint:n,onPreview:p,disabled:y,compact:J=!1,imagesOnly:k=!1,allowPdf:D=!1}){const{t:l}=le(),[R,v]=x.useState(null),H=ae(R),_=typeof g=="function",m=_?String(a||"").trim()?[String(a).trim()]:[]:u||[],A=s=>{_?g(s[0]||""):r==null||r(s)},w=j,M=b??w>1,W=m.length>=w,$=y??W,K=k?null:D?l("支持 JPG/PNG 或 PDF"):M?l("支持图片或 PDF,最多 {{n}} 个;点击图片可大图预览",{n:w}):l("支持图片或 PDF;删除后可重新上传"),T=n!==void 0?n:K,Q=k?".jpg,.jpeg,.png":D?".jpg,.jpeg,.png,.pdf":"image/*,.pdf",V=async({file:s,onSuccess:d,onError:i,onProgress:N})=>{var U,q;const c=s.fileInstance||s;if(k){if(!C(c)){F(l("只支持 JPG/PNG 格式的图片")),i();return}}else if(D&&!C(c)&&!ue(c)){F(l("只支持 JPG/PNG/PDF 格式的文件")),i();return}const G=new FormData;G.append("file",c),v(0);try{const f=await oe.post("/api/oss/upload",G,{skipErrorHandler:!0,onUploadProgress:P=>{const B=P.total||P.loaded||1,Y=Math.round(P.loaded*100/B),Z=Math.min(99,Y);v(Z),typeof N=="function"&&N({total:B,loaded:P.loaded})}}),{success:X,message:z,data:h}=f.data||{};if(!X||!(h!=null&&h.url)){i(new Error(z||"upload")),F(z||l("上传失败"));return}v(100);const L=String(h.url).trim();A(M?[...m,L]:[L]),d(h),ne(l("已上传"))}catch(f){i(f),F(((q=(U=f==null?void 0:f.response)==null?void 0:U.data)==null?void 0:q.message)||l("上传失败"))}finally{v(null)}};return t.jsxs("div",{className:J?"mb-0":"mb-4",children:[e?o?t.jsx(ie,{className:"block mb-2",children:e}):t.jsx(S,{strong:!0,className:"block mb-2 text-[var(--semi-color-text-0)]",children:e}):null,t.jsx(I,{action:"",accept:Q,showUploadList:!1,customRequest:V,limit:w,multiple:M,disabled:$,children:t.jsx(ee,{disabled:$,children:l("上传文件")})}),R!=null?t.jsx(te,{percent:H??R,showInfo:!0,className:"mt-2"}):null,T?t.jsx(S,{type:"tertiary",size:"small",className:"block mt-1",children:T}):null,m.length>0?t.jsx("div",{className:"mt-3 flex flex-wrap gap-3",children:m.map((s,d)=>ce(s)?t.jsxs("div",{className:"relative flex h-24 w-24 flex-col items-center justify-center rounded-lg border border-[var(--semi-color-border)] bg-[var(--semi-color-fill-0)]",children:[t.jsx(re,{size:"large"}),t.jsx("span",{className:"mt-1 text-xs text-[var(--semi-color-text-2)]",children:"PDF"}),t.jsx("button",{type:"button",className:"absolute inset-0 rounded-lg focus:outline-none focus-visible:ring-2 focus-visible:ring-primary",title:l("在新窗口打开"),onClick:()=>window.open(s,"_blank","noopener,noreferrer")}),t.jsx(E,{label:l("删除"),onClick:i=>{i.stopPropagation(),A(m.filter((N,c)=>c!==d))}})]},`${s}-${d}`):t.jsxs("div",{className:"relative h-24 w-24",children:[t.jsx("button",{type:"button",className:"block h-full w-full cursor-zoom-in overflow-hidden rounded-lg border border-[var(--semi-color-border)] bg-[var(--semi-color-fill-0)] p-0",onClick:()=>{p?p(s):window.open(s,"_blank","noopener,noreferrer")},children:t.jsx("img",{src:s,alt:"",className:"h-full w-full object-cover"})}),t.jsx(E,{label:l("删除"),onClick:i=>{i.stopPropagation(),A(m.filter((N,c)=>c!==d))}})]},`${s}-${d}`))}):null]})}export{ge as D};
|