Added: Hooks

This commit is contained in:
Luc Mcgrady 2025-11-19 20:39:20 +00:00
parent 98c618a048
commit fc77fab7c5
No known key found for this signature in database
GPG key ID: 4F3D7A0B17CC3D9C
2 changed files with 35 additions and 11 deletions

View file

@ -36,7 +36,7 @@ export function getTypedAnswer(): string | null {
return typeans?.value ?? null; return typeans?.value ?? null;
} }
function _runHook( export function _runHook(
hooks: Array<Callback>, hooks: Array<Callback>,
): Promise<PromiseSettledResult<void | Promise<void>>[]> { ): Promise<PromiseSettledResult<void | Promise<void>>[]> {
const promises: (Promise<void> | void)[] = []; const promises: (Promise<void> | void)[] = [];

View file

@ -6,7 +6,8 @@ import "../../reviewer/reviewer.scss";
import "../../mathjax"; import "../../mathjax";
import "mathjax/es5/tex-chtml-full.js"; import "mathjax/es5/tex-chtml-full.js";
import { renderError } from "../../reviewer"; import { registerPackage } from "@tslib/runtime-require";
import { _runHook, renderError } from "../../reviewer";
import { addBrowserClasses } from "../../reviewer/browser_selector"; import { addBrowserClasses } from "../../reviewer/browser_selector";
import { imageOcclusionAPI } from "../image-occlusion/review"; import { imageOcclusionAPI } from "../image-occlusion/review";
import { enableNightMode } from "../reviewer/reviewer"; import { enableNightMode } from "../reviewer/reviewer";
@ -25,6 +26,14 @@ function postParentMessage(message: ReviewerRequest) {
); );
} }
type Callback = () => void | Promise<void>;
export const onUpdateHook: Array<Callback> = [];
export const onShownHook: Array<Callback> = [];
globalThis.onUpdateHook = onUpdateHook;
globalThis.onShownHook = onShownHook;
declare const MathJax: any; declare const MathJax: any;
const urlParams = new URLSearchParams(location.search); const urlParams = new URLSearchParams(location.search);
const decoder = new TextDecoder(); const decoder = new TextDecoder();
@ -53,15 +62,8 @@ addEventListener("message", async (e: MessageEvent<InnerReviewerRequest>) => {
} }
} }
// wait for mathjax to ready onUpdateHook.length = 0;
await MathJax.startup.promise onShownHook.length = 0;
.then(() => {
// clear MathJax buffers from previous typesets
MathJax.typesetClear();
return MathJax.typesetPromise([document.body]);
})
.catch(renderError("MathJax"));
// "".innerHTML =" does not run scripts // "".innerHTML =" does not run scripts
for (const script of document.querySelectorAll("script")) { for (const script of document.querySelectorAll("script")) {
@ -74,6 +76,18 @@ addEventListener("message", async (e: MessageEvent<InnerReviewerRequest>) => {
parent.appendChild(new_script); parent.appendChild(new_script);
} }
_runHook(onUpdateHook);
// wait for mathjax to ready
await MathJax.startup.promise
.then(() => {
// clear MathJax buffers from previous typesets
MathJax.typesetClear();
return MathJax.typesetPromise([document.body]);
})
.catch(renderError("MathJax"));
_runHook(onShownHook);
break; break;
} }
default: { default: {
@ -190,3 +204,13 @@ Object.defineProperty(window, "localStorage", {
configurable: true, configurable: true,
enumerable: true, enumerable: true,
}); });
registerPackage("anki/reviewer", {
// If you append a function to this each time the question or answer
// is shown, it will be called before MathJax has been rendered.
onUpdateHook,
// If you append a function to this each time the question or answer
// is shown, it will be called after images have been preloaded and
// MathJax has been rendered.
onShownHook,
});