UNPKG

@compdfkit_pdf_sdk/webviewer

Version:

[ComPDFKit PDF SDK](https://www.compdf.com) seamlessly operates on [Web](https://www.compdf.com/web), [Windows](https://www.compdf.com/windows), [Android](https://www.compdf.com/android), [iOS](https://www.compdf.com/ios), [Mac](https://www.compdf.com/con

2 lines (1 loc) 4.21 kB
function e(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var n=i.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function t(t,i){for(var n=0;n<i.length;n++){var o=i[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,e(o.key),o)}}var i={init:function(e,t){return new Promise((function(i){t.addEventListener("ready",(function e(){t.removeEventListener("ready",e);var n=t.querySelector("iframe").contentWindow;try{var o=n.instance;Promise.resolve().then((function(){i(o)}))}catch(e){i()}})),new c(e,t)}))}};var n,o,r,a=0,s=new Map,c=function(){return e=function e(t,i){if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),!s.get(i)){this.docId=0,s.set(i,!0),this.instance=null,this.options=t,this.initialDoc=t.pdfUrl||null,this.element=i,this.options.path=this.options.path||e.config.path;var n=this.options.path.length-1;n>0&&"/"!==this.options.path[n]&&(this.options.path+="/"),this.options.webviewerPath=this.options.path+e.config.webviewerPath,i.addEventListener("ready",(function e(){i.removeEventListener("ready",e)})),this.create()}},(i=[{key:"_createEvent",value:function(e,t){var i;try{i=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0})}catch(n){(i=document.createEvent("Event")).initEvent(e,!0,!0),i.detail=t}return i}},{key:"create",value:function(){this.initialDoc?(this.initialDoc=encodeURIComponent(JSON.stringify(this.initialDoc)),this._create()):this._create()}},{key:"_create",value:function(){this.docId=++a,this._trigger||(this._trigger=function(e,t){var i=this._createEvent(e,t);this.element.dispatchEvent(i)});var e=this;this.messageHandler=function(t){if("requestConfig"===t.data.type&&t.data.docId===e.docId&&t.source){window.removeEventListener("message",this.messageHandler);var i=e.options.config?e._correctRelativePath(e.options.config):"";t.source.postMessage({type:"responseConfig",value:i,options:e.options},"*")}},window.addEventListener("message",this.messageHandler,!1),this.createViewer()}},{key:"_correctRelativePath",value:function(e){return function(e,t){if("string"!=typeof e||!t)return e;var i=t.substring(0,t.lastIndexOf("/"));return/^(\/|%2F|[a-z0-9-]+:)/i.test(e)?e:"".concat(i,"/").concat(e)}(e,window.location.pathname)}},{key:"createViewer",value:function(){var e,t,i,n=this,o=this.options.webviewerPath;this.initialDoc?o+="#d=".concat(this.initialDoc):o+="#",o+="&docId=".concat(this.docId),(null===(e=this.options)||void 0===e?void 0:e.css)&&(o+="&css=".concat(encodeURIComponent(this.options.css))),void 0!==this.options.showToolbarControl&&(o+="&header=".concat(this.options.showToolbarControl?"true":"false"));var r=document.createElement("iframe");r.id="webviewer-"+this.docId,r.src=o,r.frameBorder=0,r.width="100%",r.height="100%",r.setAttribute("allowfullscreen",!0),r.setAttribute("webkitallowfullscreen",!0),r.setAttribute("mozallowfullscreen",!0),this.iframe=r,null!==(t=this.options)&&void 0!==t&&t.backgroundColor&&r.setAttribute("data-bgcolor",this.options.backgroundColor),null!==(i=this.options)&&void 0!==i&&i.assetPath&&r.setAttribute("data-assetpath",encodeURIComponent(this.options.assetPath)),this.element.appendChild(r),window.addEventListener("message",(function e(t){if("viewerLoaded"===t.data.type&&t.data.docId===n.docId)try{n.instance=n.iframe.contentWindow.instance}catch(e){}finally{window.removeEventListener("message",e),n._viewerLoaded(n.iframe)}}))}},{key:"_documentViewerLoaded",value:function(e){this.options.pdfUrl&&e.contentWindow.instance.UI.loadDocument(this.options.pdfUrl)}},{key:"_viewerLoaded",value:function(e){this._trigger("ready");try{e.contentWindow.postMessage({type:"viewerLoaded",options:this.options},"*")}catch(e){}}}])&&t(e.prototype,i),n&&t(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,i,n}();n=c,r={path:"",webviewerPath:"./webviewer/index.html"},(o=e(o="config"))in n?Object.defineProperty(n,o,{value:r,enumerable:!0,configurable:!0,writable:!0}):n[o]=r;export{i as default};