f2173fddb0
* Allow theme change at runtime and add hook * Save or restore default palette on theme change * Update aqt widget styles on theme change * styling fixes - drop _light_palette, as default_palette serves the same purpose - save default platform theme, and restore it when switching away from nightmode - update macOS light/dark mode on theme switch - fix unreadable menus on Windows * update night-mode classes on theme change This is the easy part - CSS styling that uses standard_css or our css variables should update automatically. The main remaining issue is JS code that sets colors based on the theme at the time it's run - eg the graph code, and the editor. * switch night mode value on toggle * expose current theme via a store; switch graphs to use it https://github.com/ankitects/anki/issues/1471#issuecomment-972402492 * start using currentTheme in editor/components This fixes basic editing - there are still components that need updating. * add simple xcodeproj for code completion * add helper to get currently-active system theme on macOS * fix setCurrentTheme not being immediately available * live update tag color * style().name() doesn't work on Qt5 * automatic theme switching on Windows/Mac * currentTheme -> pageTheme * Replace `nightModeKey` with `pageTheme` Co-authored-by: Damien Elmes <gpg@ankiweb.net>
176 lines
5.1 KiB
TypeScript
176 lines
5.1 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
/* eslint
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
*/
|
|
|
|
import "mathjax/es5/tex-svg-full";
|
|
|
|
import type { DecoratedElement, DecoratedElementConstructor } from "./decorated";
|
|
import { nodeIsElement } from "../lib/dom";
|
|
import { noop } from "../lib/functional";
|
|
import { placeCaretAfter } from "../domlib/place-caret";
|
|
|
|
import Mathjax_svelte from "./Mathjax.svelte";
|
|
|
|
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 moveNodesInsertedOutside(element: Element, allowedChild: Node): () => void {
|
|
const observer = new MutationObserver(() => {
|
|
if (element.childNodes.length === 1) {
|
|
return;
|
|
}
|
|
|
|
const childNodes = [...element.childNodes];
|
|
const allowedIndex = childNodes.findIndex((child) => child === allowedChild);
|
|
|
|
const beforeChildren = childNodes.slice(0, allowedIndex);
|
|
const afterChildren = childNodes.slice(allowedIndex + 1);
|
|
|
|
// 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!);
|
|
}
|
|
|
|
let lastNode: Node | null = null;
|
|
|
|
for (const node of beforeChildren) {
|
|
lastNode = moveNodeOutOfElement(element, node, "beforebegin");
|
|
}
|
|
|
|
for (const node of afterChildren) {
|
|
lastNode = moveNodeOutOfElement(element, node, "afterend");
|
|
}
|
|
|
|
if (lastNode) {
|
|
placeCaretAfter(lastNode);
|
|
}
|
|
});
|
|
|
|
observer.observe(element, { childList: true, characterData: true });
|
|
return () => observer.disconnect();
|
|
}
|
|
|
|
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;
|
|
disconnect = noop;
|
|
component?: Mathjax_svelte;
|
|
|
|
static get observedAttributes(): string[] {
|
|
return ["block", "data-mathjax"];
|
|
}
|
|
|
|
connectedCallback(): void {
|
|
this.decorate();
|
|
this.disconnect = moveNodesInsertedOutside(this, this.children[0]);
|
|
}
|
|
|
|
disconnectedCallback(): void {
|
|
this.disconnect();
|
|
}
|
|
|
|
attributeChangedCallback(name: string, _old: string, newValue: string): void {
|
|
switch (name) {
|
|
case "block":
|
|
this.block = newValue !== "false";
|
|
this.component?.$set({ block: this.block });
|
|
break;
|
|
case "data-mathjax":
|
|
this.component?.$set({ mathjax: newValue });
|
|
break;
|
|
}
|
|
}
|
|
|
|
decorate(): void {
|
|
const mathjax = (this.dataset.mathjax = this.innerText);
|
|
this.innerHTML = "";
|
|
this.style.whiteSpace = "normal";
|
|
|
|
this.component = new Mathjax_svelte({
|
|
target: this,
|
|
props: {
|
|
mathjax,
|
|
block: this.block,
|
|
autofocus: this.hasAttribute("focusonmount"),
|
|
},
|
|
} as any);
|
|
}
|
|
|
|
undecorate(): void {
|
|
this.innerHTML = this.dataset.mathjax ?? "";
|
|
delete this.dataset.mathjax;
|
|
this.removeAttribute("style");
|
|
this.removeAttribute("focusonmount");
|
|
|
|
this.component?.$destroy();
|
|
this.component = undefined;
|
|
|
|
if (this.block) {
|
|
this.setAttribute("block", "true");
|
|
} else {
|
|
this.removeAttribute("block");
|
|
}
|
|
}
|
|
};
|