1
- import { debounce } from "./debounce " ;
1
+ import { elementReady } from "./elementReady " ;
2
2
3
- const defaults = {
3
+ const DEFAULTS = {
4
4
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 : ''
6
14
}
7
15
8
16
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
+ `
10
21
11
22
export class PdfjsViewerElement extends HTMLElement {
12
23
constructor ( ) {
@@ -27,44 +38,35 @@ export class PdfjsViewerElement extends HTMLElement {
27
38
}
28
39
29
40
attributeChangedCallback ( ) {
30
- this . onAttrsChanged ( )
41
+ elementReady ( 'iframe' , this . shadowRoot ! ) . then ( ( ) => this . setProps ( ) )
31
42
}
32
43
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 )
39
58
}
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
63
65
}
64
66
65
67
private setEventListeners ( ) {
66
68
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' , '' )
68
70
this . iframe . contentWindow . PDFViewerApplicationOptions . set ( 'disablePreferences' , true )
69
71
this . iframe . contentWindow . PDFViewerApplicationOptions . set ( 'pdfBugEnabled' , true )
70
72
} )
0 commit comments