1- import { Injectable } from '@angular/core' ;
1+ import { DOCUMENT } from '@angular/common' ;
2+ import {
3+ Inject ,
4+ Injectable ,
5+ } from '@angular/core' ;
26import { Observable , ReplaySubject , Subject } from 'rxjs' ;
37import { environment } from 'src/environments/environment' ;
4- import { MathJaxConfig , MathService } from './math.service' ;
8+ import {
9+ NativeWindowRef ,
10+ NativeWindowService ,
11+ } from '../services/window.service' ; import { MathJaxConfig , MathService } from './math.service' ;
512
613@Injectable ( {
714 providedIn : 'root'
815} )
16+ /**
17+ * Provide the MathService for CSR
18+ */
919export class ClientMathService extends MathService {
1020
1121 protected isReady$ : Subject < boolean > ;
@@ -31,7 +41,10 @@ export class ClientMathService extends MathService {
3141 id : 'MathJaxBackupScript'
3242 } ;
3343
34- constructor ( ) {
44+ constructor (
45+ @Inject ( DOCUMENT ) private _document : Document ,
46+ @Inject ( NativeWindowService ) protected _window : NativeWindowRef ,
47+ ) {
3548 super ( ) ;
3649
3750 this . isReady$ = new ReplaySubject < boolean > ( ) ;
@@ -44,36 +57,49 @@ export class ClientMathService extends MathService {
4457 } ) ;
4558 }
4659
60+ /**
61+ * Register the specified MathJax script in the document
62+ *
63+ * @param config The configuration object for the script
64+ */
4765 protected async registerMathJaxAsync ( config : MathJaxConfig ) : Promise < any > {
4866 if ( environment . markdown . mathjax ) {
4967 return new Promise < void > ( ( resolve , reject ) => {
5068
51- const optionsScript : HTMLScriptElement = document . createElement ( 'script' ) ;
69+ const optionsScript : HTMLScriptElement = this . _document . createElement ( 'script' ) ;
5270 optionsScript . type = 'text/javascript' ;
5371 optionsScript . text = `MathJax = ${ JSON . stringify ( this . mathJaxOptions ) } ;` ;
54- document . head . appendChild ( optionsScript ) ;
72+ this . _document . head . appendChild ( optionsScript ) ;
5573
56- const script : HTMLScriptElement = document . createElement ( 'script' ) ;
74+ const script : HTMLScriptElement = this . _document . createElement ( 'script' ) ;
5775 script . id = config . id ;
5876 script . type = 'text/javascript' ;
5977 script . src = config . source ;
6078 script . crossOrigin = 'anonymous' ;
6179 script . async = true ;
6280 script . onload = ( ) => resolve ( ) ;
6381 script . onerror = error => reject ( error ) ;
64- document . head . appendChild ( script ) ;
82+ this . _document . head . appendChild ( script ) ;
6583 } ) ;
6684 }
6785 return Promise . resolve ( ) ;
6886 }
6987
88+ /**
89+ * Return the status of the script registration
90+ */
7091 ready ( ) : Observable < boolean > {
7192 return this . isReady$ ;
7293 }
7394
95+ /**
96+ * Render the specified element using the MathJax JavaScript
97+ *
98+ * @param element The element to render with MathJax
99+ */
74100 render ( element : HTMLElement ) {
75101 if ( environment . markdown . mathjax ) {
76- ( window as any ) . MathJax . typesetPromise ( [ element ] ) ;
102+ this . _window . nativeWindow . MathJax . typesetPromise ( [ element ] ) ;
77103 }
78104 }
79105}
0 commit comments