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
816const 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
1122export 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