mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Maybe preload images (#2165)
This commit is contained in:
parent
fd2212a6cb
commit
3973f27ee4
2 changed files with 18 additions and 1 deletions
|
@ -49,3 +49,17 @@ export function preloadAnswerImages(qHtml: string, aHtml: string): void {
|
||||||
diff.forEach((src) => injectPreloadLink(src, "image"));
|
diff.forEach((src) => injectPreloadLink(src, "image"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function maybePreloadImages(html: string): Promise<void> {
|
||||||
|
const srcs = extractImageSrcs(html);
|
||||||
|
await Promise.race([
|
||||||
|
Promise.all(
|
||||||
|
srcs.map((src) => {
|
||||||
|
const img = new Image();
|
||||||
|
img.src = src;
|
||||||
|
return imageLoaded(img);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
new Promise((r) => setTimeout(r, 100)),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ globalThis.anki.mutateNextCardStates = mutateNextCardStates;
|
||||||
|
|
||||||
import { bridgeCommand } from "../lib/bridgecommand";
|
import { bridgeCommand } from "../lib/bridgecommand";
|
||||||
import { maybePreloadExternalCss } from "./css";
|
import { maybePreloadExternalCss } from "./css";
|
||||||
import { allImagesLoaded, preloadAnswerImages } from "./images";
|
import { allImagesLoaded, maybePreloadImages, preloadAnswerImages } from "./images";
|
||||||
|
|
||||||
declare const MathJax: any;
|
declare const MathJax: any;
|
||||||
|
|
||||||
|
@ -133,6 +133,9 @@ export async function _updateQA(
|
||||||
// prevent flash of unstyled content when external css used
|
// prevent flash of unstyled content when external css used
|
||||||
await maybePreloadExternalCss(html);
|
await maybePreloadExternalCss(html);
|
||||||
|
|
||||||
|
// prevent flickering & layout shift on image load
|
||||||
|
await maybePreloadImages(html);
|
||||||
|
|
||||||
qa.style.opacity = "0";
|
qa.style.opacity = "0";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue