diff --git a/packages/viewer/src/app.ts b/packages/viewer/src/app.ts index 4dc6d61fb..922de3320 100644 --- a/packages/viewer/src/app.ts +++ b/packages/viewer/src/app.ts @@ -3,6 +3,15 @@ import { renderLatex } from './latex'; import { renderMathML } from './mathml'; import { bypassEncapsulation } from './retro'; +declare global { + interface Window { + viewer: { + properties: Properties, + isLoaded: boolean + }; + } +} + // This should be the only code executed outside of a function // and the only code containing browser globals (e.g. window) // TODO try to set up the linter to check these two constraints @@ -17,9 +26,12 @@ async function main(w: Window): Promise { const properties: Properties = await Properties.generate(); // Expose the globals to the browser - (w as any).viewer = { - properties, - }; + if (!w.viewer) { + w.viewer = { + properties, + isLoaded: false, + }; + } const document = w.document; @@ -40,6 +52,11 @@ async function main(w: Window): Promise { // Initial function to call once document is loaded // Renders formulas and sets observer const start = async () => { + // Check if the viewer is alredy loaded + if (w.viewer.isLoaded) return; + + w.viewer.isLoaded = true; + // First render properties.render(); diff --git a/packages/viewer/src/properties.ts b/packages/viewer/src/properties.ts index 94221bb05..acc10c54d 100644 --- a/packages/viewer/src/properties.ts +++ b/packages/viewer/src/properties.ts @@ -66,7 +66,6 @@ export class Properties { const script: HTMLScriptElement = document.querySelector(`script[src*="${pluginName}"]`); if (!!script) { - const pluginNamePosition: number = script.src.lastIndexOf(pluginName); const params: string = script.src.substring(pluginNamePosition + pluginName.length); const urlParams = new URLSearchParams(params); @@ -86,9 +85,10 @@ export class Properties { if (urlParams.get('zoom') !== null && urlParams.get('zoom') !== undefined) { instance.config.zoom = +urlParams.get('zoom'); } - } + instance.checkServices(); + // Get backend parameters calling the configurationjson service try { instance.config.backendConfig = await configurationJson( @@ -108,6 +108,24 @@ export class Properties { return instance; } + /** + * Check if is inside Integrations Services + * @deprecated This will be removed once the viewer uncouple from the integration services. + */ + private checkServices(): void { + const path = ((document.currentScript as HTMLScriptElement).src); + + if (path.includes('pluginwiris_engine')) { + // If the path includes pluginwiris_engine use Java Integrations Services + this.config.editorServicesRoot = path; + this.config.editorServicesExtension = ''; + } else if (path.includes('integration/WIRISplugins')) { + // If the path includes 'integration/WIRISplugins' use PHP Integrations Services + this.config.editorServicesRoot = path; + this.config.editorServicesExtension = '.php'; + } + } + get editorServicesRoot(): string { return this.config.editorServicesRoot || defaultValues.editorServicesRoot;