Skip to content

Commit 509b5f1

Browse files
committed
Fix #3
1 parent 4808427 commit 509b5f1

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ <h1 class="me-2 me-sm-3 mb-0" style="white-space: nowrap">pdfjs-viewer-element</
8888
<form style="display: none" class="pt-4" :class="{ 'd-block': tab === 'api' }">
8989
<p class="fs-6 mb-2">Properties overview:</p>
9090
<div class="row">
91-
<div class="col-5 col-sm-3"><label for="src" class="col-form-label"><code style="color: #905">zoom</code></label></div>
91+
<div class="col-5 col-sm-3"><label for="src" class="col-form-label"><code style="color: #905">src</code></label></div>
9292
<div class="col-7 col-sm-5 col-form-label text-muted d-flex"><span class="ms-auto ms-sm-0 small">Path to pdf file</span></div>
9393
<div class="col-sm-4 d-flex align-items-center">
9494
<select id="src" v-model="src" class="form-select form-select-sm" aria-label="Pdf file path">

src/debounce.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/elementReady.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const elementReady = (selector: string, document: Document | ShadowRoot) => {
2+
return new Promise((resolve, _reject) => {
3+
let el = document.querySelector(selector);
4+
if (el) {
5+
resolve(el);
6+
return
7+
}
8+
new MutationObserver((_mutationRecords, observer) => {
9+
Array.from(document.querySelectorAll(selector)).forEach((element) => {
10+
resolve(element);
11+
observer.disconnect();
12+
});
13+
})
14+
.observe(document, {
15+
childList: true,
16+
subtree: true
17+
});
18+
});
19+
}

src/pdfjs-viewer-element.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
import { debounce } from "./debounce";
1+
import { elementReady } from "./elementReady";
22

3-
const defaults = {
3+
const DEFAULTS = {
44
viewerPath: '/pdfjs',
5-
viewerEntry: '/web/viewer.html'
5+
viewerEntry: '/web/viewer.html',
6+
src: '',
7+
page: '',
8+
search: '',
9+
phrase: '',
10+
zoom: '',
11+
pagemode: '',
12+
locale: '',
13+
textLayer: ''
614
}
715

816
const template = document.createElement('template')
9-
template.innerHTML = `<iframe frameborder="0" width="100%"></iframe><style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>`
17+
template.innerHTML = `
18+
<iframe frameborder="0" width="100%"></iframe>
19+
<style>:host{width:100%;display:block;overflow:hidden}:host iframe{height:100%}</style>
20+
`
1021

1122
export class PdfjsViewerElement extends HTMLElement {
1223
constructor() {
@@ -27,44 +38,35 @@ export class PdfjsViewerElement extends HTMLElement {
2738
}
2839

2940
attributeChangedCallback() {
30-
this.onAttrsChanged()
41+
elementReady('iframe', this.shadowRoot!).then(() => this.setProps())
3142
}
3243

33-
private onAttrsChanged = debounce(() => {
34-
const src = this.iframe.getAttribute('src')
35-
36-
if (src && src.split('&locale=')[1] !== this.getAttribute('locale')) {
37-
this.setProps()
38-
this.iframe.contentWindow.location.reload()
44+
private setProps() {
45+
const src = this.getFullPath(this.getAttribute('src') || DEFAULTS.src)
46+
const viewerPath = this.getFullPath(this.getAttribute('viewer-path') || DEFAULTS.viewerPath)
47+
const page = this.getAttribute('page') || DEFAULTS.page
48+
const search = this.getAttribute('search') || DEFAULTS.search
49+
const phrase = this.getAttribute('phrase') || DEFAULTS.phrase
50+
const zoom = this.getAttribute('zoom') || DEFAULTS.zoom
51+
const pagemode = this.getAttribute('pagemode') || DEFAULTS.pagemode
52+
const locale = this.getAttribute('locale') || DEFAULTS.locale
53+
const textLayer = this.getAttribute('text-layer') || DEFAULTS.textLayer
54+
55+
const updatedSrc = `${viewerPath}${DEFAULTS.viewerEntry}?file=${src}#page=${page}&zoom=${zoom}&pagemode=${pagemode}&search=${search}&phrase=${phrase}&textLayer=${textLayer}${locale ? '&locale='+locale : ''}`
56+
if (updatedSrc !== this.iframe.getAttribute('src')) {
57+
this.rerenderIframe(updatedSrc)
3958
}
40-
else if (src && src.split(defaults.viewerEntry)[0] !== this.getFullPath(this.getAttribute('viewer-path') || '') ) {
41-
this.setProps()
42-
this.iframe.contentWindow.location.reload()
43-
}
44-
else {
45-
this.setProps()
46-
}
47-
})
48-
49-
private async setProps() {
50-
const viewerPath = this.getAttribute('viewer-path') || defaults.viewerPath
51-
const page = this.getAttribute('page') || ''
52-
const search = this.getAttribute('search') || ''
53-
const phrase = this.getAttribute('phrase') || ''
54-
const zoom = this.getAttribute('zoom') || ''
55-
const pagemode = this.getAttribute('pagemode') || ''
56-
const locale = this.getAttribute('locale') || ''
57-
const textLayer = this.getAttribute('text-layer') || ''
58-
59-
this.iframe.setAttribute(
60-
'src',
61-
`${this.getFullPath(viewerPath)}${defaults.viewerEntry}#page=${page}&zoom=${zoom}&pagemode=${pagemode}&search=${search}&phrase=${phrase}&textLayer=${textLayer}${locale ? '&locale='+locale : ''}`
62-
)
59+
}
60+
61+
private rerenderIframe(src: string) {
62+
this.shadowRoot!.replaceChild(this.iframe.cloneNode(), this.iframe)
63+
this.iframe = this.shadowRoot!.querySelector('iframe') as PdfjsViewerElementIframe
64+
this.iframe.contentWindow.location.href = src
6365
}
6466

6567
private setEventListeners() {
6668
document.addEventListener('webviewerloaded', () => {
67-
this.iframe.contentWindow.PDFViewerApplicationOptions.set('defaultUrl', this.getFullPath(this.getAttribute('src') || ''))
69+
if (this.getAttribute('src') !== DEFAULTS.src) this.iframe.contentWindow.PDFViewerApplicationOptions.set('defaultUrl', '')
6870
this.iframe.contentWindow.PDFViewerApplicationOptions.set('disablePreferences', true)
6971
this.iframe.contentWindow.PDFViewerApplicationOptions.set('pdfBugEnabled', true)
7072
})

0 commit comments

Comments
 (0)