2021-08-07 23:04:36 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-08-05 06:01:43 +02:00
|
|
|
import "mathjax/es5/tex-svg-full";
|
2021-08-05 20:54:25 +02:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
import { on } from "../lib/events";
|
|
|
|
import { placeCaretBefore, placeCaretAfter } from "../domlib/place-caret";
|
2021-08-05 20:54:25 +02:00
|
|
|
import type { DecoratedElement, DecoratedElementConstructor } from "./decorated";
|
2022-01-08 02:46:01 +01:00
|
|
|
import { FrameElement, frameElement } from "./frame-element";
|
2021-08-05 06:01:43 +02:00
|
|
|
|
|
|
|
import Mathjax_svelte from "./Mathjax.svelte";
|
|
|
|
|
2021-08-05 20:54:25 +02:00
|
|
|
const mathjaxTagPattern =
|
|
|
|
/<anki-mathjax(?:[^>]*?block="(.*?)")?[^>]*?>(.*?)<\/anki-mathjax>/gsu;
|
|
|
|
|
|
|
|
const mathjaxBlockDelimiterPattern = /\\\[(.*?)\\\]/gsu;
|
|
|
|
const mathjaxInlineDelimiterPattern = /\\\((.*?)\\\)/gsu;
|
|
|
|
|
|
|
|
export const Mathjax: DecoratedElementConstructor = class Mathjax
|
|
|
|
extends HTMLElement
|
|
|
|
implements DecoratedElement
|
|
|
|
{
|
|
|
|
static tagName = "anki-mathjax";
|
|
|
|
|
|
|
|
static toStored(undecorated: string): string {
|
|
|
|
return undecorated.replace(
|
|
|
|
mathjaxTagPattern,
|
|
|
|
(_match: string, block: string | undefined, text: string) => {
|
|
|
|
return typeof block === "string" && block !== "false"
|
|
|
|
? `\\[${text}\\]`
|
|
|
|
: `\\(${text}\\)`;
|
2021-10-19 01:06:00 +02:00
|
|
|
},
|
2021-08-05 20:54:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static toUndecorated(stored: string): string {
|
|
|
|
return stored
|
|
|
|
.replace(
|
|
|
|
mathjaxBlockDelimiterPattern,
|
|
|
|
(_match: string, text: string) =>
|
2022-01-08 02:46:01 +01:00
|
|
|
`<${Mathjax.tagName} block="true">${text}</${Mathjax.tagName}>`,
|
2021-08-05 20:54:25 +02:00
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
mathjaxInlineDelimiterPattern,
|
2021-10-19 01:06:00 +02:00
|
|
|
(_match: string, text: string) =>
|
2022-01-08 02:46:01 +01:00
|
|
|
`<${Mathjax.tagName}>${text}</${Mathjax.tagName}>`,
|
2021-08-05 20:54:25 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
block = false;
|
2022-01-08 02:46:01 +01:00
|
|
|
frame?: FrameElement;
|
2021-08-06 00:59:52 +02:00
|
|
|
component?: Mathjax_svelte;
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-05 06:01:43 +02:00
|
|
|
static get observedAttributes(): string[] {
|
2021-08-06 16:52:34 +02:00
|
|
|
return ["block", "data-mathjax"];
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connectedCallback(): void {
|
|
|
|
this.decorate();
|
2022-01-08 02:46:01 +01:00
|
|
|
this.addEventListeners();
|
2021-08-05 19:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
2022-01-08 02:46:01 +01:00
|
|
|
this.removeEventListeners();
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
attributeChangedCallback(name: string, old: string, newValue: string): void {
|
|
|
|
if (newValue === old) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:52:34 +02:00
|
|
|
switch (name) {
|
|
|
|
case "block":
|
|
|
|
this.block = newValue !== "false";
|
|
|
|
this.component?.$set({ block: this.block });
|
2022-01-08 02:46:01 +01:00
|
|
|
this.frame?.setAttribute("block", String(this.block));
|
2021-08-07 02:25:16 +02:00
|
|
|
break;
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2021-08-06 16:52:34 +02:00
|
|
|
case "data-mathjax":
|
|
|
|
this.component?.$set({ mathjax: newValue });
|
2021-08-07 02:25:16 +02:00
|
|
|
break;
|
2021-08-06 16:52:34 +02:00
|
|
|
}
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decorate(): void {
|
2022-01-08 02:46:01 +01:00
|
|
|
if (this.hasAttribute("decorated")) {
|
|
|
|
this.undecorate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.parentElement?.tagName === FrameElement.tagName.toUpperCase()) {
|
|
|
|
this.frame = this.parentElement as FrameElement;
|
|
|
|
} else {
|
|
|
|
frameElement(this, this.block);
|
|
|
|
/* Framing will place this element inside of an anki-frame element,
|
|
|
|
* causing the connectedCallback to be called again.
|
|
|
|
* If we'd continue decorating at this point, we'd loose all the information */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dataset.mathjax = this.innerText;
|
2021-08-05 06:01:43 +02:00
|
|
|
this.innerHTML = "";
|
2021-08-07 03:37:03 +02:00
|
|
|
this.style.whiteSpace = "normal";
|
2021-08-05 06:01:43 +02:00
|
|
|
|
2021-08-06 00:59:52 +02:00
|
|
|
this.component = new Mathjax_svelte({
|
2021-08-05 06:01:43 +02:00
|
|
|
target: this,
|
2021-08-07 22:52:48 +02:00
|
|
|
props: {
|
2022-01-08 02:46:01 +01:00
|
|
|
mathjax: this.dataset.mathjax,
|
2021-08-07 22:52:48 +02:00
|
|
|
block: this.block,
|
2022-01-08 02:46:01 +01:00
|
|
|
fontSize: 20,
|
2021-08-07 22:52:48 +02:00
|
|
|
},
|
2022-01-08 02:46:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this.hasAttribute("focusonmount")) {
|
|
|
|
this.component.moveCaretAfter();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setAttribute("contentEditable", "false");
|
|
|
|
this.setAttribute("decorated", "true");
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
undecorate(): void {
|
2022-01-08 02:46:01 +01:00
|
|
|
if (this.parentElement?.tagName === FrameElement.tagName.toUpperCase()) {
|
|
|
|
this.parentElement.replaceWith(this);
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:01:43 +02:00
|
|
|
this.innerHTML = this.dataset.mathjax ?? "";
|
|
|
|
delete this.dataset.mathjax;
|
2021-08-07 03:37:03 +02:00
|
|
|
this.removeAttribute("style");
|
2021-08-07 22:52:48 +02:00
|
|
|
this.removeAttribute("focusonmount");
|
2021-08-05 06:01:43 +02:00
|
|
|
|
|
|
|
if (this.block) {
|
|
|
|
this.setAttribute("block", "true");
|
|
|
|
} else {
|
|
|
|
this.removeAttribute("block");
|
|
|
|
}
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
this.removeAttribute("contentEditable");
|
|
|
|
this.removeAttribute("decorated");
|
|
|
|
}
|
|
|
|
|
|
|
|
removeMoveInStart?: () => void;
|
|
|
|
removeMoveInEnd?: () => void;
|
|
|
|
|
|
|
|
addEventListeners(): void {
|
|
|
|
this.removeMoveInStart = on(
|
|
|
|
this,
|
|
|
|
"moveinstart" as keyof HTMLElementEventMap,
|
|
|
|
() => this.component!.selectAll(),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.removeMoveInEnd = on(this, "moveinend" as keyof HTMLElementEventMap, () =>
|
|
|
|
this.component!.selectAll(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEventListeners(): void {
|
|
|
|
this.removeMoveInStart?.();
|
|
|
|
this.removeMoveInStart = undefined;
|
|
|
|
|
|
|
|
this.removeMoveInEnd?.();
|
|
|
|
this.removeMoveInEnd = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
placeCaretBefore(): void {
|
|
|
|
if (this.frame) {
|
|
|
|
placeCaretBefore(this.frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
placeCaretAfter(): void {
|
|
|
|
if (this.frame) {
|
|
|
|
placeCaretAfter(this.frame);
|
|
|
|
}
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
2021-08-05 20:54:25 +02:00
|
|
|
};
|