Skip to content

Commit

Permalink
fix: viewer detect if services exist
Browse files Browse the repository at this point in the history
  • Loading branch information
icaparros-at-wiris committed Nov 10, 2023
1 parent 30e4464 commit 4fa0aff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
23 changes: 20 additions & 3 deletions packages/viewer/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,9 +26,12 @@ async function main(w: Window): Promise<void> {
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;

Expand All @@ -40,6 +52,11 @@ async function main(w: Window): Promise<void> {
// 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();

Expand Down
22 changes: 20 additions & 2 deletions packages/viewer/src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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;
Expand Down

0 comments on commit 4fa0aff

Please sign in to comment.