UNPKG

enoviq-react-document-viewer

Version:

Custom React & Next.js compatible document viewer package

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