2021-05-17 08:59:02 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-07-13 00:23:46 +02:00
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-non-null-assertion: "off",
|
2021-07-16 17:28:31 +02:00
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
2021-07-13 00:23:46 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-17 01:25:05 +02:00
|
|
|
import "css-browser-selector/css_browser_selector.min";
|
2021-07-12 16:49:32 +02:00
|
|
|
import "jquery/dist/jquery";
|
2021-07-12 16:04:27 +02:00
|
|
|
|
2021-07-05 12:45:44 +02:00
|
|
|
export { mutateNextCardStates } from "./answering";
|
2021-05-17 08:59:02 +02:00
|
|
|
|
2021-07-12 16:57:51 +02:00
|
|
|
import { bridgeCommand } from "lib/bridgecommand";
|
|
|
|
import { allImagesLoaded, preloadAnswerImages } from "./images";
|
2021-07-16 16:33:12 +02:00
|
|
|
declare const MathJax: any;
|
2021-07-05 12:45:44 +02:00
|
|
|
|
|
|
|
type Callback = () => void | Promise<void>;
|
|
|
|
|
|
|
|
export const onUpdateHook: Array<Callback> = [];
|
|
|
|
export const onShownHook: Array<Callback> = [];
|
|
|
|
|
2021-07-12 15:00:36 +02:00
|
|
|
export const ankiPlatform = "desktop";
|
|
|
|
let typeans: HTMLInputElement | undefined;
|
|
|
|
|
|
|
|
export function getTypedAnswer(): string | null {
|
|
|
|
return typeans?.value ?? null;
|
|
|
|
}
|
|
|
|
|
2021-07-05 12:45:44 +02:00
|
|
|
function _runHook(arr: Array<Callback>): Promise<void[]> {
|
|
|
|
const promises: (Promise<void> | void)[] = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
promises.push(arr[i]());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:00:36 +02:00
|
|
|
let _updatingQueue: Promise<void> = Promise.resolve();
|
|
|
|
|
2021-07-17 04:12:07 +02:00
|
|
|
export function _queueAction(action: Callback): void {
|
2021-07-05 12:45:44 +02:00
|
|
|
_updatingQueue = _updatingQueue.then(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInnerHTML(element: Element, html: string): void {
|
|
|
|
for (const oldVideo of element.getElementsByTagName("video")) {
|
|
|
|
oldVideo.pause();
|
|
|
|
|
|
|
|
while (oldVideo.firstChild) {
|
|
|
|
oldVideo.removeChild(oldVideo.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
oldVideo.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
element.innerHTML = html;
|
|
|
|
|
|
|
|
for (const oldScript of element.getElementsByTagName("script")) {
|
|
|
|
const newScript = document.createElement("script");
|
|
|
|
|
|
|
|
for (const attribute of oldScript.attributes) {
|
|
|
|
newScript.setAttribute(attribute.name, attribute.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
|
|
|
|
oldScript.parentNode!.replaceChild(newScript, oldScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 16:33:12 +02:00
|
|
|
const renderError =
|
|
|
|
(type: string) =>
|
|
|
|
(error: Error): string => {
|
|
|
|
const errorMessage = String(error).substring(0, 2000);
|
|
|
|
const errorStack = String(error.stack).substring(0, 2000);
|
|
|
|
return `<div>Invalid ${type} on card: ${errorMessage}\n${errorStack}</div>`.replace(
|
|
|
|
/\n/g,
|
|
|
|
"<br>"
|
|
|
|
);
|
|
|
|
};
|
2021-07-12 22:10:40 +02:00
|
|
|
|
2021-07-17 01:11:05 +02:00
|
|
|
export async function _updateQA(
|
2021-07-05 12:45:44 +02:00
|
|
|
html: string,
|
|
|
|
_unusused: unknown,
|
|
|
|
onupdate: Callback,
|
|
|
|
onshown: Callback
|
|
|
|
): Promise<void> {
|
2021-07-12 15:00:36 +02:00
|
|
|
onUpdateHook.length = 0;
|
|
|
|
onUpdateHook.push(onupdate);
|
|
|
|
|
|
|
|
onShownHook.length = 0;
|
|
|
|
onShownHook.push(onshown);
|
2021-07-05 12:45:44 +02:00
|
|
|
|
|
|
|
const qa = document.getElementById("qa")!;
|
|
|
|
|
2021-07-16 17:26:04 +02:00
|
|
|
qa.style.opacity = "0";
|
|
|
|
|
2021-07-05 12:45:44 +02:00
|
|
|
try {
|
2021-07-16 16:33:12 +02:00
|
|
|
setInnerHTML(qa, html);
|
2021-07-05 12:45:44 +02:00
|
|
|
} catch (error) {
|
2021-07-16 16:33:12 +02:00
|
|
|
setInnerHTML(qa, renderError("html")(error));
|
2021-07-05 12:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await _runHook(onUpdateHook);
|
2021-07-16 16:33:12 +02:00
|
|
|
|
|
|
|
// wait for mathjax to ready
|
|
|
|
await MathJax.startup.promise
|
|
|
|
.then(() => {
|
|
|
|
// clear MathJax buffers from previous typesets
|
|
|
|
MathJax.typesetClear();
|
|
|
|
|
|
|
|
return MathJax.typesetPromise([qa]);
|
|
|
|
})
|
|
|
|
.catch(renderError("MathJax"));
|
|
|
|
|
2021-07-16 17:26:04 +02:00
|
|
|
qa.style.opacity = "1";
|
|
|
|
|
2021-07-05 12:45:44 +02:00
|
|
|
await _runHook(onShownHook);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function _showQuestion(q: string, a: string, bodyclass: string): void {
|
|
|
|
_queueAction(() =>
|
|
|
|
_updateQA(
|
|
|
|
q,
|
|
|
|
null,
|
|
|
|
function () {
|
|
|
|
// return to top of window
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
|
|
|
document.body.className = bodyclass;
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
// focus typing area if visible
|
2021-07-12 15:00:36 +02:00
|
|
|
typeans = document.getElementById("typeans") as HTMLInputElement;
|
2021-07-05 12:45:44 +02:00
|
|
|
if (typeans) {
|
|
|
|
typeans.focus();
|
|
|
|
}
|
|
|
|
// preload images
|
|
|
|
allImagesLoaded().then(() => preloadAnswerImages(q, a));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:11:14 +02:00
|
|
|
function scrollToAnswer(): void {
|
|
|
|
document.getElementById("answer")?.scrollIntoView();
|
|
|
|
}
|
|
|
|
|
2021-07-05 12:45:44 +02:00
|
|
|
export function _showAnswer(a: string, bodyclass: string): void {
|
|
|
|
_queueAction(() =>
|
|
|
|
_updateQA(
|
|
|
|
a,
|
|
|
|
null,
|
|
|
|
function () {
|
|
|
|
if (bodyclass) {
|
|
|
|
// when previewing
|
|
|
|
document.body.className = bodyclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
// avoid scrolling to the answer until images load
|
|
|
|
allImagesLoaded().then(scrollToAnswer);
|
|
|
|
},
|
2021-07-13 00:30:20 +02:00
|
|
|
function () {
|
|
|
|
/* noop */
|
|
|
|
}
|
2021-07-05 12:45:44 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function _drawFlag(flag: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7): void {
|
|
|
|
const elem = document.getElementById("_flag")!;
|
2021-07-12 17:11:14 +02:00
|
|
|
elem.toggleAttribute("hidden", flag === 0);
|
|
|
|
elem.style.color = `var(--flag${flag}-fg)`;
|
2021-07-05 12:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function _drawMark(mark: boolean): void {
|
2021-07-12 17:11:14 +02:00
|
|
|
document.getElementById("_mark")!.toggleAttribute("hidden", !mark);
|
2021-07-05 12:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function _typeAnsPress(): void {
|
|
|
|
const code = (window.event as KeyboardEvent).code;
|
|
|
|
if (["Enter", "NumpadEnter"].includes(code)) {
|
|
|
|
bridgeCommand("ans");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function _emulateMobile(enabled: boolean): void {
|
2021-07-12 17:11:14 +02:00
|
|
|
document.documentElement.classList.toggle("mobile", enabled);
|
2021-07-05 12:45:44 +02:00
|
|
|
}
|