pdfjs-viewer-element
Advanced tools
Comparing version 2.3.2 to 2.3.3
@@ -1,10 +0,10 @@ | ||
const w = (s, e) => new Promise((r, n) => { | ||
let i = e.querySelector(s); | ||
if (i) { | ||
r(i); | ||
const w = (l, e) => new Promise((t, n) => { | ||
let r = e.querySelector(l); | ||
if (r) { | ||
t(r); | ||
return; | ||
} | ||
new MutationObserver((c, o) => { | ||
Array.from(e.querySelectorAll(s)).forEach((a) => { | ||
r(a), o.disconnect(); | ||
new MutationObserver((h, s) => { | ||
Array.from(e.querySelectorAll(l)).forEach((o) => { | ||
t(o), s.disconnect(); | ||
}); | ||
@@ -15,3 +15,32 @@ }).observe(e, { | ||
}); | ||
}), t = { | ||
}), g = { | ||
trailing: !0 | ||
}; | ||
function b(l, e = 25, t = {}) { | ||
if (t = { ...g, ...t }, !Number.isFinite(e)) | ||
throw new TypeError("Expected `wait` to be a finite number"); | ||
let n, r, h = [], s, o; | ||
const d = (a, c) => (s = y(l, a, c), s.finally(() => { | ||
if (s = null, t.trailing && o && !r) { | ||
const u = d(a, o); | ||
return o = null, u; | ||
} | ||
}), s); | ||
return function(...a) { | ||
return s ? (t.trailing && (o = a), s) : new Promise((c) => { | ||
const u = !r && t.leading; | ||
clearTimeout(r), r = setTimeout(() => { | ||
r = null; | ||
const f = t.leading ? n : d(this, a); | ||
for (const p of h) | ||
p(f); | ||
h = []; | ||
}, e), u ? (n = d(this, a), c(n)) : h.push(c); | ||
}); | ||
}; | ||
} | ||
async function y(l, e, t) { | ||
return await l.apply(e, t); | ||
} | ||
const i = { | ||
viewerPath: "/pdfjs", | ||
@@ -23,14 +52,17 @@ viewerEntry: "/web/viewer.html", | ||
phrase: "", | ||
zoom: "", | ||
pagemode: "", | ||
zoom: "auto", | ||
pagemode: "none", | ||
locale: "", | ||
textLayer: "" | ||
}, m = document.createElement("template"); | ||
m.innerHTML = ` | ||
<iframe frameborder="0" width="100%"></iframe> | ||
<style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style> | ||
`; | ||
class d extends HTMLElement { | ||
}; | ||
class m extends HTMLElement { | ||
constructor() { | ||
super(), this.attachShadow({ mode: "open" }).appendChild(m.content.cloneNode(!0)); | ||
super(), this.debouncedRenderIframe = b(async () => { | ||
await w("iframe", this.shadowRoot), this.renderViewer(this.getIframeSrc()); | ||
}, 0, { leading: !0 }); | ||
const e = this.attachShadow({ mode: "open" }), t = document.createElement("template"); | ||
t.innerHTML = ` | ||
<iframe frameborder="0" width="100%"></iframe> | ||
<style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style> | ||
`, e.appendChild(t.content.cloneNode(!0)); | ||
} | ||
@@ -44,14 +76,15 @@ static get observedAttributes() { | ||
attributeChangedCallback() { | ||
w("iframe", this.shadowRoot).then(() => this.setProps()); | ||
this.debouncedRenderIframe(); | ||
} | ||
setProps() { | ||
const e = this.getFullPath(this.getAttribute("src") || t.src), r = this.getFullPath(this.getAttribute("viewer-path") || t.viewerPath), n = this.getAttribute("page") || t.page, i = this.getAttribute("search") || t.search, c = this.getAttribute("phrase") || t.phrase, o = this.getAttribute("zoom") || t.zoom, a = this.getAttribute("pagemode") || t.pagemode, h = this.getAttribute("locale") || t.locale, p = this.getAttribute("text-layer") || t.textLayer, l = `${r}${t.viewerEntry}?file=${e}#page=${n}&zoom=${o}&pagemode=${a}&search=${i}&phrase=${c}&textLayer=${p}${h ? "&locale=" + h : ""}`; | ||
l !== this.iframe.getAttribute("src") && this.rerenderIframe(l); | ||
getIframeSrc() { | ||
const e = this.getFullPath(this.getAttribute("src") || i.src), t = this.getFullPath(this.getAttribute("viewer-path") || i.viewerPath), n = this.getAttribute("page") || i.page, r = this.getAttribute("search") || i.search, h = this.getAttribute("phrase") || i.phrase, s = this.getAttribute("zoom") || i.zoom, o = this.getAttribute("pagemode") || i.pagemode, d = this.getAttribute("locale") || i.locale, a = this.getAttribute("text-layer") || i.textLayer, c = `${t}${i.viewerEntry}?file=${encodeURIComponent(e)}#page=${n}&zoom=${s}&pagemode=${o}&search=${r}&phrase=${h}&textLayer=${a}${d ? "&locale=" + d : ""}`; | ||
return c !== this.iframe.getAttribute("src") ? c : ""; | ||
} | ||
rerenderIframe(e) { | ||
this.shadowRoot.replaceChild(this.iframe.cloneNode(), this.iframe), this.iframe = this.shadowRoot.querySelector("iframe"), this.iframe.contentWindow.location.href = e; | ||
renderViewer(e) { | ||
!e || !this.iframe || (this.shadowRoot.replaceChild(this.iframe.cloneNode(), this.iframe), this.iframe = this.shadowRoot.querySelector("iframe"), this.iframe.src = e); | ||
} | ||
setEventListeners() { | ||
document.addEventListener("webviewerloaded", () => { | ||
this.getAttribute("src") !== t.src && this.iframe.contentWindow.PDFViewerApplicationOptions.set("defaultUrl", ""), this.iframe.contentWindow.PDFViewerApplicationOptions.set("disablePreferences", !0), this.iframe.contentWindow.PDFViewerApplicationOptions.set("pdfBugEnabled", !0); | ||
var e, t, n, r; | ||
this.getAttribute("src") !== i.src && ((e = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || e.set("defaultUrl", "")), (t = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || t.set("disablePreferences", !0), (n = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || n.set("pdfBugEnabled", !0), (r = this.iframe.contentWindow.PDFViewerApplicationOptions) == null || r.set("eventBusDispatchToDOM", !0); | ||
}); | ||
@@ -63,6 +96,6 @@ } | ||
} | ||
window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = d, window.customElements.define("pdfjs-viewer-element", d)); | ||
window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = m, window.customElements.define("pdfjs-viewer-element", m)); | ||
export { | ||
d as PdfjsViewerElement, | ||
d as default | ||
m as PdfjsViewerElement, | ||
m as default | ||
}; |
{ | ||
"name": "pdfjs-viewer-element", | ||
"version": "2.3.2", | ||
"version": "2.3.3", | ||
"license": "MIT", | ||
@@ -34,9 +34,16 @@ "author": { | ||
"dev": "vite", | ||
"build": "tsc && vite build" | ||
"build": "tsc && vite build", | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.7.1", | ||
"@vitest/browser": "^0.34.3", | ||
"jsdom": "^22.1.0", | ||
"perfect-debounce": "^1.0.0", | ||
"typescript": "^4.6.4", | ||
"vite": "^4.1.4" | ||
"vite": "^4.4.9", | ||
"vitest": "^0.34.3", | ||
"webdriverio": "^8.16.4" | ||
} | ||
} |
# pdfjs-viewer-element | ||
A web component for viewing pdf files in the browser. The package is based on [PDF.js default viewer](https://mozilla.github.io/pdf.js/web/viewer.html) build and works with any framework. | ||
A custom element, based on [PDF.js default viewer](https://mozilla.github.io/pdf.js/web/viewer.html). Supported in all [major browsers](https://caniuse.com/custom-elementsv1) and works with most [JS frameworks](https://custom-elements-everywhere.com/). See [examples](https://alekswebnet.github.io/pdfjs-viewer-element/#demo) of usage in the different frameworks. | ||
⚠️ `pdfjs-viewer-element` uses PDF.js [prebuilt](http://mozilla.github.io/pdf.js/getting_started/), that includes the generic build of PDF.js and the viewer. To use the package you should download and **place the prebuilt** files to some directory of your project. Then specify the path to this directory with `viewer-path` property (`/pdfjs` by default). | ||
⚠️ `pdfjs-viewer-element` requires PDF.js [prebuilt](http://mozilla.github.io/pdf.js/getting_started/), that includes the generic build of PDF.js and the viewer. To use the package you should [download](http://mozilla.github.io/pdf.js/getting_started/) and **place the prebuilt** files to some directory of your project. Then specify the path to this directory with `viewer-path` property (`/pdfjs` by default). | ||
@@ -50,7 +50,7 @@ ## Status | ||
`src` - Path to pdf file | ||
`src` - PDF file URL, should refer to the [same origin](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#can-i-load-a-pdf-from-another-server-cross-domain-request) | ||
`viewer-path` - Path to PDF.js prebuilt | ||
`viewer-path` - Path to PDF.js [prebuilt](http://mozilla.github.io/pdf.js/getting_started/) | ||
`locale` - Language of the interface, see [all l10n files](https://github.com/mozilla/pdf.js/tree/master/l10n) | ||
`locale` - Specifies which language to use in the viewer UI. For a list of the available locales, see [all l10n files](https://github.com/mozilla/pdf.js/tree/master/l10n) | ||
@@ -72,4 +72,2 @@ `text-layer` - Text layer, that is used for text selection | ||
## License | ||
For this package - [MIT](http://opensource.org/licenses/MIT). | ||
For the `pdf.js` library - https://github.com/mozilla/pdf.js/blob/master/LICENSE. | ||
[MIT](http://opensource.org/licenses/MIT). |
@@ -7,4 +7,5 @@ export declare class PdfjsViewerElement extends HTMLElement { | ||
attributeChangedCallback(): void; | ||
private setProps; | ||
private rerenderIframe; | ||
private debouncedRenderIframe; | ||
private getIframeSrc; | ||
private renderViewer; | ||
private setEventListeners; | ||
@@ -15,3 +16,3 @@ private getFullPath; | ||
interface Window { | ||
PdfjsViewerElement: typeof PdfjsViewerElement; | ||
'PdfjsViewerElement': typeof PdfjsViewerElement; | ||
} | ||
@@ -18,0 +19,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30631373
8
376
95389
72
3
16