Remove jQuery from _updateQA

This commit is contained in:
Henrik Giesel 2021-04-13 00:22:00 +02:00
parent 3f3c509bad
commit e131b70c4c

View file

@ -9,9 +9,6 @@ var ankiPlatform = "desktop";
var typeans; var typeans;
var _updatingQueue: Promise<void> = Promise.resolve(); var _updatingQueue: Promise<void> = Promise.resolve();
var qFade = 0;
var aFade = 0;
var onUpdateHook: Array<Callback>; var onUpdateHook: Array<Callback>;
var onShownHook: Array<Callback>; var onShownHook: Array<Callback>;
@ -31,32 +28,25 @@ function _queueAction(action: Callback): void {
async function _updateQA( async function _updateQA(
html: string, html: string,
fadeTime: number,
onupdate: Callback, onupdate: Callback,
onshown: Callback onshown: Callback
): Promise<void> { ): Promise<void> {
onUpdateHook = [onupdate]; onUpdateHook = [onupdate];
onShownHook = [onshown]; onShownHook = [onshown];
const qa = $("#qa"); const qa = document.getElementById("qa")!;
const renderError = (kind: string) => (error: Error): void => { const renderError = (kind: string) => (error: Error): void => {
const errorMessage = String(error).substring(0, 2000); const errorMessage = String(error).substring(0, 2000);
const errorStack = String(error.stack).substring(0, 2000); const errorStack = String(error.stack).substring(0, 2000);
qa.innerHTML = `Invalid ${kind} on card: ${errorMessage}\n${errorStack}`.replace(/\n/g, "<br>");
qa.html(
`Invalid ${kind} on card: ${errorMessage}\n${errorStack}`.replace(
/\n/g,
"<br />"
)
);
}; };
// fade out current text // hide current card
await qa.fadeTo(fadeTime, 0).promise(); qa.style.opacity = "0";
// update text // update card
try { try {
qa.html(html); qa.innerHTML = html;
} catch (error) { } catch (error) {
renderError("HTML")(error); renderError("HTML")(error);
} }
@ -69,15 +59,15 @@ async function _updateQA(
// clear MathJax buffers from previous typesets // clear MathJax buffers from previous typesets
MathJax.typesetClear(); MathJax.typesetClear();
return MathJax.typesetPromise(qa.slice(0, 1)); return MathJax.typesetPromise([qa]);
}) })
.catch(renderError("MathJax")); .catch(renderError("MathJax"));
// defer display for up to 100ms to allow images to load // defer display for up to 100ms to allow images to load
await Promise.race([allImagesLoaded(), new Promise((r) => setTimeout(r, 100))]); await Promise.race([allImagesLoaded(), new Promise((r) => setTimeout(r, 100))]);
// and reveal when processing is done // and reveal card when processing is done
await qa.fadeTo(fadeTime, 1).promise(); qa.style.opacity = "1";
await _runHook(onShownHook); await _runHook(onShownHook);
} }
@ -85,7 +75,6 @@ function _showQuestion(q: string, bodyclass: string): void {
_queueAction(() => _queueAction(() =>
_updateQA( _updateQA(
q, q,
qFade,
function () { function () {
// return to top of window // return to top of window
window.scrollTo(0, 0); window.scrollTo(0, 0);
@ -107,7 +96,6 @@ function _showAnswer(a: string, bodyclass: string): void {
_queueAction(() => _queueAction(() =>
_updateQA( _updateQA(
a, a,
aFade,
function () { function () {
if (bodyclass) { if (bodyclass) {
// when previewing // when previewing