enoviq-react-document-viewer
Version:
Custom React & Next.js compatible document viewer package
3 lines (2 loc) • 5.73 kB
JavaScript
import{createContext as e,useState as i,lazy as t,useContext as n,useEffect as o}from"react";var a=e(),l=function(e){var t=e.children,n=i("");return h(a.Provider,{value:{setDocumentViewerOpen:n[1]}},n[0],t)},r=t(function(){return import("./PDFViewer-3bfe304b.js")}),s=function(e){var i,t=e.file,o=n(a).setDocumentViewerOpen;if(!t)return h("span",{className:"noFile"},"No File");"string"==typeof t||URL.createObjectURL(t);var l="string"==typeof t?null==(i=t.split(".").pop())?void 0:i.toLowerCase():t.type;"string"==typeof t&&t.split("/").pop();var r=function(e){return h("div",{onClick:function(){o(h(c,{file:t,onClose:function(){return o("")}}))},className:"d-flex justify-content-center flex-column align-content-center",style:{cursor:"pointer"}},e.children)};return l.includes("image")||["jpg","jpeg","png","gif","webp"].includes(l)?h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/image_doc_icon.png",alt:"Image preview",className:"imagePreview"})):l.includes("pdf")||"pdf"===l?h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/pdf_doc_icon.png",alt:"PDF preview",className:"imagePreview"})):l.includes("video")||["mp4","webm","ogg"].includes(l)?h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/video_doc_icon.png",alt:"Video preview",className:"imagePreview"})):l.includes("doc")||l.includes("docx")?h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/word_doc_icon.png",alt:"Word preview",className:"imagePreview"})):l.includes("xls")||l.includes("xlsx")?h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/xls_doc_icon.png",alt:"Excel preview",className:"imagePreview"})):h(r,null,h("img",{src:"/enoviq-react-document-viewer/icons/text_doc_icon.png",alt:"File preview",className:"imagePreview"}))},c=function(e){var t=e.file,n=e.fileType,a=e.fileName,l=void 0===a?"Document":a,s=e.onClose,c=i(null),p=c[0],d=c[1],u=i(null),m=u[0],v=u[1],f=i(!1),g=f[0],w=f[1],x=i(null),N=x[0],y=x[1],b=i(""),j=b[0],F=b[1];o(function(){return t?(w(!0),y(null),function(){try{try{var e=null,i=n;if("string"==typeof t)if(t.startsWith("data:")){if(e=t,!i){var o=t.match(/data:([^;]+)/);i=o?o[1]:"application/octet-stream"}}else if(t.startsWith("http")||t.startsWith("/"))e=t,i||(i={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"}[t.split(".").pop().toLowerCase()]||"application/octet-stream");else{if(!i)throw new Error("fileType is required for base64 strings without data URL prefix");e="data:"+i+";base64,"+t}else{if(!(t instanceof File||t instanceof Blob))throw new Error("Unsupported file format");e=URL.createObjectURL(t),i=i||t.type}d(e),v(i)}catch(e){y(e.message)}finally{w(!1)}return Promise.resolve()}catch(e){return Promise.reject(e)}}(),function(){p&&p.startsWith("blob:")&&URL.revokeObjectURL(p)}):(d(null),void v(null))},[t,n]);var P,L=function(){return"string"==typeof t&&t.startsWith("http")?t.split("/").pop()||l:null!=t&&t.name?t.name:l};return o(function(){if(p&&!p.startsWith("data:")){var e=new FileReader;e.onload=function(){return F(e.result)},e.readAsText(t)}else if(null!=p&&p.startsWith("data:"))try{var i=p.split(",")[1],n=atob(i);F(n)}catch(e){y("Failed to decode text content")}},[p]),h("div",{className:"overlay",onClick:s}," ",h("div",{className:"modal",onClick:function(e){return e.stopPropagation()}}," ",h("div",{className:"modalHeader"}," ",h("div",{className:"fileInfo"}," ",h("h3",null,L()),m&&h("span",{className:"fileType"},m)),h("button",{onClick:s,className:"closeBtn"}," ","×")),h("div",{className:"modalBody"},g?h("div",{className:"loading"}," ",h("div",{className:"spinner"}),h("p",null,"Loading file...")):N?h("div",{className:"error"}," ",h("div",{className:"errorIcon"},"⚠️"),h("h4",null,"Error loading file"),h("p",null,N)):p&&m?(null==(P=m)?void 0:P.includes("pdf"))?h(Suspense,{fallback:h("div",null,"Loading PDF...")},h(r,{file:t})):function(e){return null==e?void 0:e.startsWith("image/")}(m)?h(Fragment,null,h("img",{src:p,alt:L(),className:"imagePreview",onError:function(){return y("Failed to load image")}})):function(e){return null==e?void 0:e.startsWith("video/")}(m)?h("video",{src:p,controls:!0,className:"mediaPlayer",onError:function(){return y("Failed to load video")}},"Your browser does not support the video tag."):function(e){return null==e?void 0:e.startsWith("audio/")}(m)?h("audio",{src:p,controls:!0,className:"mediaPlayer",onError:function(){return y("Failed to load audio")}},"Your browser does not support the audio tag."):function(e){return(null==e?void 0:e.startsWith("text/"))||["application/json","application/xml","application/javascript"].includes(e)}(m)?h("pre",{className:"textPreview"},j||"Loading text content..."):h("div",{className:"unsupported"}," ",h("div",{className:"unsupportedIcon"},"📄"),h("h4",null,"Cannot preview this file type"),h("p",null,"File type: ",m),h("p",null,"File name: ",L()),p&&h("a",{href:p,download:L(),className:"downloadLink"}," ","Download File")):h("div",{className:"noFile"}," ",h("p",null,"No file to display")))," "))};export{a as DocumentViewerContext,l as DocumentViewerProvider,s as RenderFilePreview};
//# sourceMappingURL=index.module.js.map