UNPKG

enoviq-react-document-viewer

Version:

Custom React & Next.js compatible document viewer package

3 lines (2 loc) 6 kB
import{createContext as e,useState as a,lazy as i,useContext as o,useEffect as l,Suspense as t}from"react";const n=e(null),s=({children:e})=>{const[i,o]=a("");return h(n.Provider,{value:{setDocumentViewerOpen:o,documentViewerOpen:i}},i,e)},c=i(()=>import("./PDFViewer-88041b36.js")),r=new URL("../icons/xls_doc_icon.png",import.meta.url).href,p=new URL("../icons/image_doc_icon.png",import.meta.url).href,d=new URL("../icons/text_doc_icon.png",import.meta.url).href,m=new URL("../icons/image_doc_icon.png",import.meta.url).href,g=new URL("../icons/video_doc_icon.png",import.meta.url).href,v=new URL("../icons/pdf_doc_icon.png",import.meta.url).href,u=()=>h("h2",null,"Hello World"),f=({file:e})=>{var a;const{setDocumentViewerOpen:i}=o(DocumentViewerContext);if(!e)return h("span",{className:"EnoviqPackage-noFile"},"No File");"string"==typeof e||URL.createObjectURL(e);const l="string"==typeof e?null==(a=e.split(".").pop())?void 0:a.toLowerCase():e.type;"string"==typeof e&&e.split("/").pop();const t=({children:a})=>h("div",{onClick:()=>{console.log("clicked"),i(h(w,{file:e,onClose:()=>i("")}))},style:{display:"flex",width:"200px",height:"200px",justifyContent:"center",alignItems:"center",cursor:"pointer"}},a);return l.includes("image")||["jpg","jpeg","png","gif","webp"].includes(l)?h(t,null,h("img",{src:m,alt:"Image preview",className:"EnoviqPackage-imagePreview"})):l.includes("pdf")||"pdf"===l?h(t,null,h("img",{src:v.src,alt:"PDF preview",className:"EnoviqPackage-imagePreview"})):l.includes("video")||["mp4","webm","ogg"].includes(l)?h(t,null,h("img",{src:g.src,alt:"Video preview",className:"EnoviqPackage-imagePreview"})):l.includes("doc")||l.includes("docx")?h(t,null,h("img",{src:p.src,alt:"Word preview",className:"EnoviqPackage-imagePreview"})):l.includes("xls")||l.includes("xlsx")?h(t,null,h("img",{src:r.src,alt:"Excel preview",className:"EnoviqPackage-imagePreview"})):h(t,null,h("img",{src:d.src,alt:"File preview",className:"EnoviqPackage-imagePreview"}))},w=({file:e,fileType:i,fileName:o="Document",onClose:n})=>{const[s,r]=a(null),[p,d]=a(null),[m,g]=a(!1),[v,u]=a(null),[f,w]=a("");l(()=>e?(g(!0),u(null),(async()=>{try{let a=null,o=i;if("string"==typeof e)if(e.startsWith("data:")){if(a=e,!o){const a=e.match(/data:([^;]+)/);o=a?a[1]:"application/octet-stream"}}else if(e.startsWith("http")||e.startsWith("/"))a=e,o||(o={pdf:"application/pdf",jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",webp:"image/webp",svg:"image/svg+xml",mp4:"video/mp4",webm:"video/webm",ogv:"video/ogg",mp3:"audio/mpeg",wav:"audio/wav",ogg:"audio/ogg",txt:"text/plain",json:"application/json",xml:"application/xml",html:"text/html",css:"text/css",js:"application/javascript",doc:"application/msword",docx:"application/vnd.openxmlformats-officedocument.wordprocessingml.document",xls:"application/vnd.ms-excel",xlsx:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",ppt:"application/vnd.ms-powerpoint",pptx:"application/vnd.openxmlformats-officedocument.presentationml.presentation"}[e.split(".").pop().toLowerCase()]||"application/octet-stream");else{if(!o)throw new Error("fileType is required for base64 strings without data URL prefix");a=`data:${o};base64,${e}`}else{if(!(e instanceof File||e instanceof Blob))throw new Error("Unsupported file format");a=URL.createObjectURL(e),o=o||e.type}r(a),d(o)}catch(e){u(e.message)}finally{g(!1)}})(),()=>{s&&s.startsWith("blob:")&&URL.revokeObjectURL(s)}):(r(null),void d(null)),[e,i]);const P=()=>"string"==typeof e&&e.startsWith("http")?e.split("/").pop()||o:null!=e&&e.name?e.name:o;return l(()=>{if(s&&(null!=p&&p.startsWith("text/")||["application/json","application/xml","application/javascript"].includes(p))){const a=new FileReader;a.onload=()=>{w(a.result)},a.readAsText(e)}else if(null!=s&&s.startsWith("data:"))try{const e=s.split(",")[1],a=atob(e);w(a)}catch(e){u("Failed to decode text content")}},[s,p]),h("div",{className:"EnoviqPackage-overlay",onClick:n},h("div",{className:"EnoviqPackage-modal",onClick:e=>e.stopPropagation()},h("div",{className:"EnoviqPackage-modalHeader"},h("div",{className:"EnoviqPackage-fileInfo"},h("h3",null,P()),p&&h("span",{className:"EnoviqPackage-fileType"},p)),h("button",{onClick:n,className:"EnoviqPackage-closeBtn"},"×")),h("div",{className:"EnoviqPackage-modalBody"},m?h("div",{className:"EnoviqPackage-loading"}," ",h("div",{className:"EnoviqPackage-spinner"}),h("p",null,"Loading file...")):v?h("div",{className:"EnoviqPackage-error"}," ",h("div",{className:"EnoviqPackage-errorIcon"},"⚠️"),h("h4",null,"Error loading file"),h("p",null,v)):s&&p?(null==(x=p)?void 0:x.includes("pdf"))?h(t,{fallback:h("div",null,"Loading PDF...")},h(c,{file:e})):(e=>null==e?void 0:e.startsWith("image/"))(p)?h(Fragment,null,h("img",{src:s,alt:P(),className:"EnoviqPackage-imagePreview",onError:()=>u("Failed to load image")})):(e=>null==e?void 0:e.startsWith("video/"))(p)?h("video",{src:s,controls:!0,className:"EnoviqPackage-mediaPlayer",onError:()=>u("Failed to load video")},"Your browser does not support the video tag."):(e=>null==e?void 0:e.startsWith("audio/"))(p)?h("audio",{src:s,controls:!0,className:"EnoviqPackage-mediaPlayer",onError:()=>u("Failed to load audio")},"Your browser does not support the audio tag."):(e=>(null==e?void 0:e.startsWith("text/"))||["application/json","application/xml","application/javascript"].includes(e))(p)?h("pre",{className:"EnoviqPackage-textPreview"},f||"Loading text content..."):h("div",{className:"EnoviqPackage-unsupported"}," ",h("div",{className:"EnoviqPackage-unsupportedIcon"},"📄"),h("h4",null,"Cannot preview this file type"),h("p",null,"File type: ",p),h("p",null,"File name: ",P()),s&&h("a",{href:s,download:P(),className:"EnoviqPackage-downloadLink"},"Download File")):h("div",{className:"EnoviqPackage-noFile"}," ",h("p",null,"No file to display")))));var x};export{n as DocumentViewerContext,s as DocumentViewerProvider,u as HelloWorld,f as RenderFilePreview}; //# sourceMappingURL=index.modern.js.map