astrion-website/demo/static/dist/assets/FilePreviewDialog.js

2 lines
7.2 KiB
JavaScript

import{d as V,a as Y,w as Z,b as a,c as l,n as H,e,y as k,t as f,h as p,F as _,r as I,k as b,l as u,q as ee,z as te}from"./runtime-dom.esm-bundler.js";import{P as U,r as se}from"./useMarkdownRenderer.js";import{_ as q}from"./_plugin-vue_export-helper.js";const ae={class:"sfc-header"},le={class:"sfc-title-group"},ne=["title"],ie={key:0,class:"sfc-meta"},oe={class:"sfc-actions"},ce={key:0,class:"sfc-description"},re={key:0,class:"sfc-loading"},ue={key:1,class:"sfc-error"},de={key:0,class:"sfc-code-header"},ve={class:"sfc-lang"},fe=["innerHTML"],pe={class:"sfc-csv-scroll"},ye={class:"sfc-csv-table"},he={key:0,class:"sfc-csv-meta"},me=["innerHTML"],we=["src","alt"],ge=["src","title"],ke=V({name:"ShowFileCard",__name:"ShowFileCard",props:{path:{},name:{},type:{},description:{},preview:{},iconStyle:{type:Function},downloadFile:{type:Function}},setup(n){const o=n,d=b(!1),v=b(""),y=b(!1),r=b(""),j=b(!1),x=b(""),i=u(()=>o.type||"binary"),F=u(()=>o.name||o.path.split("/").pop()||"file"),S=u(()=>o.preview==="off"?!1:["text","code","json","csv","markdown","image","pdf"].includes(i.value)),E=u(()=>{switch(i.value){case"image":return"file";case"pdf":return"file";case"csv":return"file";case"markdown":return"file";default:return"file"}}),N=u(()=>{var c;const t=[],s=(c=o.path.split(".").pop())==null?void 0:c.toUpperCase();if(s&&t.push(s),r.value){const h=(new Blob([r.value]).size/1024).toFixed(1);t.push(`${h} KB`)}return t.join(" · ")}),$=u(()=>{var c;if(i.value==="json")return"json";const t=((c=o.path.split(".").pop())==null?void 0:c.toLowerCase())||"";return{js:"javascript",ts:"typescript",jsx:"jsx",tsx:"tsx",py:"python",rb:"ruby",go:"go",rs:"rust",java:"java",c:"c",cpp:"cpp",cs:"csharp",php:"php",swift:"swift",kt:"kotlin",sh:"bash",bash:"bash",html:"html",css:"css",scss:"scss",xml:"xml",sql:"sql",vue:"markup"}[t]||"text"}),K=u(()=>{if(!r.value)return"";let t=r.value;if(i.value==="json")try{t=JSON.stringify(JSON.parse(t),null,2)}catch{}try{const s=U.languages[$.value];if(s)return U.highlight(t,s,$.value)}catch{}return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}),R=u(()=>r.value?se(r.value,!1):""),T=u(()=>{if(i.value!=="csv"||!r.value)return null;const t=r.value.split(/\r?\n/).filter(m=>m.trim()),s=m=>{const L=[];let w="",P=!1;for(let C=0;C<m.length;C++){const g=m[C];P?g==='"'&&m[C+1]==='"'?(w+='"',C++):g==='"'?P=!1:w+=g:g==='"'?P=!0:g===","?(L.push(w),w=""):w+=g}return L.push(w),L},c=100,h=s(t[0]||""),B=t.slice(1,c+1).map(s);return{header:h,rows:B,truncated:t.length>c+1}}),J=u(()=>{var t;return((t=T.value)==null?void 0:t.header)||[]}),z=u(()=>{var t;return((t=T.value)==null?void 0:t.rows)||[]}),O=u(()=>{var t;return((t=T.value)==null?void 0:t.truncated)||!1});function D(){return`/api/file/content?path=${encodeURIComponent(o.path)}`}async function M(){y.value?y.value=!1:(y.value=!0,!r.value&&!v.value&&await A())}async function A(){d.value=!0,v.value="";try{if(["image","pdf"].includes(i.value))x.value=D();else{const t=await fetch(D());if(!t.ok){let s=t.statusText;try{s=(await t.json()).error||s}catch{}v.value=s||"加载失败";return}r.value=await t.text()}}catch(t){v.value=t.message||"网络错误"}finally{d.value=!1}}async function Q(){await o.downloadFile(o.path)}async function G(){if(r.value)try{await navigator.clipboard.writeText(r.value),j.value=!0,setTimeout(()=>{j.value=!1},2e3)}catch{}}function W(){x.value&&window.open(x.value,"_blank")}function X(){v.value="图片加载失败"}return Y(()=>{}),Z(()=>o.preview,t=>{t==="auto"&&S.value&&!y.value&&M()},{immediate:!0}),(t,s)=>(a(),l("div",{class:H(["show-file-card",{"is-loading":d.value,"is-error":v.value}])},[e("div",ae,[e("span",{class:"sfc-file-icon",style:k(n.iconStyle(E.value)),"aria-hidden":"true"},null,4),e("div",le,[e("span",{class:"sfc-name",title:F.value},f(F.value),9,ne),N.value?(a(),l("span",ie,f(N.value),1)):p("",!0)]),e("div",oe,[S.value&&!y.value?(a(),l("button",{key:0,type:"button",class:"sfc-btn sfc-btn-preview",onClick:M,title:"预览"},[e("span",{class:"icon icon-sm",style:k(n.iconStyle("eye")),"aria-hidden":"true"},null,4),s[0]||(s[0]=e("span",{class:"sfc-btn-text"},"预览",-1))])):p("",!0),S.value&&y.value?(a(),l("button",{key:1,type:"button",class:"sfc-btn sfc-btn-preview",onClick:M,title:"收起"},[e("span",{class:"icon icon-sm",style:k(n.iconStyle("eye")),"aria-hidden":"true"},null,4),s[1]||(s[1]=e("span",{class:"sfc-btn-text"},"收起",-1))])):p("",!0),e("button",{type:"button",class:"sfc-btn sfc-btn-download",onClick:Q,title:"下载"},[e("span",{class:"icon icon-sm",style:k(n.iconStyle("save")),"aria-hidden":"true"},null,4),s[2]||(s[2]=e("span",{class:"sfc-btn-text"},"下载",-1))])])]),n.description?(a(),l("div",ce,f(n.description),1)):p("",!0),y.value?(a(),l("div",{key:1,class:H(["sfc-preview",`sfc-preview-${i.value}`])},[d.value?(a(),l("div",re,[...s[3]||(s[3]=[e("span",{class:"sfc-spinner"},null,-1),e("span",null,"加载中...",-1)])])):v.value?(a(),l("div",ue,[e("span",{class:"icon icon-sm",style:k(n.iconStyle("triangleAlert")),"aria-hidden":"true"},null,4),e("span",null,f(v.value),1)])):i.value==="text"||i.value==="code"||i.value==="json"?(a(),l(_,{key:2},[i.value==="code"||i.value==="json"?(a(),l("div",de,[e("span",ve,f($.value),1),e("button",{type:"button",class:"sfc-btn sfc-btn-copy",onClick:G},[e("span",{class:"icon icon-sm",style:k(n.iconStyle("copy")),"aria-hidden":"true"},null,4),e("span",null,f(j.value?"已复制":"复制"),1)])])):p("",!0),e("pre",{class:"sfc-code",innerHTML:K.value},null,8,fe)],64)):i.value==="csv"?(a(),l(_,{key:3},[e("div",pe,[e("table",ye,[e("thead",null,[e("tr",null,[(a(!0),l(_,null,I(J.value,(c,h)=>(a(),l("th",{key:h},f(c),1))),128))])]),e("tbody",null,[(a(!0),l(_,null,I(z.value,(c,h)=>(a(),l("tr",{key:h},[(a(!0),l(_,null,I(c,(B,m)=>(a(),l("td",{key:m},f(B),1))),128))]))),128))])])]),O.value?(a(),l("div",he," 仅显示前 "+f(z.value.length)+" 行,完整内容请下载查看 ",1)):p("",!0)],64)):i.value==="markdown"?(a(),l("div",{key:4,class:"sfc-md",innerHTML:R.value},null,8,me)):i.value==="image"?(a(),l("img",{key:5,class:"sfc-image",src:x.value,alt:F.value,onClick:W,onError:X},null,40,we)):i.value==="pdf"?(a(),l("iframe",{key:6,class:"sfc-pdf",src:x.value,title:F.value},null,8,ge)):p("",!0)],2)):p("",!0)],2))}}),be=q(ke,[["__scopeId","data-v-cce8e4a8"]]),xe={class:"file-preview-modal"},Ce={class:"file-preview-header"},_e={class:"file-preview-title"},Fe={class:"file-preview-body"},je=V({name:"FilePreviewDialog",__name:"FilePreviewDialog",props:{path:{},name:{},type:{},iconStyle:{type:Function},downloadFile:{type:Function}},emits:["close"],setup(n){return(o,d)=>(a(),l("div",{class:"file-preview-overlay",onClick:d[1]||(d[1]=te(v=>o.$emit("close"),["self"]))},[e("div",xe,[e("div",Ce,[e("span",_e,f(n.name),1),e("button",{type:"button",class:"file-preview-close",onClick:d[0]||(d[0]=v=>o.$emit("close")),title:"关闭"},[...d[2]||(d[2]=[e("svg",{viewBox:"0 0 24 24",width:"20",height:"20",fill:"none",stroke:"currentColor","stroke-width":"2"},[e("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),e("line",{x1:"6",y1:"6",x2:"18",y2:"18"})],-1)])])]),e("div",Fe,[ee(be,{path:n.path,name:n.name,type:n.type,preview:"auto","icon-style":n.iconStyle,"download-file":n.downloadFile},null,8,["path","name","type","icon-style","download-file"])])])]))}}),Me=q(je,[["__scopeId","data-v-8904da9a"]]);export{Me as default};