mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
delay scrolling to answer until images load
This commit is contained in:
parent
87801668e9
commit
c3145977f0
1 changed files with 26 additions and 5 deletions
|
@ -73,6 +73,9 @@ async function _updateQA(
|
|||
})
|
||||
.catch(renderError("MathJax"));
|
||||
|
||||
// defer display for up to 100ms to allow images to load
|
||||
await Promise.race([allImagesLoaded(), new Promise((r) => setTimeout(r, 100))]);
|
||||
|
||||
// and reveal when processing is done
|
||||
await qa.fadeTo(fadeTime, 1).promise();
|
||||
await _runHook(onShownHook);
|
||||
|
@ -111,11 +114,9 @@ function _showAnswer(a: string, bodyclass: string): void {
|
|||
document.body.className = bodyclass;
|
||||
}
|
||||
|
||||
// scroll to answer?
|
||||
var e = $("#answer");
|
||||
if (e[0]) {
|
||||
e[0].scrollIntoView();
|
||||
}
|
||||
// avoid scrolling to the answer until images load, even if it
|
||||
// takes more than 100ms
|
||||
allImagesLoaded().then(scrollToAnswer);
|
||||
},
|
||||
function () {}
|
||||
)
|
||||
|
@ -162,3 +163,23 @@ function _emulateMobile(enabled: boolean): void {
|
|||
list.remove("mobile");
|
||||
}
|
||||
}
|
||||
|
||||
function allImagesLoaded(): Promise<void[]> {
|
||||
return Promise.all(
|
||||
Array.from(document.getElementsByTagName("img")).map(imageLoaded)
|
||||
);
|
||||
}
|
||||
|
||||
function imageLoaded(img: HTMLImageElement): Promise<void> {
|
||||
if (img.complete) {
|
||||
return;
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
img.addEventListener("load", () => resolve());
|
||||
img.addEventListener("error", () => resolve());
|
||||
});
|
||||
}
|
||||
|
||||
function scrollToAnswer(): void {
|
||||
document.getElementById("answer")?.scrollIntoView();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue