@artmate/chat
Version:
借鉴字节开源react库AntX,通过vue实现的版本
2 lines (1 loc) • 1.05 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=t=>t.indexOf("image/")===0,i=200;function u(t){return new Promise(r=>{if(!t||!t.type||!g(t.type)){r("");return}const n=new Image;if(n.addEventListener("load",()=>{const{width:e,height:m}=n,a=e/m,o=a>1?i:i*a,d=a>1?i/a:i,s=document.createElement("canvas");s.width=o,s.height=d,s.style.cssText=`position: fixed; left: 0; top: 0; width: ${o}px; height: ${d}px; z-index: 9999; display: none;`,document.body.append(s);const c=s.getContext("2d");c==null||c.drawImage(n,0,0,o,d);const p=s.toDataURL();s.remove(),window.URL.revokeObjectURL(n.src),r(p)}),n.crossOrigin="anonymous",t.type.startsWith("image/svg+xml")){const e=new FileReader;e.addEventListener("load",()=>{e.result&&typeof e.result=="string"&&(n.src=e.result)}),e.readAsDataURL(t)}else if(t.type.startsWith("image/gif")){const e=new FileReader;e.addEventListener("load",()=>{e.result&&r(e.result)}),e.readAsDataURL(t)}else n.src=window.URL.createObjectURL(t)})}exports.isImageFileType=g;exports.previewImage=u;