mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-09-30 03:01:21 +02:00
2 lines
5.4 KiB
JavaScript
2 lines
5.4 KiB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/papaparse.min-CR43Mv99.js","assets/chunk-aKtaBQYM.js"])))=>i.map(i=>d[i]);
|
|
import{i as e}from"./chunk-aKtaBQYM.js";import{_ as t,a as n,g as r,i,n as a,o,p as s,t as c,u as l,v as u,y as d}from"./index-Cn5L2jF0.js";var f=e(d(),1),p=r(),m=500,h=40;function g(e){let t=``,n=e;for(;n>=0;)t=String.fromCharCode(65+n%26)+t,n=Math.floor(n/26)-1;return t}function _(e){let t=document.createElement(`canvas`);t.width=256,t.height=256;let n=t.getContext(`2d`);if(!n)return Promise.resolve(null);let r=256/5,i=256/9;n.fillStyle=`#1a1a1a`,n.fillRect(0,0,256,256),n.fillStyle=`rgba(0, 174, 66, 0.25)`,n.fillRect(0,0,256,i),n.strokeStyle=`#333333`,n.lineWidth=1;for(let e=1;e<5;e++)n.beginPath(),n.moveTo(e*r+.5,0),n.lineTo(e*r+.5,256),n.stroke();for(let e=1;e<9;e++)n.beginPath(),n.moveTo(0,e*i+.5),n.lineTo(256,e*i+.5),n.stroke();n.fillStyle=`#d4d4d4`,n.font=`11px sans-serif`,n.textBaseline=`middle`;for(let t=0;t<9;t++)for(let a=0;a<5;a++){let o=e[t]?.[a];o&&n.fillText(String(o),a*r+4,t*i+i/2,r-8)}return new Promise(e=>t.toBlob(e,`image/png`))}function v({libraryFileId:r,filename:d,fileType:v,fileSize:y,onClose:b,onSnapshot:x}){let{t:S}=u(),[C,w]=(0,f.useState)(null),[T,E]=(0,f.useState)(0),[D,O]=(0,f.useState)(null),k=(0,f.useRef)(!1),A=(0,f.useRef)(x);(0,f.useEffect)(()=>{A.current=x});let j=c(),{isFullscreen:M,toggleFullscreen:N}=j;(0,f.useEffect)(()=>{let a=!1;if(w(null),O(null),E(0),y>20971520){O(S(`fileManager.preview.tooLarge`,{size:i(y)}));return}let s={},c=o();return c&&(s.Authorization=`Bearer ${c}`),(async()=>{let i=await fetch(n.getLibraryFileDownloadUrl(r),{headers:s});if(!i.ok)throw Error(`HTTP ${i.status}`);let o=await i.arrayBuffer(),c;if(v===`csv`){let n=(await t(async()=>{let{default:t}=await import(`./papaparse.min-CR43Mv99.js`).then(t=>e(t.default,1));return{default:t}},__vite__mapDeps([0,1]))).default,r=new TextDecoder().decode(o),i=[],a=0,s=0,l=[],u=e=>{a+=1,s=Math.max(s,e.length),i.length<m&&i.push(e.slice(0,h).map(e=>e??``))};n.parse(r,{skipEmptyLines:!1,step:({data:e})=>{if(Array.isArray(e)){if(e.every(e=>!e)){l.push(e);return}l.forEach(u),l=[],u(e)}}}),c=[{name:d,rows:i,totalRows:a,totalCols:s}]}else{let e=await t(()=>import(`./xlsx-CKkngM-o.js`),[]),n=e.read(o,{dense:!0});c=n.SheetNames.map(t=>{let r=n.Sheets[t],i=r?.[`!ref`];if(!r||!i)return{name:t,rows:[],totalRows:0,totalCols:0};let a=e.utils.decode_range(i),o=a.e.r-a.s.r+1,s=a.e.c-a.s.c+1,c={s:a.s,e:{r:Math.min(a.e.r,a.s.r+m-1),c:Math.min(a.e.c,a.s.c+h-1)}};return{name:t,rows:e.utils.sheet_to_json(r,{header:1,raw:!1,defval:``,range:e.utils.encode_range(c)}),totalRows:o,totalCols:s}})}if(!a&&(w(c),A.current&&!k.current)){let e=c.find(e=>e.rows.length>0);if(e){k.current=!0;let t=await _(e.rows);t&&!a&&A.current?.(t)}}})().catch(e=>{console.error(`[sheet-preview] load failed`,e),a||O(S(`fileManager.preview.error`))}),()=>{a=!0}},[r,v,y,d,S]);let P=C?.[T]??null,F=P!=null&&P.totalRows>P.rows.length,I=P!=null&&P.totalCols>h,L=P==null?0:Math.min(P.totalCols,h);return(0,p.jsxs)(a,{title:d,fullscreen:j,onClose:b,icon:(0,p.jsx)(s,{className:`w-5 h-5 text-bambu-green flex-shrink-0`}),children:[C&&C.length>1&&(0,p.jsx)(`div`,{className:`flex gap-1 px-4 pt-2 overflow-x-auto flex-shrink-0`,children:C.map((e,t)=>(0,p.jsx)(`button`,{onClick:()=>E(t),className:`px-3 py-1.5 text-sm rounded-t whitespace-nowrap transition-colors ${t===T?`bg-bambu-dark text-white border border-b-0 border-bambu-dark-tertiary`:`text-bambu-gray hover:text-white hover:bg-bambu-dark/50`}`,children:e.name},`${e.name}-${t}`))}),(0,p.jsx)(`div`,{"data-testid":`spreadsheet-preview-content`,className:`flex-1 min-h-0 overflow-auto bg-bambu-dark ${M?``:`rounded-b-lg`}`,onDoubleClick:N,children:D?(0,p.jsx)(`div`,{className:`h-full flex items-center justify-center p-6`,children:(0,p.jsx)(`p`,{className:`text-bambu-gray text-center`,children:D})}):C?!P||P.rows.length===0?(0,p.jsx)(`div`,{className:`h-full flex items-center justify-center p-6`,children:(0,p.jsx)(`p`,{className:`text-bambu-gray`,children:S(`fileManager.preview.emptySheet`)})}):(0,p.jsxs)(`table`,{className:`border-collapse text-xs min-w-full`,children:[(0,p.jsx)(`thead`,{children:(0,p.jsxs)(`tr`,{children:[(0,p.jsx)(`th`,{className:`sticky top-0 bg-bambu-dark-secondary border border-bambu-dark-tertiary px-2 py-1 text-bambu-gray font-medium w-10`}),Array.from({length:L},(e,t)=>(0,p.jsx)(`th`,{className:`sticky top-0 bg-bambu-dark-secondary border border-bambu-dark-tertiary px-2 py-1 text-bambu-gray font-medium text-left min-w-[80px]`,children:g(t)},t))]})}),(0,p.jsx)(`tbody`,{children:P.rows.map((e,t)=>(0,p.jsxs)(`tr`,{children:[(0,p.jsx)(`td`,{className:`border border-bambu-dark-tertiary px-2 py-1 text-bambu-gray text-right bg-bambu-dark-secondary/50`,children:t+1}),Array.from({length:L},(t,n)=>(0,p.jsx)(`td`,{className:`border border-bambu-dark-tertiary px-2 py-1 text-bambu-gray-light whitespace-nowrap max-w-[280px] overflow-hidden text-ellipsis`,title:e[n]||void 0,children:e[n]??``},n))]},t))})]}):(0,p.jsx)(`div`,{className:`h-full flex items-center justify-center`,children:(0,p.jsx)(l,{className:`w-8 h-8 text-bambu-green animate-spin`})})}),(F||I)&&P&&(0,p.jsxs)(`div`,{className:`px-4 py-2 border-t border-bambu-dark-tertiary text-xs text-bambu-gray flex gap-4 flex-shrink-0`,children:[F&&(0,p.jsx)(`span`,{children:S(`fileManager.preview.truncatedRows`,{shown:P.rows.length,total:P.totalRows})}),I&&(0,p.jsx)(`span`,{children:S(`fileManager.preview.truncatedCols`,{shown:L,total:P.totalCols})})]})]})}export{v as SpreadsheetPreviewModal}; |