2021-08-05 06:01:43 +02:00
|
|
|
import "mathjax/es5/tex-svg-full";
|
2021-08-05 20:54:25 +02:00
|
|
|
|
|
|
|
import type { DecoratedElement, DecoratedElementConstructor } from "./decorated";
|
|
|
|
import { decoratedComponents } from "./decorated";
|
2021-08-05 19:25:35 +02:00
|
|
|
import { nodeIsElement } from "lib/dom";
|
2021-08-07 21:03:36 +02:00
|
|
|
import { nightModeKey } from "components/context-keys";
|
2021-08-05 06:01:43 +02:00
|
|
|
|
|
|
|
import Mathjax_svelte from "./Mathjax.svelte";
|
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
function moveNodeOutOfElement(
|
|
|
|
element: Element,
|
|
|
|
node: Node,
|
|
|
|
placement: "beforebegin" | "afterend"
|
|
|
|
): Node {
|
|
|
|
element.removeChild(node);
|
|
|
|
|
|
|
|
let referenceNode: Node;
|
|
|
|
|
|
|
|
if (nodeIsElement(node)) {
|
|
|
|
referenceNode = element.insertAdjacentElement(placement, node)!;
|
|
|
|
} else {
|
|
|
|
element.insertAdjacentText(placement, (node as Text).wholeText);
|
|
|
|
referenceNode =
|
|
|
|
placement === "beforebegin"
|
|
|
|
? element.previousSibling!
|
|
|
|
: element.nextSibling!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return referenceNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function placeCaretAfter(node: Node): void {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.setStartAfter(node);
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
|
|
const selection = document.getSelection()!;
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
function moveNodesInsertedOutside(element: Element, allowedChild: Node): () => void {
|
2021-08-05 19:25:35 +02:00
|
|
|
const observer = new MutationObserver(() => {
|
2021-08-07 03:37:03 +02:00
|
|
|
if (element.childNodes.length === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
const childNodes = [...element.childNodes];
|
|
|
|
const allowedIndex = childNodes.findIndex((child) => child === allowedChild);
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
const beforeChildren = childNodes.slice(0, allowedIndex);
|
|
|
|
const afterChildren = childNodes.slice(allowedIndex + 1);
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-07 03:37:03 +02:00
|
|
|
// Special treatment for pressing return after mathjax block
|
|
|
|
if (
|
|
|
|
afterChildren.length === 2 &&
|
|
|
|
afterChildren.every((child) => (child as Element).tagName === "BR")
|
|
|
|
) {
|
|
|
|
const first = afterChildren.pop();
|
|
|
|
element.removeChild(first!);
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
let lastNode: Node | null = null;
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
for (const node of beforeChildren) {
|
|
|
|
lastNode = moveNodeOutOfElement(element, node, "beforebegin");
|
|
|
|
}
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
for (const node of afterChildren) {
|
|
|
|
lastNode = moveNodeOutOfElement(element, node, "afterend");
|
|
|
|
}
|
2021-08-05 19:25:35 +02:00
|
|
|
|
2021-08-06 19:02:39 +02:00
|
|
|
if (lastNode) {
|
|
|
|
placeCaretAfter(lastNode);
|
2021-08-05 19:25:35 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-07 02:37:08 +02:00
|
|
|
observer.observe(element, { childList: true, characterData: true });
|
2021-08-05 19:25:35 +02:00
|
|
|
return () => observer.disconnect();
|
|
|
|
}
|
|
|
|
|
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}\\)`;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static toUndecorated(stored: string): string {
|
|
|
|
return stored
|
|
|
|
.replace(
|
|
|
|
mathjaxBlockDelimiterPattern,
|
|
|
|
(_match: string, text: string) =>
|
|
|
|
`<anki-mathjax block="true">${text}</anki-mathjax>`
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
mathjaxInlineDelimiterPattern,
|
|
|
|
(_match: string, text: string) => `<anki-mathjax>${text}</anki-mathjax>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
block = false;
|
2021-08-05 22:18:40 +02:00
|
|
|
disconnect: () => void = () => {
|
|
|
|
/* noop */
|
|
|
|
};
|
2021-08-06 00:59:52 +02:00
|
|
|
component?: Mathjax_svelte;
|
2021-08-05 19:25:35 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
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();
|
2021-08-06 19:02:39 +02:00
|
|
|
this.disconnect = moveNodesInsertedOutside(this, this.children[0]);
|
2021-08-05 19:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.disconnect();
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 16:52:34 +02:00
|
|
|
attributeChangedCallback(name: string, _old: string, newValue: string): void {
|
|
|
|
switch (name) {
|
|
|
|
case "block":
|
|
|
|
this.block = newValue !== "false";
|
|
|
|
this.component?.$set({ block: this.block });
|
2021-08-07 02:25:16 +02:00
|
|
|
break;
|
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 {
|
|
|
|
const mathjax = (this.dataset.mathjax = this.innerText);
|
|
|
|
this.innerHTML = "";
|
2021-08-07 03:37:03 +02:00
|
|
|
this.style.whiteSpace = "normal";
|
2021-08-05 06:01:43 +02:00
|
|
|
|
2021-08-07 21:03:36 +02:00
|
|
|
const context = new Map();
|
|
|
|
context.set(
|
|
|
|
nightModeKey,
|
|
|
|
document.documentElement.classList.contains("night-mode")
|
|
|
|
);
|
|
|
|
|
2021-08-06 00:59:52 +02:00
|
|
|
this.component = new Mathjax_svelte({
|
2021-08-05 06:01:43 +02:00
|
|
|
target: this,
|
|
|
|
props: { mathjax, block: this.block },
|
2021-08-07 21:03:36 +02:00
|
|
|
context,
|
|
|
|
} as any);
|
2021-08-05 06:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
undecorate(): void {
|
|
|
|
this.innerHTML = this.dataset.mathjax ?? "";
|
|
|
|
delete this.dataset.mathjax;
|
2021-08-07 03:37:03 +02:00
|
|
|
this.removeAttribute("style");
|
2021-08-05 06:01:43 +02:00
|
|
|
|
2021-08-06 00:59:52 +02:00
|
|
|
this.component?.$destroy();
|
|
|
|
this.component = undefined;
|
|
|
|
|
2021-08-05 06:01:43 +02:00
|
|
|
if (this.block) {
|
|
|
|
this.setAttribute("block", "true");
|
|
|
|
} else {
|
|
|
|
this.removeAttribute("block");
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 20:54:25 +02:00
|
|
|
};
|
2021-08-05 06:01:43 +02:00
|
|
|
|
2021-08-05 20:54:25 +02:00
|
|
|
decoratedComponents.push(Mathjax);
|