mirror of
https://github.com/ankitects/anki.git
synced 2025-09-22 07:52:24 -04:00
commit
1ee08c049e
1 changed files with 33 additions and 32 deletions
|
@ -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,35 +41,34 @@ 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
|
|
||||||
.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))
|
|
||||||
.then(() =>
|
|
||||||
// @ts-ignore wait for mathjax to ready
|
|
||||||
MathJax.startup.promise.then(() => {
|
|
||||||
// @ts-ignore clear MathJax buffer
|
|
||||||
MathJax.typesetClear();
|
|
||||||
|
|
||||||
// @ts-ignore typeset
|
// update text
|
||||||
return MathJax.typesetPromise(qa.slice(0, 1));
|
try {
|
||||||
})
|
qa.html(html);
|
||||||
)
|
} catch (err) {
|
||||||
// and reveal when processing is done
|
qa.html(
|
||||||
.then(() => new Promise((resolve) => qa.fadeTo(fadeTime, 1, () => resolve())))
|
(
|
||||||
.then(() => _runHook(onShownHook))
|
`Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
|
||||||
.then(() => (_updatingQA = false));
|
String(err.stack).substring(0, 2000)
|
||||||
|
).replace(/\n/g, "<br />")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await _runHook(onUpdateHook);
|
||||||
|
|
||||||
|
// wait for mathjax to ready
|
||||||
|
await MathJax.startup.promise.then(() => {
|
||||||
|
// clear MathJax buffers from previous typesets
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _showQuestion(q, bodyclass) {
|
function _showQuestion(q, bodyclass) {
|
||||||
|
|
Loading…
Reference in a new issue