2019-02-05 04:59:03 +01:00
|
|
|
/* Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
|
|
|
|
2020-12-29 13:43:47 +01:00
|
|
|
declare var MathJax: any;
|
|
|
|
|
2020-12-30 15:42:30 +01:00
|
|
|
type Callback = () => void | Promise<void>;
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
var ankiPlatform = "desktop";
|
2021-04-13 00:37:58 +02:00
|
|
|
var typeans: HTMLElement | undefined;
|
2020-12-30 14:45:27 +01:00
|
|
|
var _updatingQueue: Promise<void> = Promise.resolve();
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2020-12-30 15:42:30 +01:00
|
|
|
var onUpdateHook: Array<Callback>;
|
|
|
|
var onShownHook: Array<Callback>;
|
2017-08-28 06:54:22 +02:00
|
|
|
|
2020-12-30 15:42:30 +01:00
|
|
|
function _runHook(arr: Array<Callback>): Promise<void[]> {
|
2021-02-02 18:11:16 +01:00
|
|
|
const promises = [];
|
2020-11-15 20:57:07 +01:00
|
|
|
|
2021-02-02 18:11:16 +01:00
|
|
|
for (let i = 0; i < arr.length; i++) {
|
2020-11-15 20:57:07 +01:00
|
|
|
promises.push(arr[i]());
|
2017-08-28 06:54:22 +02:00
|
|
|
}
|
2020-11-15 20:57:07 +01:00
|
|
|
|
|
|
|
return Promise.all(promises);
|
2017-08-28 06:54:22 +02:00
|
|
|
}
|
|
|
|
|
2020-12-30 15:42:30 +01:00
|
|
|
function _queueAction(action: Callback): void {
|
2020-12-30 14:45:27 +01:00
|
|
|
_updatingQueue = _updatingQueue.then(action);
|
|
|
|
}
|
2017-10-24 09:09:22 +02:00
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
async function _updateQA(
|
|
|
|
html: string,
|
2020-12-30 15:42:30 +01:00
|
|
|
onupdate: Callback,
|
|
|
|
onshown: Callback
|
2020-12-30 15:06:22 +01:00
|
|
|
): Promise<void> {
|
2017-08-28 06:54:22 +02:00
|
|
|
onUpdateHook = [onupdate];
|
|
|
|
onShownHook = [onshown];
|
|
|
|
|
2021-04-13 00:22:00 +02:00
|
|
|
const qa = document.getElementById("qa")!;
|
2021-02-02 18:11:16 +01:00
|
|
|
const renderError = (kind: string) => (error: Error): void => {
|
|
|
|
const errorMessage = String(error).substring(0, 2000);
|
|
|
|
const errorStack = String(error.stack).substring(0, 2000);
|
2021-04-13 00:37:58 +02:00
|
|
|
qa.innerHTML = `Invalid ${kind} on card: ${errorMessage}\n${errorStack}`.replace(
|
|
|
|
/\n/g,
|
|
|
|
"<br>"
|
|
|
|
);
|
2021-02-02 18:11:16 +01:00
|
|
|
};
|
2020-11-15 20:57:07 +01:00
|
|
|
|
2021-04-13 00:22:00 +02:00
|
|
|
// hide current card
|
|
|
|
qa.style.opacity = "0";
|
2020-12-29 13:32:04 +01:00
|
|
|
|
2021-04-13 00:22:00 +02:00
|
|
|
// update card
|
2020-12-29 13:32:04 +01:00
|
|
|
try {
|
2021-04-13 00:22:00 +02:00
|
|
|
qa.innerHTML = html;
|
2021-02-02 18:11:16 +01:00
|
|
|
} catch (error) {
|
|
|
|
renderError("HTML")(error);
|
2020-12-29 14:45:20 +01:00
|
|
|
}
|
2020-12-29 13:32:04 +01:00
|
|
|
|
2021-02-03 13:32:55 +01:00
|
|
|
await _runHook(onUpdateHook);
|
|
|
|
|
2020-12-29 13:43:47 +01:00
|
|
|
// wait for mathjax to ready
|
2021-02-02 18:11:16 +01:00
|
|
|
await MathJax.startup.promise
|
|
|
|
.then(() => {
|
|
|
|
// clear MathJax buffers from previous typesets
|
|
|
|
MathJax.typesetClear();
|
|
|
|
|
2021-04-13 00:22:00 +02:00
|
|
|
return MathJax.typesetPromise([qa]);
|
2021-02-02 18:11:16 +01:00
|
|
|
})
|
|
|
|
.catch(renderError("MathJax"));
|
2020-12-29 13:32:04 +01:00
|
|
|
|
2021-03-22 03:05:18 +01:00
|
|
|
// defer display for up to 100ms to allow images to load
|
|
|
|
await Promise.race([allImagesLoaded(), new Promise((r) => setTimeout(r, 100))]);
|
|
|
|
|
2021-04-13 00:22:00 +02:00
|
|
|
// and reveal card when processing is done
|
|
|
|
qa.style.opacity = "1";
|
2020-12-29 13:32:04 +01:00
|
|
|
await _runHook(onShownHook);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
function _showQuestion(q: string, bodyclass: string): void {
|
|
|
|
_queueAction(() =>
|
|
|
|
_updateQA(
|
|
|
|
q,
|
|
|
|
function () {
|
|
|
|
// return to top of window
|
|
|
|
window.scrollTo(0, 0);
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
document.body.className = bodyclass;
|
2020-12-30 15:06:22 +01:00
|
|
|
},
|
|
|
|
function () {
|
|
|
|
// focus typing area if visible
|
|
|
|
typeans = document.getElementById("typeans");
|
|
|
|
if (typeans) {
|
|
|
|
typeans.focus();
|
|
|
|
}
|
2019-12-18 07:12:39 +01:00
|
|
|
}
|
2020-12-30 15:06:22 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-12-18 07:12:39 +01:00
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
function _showAnswer(a: string, bodyclass: string): void {
|
|
|
|
_queueAction(() =>
|
|
|
|
_updateQA(
|
|
|
|
a,
|
|
|
|
function () {
|
|
|
|
if (bodyclass) {
|
|
|
|
// when previewing
|
|
|
|
document.body.className = bodyclass;
|
|
|
|
}
|
|
|
|
|
2021-03-22 03:05:18 +01:00
|
|
|
// avoid scrolling to the answer until images load, even if it
|
|
|
|
// takes more than 100ms
|
|
|
|
allImagesLoaded().then(scrollToAnswer);
|
2020-12-30 15:06:22 +01:00
|
|
|
},
|
|
|
|
function () {}
|
|
|
|
)
|
|
|
|
);
|
2017-07-29 06:24:45 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 04:32:32 +01:00
|
|
|
const _flagColours = {
|
2018-11-01 05:58:41 +01:00
|
|
|
1: "#ff6666",
|
|
|
|
2: "#ff9900",
|
|
|
|
3: "#77ff77",
|
2019-12-18 07:12:39 +01:00
|
|
|
4: "#77aaff",
|
2017-08-12 08:08:10 +02:00
|
|
|
};
|
|
|
|
|
2021-01-01 14:14:50 +01:00
|
|
|
function _drawFlag(flag: 0 | 1 | 2 | 3 | 4): void {
|
2021-04-13 00:37:58 +02:00
|
|
|
const elem = document.getElementById("_flag");
|
2017-08-12 08:08:10 +02:00
|
|
|
if (flag === 0) {
|
2021-04-13 00:37:58 +02:00
|
|
|
elem.setAttribute("hidden", "");
|
2017-08-12 08:08:10 +02:00
|
|
|
return;
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2021-04-13 00:37:58 +02:00
|
|
|
elem.removeAttribute("hidden");
|
|
|
|
elem.style.color = _flagColours[flag];
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
function _drawMark(mark: boolean): void {
|
2021-04-13 00:37:58 +02:00
|
|
|
const elem = document.getElementById("_mark");
|
2017-08-16 12:30:29 +02:00
|
|
|
if (!mark) {
|
2021-04-13 00:37:58 +02:00
|
|
|
elem.setAttribute("hidden", "");
|
2017-08-16 12:30:29 +02:00
|
|
|
} else {
|
2021-04-13 00:37:58 +02:00
|
|
|
elem.removeAttribute("hidden");
|
2017-08-16 12:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
function _typeAnsPress(): void {
|
2021-04-13 00:37:58 +02:00
|
|
|
if ((window.event as KeyboardEvent).code === "Enter") {
|
2017-07-28 08:19:06 +02:00
|
|
|
pycmd("ans");
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 05:30:34 +02:00
|
|
|
|
2020-12-30 15:06:22 +01:00
|
|
|
function _emulateMobile(enabled: boolean): void {
|
2020-08-03 05:30:34 +02:00
|
|
|
const list = document.documentElement.classList;
|
|
|
|
if (enabled) {
|
|
|
|
list.add("mobile");
|
|
|
|
} else {
|
|
|
|
list.remove("mobile");
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 03:05:18 +01:00
|
|
|
|
|
|
|
function allImagesLoaded(): Promise<void[]> {
|
|
|
|
return Promise.all(
|
|
|
|
Array.from(document.getElementsByTagName("img")).map(imageLoaded)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function imageLoaded(img: HTMLImageElement): Promise<void> {
|
2021-04-13 00:42:09 +02:00
|
|
|
return img.complete
|
|
|
|
? Promise.resolve()
|
|
|
|
: new Promise((resolve) => {
|
|
|
|
img.addEventListener("load", () => resolve());
|
|
|
|
img.addEventListener("error", () => resolve());
|
|
|
|
});
|
2021-03-22 03:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function scrollToAnswer(): void {
|
|
|
|
document.getElementById("answer")?.scrollIntoView();
|
|
|
|
}
|