From 97bfd4af34a8ba1d74e890764fbbda79aa72e239 Mon Sep 17 00:00:00 2001 From: hkonsti <45428767+hkonsti@users.noreply.github.com> Date: Sun, 20 Apr 2025 21:24:47 +0200 Subject: [PATCH 1/2] chore: move init into static function --- packages/2d/src/lib/components/Latex.ts | 35 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/2d/src/lib/components/Latex.ts b/packages/2d/src/lib/components/Latex.ts index 0dd535d7a..72bf7952b 100644 --- a/packages/2d/src/lib/components/Latex.ts +++ b/packages/2d/src/lib/components/Latex.ts @@ -1,6 +1,8 @@ import type {SignalValue, SimpleSignal} from '@revideo/core'; import {DependencyContext, useLogger} from '@revideo/core'; +import type {LiteAdaptor} from 'mathjax-full/js/adaptors/liteAdaptor'; import {liteAdaptor} from 'mathjax-full/js/adaptors/liteAdaptor'; +import type {MathDocument} from 'mathjax-full/js/core/MathDocument'; import {RegisterHTMLHandler} from 'mathjax-full/js/handlers/html'; import {TeX} from 'mathjax-full/js/input/tex'; import {AllPackages} from 'mathjax-full/js/input/tex/AllPackages'; @@ -11,16 +13,6 @@ import {initial, signal} from '../decorators'; import type {ImgProps} from './Img'; import {Img} from './Img'; -const Adaptor = liteAdaptor(); -RegisterHTMLHandler(Adaptor); - -const JaxDocument = mathjax.document('', { - // eslint-disable-next-line @typescript-eslint/naming-convention - InputJax: new TeX({packages: AllPackages}), - // eslint-disable-next-line @typescript-eslint/naming-convention - OutputJax: new SVG({fontCache: 'local'}), -}); - export interface LatexProps extends ImgProps { tex?: SignalValue; renderProps?: SignalValue; @@ -46,6 +38,24 @@ export interface LatexProps extends ImgProps { */ export class Latex extends Img { private static svgContentsPool: Record = {}; + private static mathJaxInitialized = false; + private static Adaptor: LiteAdaptor; + private static JaxDocument: MathDocument; + + private static initializeMathJax() { + if (this.mathJaxInitialized) { + return; + } + this.Adaptor = liteAdaptor(); + RegisterHTMLHandler(this.Adaptor); + this.JaxDocument = mathjax.document('', { + // eslint-disable-next-line @typescript-eslint/naming-convention + InputJax: new TeX({packages: AllPackages}), + // eslint-disable-next-line @typescript-eslint/naming-convention + OutputJax: new SVG({fontCache: 'local'}), + }); + this.mathJaxInitialized = true; + } private readonly imageElement = document.createElement('img'); @@ -58,6 +68,7 @@ export class Latex extends Img { public constructor(props: LatexProps) { super({...props, src: null}); + Latex.initializeMathJax(); } protected override image(): HTMLImageElement { @@ -79,7 +90,9 @@ export class Latex extends Img { // Convert to TeX, look for any errors const tex = this.tex(); - const svg = Adaptor.innerHTML(JaxDocument.convert(tex, this.options())); + const svg = Latex.Adaptor.innerHTML( + Latex.JaxDocument.convert(tex, this.options()) as any, + ); if (svg.includes('data-mjx-error')) { const errors = svg.match(/data-mjx-error="(.*?)"/); if (errors && errors.length > 0) { From a6c8b39df3ba217e8bf31d8fd1e0c456836db3da Mon Sep 17 00:00:00 2001 From: hkonsti <45428767+hkonsti@users.noreply.github.com> Date: Sun, 20 Apr 2025 21:27:40 +0200 Subject: [PATCH 2/2] fix: lint --- packages/2d/src/lib/components/Latex.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/2d/src/lib/components/Latex.ts b/packages/2d/src/lib/components/Latex.ts index 72bf7952b..17e04fa27 100644 --- a/packages/2d/src/lib/components/Latex.ts +++ b/packages/2d/src/lib/components/Latex.ts @@ -39,16 +39,16 @@ export interface LatexProps extends ImgProps { export class Latex extends Img { private static svgContentsPool: Record = {}; private static mathJaxInitialized = false; - private static Adaptor: LiteAdaptor; - private static JaxDocument: MathDocument; + private static adaptor: LiteAdaptor; + private static jaxDocument: MathDocument; private static initializeMathJax() { if (this.mathJaxInitialized) { return; } - this.Adaptor = liteAdaptor(); - RegisterHTMLHandler(this.Adaptor); - this.JaxDocument = mathjax.document('', { + this.adaptor = liteAdaptor(); + RegisterHTMLHandler(this.adaptor); + this.jaxDocument = mathjax.document('', { // eslint-disable-next-line @typescript-eslint/naming-convention InputJax: new TeX({packages: AllPackages}), // eslint-disable-next-line @typescript-eslint/naming-convention @@ -90,8 +90,8 @@ export class Latex extends Img { // Convert to TeX, look for any errors const tex = this.tex(); - const svg = Latex.Adaptor.innerHTML( - Latex.JaxDocument.convert(tex, this.options()) as any, + const svg = Latex.adaptor.innerHTML( + Latex.jaxDocument.convert(tex, this.options()) as any, ); if (svg.includes('data-mjx-error')) { const errors = svg.match(/data-mjx-error="(.*?)"/);