pdfjs-viewer-element 2.5.5 → 2.6.0

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
@@ -3,8 +3,11 @@
3
3
  </p>
4
4
  <h1 align="center">pdfjs-viewer-element</h1>
5
5
 
6
- Probably the simplest integration of PDF.js viewer.
7
- The package provide 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/).
6
+ The simplest integration of [PDF.js default viewer](https://mozilla.github.io/pdf.js/web/viewer.html) using the `iframe` element and web component.
7
+ The package provides a custom element, based on PDF.js [viewer options](https://github.com/mozilla/pdf.js/wiki/Viewer-options) and [URL parameters](https://github.com/mozilla/pdf.js/wiki/Debugging-PDF.js#url-parameters) API.
8
+ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and works with most [JS frameworks](https://custom-elements-everywhere.com/).
9
+
10
+ See [examples](https://alekswebnet.github.io/pdfjs-viewer-element/#demo) of usage in Vue, React, Svelte, or simple HTML pages.
8
11
 
9
12
  ⚠️ `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.
10
13
 
@@ -17,11 +20,9 @@ The prebuilt comes with each PDF.js release. Supported releases:
17
20
  ✅ [v4.0.189](https://github.com/mozilla/pdf.js/releases/tag/v4.0.189)
18
21
 
19
22
 
20
- To use the package you should download and **place the prebuilt** files to some directory of your project.
21
-
22
- Then specify the path to the directory with `viewer-path` property (`/pdfjs` by default).
23
+ To use the package you should download and **place the prebuilt** files in the project.
23
24
 
24
- See [examples](https://alekswebnet.github.io/pdfjs-viewer-element/#demo) of usage in Vue, React, Svelte or simple HTML page.
25
+ Then specify the path to the directory with the `viewer-path` property (`/pdfjs` by default).
25
26
 
26
27
  ## Status
27
28
 
@@ -86,9 +87,9 @@ Using browser:
86
87
 
87
88
  `pagemode` - Page mode `thumbs | bookmarks | attachments | layers | none`
88
89
 
89
- `viewer-css-theme` - Apply automatic, light or dark theme `AUTOMATIC | LIGHT | DARK`
90
+ `viewer-css-theme` - Apply automatic, light, or dark theme `AUTOMATIC | LIGHT | DARK`
90
91
 
91
- `viewer-extra-styles` - Add your CSS rules to viewer application
92
+ `viewer-extra-styles` - Add your CSS rules to the viewer application
92
93
 
93
94
  Play with attributes on [Api docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
94
95
 
@@ -138,4 +139,4 @@ viewerApp.eventBus.on('pagesloaded', () => {
138
139
  ```
139
140
 
140
141
  ## License
141
- [MIT](http://opensource.org/licenses/MIT)
142
+ [MIT](http://opensource.org/licenses/MIT)
@@ -1,4 +1,4 @@
1
- const f = (m, e) => new Promise((r) => {
1
+ const w = (m, e) => new Promise((r) => {
2
2
  let i = e.querySelector(m);
3
3
  if (i) {
4
4
  r(i);
@@ -12,71 +12,71 @@ const f = (m, e) => new Promise((r) => {
12
12
  childList: !0,
13
13
  subtree: !0
14
14
  });
15
- }), y = {
15
+ }), g = {
16
16
  trailing: !0
17
17
  };
18
18
  function b(m, e = 25, r = {}) {
19
- if (r = { ...y, ...r }, !Number.isFinite(e))
19
+ if (r = { ...g, ...r }, !Number.isFinite(e))
20
20
  throw new TypeError("Expected `wait` to be a finite number");
21
21
  let i, s, n = [], t, o;
22
- const a = (c, h) => (t = v(m, c, h), t.finally(() => {
22
+ const a = (l, h) => (t = v(m, l, h), t.finally(() => {
23
23
  if (t = null, r.trailing && o && !s) {
24
- const u = a(c, o);
24
+ const u = a(l, o);
25
25
  return o = null, u;
26
26
  }
27
27
  }), t);
28
- return function(...c) {
29
- return t ? (r.trailing && (o = c), t) : new Promise((h) => {
28
+ return function(...l) {
29
+ return t ? (r.trailing && (o = l), t) : new Promise((h) => {
30
30
  const u = !s && r.leading;
31
31
  clearTimeout(s), s = setTimeout(() => {
32
32
  s = null;
33
- const d = r.leading ? i : a(this, c);
34
- for (const g of n)
35
- g(d);
33
+ const p = r.leading ? i : a(this, l);
34
+ for (const y of n)
35
+ y(p);
36
36
  n = [];
37
- }, e), u ? (i = a(this, c), h(i)) : n.push(h);
37
+ }, e), u ? (i = a(this, l), h(i)) : n.push(h);
38
38
  });
39
39
  };
40
40
  }
41
41
  async function v(m, e, r) {
42
42
  return await m.apply(e, r);
43
43
  }
44
- const l = {
44
+ const c = {
45
45
  viewerPath: "/pdfjs",
46
46
  viewerEntry: "/web/viewer.html",
47
47
  src: "",
48
48
  page: "",
49
49
  search: "",
50
50
  phrase: "",
51
- zoom: "auto",
51
+ zoom: "",
52
52
  pagemode: "none",
53
53
  locale: "",
54
54
  textLayer: "",
55
55
  viewerCssTheme: "AUTOMATIC",
56
56
  viewerExtraStyles: ""
57
- }, w = {
57
+ }, d = {
58
58
  AUTOMATIC: 0,
59
59
  // Default value.
60
60
  LIGHT: 1,
61
61
  DARK: 2
62
- };
63
- class p extends HTMLElement {
62
+ }, A = ["src", "viewer-path", "locale", "viewer-css-theme", "viewer-extra-styles"];
63
+ class f extends HTMLElement {
64
64
  constructor() {
65
65
  super(), this.onIframeReady = b(async (i) => {
66
- await f("iframe", this.shadowRoot), i();
66
+ await w("iframe", this.shadowRoot), i();
67
67
  }, 0, { leading: !0 }), this.setViewerExtraStyles = (i, s = "extra") => {
68
- var t, o, a, c, h;
68
+ var t, o, a, l, h;
69
69
  if (!i) {
70
70
  (o = (t = this.iframe.contentDocument) == null ? void 0 : t.head.querySelector(`style[${s}]`)) == null || o.remove();
71
71
  return;
72
72
  }
73
- if (((c = (a = this.iframe.contentDocument) == null ? void 0 : a.head.querySelector(`style[${s}]`)) == null ? void 0 : c.innerHTML) === i)
73
+ if (((l = (a = this.iframe.contentDocument) == null ? void 0 : a.head.querySelector(`style[${s}]`)) == null ? void 0 : l.innerHTML) === i)
74
74
  return;
75
75
  const n = document.createElement("style");
76
76
  n.innerHTML = i, n.setAttribute(s, ""), (h = this.iframe.contentDocument) == null || h.head.appendChild(n);
77
77
  }, this.initialize = () => new Promise(async (i) => {
78
78
  var s;
79
- await f("iframe", this.shadowRoot), (s = this.iframe) == null || s.addEventListener("load", async () => {
79
+ await w("iframe", this.shadowRoot), (s = this.iframe) == null || s.addEventListener("load", async () => {
80
80
  var n, t, o;
81
81
  await ((t = (n = this.iframe.contentWindow) == null ? void 0 : n.PDFViewerApplication) == null ? void 0 : t.initializedPromise), i((o = this.iframe.contentWindow) == null ? void 0 : o.PDFViewerApplication);
82
82
  }, { once: !0 });
@@ -91,17 +91,23 @@ class p extends HTMLElement {
91
91
  return ["src", "viewer-path", "locale", "page", "search", "phrase", "zoom", "pagemode", "text-layer", "viewer-css-theme", "viewer-extra-styles"];
92
92
  }
93
93
  connectedCallback() {
94
- this.iframe = this.shadowRoot.querySelector("iframe"), document.addEventListener("webviewerloaded", () => {
94
+ this.iframe = this.shadowRoot.querySelector("iframe"), document.addEventListener("webviewerloaded", async () => {
95
95
  var e, r, i, s, n, t, o, a;
96
- this.setCssTheme(), this.setViewerExtraStyles(this.getAttribute("viewer-extra-styles")), this.getAttribute("src") !== l.src && ((r = (e = this.iframe.contentWindow) == null ? void 0 : e.PDFViewerApplicationOptions) == null || r.set("defaultUrl", "")), (s = (i = this.iframe.contentWindow) == null ? void 0 : i.PDFViewerApplicationOptions) == null || s.set("disablePreferences", !0), (t = (n = this.iframe.contentWindow) == null ? void 0 : n.PDFViewerApplicationOptions) == null || t.set("pdfBugEnabled", !0), (a = (o = this.iframe.contentWindow) == null ? void 0 : o.PDFViewerApplicationOptions) == null || a.set("eventBusDispatchToDOM", !0);
96
+ this.setCssTheme(this.getCssThemeOption()), this.setViewerExtraStyles(this.getAttribute("viewer-extra-styles")), this.getAttribute("src") !== c.src && ((r = (e = this.iframe.contentWindow) == null ? void 0 : e.PDFViewerApplicationOptions) == null || r.set("defaultUrl", "")), (s = (i = this.iframe.contentWindow) == null ? void 0 : i.PDFViewerApplicationOptions) == null || s.set("disablePreferences", !0), (t = (n = this.iframe.contentWindow) == null ? void 0 : n.PDFViewerApplicationOptions) == null || t.set("pdfBugEnabled", !0), (a = (o = this.iframe.contentWindow) == null ? void 0 : o.PDFViewerApplicationOptions) == null || a.set("eventBusDispatchToDOM", !0);
97
97
  });
98
98
  }
99
- attributeChangedCallback() {
99
+ attributeChangedCallback(e) {
100
+ if (!A.includes(e)) {
101
+ this.onIframeReady(() => {
102
+ this.iframe.src = this.getIframeSrc();
103
+ });
104
+ return;
105
+ }
100
106
  this.onIframeReady(() => this.mountViewer(this.getIframeSrc()));
101
107
  }
102
108
  getIframeSrc() {
103
- const e = this.getFullPath(this.getAttribute("src") || l.src), r = this.getFullPath(this.getAttribute("viewer-path") || l.viewerPath), i = this.getAttribute("page") || l.page, s = this.getAttribute("search") || l.search, n = this.getAttribute("phrase") || l.phrase, t = this.getAttribute("zoom") || l.zoom, o = this.getAttribute("pagemode") || l.pagemode, a = this.getAttribute("locale") || l.locale, c = this.getAttribute("text-layer") || l.textLayer, h = this.getAttribute("viewer-css-theme") || l.viewerCssTheme, u = !!(this.getAttribute("viewer-extra-styles") || l.viewerExtraStyles), d = `${r}${l.viewerEntry}?file=${encodeURIComponent(e)}#page=${i}&zoom=${t}&pagemode=${o}&search=${s}&phrase=${n}&textLayer=${c}${a ? "&locale=" + a : ""}&viewerCssTheme=${h}&viewerExtraStyles=${u}`;
104
- return d !== this.iframe.getAttribute("src") ? d : "";
109
+ const e = this.getFullPath(this.getAttribute("src") || c.src), r = this.getFullPath(this.getAttribute("viewer-path") || c.viewerPath), i = this.getAttribute("page") || c.page, s = this.getAttribute("search") || c.search, n = this.getAttribute("phrase") || c.phrase, t = this.getAttribute("zoom") || c.zoom, o = this.getAttribute("pagemode") || c.pagemode, a = this.getAttribute("locale") || c.locale, l = this.getAttribute("text-layer") || c.textLayer, h = this.getAttribute("viewer-css-theme") || c.viewerCssTheme, u = !!(this.getAttribute("viewer-extra-styles") || c.viewerExtraStyles);
110
+ return `${r}${c.viewerEntry}?file=${encodeURIComponent(e)}#page=${i}&zoom=${t}&pagemode=${o}&search=${s}&phrase=${n}&textLayer=${l}${a ? "&locale=" + a : ""}&viewerCssTheme=${h}&viewerExtraStyles=${u}`;
105
111
  }
106
112
  mountViewer(e) {
107
113
  !e || !this.iframe || (this.shadowRoot.replaceChild(this.iframe.cloneNode(), this.iframe), this.iframe = this.shadowRoot.querySelector("iframe"), this.iframe.src = e);
@@ -111,14 +117,14 @@ class p extends HTMLElement {
111
117
  }
112
118
  getCssThemeOption() {
113
119
  const e = this.getAttribute("viewer-css-theme");
114
- return Object.keys(w).includes(e) ? w[e] : w[l.viewerCssTheme];
120
+ return Object.keys(d).includes(e) ? d[e] : d[c.viewerCssTheme];
115
121
  }
116
- setCssTheme() {
122
+ setCssTheme(e) {
117
123
  var r, i, s;
118
- if (this.getCssThemeOption() === w.DARK) {
124
+ if (e === d.DARK) {
119
125
  const n = (r = this.iframe.contentDocument) == null ? void 0 : r.styleSheets[0], t = (n == null ? void 0 : n.cssRules) || [], o = Object.keys(t).filter((a) => {
120
- var c;
121
- return ((c = t[Number(a)]) == null ? void 0 : c.conditionText) === "(prefers-color-scheme: dark)";
126
+ var l;
127
+ return ((l = t[Number(a)]) == null ? void 0 : l.conditionText) === "(prefers-color-scheme: dark)";
122
128
  }).map((a) => t[Number(a)].cssText.split(`@media (prefers-color-scheme: dark) {
123
129
  `)[1].split(`
124
130
  }`)[0]);
@@ -127,9 +133,10 @@ class p extends HTMLElement {
127
133
  (s = (i = this.iframe.contentDocument) == null ? void 0 : i.head.querySelector("style[theme]")) == null || s.remove();
128
134
  }
129
135
  }
130
- window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = p, window.customElements.define("pdfjs-viewer-element", p));
136
+ window.customElements.get("pdfjs-viewer-element") || (window.PdfjsViewerElement = f, window.customElements.define("pdfjs-viewer-element", f));
131
137
  export {
132
- p as PdfjsViewerElement,
133
- w as ViewerCssTheme,
134
- p as default
138
+ f as PdfjsViewerElement,
139
+ d as ViewerCssTheme,
140
+ f as default,
141
+ A as hardRefreshAttributes
135
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdfjs-viewer-element",
3
- "version": "2.5.5",
3
+ "version": "2.6.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Oleksandr Shevchuk",
@@ -3,18 +3,19 @@ export declare const ViewerCssTheme: {
3
3
  readonly LIGHT: 1;
4
4
  readonly DARK: 2;
5
5
  };
6
+ export declare const hardRefreshAttributes: string[];
6
7
  export declare class PdfjsViewerElement extends HTMLElement {
7
8
  constructor();
8
9
  iframe: PdfjsViewerElementIframe;
9
10
  static get observedAttributes(): string[];
10
11
  connectedCallback(): void;
11
- attributeChangedCallback(): void;
12
+ attributeChangedCallback(name: string): void;
12
13
  private onIframeReady;
13
14
  private getIframeSrc;
14
15
  private mountViewer;
15
16
  private getFullPath;
16
17
  private getCssThemeOption;
17
- setCssTheme(): void;
18
+ private setCssTheme;
18
19
  private setViewerExtraStyles;
19
20
  initialize: () => Promise<PdfjsViewerElementIframeWindow['PDFViewerApplication']>;
20
21
  }