mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Nicely portray mathjax with right color + alignment in Editor
This commit is contained in:
parent
b0378690c0
commit
7950078e2b
4 changed files with 25 additions and 66 deletions
|
@ -50,6 +50,7 @@ ts_library(
|
|||
"//ts:image_module_support",
|
||||
"@npm//svelte",
|
||||
"@npm//mathjax-full",
|
||||
"@npm//mathjax",
|
||||
] + svelte_names,
|
||||
)
|
||||
|
||||
|
|
|
@ -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);
|
||||
</script>
|
||||
|
||||
{#if edit}
|
||||
{#if type === "block"}
|
||||
<div
|
||||
contenteditable="true"
|
||||
bind:innerHTML={mathjax}
|
||||
on:blur={() => (edit = false)}
|
||||
use:autofocus
|
||||
/>
|
||||
{:else}
|
||||
<span
|
||||
contenteditable="true"
|
||||
bind:innerHTML={mathjax}
|
||||
on:blur={() => (edit = false)}
|
||||
use:autofocus
|
||||
/>
|
||||
{/if}
|
||||
{:else}
|
||||
<div class="d-contents" on:click={() => (edit = true)}>
|
||||
{@html converted}
|
||||
</div>
|
||||
{/if}
|
||||
<img
|
||||
src="data:image/svg+xml,{encoded}"
|
||||
class:block={type === "block"}
|
||||
alt="Mathjax"
|
||||
data-anki="mathjax"
|
||||
data-mathjax={mathjax}
|
||||
/>
|
||||
|
||||
<style lang="scss">
|
||||
.d-contents {
|
||||
display: contents;
|
||||
img {
|
||||
vertical-align: middle;
|
||||
|
||||
&.block {
|
||||
display: block;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue