UNPKG

enoviq-react-document-viewer

Version:

Custom React & Next.js compatible document viewer package

3 lines (2 loc) 4.81 kB
import{useRef as t,useState as n,useEffect as e}from"react";import{GlobalWorkerOptions as r,getDocument as i}from"pdfjs-dist/build/pdf";import*as o from"pdfjs-dist/build/pdf.worker.min";function a(t,n){(null==n||n>t.length)&&(n=t.length);for(var e=0,r=Array(n);e<n;e++)r[e]=t[e];return r}function u(t,n,e){if(!t.s){if(e instanceof c){if(!e.s)return void(e.o=u.bind(null,t,n));1&n&&(n=e.s),e=e.v}if(e&&e.then)return void e.then(u.bind(null,t,n),u.bind(null,t,2));t.s=n,t.v=e;const r=t.o;r&&r(t)}}var c=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(n,e){var r=new t,i=this.s;if(i){var o=1&i?n:e;if(o){try{u(r,1,o(this.v))}catch(t){u(r,2,t)}return r}return this}return this.o=function(t){try{var i=t.v;1&t.s?u(r,1,n?n(i):i):e?u(r,1,e(i)):u(r,2,i)}catch(t){u(r,2,t)}},r},t}();function f(t){return t instanceof c&&1&t.s}function d(r){var o=r.file,d=r.base64String,l=t(null),s=t(1),v=n(0),p=v[0],m=v[1],b=n(1),g=b[0],y=b[1],w=n(null),x=w[0],k=w[1];e(function(){var t;if(o||d)return o?(t=URL.createObjectURL(o),k(t)):d&&k("data:application/pdf;base64,"+d),function(){t&&URL.revokeObjectURL(t)}},[o,d]),e(function(){if(x&&l.current){var t=!1;return function(){try{return Promise.resolve(function(n,e){try{var r=function(){function n(){return Promise.resolve(e.promise).then(function(n){if(!t){m(n.numPages);var e=1,r=function(t,n,e){for(var r;;){var i=t();if(f(i)&&(i=i.v),!i)return o;if(i.then){r=0;break}var o=e();if(o&&o.then){if(!f(o)){r=1;break}o=o.s}if(n){var a=n();if(a&&a.then&&!f(a)){r=2;break}}}var d=new c,l=u.bind(null,d,2);return(0===r?i.then(h):1===r?o.then(s):a.then(v)).then(void 0,l),d;function s(r){o=r;do{if(n&&(a=n())&&a.then&&!f(a))return void a.then(v).then(void 0,l);if(!(i=t())||f(i)&&!i.v)return void u(d,1,o);if(i.then)return void i.then(h).then(void 0,l);f(o=e())&&(o=o.v)}while(!o||!o.then);o.then(s).then(void 0,l)}function h(t){t?(o=e())&&o.then?o.then(s).then(void 0,l):s(o):u(d,1,o)}function v(){(i=t())?i.then?i.then(h).then(void 0,l):h(i):u(d,1,o)}}(function(){return e<=n.numPages},function(){return e++},function(){return Promise.resolve(n.getPage(e)).then(function(t){var n=t.getViewport({scale:1.5}),r=document.createElement("canvas");r.className="pdf-page pdf-page-"+e,r.dataset.page=e,r.width=n.width,r.height=n.height,r.style.display="block",r.style.marginBottom="10px",l.current.appendChild(r);var i=r.getContext("2d");return Promise.resolve(t.render({canvasContext:i,viewport:n}).promise).then(function(){})})});return r&&r.then?r.then(function(){}):void 0}})}var e;l.current.innerHTML="";var r=function(){if(o)return Promise.resolve(o.arrayBuffer()).then(function(t){e=i({data:new Uint8Array(t)})});for(var t=d.replace(/^data:.*;base64,/,""),n=atob(t),r=new Uint8Array(n.length),a=0;a<n.length;a++)r[a]=n.charCodeAt(a);e=i({data:r})}();return r&&r.then?r.then(n):n()}()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}(0,function(t){console.error("PDF rendering error:",t)}))}catch(t){return Promise.reject(t)}}(),function(){t=!0,l.current&&(l.current.innerHTML="")}}},[x,o,d]);var P=function(t){var n=document.querySelector(".pdf-page-"+t);n&&n.scrollIntoView({behavior:"smooth"})};return h("div",{style:{width:"100%",textAlign:"center",maxWidth:"95%",margin:"auto"}},h("div",{ref:l,onScroll:function(){var t=l.current;if(t){for(var n,e=t.querySelectorAll(".pdf-page"),r=s.current,i=function(t){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,n){if(t){if("string"==typeof t)return a(t,n);var e={}.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?a(t,n):void 0}}(t))){n&&(t=n);var e=0;return function(){return e>=t.length?{done:!0}:{done:!1,value:t[e++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e);!(n=i()).done;){var o=n.value,u=o.getBoundingClientRect();if(u.top>=0&&u.top<=.5*window.innerHeight){r=parseInt(o.dataset.page,10);break}}r!==s.current&&(s.current=r,y(r))}},style:{width:"100%",height:"65vh",overflowY:"scroll",border:"1px solid #ddd",background:"#f9f9f9"}}),h("div",{style:{marginTop:"10px",display:"flex",alignItems:"center",justifyContent:"center"}},h("button",{onClick:function(){return P(Math.max(g-1,1))},disabled:1===g,className:"btn text-white add_btn"},"Prev"),h("span",{style:{margin:"0 10px"}},"Page ",g," of ",p),h("button",{onClick:function(){return P(Math.min(g+1,p))},disabled:g===p,className:"btn sub_btn"},"Next")))}"undefined"!=typeof window&&void 0===r.workerSrc&&(r.workerSrc=o);export{d as default}; //# sourceMappingURL=PDFViewer-3bfe304b.js.map