anki/qt/aqt/data/web/js/reviewer.ts

154 lines
3.3 KiB
TypeScript
Raw Normal View History

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;
var ankiPlatform = "desktop";
var typeans;
var _updatingQA = false;
2020-12-22 04:02:07 +01:00
var qFade = 50;
var aFade = 0;
2020-12-29 13:42:30 +01:00
var onUpdateHook: Array<() => Promise<void>>;
var onShownHook: Array<() => Promise<void>>;
2020-12-29 13:42:30 +01:00
function _runHook(arr: Array<() => Promise<void>>): Promise<void[]> {
var promises = [];
2019-12-18 07:12:39 +01:00
for (var i = 0; i < arr.length; i++) {
promises.push(arr[i]());
}
return Promise.all(promises);
}
2020-12-29 13:32:04 +01:00
async 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) {
2020-06-24 13:54:38 +02:00
setTimeout(function () {
2019-12-18 07:12:39 +01:00
_updateQA(html, fadeTime, onupdate, onshown);
}, 50);
return;
}
_updatingQA = true;
onUpdateHook = [onupdate];
onShownHook = [onshown];
var qa = $("#qa");
// fade out current text
2020-12-29 13:32:04 +01:00
await qa.fadeTo(fadeTime, 0).promise();
// update text
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 />")
);
};
await _runHook(onUpdateHook);
2020-12-29 13:43:47 +01:00
// wait for mathjax to ready
2020-12-29 13:32:04 +01:00
await MathJax.startup.promise.then(() => {
2020-12-29 13:43:47 +01:00
// clear MathJax buffers from previous typesets
2020-12-29 13:32:04 +01:00
MathJax.typesetClear();
return MathJax.typesetPromise(qa.slice(0, 1));
});
// and reveal when processing is done
await qa.fadeTo(fadeTime, 1).promise();
await _runHook(onShownHook);
_updatingQA = false;
2017-07-28 08:48:49 +02:00
}
function _showQuestion(q, bodyclass) {
2019-12-18 07:12:39 +01:00
_updateQA(
q,
qFade,
2020-06-24 13:54:38 +02:00
function () {
2019-12-18 07:12:39 +01:00
// return to top of window
window.scrollTo(0, 0);
document.body.className = bodyclass;
2019-12-18 07:12:39 +01:00
},
2020-06-24 13:54:38 +02:00
function () {
2019-12-18 07:12:39 +01:00
// focus typing area if visible
typeans = document.getElementById("typeans");
if (typeans) {
typeans.focus();
}
}
2019-12-18 07:12:39 +01:00
);
}
2019-12-18 07:12:39 +01:00
function _showAnswer(a, bodyclass) {
_updateQA(
a,
aFade,
2020-06-24 13:54:38 +02:00
function () {
2019-12-18 07:12:39 +01:00
if (bodyclass) {
// when previewing
document.body.className = bodyclass;
}
// scroll to answer?
var e = $("#answer");
if (e[0]) {
e[0].scrollIntoView();
}
},
2020-06-24 13:54:38 +02:00
function () {}
2019-12-18 07:12:39 +01: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",
};
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() {
2019-12-18 04:32:32 +01:00
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");
}
}