Merge pull request #874 from hgiesel/jqueryreview

Update _updateQa
This commit is contained in:
Damien Elmes 2020-12-30 13:51:54 +10:00 committed by GitHub
commit 1ee08c049e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,8 @@
/* Copyright: Ankitects Pty Ltd and contributors /* Copyright: Ankitects Pty Ltd and contributors
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
declare var MathJax: any;
var ankiPlatform = "desktop"; var ankiPlatform = "desktop";
var typeans; var typeans;
var _updatingQA = false; var _updatingQA = false;
@ -8,10 +10,10 @@ var _updatingQA = false;
var qFade = 50; var qFade = 50;
var aFade = 0; var aFade = 0;
var onUpdateHook; var onUpdateHook: Array<() => void | Promise<void>>;
var onShownHook; var onShownHook: Array<() => void | Promise<void>>;
function _runHook(arr: () => Promise<any>[]): Promise<any[]> { function _runHook(arr: Array<() => void | Promise<void>>): Promise<void[]> {
var promises = []; var promises = [];
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
@ -21,7 +23,7 @@ function _runHook(arr: () => Promise<any>[]): Promise<any[]> {
return Promise.all(promises); return Promise.all(promises);
} }
function _updateQA(html, fadeTime, onupdate, onshown) { async function _updateQA(html, fadeTime, onupdate, onshown) {
// if a request to update q/a comes in before the previous content // if a request to update q/a comes in before the previous content
// has been loaded, wait a while and try again // has been loaded, wait a while and try again
if (_updatingQA) { if (_updatingQA) {
@ -39,9 +41,9 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
var qa = $("#qa"); var qa = $("#qa");
// fade out current text // fade out current text
new Promise((resolve) => qa.fadeTo(fadeTime, 0, () => resolve())) await qa.fadeTo(fadeTime, 0).promise();
// update text // update text
.then(() => {
try { try {
qa.html(html); qa.html(html);
} catch (err) { } catch (err) {
@ -52,22 +54,21 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
).replace(/\n/g, "<br />") ).replace(/\n/g, "<br />")
); );
} }
}) await _runHook(onUpdateHook);
.then(() => _runHook(onUpdateHook))
.then(() => // wait for mathjax to ready
// @ts-ignore wait for mathjax to ready await MathJax.startup.promise.then(() => {
MathJax.startup.promise.then(() => { // clear MathJax buffers from previous typesets
// @ts-ignore clear MathJax buffer
MathJax.typesetClear(); MathJax.typesetClear();
// @ts-ignore typeset
return MathJax.typesetPromise(qa.slice(0, 1)); return MathJax.typesetPromise(qa.slice(0, 1));
}) });
)
// and reveal when processing is done // and reveal when processing is done
.then(() => new Promise((resolve) => qa.fadeTo(fadeTime, 1, () => resolve()))) await qa.fadeTo(fadeTime, 1).promise();
.then(() => _runHook(onShownHook)) await _runHook(onShownHook);
.then(() => (_updatingQA = false));
_updatingQA = false;
} }
function _showQuestion(q, bodyclass) { function _showQuestion(q, bodyclass) {