mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[DURACOM-240] Assure the MathJax script has been registered before rendering
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { DOCUMENT } from '@angular/common';
|
||||||
|
import {
|
||||||
|
Inject,
|
||||||
|
Injectable,
|
||||||
|
} from '@angular/core';
|
||||||
import {
|
import {
|
||||||
BehaviorSubject,
|
BehaviorSubject,
|
||||||
Observable,
|
Observable,
|
||||||
@@ -6,6 +10,10 @@ import {
|
|||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
import { environment } from 'src/environments/environment';
|
import { environment } from 'src/environments/environment';
|
||||||
|
|
||||||
|
import {
|
||||||
|
NativeWindowRef,
|
||||||
|
NativeWindowService,
|
||||||
|
} from '../services/window.service';
|
||||||
import {
|
import {
|
||||||
MathJaxConfig,
|
MathJaxConfig,
|
||||||
MathService,
|
MathService,
|
||||||
@@ -14,6 +22,9 @@ import {
|
|||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
|
/**
|
||||||
|
* Provide the MathService for CSR
|
||||||
|
*/
|
||||||
export class ClientMathService extends MathService {
|
export class ClientMathService extends MathService {
|
||||||
|
|
||||||
protected isReady$: Subject<boolean>;
|
protected isReady$: Subject<boolean>;
|
||||||
@@ -39,7 +50,10 @@ export class ClientMathService extends MathService {
|
|||||||
id: 'MathJaxBackupScript',
|
id: 'MathJaxBackupScript',
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor(
|
||||||
|
@Inject(DOCUMENT) private _document: Document,
|
||||||
|
@Inject(NativeWindowService) protected _window: NativeWindowRef,
|
||||||
|
) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.isReady$ = new BehaviorSubject<boolean>(false);
|
this.isReady$ = new BehaviorSubject<boolean>(false);
|
||||||
@@ -52,16 +66,21 @@ export class ClientMathService extends MathService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the specified MathJax script in the document
|
||||||
|
*
|
||||||
|
* @param config The configuration object for the script
|
||||||
|
*/
|
||||||
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
|
protected async registerMathJaxAsync(config: MathJaxConfig): Promise<any> {
|
||||||
if (environment.markdown.mathjax) {
|
if (environment.markdown.mathjax) {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
|
||||||
const optionsScript: HTMLScriptElement = document.createElement('script');
|
const optionsScript: HTMLScriptElement = this._document.createElement('script');
|
||||||
optionsScript.type = 'text/javascript';
|
optionsScript.type = 'text/javascript';
|
||||||
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`;
|
optionsScript.text = `MathJax = ${JSON.stringify(this.mathJaxOptions)};`;
|
||||||
document.head.appendChild(optionsScript);
|
this._document.head.appendChild(optionsScript);
|
||||||
|
|
||||||
const script: HTMLScriptElement = document.createElement('script');
|
const script: HTMLScriptElement = this._document.createElement('script');
|
||||||
script.id = config.id;
|
script.id = config.id;
|
||||||
script.type = 'text/javascript';
|
script.type = 'text/javascript';
|
||||||
script.src = config.source;
|
script.src = config.source;
|
||||||
@@ -69,19 +88,27 @@ export class ClientMathService extends MathService {
|
|||||||
script.async = true;
|
script.async = true;
|
||||||
script.onload = () => resolve();
|
script.onload = () => resolve();
|
||||||
script.onerror = error => reject(error);
|
script.onerror = error => reject(error);
|
||||||
document.head.appendChild(script);
|
this._document.head.appendChild(script);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the status of the script registration
|
||||||
|
*/
|
||||||
ready(): Observable<boolean> {
|
ready(): Observable<boolean> {
|
||||||
return this.isReady$;
|
return this.isReady$;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the specified element using the MathJax JavaScript
|
||||||
|
*
|
||||||
|
* @param element The element to render with MathJax
|
||||||
|
*/
|
||||||
render(element: HTMLElement) {
|
render(element: HTMLElement) {
|
||||||
if (environment.markdown.mathjax) {
|
if (environment.markdown.mathjax) {
|
||||||
(window as any).MathJax.typesetPromise([element]);
|
this._window.nativeWindow.MathJax.typesetPromise([element]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,9 @@ export interface MathJaxConfig {
|
|||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service is used to provide the MathJax library with the ability to render markdown code
|
||||||
|
*/
|
||||||
export abstract class MathService {
|
export abstract class MathService {
|
||||||
protected abstract mathJaxOptions: any;
|
protected abstract mathJaxOptions: any;
|
||||||
protected abstract mathJax: MathJaxConfig;
|
protected abstract mathJax: MathJaxConfig;
|
||||||
|
@@ -13,6 +13,9 @@ import {
|
|||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
|
/**
|
||||||
|
* Provide the MathService for SSR
|
||||||
|
*/
|
||||||
export class ServerMathService extends MathService {
|
export class ServerMathService extends MathService {
|
||||||
|
|
||||||
protected isReady$: Subject<boolean>;
|
protected isReady$: Subject<boolean>;
|
||||||
|
@@ -14,6 +14,7 @@ import {
|
|||||||
} from '@angular/platform-browser';
|
} from '@angular/platform-browser';
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
import {
|
import {
|
||||||
|
filter,
|
||||||
take,
|
take,
|
||||||
takeUntil,
|
takeUntil,
|
||||||
} from 'rxjs/operators';
|
} from 'rxjs/operators';
|
||||||
@@ -41,8 +42,8 @@ export class MarkdownDirective implements OnInit, OnDestroy {
|
|||||||
el: HTMLElement;
|
el: HTMLElement;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
protected sanitizer: DomSanitizer,
|
|
||||||
@Inject(MARKDOWN_IT) private markdownIt: LazyMarkdownIt,
|
@Inject(MARKDOWN_IT) private markdownIt: LazyMarkdownIt,
|
||||||
|
protected sanitizer: DomSanitizer,
|
||||||
private mathService: MathService,
|
private mathService: MathService,
|
||||||
private elementRef: ElementRef) {
|
private elementRef: ElementRef) {
|
||||||
this.el = elementRef.nativeElement;
|
this.el = elementRef.nativeElement;
|
||||||
@@ -72,6 +73,7 @@ export class MarkdownDirective implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
private renderMathjax() {
|
private renderMathjax() {
|
||||||
this.mathService.ready().pipe(
|
this.mathService.ready().pipe(
|
||||||
|
filter((ready) => ready),
|
||||||
take(1),
|
take(1),
|
||||||
takeUntil(this.alive$),
|
takeUntil(this.alive$),
|
||||||
).subscribe(() => {
|
).subscribe(() => {
|
||||||
|
Reference in New Issue
Block a user