pdfjs-viewer-element 3.1.0 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- const e_src="",e_iframeTitle="PDF viewer window",e_page="",e_search="",e_phrase="",e_zoom="",e_pagemode="none",e_locale="",e_viewerCssTheme="AUTOMATIC",e_workerSrc="https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.624/build/pdf.worker.min.mjs",t={AUTOMATIC:0,LIGHT:1,DARK:2};class i extends HTMLElement{constructor(){super(),this.viewerStyles=new Set,this.injectLocaleData=async()=>{const e=this.iframe.contentDocument,t=this.getAttribute("locale");if(!t)return void this.cleanupLocaleResource();const i=await import("./locale-DENudnjU.js");if(!Object.keys(JSON.parse(i.default)).includes(t))return void this.cleanupLocaleResource();const s={[String(t)]:`https://raw.githubusercontent.com/mozilla-l10n/firefox-l10n/main/${t}/toolkit/toolkit/pdfviewer/viewer.ftl`},r=e.createElement("link");r.rel="resource",r.type="application/l10n",this.cleanupLocaleResource(),this.localeResourceUrl=URL.createObjectURL(new Blob([JSON.stringify(s)],{type:"application/json"})),r.href=this.localeResourceUrl,this.iframe.contentDocument?.head.appendChild(r),this.localeResourceLink=r},this.onViewerAppCreated=()=>new Promise(e=>{const t=this.iframe.contentWindow;if(t.PDFViewerApplication)return e(t.PDFViewerApplication);let i;Object.defineProperty(t,"PDFViewerApplication",{get:()=>i,set(s){i=s,e(s),delete t.PDFViewerApplication,t.PDFViewerApplication=s},configurable:!0})}),this.applyViewerOptions=()=>{const t=this.iframe.contentWindow?.PDFViewerApplicationOptions;return t?.set("workerSrc",this.getAttribute("worker-src")||e_workerSrc),t?.set("defaultUrl",this.getFullPath(this.getAttribute("src")||e_src)),t?.set("disablePreferences",!0),t?.set("eventBusDispatchToDOM",!0),t?.set("localeProperties",{lang:this.getAttribute("locale")||e_locale}),t?.set("viewerCssTheme",this.getCssThemeOption()),t},this.getIframeLocationHash=()=>{const t={page:this.getAttribute("page")||e_page,zoom:this.getAttribute("zoom")||e_zoom,pagemode:this.getAttribute("pagemode")||e_pagemode,search:this.getAttribute("search")||e_search,phrase:this.getAttribute("phrase")||e_phrase,locale:this.getAttribute("locale")||e_locale};return"#"+Object.entries(t).map(([e,t])=>`${e}=${t}`).join("&")},this.buildViewerEntry=async()=>{const[e,t]=await Promise.all([import("./viewer-min-DkHkP1Ha.js"),import("./viewer-Cg70hpj3.js")]),i=e.default.replace("</head>",`\n <style>${t.default}</style>\n ${Array.from(this.viewerStyles).map(e=>`<style>${e}</style>`).join("\n")}\n </head>`);this.iframe.addEventListener("load",this.buildViewerApp,{once:!0}),this.iframe.srcdoc=i},this.setupViewerApp=async()=>{const e=await this.onViewerAppCreated(),t=this.applyViewerOptions();await(e?.initializedPromise),this.applyQueuedRuntimeStyles(),this.applyIframeHash(),this.dispatchEvent(new CustomEvent("initialized",{detail:{viewerApp:e,viewerOptions:t},bubbles:!0,composed:!0}))},this.buildViewerApp=async()=>{const[e,t]=await Promise.all([import("./pdf.min-x5NwD-YK.js"),import("./viewer.min-Dyq1R7lM.js")]);await this.injectLocaleData(),this.injectScript(e.default),this.injectScript(t.default),await this.setupViewerApp()},this.attachShadow({mode:"open"}).innerHTML='\n <style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>\n <iframe frameborder="0" width="100%" loading="lazy"></iframe>\n '}static get observedAttributes(){return["src","locale","viewer-css-theme","worker-src","page","search","phrase","zoom","pagemode","iframe-title"]}getFullPath(e){return e.startsWith("/")?`${window.location.origin}${e}`:e}getCssThemeOption(){const i=this.getAttribute("viewer-css-theme");return Object.keys(t).includes(i)?t[i]:t[e_viewerCssTheme]}applyIframeHash(){this.iframe?.contentWindow&&(this.iframe.contentWindow.location.hash=this.getIframeLocationHash())}applyViewerTheme(){const e=this.getCssThemeOption(),i=this.iframe.contentWindow?.PDFViewerApplicationOptions;i?.set("viewerCssTheme",e);const s=this.iframe.contentDocument;if(!s?.documentElement)return;const r=e===t.LIGHT?"light":e===t.DARK?"dark":"";r?s.documentElement.style.setProperty("color-scheme",r):s.documentElement.style.removeProperty("color-scheme")}appendRuntimeStyle(e){const t=this.iframe?.contentDocument;if(!t?.head||!e)return;if(Array.from(t.querySelectorAll("style")).some(t=>t.textContent===e))return;const i=t.createElement("style");i.setAttribute("data-pdfjs-viewer-runtime-style","true"),i.textContent=e,t.head.appendChild(i)}applyQueuedRuntimeStyles(){this.viewerStyles.forEach(e=>{this.appendRuntimeStyle(e)})}injectScript(e,t="module"){const i=this.iframe.contentDocument;if(!i)return;if(!i.head){const e=i.createElement("head");i.documentElement?.prepend(e)}const s=document.createElement("script");s.type=t,s.textContent=e,i.head?.appendChild(s)}cleanupLocaleResource(){this.localeResourceLink&&(this.localeResourceLink.remove(),this.localeResourceLink=void 0),this.localeResourceUrl&&(URL.revokeObjectURL(this.localeResourceUrl),this.localeResourceUrl=void 0)}async connectedCallback(){this.iframe=this.shadowRoot?.querySelector("iframe"),this.iframe.setAttribute("title",this.getAttribute("iframe-title")||e_iframeTitle),await this.buildViewerEntry()}disconnectedCallback(){for(this.cleanupLocaleResource(),this.iframe.removeEventListener("load",this.buildViewerApp),this.iframe.src="about:blank";this.firstChild;)this.removeChild(this.firstChild)}async attributeChangedCallback(t,i,s){if(i!==s&&this.iframe)switch(t){case"src":{const t=this.iframe.contentWindow?.PDFViewerApplication;if(t){await t.initializedPromise;const i=this.getFullPath(s||e_src);i&&t.open({url:i})}return}case"locale":return this.cleanupLocaleResource(),void await this.buildViewerEntry();case"viewer-css-theme":return void this.applyViewerTheme();case"worker-src":{const t=this.iframe.contentWindow?.PDFViewerApplicationOptions;return void t?.set("workerSrc",s||e_workerSrc)}default:this.applyIframeHash()}}async injectViewerStyles(e){e&&(this.viewerStyles.add(e),this.appendRuntimeStyle(e))}}window.customElements.get("pdfjs-viewer-element")||window.customElements.define("pdfjs-viewer-element",i);export{i as PdfjsViewerElement,t as ViewerCssTheme,i as default};
1
+ const e={src:"",iframeTitle:"PDF viewer window",page:"",search:"",phrase:"",zoom:"",pagemode:"none",locale:"",viewerCssTheme:"AUTOMATIC",workerSrc:new URL("./pdf.worker.min.mjs",import.meta.url).href,localeSrcTemplate:"https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl"},t={AUTOMATIC:0,LIGHT:1,DARK:2};class i extends HTMLElement{constructor(){super(),this.initPromise=Promise.resolve({}),this.viewerStyles=new Set,this.applyIframeHash=async()=>new Promise(e=>{if(!this.iframe?.contentWindow)return e();this.iframe.contentWindow?.addEventListener("hashchange",()=>{e()},{once:!0}),this.iframe.contentWindow.location.hash=this.getIframeLocationHash()}),this.injectLocaleData=async()=>{const t=this.iframe.contentDocument,i=this.getAttribute("locale");if(!i)return void this.cleanupLocaleResource();const r=await import("./locale-DENudnjU.js");if(!Object.keys(JSON.parse(r.default)).includes(i))return void this.cleanupLocaleResource();const s=this.formatTemplate(this.getAttribute("locale-src-template")||e.localeSrcTemplate,{locale:i}),a={[String(i)]:s},o=t.createElement("link");o.rel="resource",o.type="application/l10n",this.cleanupLocaleResource(),this.localeResourceUrl=URL.createObjectURL(new Blob([JSON.stringify(a)],{type:"application/json"})),o.href=this.localeResourceUrl,this.iframe.contentDocument?.head.appendChild(o),this.localeResourceLink=o},this.onViewerAppCreated=()=>new Promise(e=>{const t=this.iframe.contentWindow;if(t.PDFViewerApplication)return e(t.PDFViewerApplication);let i;Object.defineProperty(t,"PDFViewerApplication",{get:()=>i,set(r){i=r,e(r),delete t.PDFViewerApplication,t.PDFViewerApplication=r},configurable:!0})}),this.applyViewerOptions=()=>{const t=this.iframe.contentWindow?.PDFViewerApplicationOptions;t?.set("workerSrc",this.getAttribute("worker-src")||e.workerSrc),t?.set("defaultUrl",this.getFullPath(this.getAttribute("src")||e.src)),t?.set("disablePreferences",!0),t?.set("eventBusDispatchToDOM",!0),t?.set("localeProperties",{lang:this.getAttribute("locale")||e.locale}),t?.set("viewerCssTheme",this.getCssThemeOption())},this.getIframeLocationHash=()=>{const t={page:this.getAttribute("page")||e.page,zoom:this.getAttribute("zoom")||e.zoom,pagemode:this.getAttribute("pagemode")||e.pagemode,search:this.getAttribute("search")||e.search,phrase:this.getAttribute("phrase")||e.phrase,locale:this.getAttribute("locale")||e.locale};return"#"+Object.entries(t).map(([e,t])=>`${e}=${t}`).join("&")},this.buildViewerEntry=async()=>new Promise(async e=>{const[t,i]=await Promise.all([import("./viewer-gf2SDly8.js"),import("./viewer-00vA-OlL.js")]),r=t.default.replace("</head>",`\n <style>${i.default}</style>\n ${Array.from(this.viewerStyles).map(e=>`<style>${e}</style>`).join("\n")}\n </head>`);this.iframe.addEventListener("load",()=>e(),{once:!0}),this.iframe.srcdoc=r}),this.setupViewerApp=async()=>{const e=await this.onViewerAppCreated();return this.applyViewerOptions(),await(e?.initializedPromise),this.applyQueuedRuntimeStyles(),{viewerApp:e}},this.buildViewerApp=async()=>{await this.applyIframeHash();const[e,t]=await Promise.all([import("./pdf-DRXGYF_W.js"),import("./viewer-DVUWiHIQ.js")]);return await this.injectLocaleData(),this.injectScript(e.default),this.injectScript(t.default),await this.setupViewerApp()},this.attachShadow({mode:"open"}).innerHTML='\n <style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>\n <iframe frameborder="0" width="100%" loading="lazy"></iframe>\n '}static get observedAttributes(){return["src","locale","viewer-css-theme","worker-src","page","search","phrase","zoom","pagemode","iframe-title"]}formatTemplate(e,t){return e.replace(/\{(\w+)\}/g,(e,i)=>{if(!(i in t))throw new Error(`Missing param: ${i}`);return String(t[i])})}getFullPath(e){return e.startsWith("/")?`${window.location.origin}${e}`:e}getCssThemeOption(){const i=this.getAttribute("viewer-css-theme");return Object.keys(t).includes(i)?t[i]:t[e.viewerCssTheme]}applyViewerTheme(){const e=this.getCssThemeOption(),i=this.iframe.contentWindow?.PDFViewerApplicationOptions;i?.set("viewerCssTheme",e);const r=this.iframe.contentDocument;if(!r?.documentElement)return;const s=e===t.LIGHT?"light":e===t.DARK?"dark":"";s?r.documentElement.style.setProperty("color-scheme",s):r.documentElement.style.removeProperty("color-scheme")}appendRuntimeStyle(e){const t=this.iframe?.contentDocument;if(!t?.head||!e)return;if(Array.from(t.querySelectorAll("style")).some(t=>t.textContent===e))return;const i=t.createElement("style");i.setAttribute("data-pdfjs-viewer-runtime-style","true"),i.textContent=e,t.head.appendChild(i)}applyQueuedRuntimeStyles(){this.viewerStyles.forEach(e=>{this.appendRuntimeStyle(e)})}injectScript(e,t="module"){const i=this.iframe.contentDocument;if(!i)return;if(!i.head){const e=i.createElement("head");i.documentElement?.prepend(e)}const r=document.createElement("script");r.type=t,r.textContent=e,i.head?.appendChild(r)}cleanupLocaleResource(){this.localeResourceLink&&(this.localeResourceLink.remove(),this.localeResourceLink=void 0),this.localeResourceUrl&&(URL.revokeObjectURL(this.localeResourceUrl),this.localeResourceUrl=void 0)}async connectedCallback(){this.iframe=this.shadowRoot?.querySelector("iframe"),this.iframe.setAttribute("title",this.getAttribute("iframe-title")||e.iframeTitle),this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))()}disconnectedCallback(){for(this.cleanupLocaleResource(),this.iframe.src="about:blank";this.firstChild;)this.removeChild(this.firstChild)}async attributeChangedCallback(t,i,r){if(i!==r&&this.iframe)switch(t){case"src":{const t=this.iframe.contentWindow?.PDFViewerApplication;if(t){await t.initializedPromise;const i=this.getFullPath(r||e.src);i&&t.open({url:i})}return}case"locale":return this.cleanupLocaleResource(),this.initPromise=(async()=>(await this.buildViewerEntry(),await this.buildViewerApp()))(),void await this.initPromise;case"viewer-css-theme":return void this.applyViewerTheme();case"worker-src":{const t=this.iframe.contentWindow?.PDFViewerApplicationOptions;return void t?.set("workerSrc",r||e.workerSrc)}default:await this.applyIframeHash()}}async injectViewerStyles(e){e&&(this.viewerStyles.add(e),this.appendRuntimeStyle(e))}}window.customElements.get("pdfjs-viewer-element")||window.customElements.define("pdfjs-viewer-element",i);export{i as PdfjsViewerElement,t as ViewerCssTheme,i as default};