anki/qt/aqt/data/web/js/reviewer.ts
Henrik Giesel a7b5a7efe1 Improve ts hooks to allow for asynchronous actions
* reviewer ts hooks may now return a promise, which are then waited upon with Promise.all
* this does not break old ts hooks, as Promise.all allows for non-Promises in its array
2020-11-15 21:16:06 +01:00

153 lines
3.5 KiB
TypeScript

/* Copyright: Ankitects Pty Ltd and contributors
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
var ankiPlatform = "desktop";
var typeans;
var _updatingQA = false;
var qFade = 100;
var aFade = 0;
var onUpdateHook;
var onShownHook;
function _runHook(arr: () => Promise<any>[]): Promise<any[]> {
var promises = [];
for (var i = 0; i < arr.length; i++) {
promises.push(arr[i]());
}
return Promise.all(promises);
}
function _updateQA(html, fadeTime, onupdate, onshown) {
// if a request to update q/a comes in before the previous content
// has been loaded, wait a while and try again
if (_updatingQA) {
setTimeout(function () {
_updateQA(html, fadeTime, onupdate, onshown);
}, 50);
return;
}
_updatingQA = true;
onUpdateHook = [onupdate];
onShownHook = [onshown];
var qa = $("#qa");
// fade out current text
qa.fadeOut(fadeTime).promise()
// update text
.then(() => {
try {
qa.html(html)
}
catch (err) {
qa.html(
(
`Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
String(err.stack).substring(0, 2000)
).replace(/\n/g, "<br />")
);
}
})
.then(() => _runHook(onUpdateHook))
// @ts-ignore wait for mathjax to ready
.then(() => MathJax.startup.promise
.then(() => {
// @ts-ignore clear MathJax buffer
MathJax.typesetClear();
// @ts-ignore typeset
return MathJax.typesetPromise(qa.slice(0, 1));
}))
// and reveal when processing is done
.then(() => qa.fadeIn(fadeTime).promise())
.then(() => _runHook(onShownHook))
.then(() => _updatingQA = false);
}
function _showQuestion(q, bodyclass) {
_updateQA(
q,
qFade,
function () {
// return to top of window
window.scrollTo(0, 0);
document.body.className = bodyclass;
},
function () {
// focus typing area if visible
typeans = document.getElementById("typeans");
if (typeans) {
typeans.focus();
}
}
);
}
function _showAnswer(a, bodyclass) {
_updateQA(
a,
aFade,
function () {
if (bodyclass) {
// when previewing
document.body.className = bodyclass;
}
// scroll to answer?
var e = $("#answer");
if (e[0]) {
e[0].scrollIntoView();
}
},
function () {}
);
}
const _flagColours = {
1: "#ff6666",
2: "#ff9900",
3: "#77ff77",
4: "#77aaff",
};
function _drawFlag(flag) {
var elem = $("#_flag");
if (flag === 0) {
elem.hide();
return;
}
elem.show();
elem.css("color", _flagColours[flag]);
}
function _drawMark(mark) {
var elem = $("#_mark");
if (!mark) {
elem.hide();
} else {
elem.show();
}
}
function _typeAnsPress() {
if ((window.event as KeyboardEvent).keyCode === 13) {
pycmd("ans");
}
}
function _emulateMobile(enabled: boolean) {
const list = document.documentElement.classList;
if (enabled) {
list.add("mobile");
} else {
list.remove("mobile");
}
}