pdfjs-viewer-element 2.3.3 → 2.4.1

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.
package/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # pdfjs-viewer-element
2
2
 
3
- 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.
3
+ 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 Vue, React and Svelte or pure HTML.
4
4
 
5
5
  ⚠️ `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).
6
6
 
7
+ You have full access to PDF.js viewer application using `initialize` method.
8
+
7
9
  ## Status
8
10
 
9
11
  [![npm version](https://img.shields.io/npm/v/pdfjs-viewer-element?logo=npm&logoColor=fff)](https://www.npmjs.com/package/pdfjs-viewer-element)
@@ -14,7 +16,7 @@ A custom element, based on [PDF.js default viewer](https://mozilla.github.io/pdf
14
16
 
15
17
  [Getting started](https://alekswebnet.github.io/pdfjs-viewer-element/)
16
18
 
17
- [Api](https://alekswebnet.github.io/pdfjs-viewer-element/#api)
19
+ [API](https://alekswebnet.github.io/pdfjs-viewer-element/#api)
18
20
 
19
21
  [Live examples](https://alekswebnet.github.io/pdfjs-viewer-element/#demo)
20
22
 
@@ -67,5 +69,21 @@ Using browser:
67
69
 
68
70
  For more clarity, see the [Api docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
69
71
 
72
+ ## PDF.js Viewer Application
73
+
74
+ `initialize` - using this method you can access PDFViewerApplication and use methods and events of PDF.js default viewer
75
+
76
+ ```javascript
77
+ const viewer = document.querySelector('pdfjs-viewer-element')
78
+ // Wait for the viewer initialization, receive PDFViewerApplication
79
+ const viewerApp = await viewer.initialize()
80
+ // Open PDF file data using Uint8Array instead of URL
81
+ viewerApp.open(pdfData)
82
+ // Use event bus to handle viewer application events
83
+ viewerApp.eventBus.on('pagesloaded', () => {
84
+ console.log('Viewer pages loaded')
85
+ })
86
+ ```
87
+
70
88
  ## License
71
- [MIT](http://opensource.org/licenses/MIT).
89
+ [MIT](http://opensource.org/licenses/MIT)
@@ -1,12 +1,12 @@
1
- const w = (l, e) => new Promise((t, n) => {
2
- let r = e.querySelector(l);
3
- if (r) {
4
- t(r);
1
+ const m = (h, e) => new Promise((t, o) => {
2
+ let i = e.querySelector(h);
3
+ if (i) {
4
+ t(i);
5
5
  return;
6
6
  }
7
- new MutationObserver((h, s) => {
8
- Array.from(e.querySelectorAll(l)).forEach((o) => {
9
- t(o), s.disconnect();
7
+ new MutationObserver((s, r) => {
8
+ Array.from(e.querySelectorAll(h)).forEach((n) => {
9
+ t(n), r.disconnect();
10
10
  });
11
11
  }).observe(e, {
12
12
  childList: !0,
@@ -15,33 +15,33 @@ const w = (l, e) => new Promise((t, n) => {
15
15
  }), g = {
16
16
  trailing: !0
17
17
  };
18
- function b(l, e = 25, t = {}) {
18
+ function b(h, e = 25, t = {}) {
19
19
  if (t = { ...g, ...t }, !Number.isFinite(e))
20
20
  throw new TypeError("Expected `wait` to be a finite number");
21
- let n, r, h = [], s, o;
22
- const d = (a, c) => (s = y(l, a, c), s.finally(() => {
23
- if (s = null, t.trailing && o && !r) {
24
- const u = d(a, o);
25
- return o = null, u;
21
+ let o, i, s = [], r, n;
22
+ const l = (c, d) => (r = y(h, c, d), r.finally(() => {
23
+ if (r = null, t.trailing && n && !i) {
24
+ const u = l(c, n);
25
+ return n = null, u;
26
26
  }
27
- }), s);
28
- return function(...a) {
29
- return s ? (t.trailing && (o = a), s) : new Promise((c) => {
30
- const u = !r && t.leading;
31
- clearTimeout(r), r = setTimeout(() => {
32
- r = null;
33
- const f = t.leading ? n : d(this, a);
34
- for (const p of h)
27
+ }), r);
28
+ return function(...c) {
29
+ return r ? (t.trailing && (n = c), r) : new Promise((d) => {
30
+ const u = !i && t.leading;
31
+ clearTimeout(i), i = setTimeout(() => {
32
+ i = null;
33
+ const f = t.leading ? o : l(this, c);
34
+ for (const p of s)
35
35
  p(f);
36
- h = [];
37
- }, e), u ? (n = d(this, a), c(n)) : h.push(c);
36
+ s = [];
37
+ }, e), u ? (o = l(this, c), d(o)) : s.push(d);
38
38
  });
39
39
  };
40
40
  }
41
- async function y(l, e, t) {
42
- return await l.apply(e, t);
41
+ async function y(h, e, t) {
42
+ return await h.apply(e, t);
43
43
  }
44
- const i = {
44
+ const a = {
45
45
  viewerPath: "/pdfjs",
46
46
  viewerEntry: "/web/viewer.html",
47
47
  src: "",
@@ -53,11 +53,17 @@ const i = {
53
53
  locale: "",
54
54
  textLayer: ""
55
55
  };
56
- class m extends HTMLElement {
56
+ class w extends HTMLElement {
57
57
  constructor() {
58
- super(), this.debouncedRenderIframe = b(async () => {
59
- await w("iframe", this.shadowRoot), this.renderViewer(this.getIframeSrc());
60
- }, 0, { leading: !0 });
58
+ super(), this.onIframeReady = b(async (o) => {
59
+ await m("iframe", this.shadowRoot), o();
60
+ }, 0, { leading: !0 }), this.initialize = () => new Promise(async (o) => {
61
+ var i;
62
+ await m("iframe", this.shadowRoot), (i = this.iframe) == null || i.addEventListener("load", async () => {
63
+ var s, r, n;
64
+ await ((r = (s = this.iframe.contentWindow) == null ? void 0 : s.PDFViewerApplication) == null ? void 0 : r.initializedPromise), o((n = this.iframe.contentWindow) == null ? void 0 : n.PDFViewerApplication);
65
+ }, { once: !0 });
66
+ });
61
67
  const e = this.attachShadow({ mode: "open" }), t = document.createElement("template");
62
68
  t.innerHTML = `
63
69
  <iframe frameborder="0" width="100%"></iframe>
@@ -71,27 +77,27 @@ class m extends HTMLElement {
71
77
  this.iframe = this.shadowRoot.querySelector("iframe"), this.setEventListeners();
72
78
  }
73
79
  attributeChangedCallback() {
74
- this.debouncedRenderIframe();
80
+ this.onIframeReady(() => this.mountViewer(this.getIframeSrc()));
75
81
  }
76
82
  getIframeSrc() {
77
- 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 : ""}`;
78
- return c !== this.iframe.getAttribute("src") ? c : "";
83
+ const e = this.getFullPath(this.getAttribute("src") || a.src), t = this.getFullPath(this.getAttribute("viewer-path") || a.viewerPath), o = this.getAttribute("page") || a.page, i = this.getAttribute("search") || a.search, s = this.getAttribute("phrase") || a.phrase, r = this.getAttribute("zoom") || a.zoom, n = this.getAttribute("pagemode") || a.pagemode, l = this.getAttribute("locale") || a.locale, c = this.getAttribute("text-layer") || a.textLayer, d = `${t}${a.viewerEntry}?file=${encodeURIComponent(e)}#page=${o}&zoom=${r}&pagemode=${n}&search=${i}&phrase=${s}&textLayer=${c}${l ? "&locale=" + l : ""}`;
84
+ return d !== this.iframe.getAttribute("src") ? d : "";
79
85
  }
80
- renderViewer(e) {
86
+ mountViewer(e) {
81
87
  !e || !this.iframe || (this.shadowRoot.replaceChild(this.iframe.cloneNode(), this.iframe), this.iframe = this.shadowRoot.querySelector("iframe"), this.iframe.src = e);
82
88
  }
83
89
  setEventListeners() {
84
90
  document.addEventListener("webviewerloaded", () => {
85
- var e, t, n, r;
86
- 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);
91
+ var e, t, o, i, s, r, n, l;
92
+ this.getAttribute("src") !== a.src && ((t = (e = this.iframe.contentWindow) == null ? void 0 : e.PDFViewerApplicationOptions) == null || t.set("defaultUrl", "")), (i = (o = this.iframe.contentWindow) == null ? void 0 : o.PDFViewerApplicationOptions) == null || i.set("disablePreferences", !0), (r = (s = this.iframe.contentWindow) == null ? void 0 : s.PDFViewerApplicationOptions) == null || r.set("pdfBugEnabled", !0), (l = (n = this.iframe.contentWindow) == null ? void 0 : n.PDFViewerApplicationOptions) == null || l.set("eventBusDispatchToDOM", !0);
87
93
  });
88
94
  }
89
95
  getFullPath(e) {
90
96
  return e.startsWith("/") ? `${window.location.origin}${e}` : e;
91
97
  }
92
98
  }
93
- window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = m, window.customElements.define("pdfjs-viewer-element", m));
99
+ window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = w, window.customElements.define("pdfjs-viewer-element", w));
94
100
  export {
95
- m as PdfjsViewerElement,
96
- m as default
101
+ w as PdfjsViewerElement,
102
+ w as default
97
103
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfjs-viewer-element",
3
- "version": "2.3.3",
3
+ "version": "2.4.1",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Oleksandr Shevchuk",
@@ -43,6 +43,6 @@
43
43
  "typescript": "^4.6.4",
44
44
  "vite": "^4.4.9",
45
45
  "vitest": "^0.34.3",
46
- "webdriverio": "^8.16.4"
46
+ "webdriverio": "^8.16.7"
47
47
  }
48
48
  }
@@ -4,18 +4,28 @@ export declare class PdfjsViewerElement extends HTMLElement {
4
4
  static get observedAttributes(): string[];
5
5
  connectedCallback(): void;
6
6
  attributeChangedCallback(): void;
7
- private debouncedRenderIframe;
7
+ private onIframeReady;
8
8
  private getIframeSrc;
9
- private renderViewer;
9
+ private mountViewer;
10
10
  private setEventListeners;
11
11
  private getFullPath;
12
+ initialize: () => Promise<unknown>;
12
13
  }
13
14
  declare global {
14
15
  interface Window {
15
- 'PdfjsViewerElement': typeof PdfjsViewerElement;
16
+ PdfjsViewerElement: typeof PdfjsViewerElement;
16
17
  }
17
18
  }
19
+ export interface IPdfjsViewerElement extends HTMLElement {
20
+ initialize: () => Promise<PdfjsViewerElementIframeWindow['PDFViewerApplication']>;
21
+ }
18
22
  export interface PdfjsViewerElementIframeWindow extends Window {
23
+ PDFViewerApplication: {
24
+ initializedPromise: Promise<void>;
25
+ initialized: boolean;
26
+ open: (data: Uint8Array) => void;
27
+ eventBus: Record<string, any>;
28
+ };
19
29
  PDFViewerApplicationOptions: {
20
30
  set: (name: string, value: string | boolean) => void;
21
31
  };
@@ -23,4 +33,9 @@ export interface PdfjsViewerElementIframeWindow extends Window {
23
33
  export interface PdfjsViewerElementIframe extends HTMLIFrameElement {
24
34
  contentWindow: PdfjsViewerElementIframeWindow;
25
35
  }
36
+ export interface PdfjsViewerLoadedEvent extends Event {
37
+ detail: {
38
+ source: PdfjsViewerElementIframeWindow;
39
+ };
40
+ }
26
41
  export default PdfjsViewerElement;