diff --git a/ts/editable/BUILD.bazel b/ts/editable/BUILD.bazel index 3c5447b76..0b8a417f4 100644 --- a/ts/editable/BUILD.bazel +++ b/ts/editable/BUILD.bazel @@ -50,6 +50,7 @@ ts_library( "//ts:image_module_support", "@npm//svelte", "@npm//mathjax-full", + "@npm//mathjax", ] + svelte_names, ) diff --git a/ts/editable/Mathjax.svelte b/ts/editable/Mathjax.svelte index 4ae075a3b..6188898e8 100644 --- a/ts/editable/Mathjax.svelte +++ b/ts/editable/Mathjax.svelte @@ -8,39 +8,25 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export let mathjax: string; export let type: "inline" | "block" | "chemistry"; - let edit = false; - $: delimiters = type === "inline" ? ["\\(", "\\)"] : ["\\[", "\\]"]; - $: converted = convertMathjax(`${delimiters[0]}${mathjax}${delimiters[1]}`); - - function autofocus(element: HTMLElement): void { - element.focus(); - } + $: converted = convertMathjax(mathjax); + $: encoded = encodeURIComponent(converted); -{#if edit} - {#if type === "block"} -
(edit = false)} - use:autofocus - /> - {:else} - (edit = false)} - use:autofocus - /> - {/if} -{:else} -
(edit = true)}> - {@html converted} -
-{/if} +Mathjax diff --git a/ts/editable/mathjax-components.ts b/ts/editable/mathjax-components.ts index dc5b3a64f..7da335ce9 100644 --- a/ts/editable/mathjax-components.ts +++ b/ts/editable/mathjax-components.ts @@ -6,8 +6,6 @@ class MathjaxInline extends HTMLElement { } decorate(): void { - this.contentEditable = "false"; - const mathjax = (this.dataset.mathjax = this.innerText); const type = "inline"; this.innerHTML = ""; @@ -19,7 +17,6 @@ class MathjaxInline extends HTMLElement { } undecorate(): void { - this.removeAttribute("contentEditable"); this.innerHTML = this.dataset.mathjax ?? ""; delete this.dataset.mathjax; } @@ -33,8 +30,6 @@ class MathjaxBlock extends HTMLElement { } decorate(): void { - this.contentEditable = "false"; - const mathjax = (this.dataset.mathjax = this.innerText); const type = "block"; this.innerHTML = ""; @@ -46,7 +41,6 @@ class MathjaxBlock extends HTMLElement { } undecorate(): void { - this.removeAttribute("contentEditable"); this.innerHTML = this.dataset.mathjax ?? ""; delete this.dataset.mathjax; } diff --git a/ts/editable/mathjax.ts b/ts/editable/mathjax.ts index 556e64dcf..ba360bc66 100644 --- a/ts/editable/mathjax.ts +++ b/ts/editable/mathjax.ts @@ -1,38 +1,16 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import { mathjax } from "mathjax-full/js/mathjax"; -import { TeX } from "mathjax-full/js/input/tex"; -import { CHTML } from "mathjax-full/js/output/chtml"; -import { HTMLAdaptor } from "mathjax-full/js/adaptors/HTMLAdaptor"; -import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html"; - -import { AllPackages } from "mathjax-full/js/input/tex/AllPackages.js"; -import "mathjax-full/js/util/entities/all"; - -// @ts-expect-error Minor interface mismatch: document.documentElement.nodeValue might be null -const adaptor = new HTMLAdaptor(window); -RegisterHTMLHandler(adaptor); - -const texOptions = { - displayMath: [["\\[", "\\]"]], - processRefs: false, - processEnvironments: false, - processEscapes: false, - packages: AllPackages, -}; +import "mathjax/es5/tex-svg-full"; export function convertMathjax(input: string): string { - const tex = new TeX(texOptions); - const chtml = new CHTML({ - fontURL: "/_anki/js/vendor/mathjax/output/chtml/fonts/woff-v2", - }); + const svg = globalThis.MathJax.tex2svg(input).children[0]; - const html = mathjax.document(input, { InputJax: tex, OutputJax: chtml }); - html.render(); + const style = document.createElement("style") as HTMLStyleElement; - return ( - adaptor.innerHTML(adaptor.head(html.document)) + - adaptor.innerHTML(adaptor.body(html.document)) - ); + const styles = `svg { color: white; font-size: 24px; }`; + style.appendChild(document.createTextNode(styles)); + + svg.insertBefore(style, svg.children[0]); + return svg.outerHTML; }