@@ -13,7 +13,8 @@ const DEFAULTS = {
1313 locale : '' ,
1414 textLayer : '' ,
1515 viewerCssTheme : 'AUTOMATIC' ,
16- viewerExtraStyles : ''
16+ viewerExtraStyles : '' ,
17+ viewerExtraStylesUrls : ''
1718} as const
1819
1920export const ViewerCssTheme = {
@@ -22,7 +23,7 @@ export const ViewerCssTheme = {
2223 DARK : 2 ,
2324} as const
2425
25- export const hardRefreshAttributes = [ 'src' , 'viewer-path' , 'locale' , 'text-layer' , 'viewer-css-theme' , 'viewer-extra-styles' ]
26+ export const hardRefreshAttributes = [ 'src' , 'viewer-path' , 'locale' , 'text-layer' , 'viewer-css-theme' , 'viewer-extra-styles' , 'viewer-extra-styles-urls' ]
2627
2728export class PdfjsViewerElement extends HTMLElement {
2829 constructor ( ) {
@@ -39,14 +40,15 @@ export class PdfjsViewerElement extends HTMLElement {
3940 public iframe ! : PdfjsViewerElementIframe
4041
4142 static get observedAttributes ( ) {
42- return [ 'src' , 'viewer-path' , 'locale' , 'page' , 'search' , 'phrase' , 'zoom' , 'pagemode' , 'text-layer' , 'viewer-css-theme' , 'viewer-extra-styles' ]
43+ return [ 'src' , 'viewer-path' , 'locale' , 'page' , 'search' , 'phrase' , 'zoom' , 'pagemode' , 'text-layer' , 'viewer-css-theme' , 'viewer-extra-styles' , 'viewer-extra-styles-urls' ]
4344 }
4445
4546 connectedCallback ( ) {
4647 this . iframe = this . shadowRoot ! . querySelector ( 'iframe' ) as PdfjsViewerElementIframe
4748 document . addEventListener ( 'webviewerloaded' , async ( ) => {
4849 this . setCssTheme ( this . getCssThemeOption ( ) )
49- this . setViewerExtraStyles ( this . getAttribute ( 'viewer-extra-styles' ) )
50+ this . injectExtraStylesLinks ( this . getAttribute ( 'viewer-extra-styles-urls' ) ?? DEFAULTS . viewerExtraStylesUrls )
51+ this . setViewerExtraStyles ( this . getAttribute ( 'viewer-extra-styles' ) ?? DEFAULTS . viewerExtraStyles )
5052 if ( this . getAttribute ( 'src' ) !== DEFAULTS . src ) this . iframe . contentWindow ?. PDFViewerApplicationOptions ?. set ( 'defaultUrl' , '' )
5153 this . iframe . contentWindow ?. PDFViewerApplicationOptions ?. set ( 'disablePreferences' , true )
5254 this . iframe . contentWindow ?. PDFViewerApplicationOptions ?. set ( 'pdfBugEnabled' , true )
@@ -132,6 +134,19 @@ export class PdfjsViewerElement extends HTMLElement {
132134 this . iframe . contentDocument ?. head . appendChild ( style )
133135 }
134136
137+ private injectExtraStylesLinks = ( rawLinks ?: string ) => {
138+ if ( ! rawLinks ) return
139+ const linksArray = rawLinks . replace ( / ' | ] | \[ / g, '' ) . split ( ',' ) . map ( ( link ) => link . trim ( ) )
140+ linksArray . forEach ( ( url ) => {
141+ const linkExists = this . iframe . contentDocument ?. head . querySelector ( `link[href="${ url } "]` ) ;
142+ if ( linkExists ) return
143+ const linkEl = document . createElement ( 'link' )
144+ linkEl . rel = 'stylesheet'
145+ linkEl . href = url
146+ this . iframe . contentDocument ?. head . appendChild ( linkEl )
147+ } )
148+ }
149+
135150 public initialize = ( ) : Promise < PdfjsViewerElementIframeWindow [ 'PDFViewerApplication' ] > => new Promise ( async ( resolve ) => {
136151 await elementReady ( 'iframe' , this . shadowRoot ! )
137152 this . iframe ?. addEventListener ( 'load' , async ( ) => {
0 commit comments