UNPKG

enoviq-react-document-viewer

Version:

Custom React & Next.js compatible document viewer package

3 lines (2 loc) 7.71 kB
var e=require("react"),n=e.createContext(null),t=e.lazy(function(){return Promise.resolve().then(function(){return require("./PDFViewer-b2658978.js")})}),i=new URL("../icons/xls_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,a=new URL("../icons/image_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,o=new URL("../icons/text_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,r=new URL("../icons/image_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,c=new URL("../icons/video_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,l=new URL("../icons/pdf_doc_icon.png","undefined"==typeof document?new(require("url").URL)("file:"+__filename).href:document.currentScript&&"SCRIPT"===document.currentScript.tagName.toUpperCase()&&document.currentScript.src||new URL("index.cjs",document.baseURI).href).href,s=function(n){var i=n.file,a=n.fileType,o=n.fileName,r=void 0===o?"Document":o,c=n.onClose,l=e.useState(null),s=l[0],u=l[1],p=e.useState(null),d=p[0],m=p[1],f=e.useState(!1),g=f[0],v=f[1],w=e.useState(null),P=w[0],x=w[1],R=e.useState(""),U=R[0],q=R[1];e.useEffect(function(){return i?(v(!0),x(null),function(){try{try{var e=null,n=a;if("string"==typeof i)if(i.startsWith("data:")){if(e=i,!n){var t=i.match(/data:([^;]+)/);n=t?t[1]:"application/octet-stream"}}else if(i.startsWith("http")||i.startsWith("/"))e=i,n||(n={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"}[i.split(".").pop().toLowerCase()]||"application/octet-stream");else{if(!n)throw new Error("fileType is required for base64 strings without data URL prefix");e="data:"+n+";base64,"+i}else{if(!(i instanceof File||i instanceof Blob))throw new Error("Unsupported file format");e=URL.createObjectURL(i),n=n||i.type}u(e),m(n)}catch(e){x(e.message)}finally{v(!1)}return Promise.resolve()}catch(e){return Promise.reject(e)}}(),function(){s&&s.startsWith("blob:")&&URL.revokeObjectURL(s)}):(u(null),void m(null))},[i,a]);var E,N=function(){return"string"==typeof i&&i.startsWith("http")?i.split("/").pop()||r:null!=i&&i.name?i.name:r};return e.useEffect(function(){if(s&&(null!=d&&d.startsWith("text/")||["application/json","application/xml","application/javascript"].includes(d))){var e=new FileReader;e.onload=function(){q(e.result)},e.readAsText(i)}else if(null!=s&&s.startsWith("data:"))try{var n=s.split(",")[1],t=atob(n);q(t)}catch(e){x("Failed to decode text content")}},[s,d]),h("div",{className:"EnoviqPackage-overlay",onClick:c},h("div",{className:"EnoviqPackage-modal",onClick:function(e){return e.stopPropagation()}},h("div",{className:"EnoviqPackage-modalHeader"},h("div",{className:"EnoviqPackage-fileInfo"},h("h3",null,N()),d&&h("span",{className:"EnoviqPackage-fileType"},d)),h("button",{onClick:c,className:"EnoviqPackage-closeBtn"},"×")),h("div",{className:"EnoviqPackage-modalBody"},g?h("div",{className:"EnoviqPackage-loading"}," ",h("div",{className:"EnoviqPackage-spinner"}),h("p",null,"Loading file...")):P?h("div",{className:"EnoviqPackage-error"}," ",h("div",{className:"EnoviqPackage-errorIcon"},"⚠️"),h("h4",null,"Error loading file"),h("p",null,P)):s&&d?(null==(E=d)?void 0:E.includes("pdf"))?h(e.Suspense,{fallback:h("div",null,"Loading PDF...")},h(t,{file:i})):function(e){return null==e?void 0:e.startsWith("image/")}(d)?h(Fragment,null,h("img",{src:s,alt:N(),className:"EnoviqPackage-imagePreview",onError:function(){return x("Failed to load image")}})):function(e){return null==e?void 0:e.startsWith("video/")}(d)?h("video",{src:s,controls:!0,className:"EnoviqPackage-mediaPlayer",onError:function(){return x("Failed to load video")}},"Your browser does not support the video tag."):function(e){return null==e?void 0:e.startsWith("audio/")}(d)?h("audio",{src:s,controls:!0,className:"EnoviqPackage-mediaPlayer",onError:function(){return x("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)}(d)?h("pre",{className:"EnoviqPackage-textPreview"},U||"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: ",d),h("p",null,"File name: ",N()),s&&h("a",{href:s,download:N(),className:"EnoviqPackage-downloadLink"},"Download File")):h("div",{className:"EnoviqPackage-noFile"}," ",h("p",null,"No file to display")))))};exports.DocumentViewerContext=n,exports.DocumentViewerProvider=function(t){var i=t.children,a=e.useState(""),o=a[0];return h(n.Provider,{value:{setDocumentViewerOpen:a[1],documentViewerOpen:o}},o,i)},exports.HelloWorld=function(){return h("h2",null,"Hello World")},exports.RenderFilePreview=function(n){var t,u=n.file,p=e.useContext(DocumentViewerContext).setDocumentViewerOpen;if(!u)return h("span",{className:"EnoviqPackage-noFile"},"No File");"string"==typeof u||URL.createObjectURL(u);var d="string"==typeof u?null==(t=u.split(".").pop())?void 0:t.toLowerCase():u.type;"string"==typeof u&&u.split("/").pop();var m=function(e){return h("div",{onClick:function(){console.log("clicked"),p(h(s,{file:u,onClose:function(){return p("")}}))},style:{display:"flex",width:"200px",height:"200px",justifyContent:"center",alignItems:"center",cursor:"pointer"}},e.children)};return d.includes("image")||["jpg","jpeg","png","gif","webp"].includes(d)?h(m,null,h("img",{src:r,alt:"Image preview",className:"EnoviqPackage-imagePreview"})):d.includes("pdf")||"pdf"===d?h(m,null,h("img",{src:l.src,alt:"PDF preview",className:"EnoviqPackage-imagePreview"})):d.includes("video")||["mp4","webm","ogg"].includes(d)?h(m,null,h("img",{src:c.src,alt:"Video preview",className:"EnoviqPackage-imagePreview"})):d.includes("doc")||d.includes("docx")?h(m,null,h("img",{src:a.src,alt:"Word preview",className:"EnoviqPackage-imagePreview"})):d.includes("xls")||d.includes("xlsx")?h(m,null,h("img",{src:i.src,alt:"Excel preview",className:"EnoviqPackage-imagePreview"})):h(m,null,h("img",{src:o.src,alt:"File preview",className:"EnoviqPackage-imagePreview"}))}; //# sourceMappingURL=index.cjs.map