anki/ts/reviewer/images.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-07-12 16:57:51 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const template = document.createElement("template");
2021-07-12 16:57:51 +02:00
export function allImagesLoaded(): Promise<void[]> {
return Promise.all(
Array.from(document.getElementsByTagName("img")).map(imageLoaded),
2021-07-12 16:57:51 +02:00
);
}
function imageLoaded(img: HTMLImageElement): Promise<void> {
return img.complete
? Promise.resolve()
: new Promise((resolve) => {
img.addEventListener("load", () => resolve());
img.addEventListener("error", () => resolve());
});
2021-07-12 16:57:51 +02:00
}
function extractImageSrcs(fragment: DocumentFragment): string[] {
2021-07-12 16:57:51 +02:00
const srcs = [...fragment.querySelectorAll("img[src]")].map(
(img) => (img as HTMLImageElement).src,
2021-07-12 16:57:51 +02:00
);
return srcs;
}
function createImage(src: string): HTMLImageElement {
const img = new Image();
img.src = src;
return img;
}
export function preloadAnswerImages(html: string): void {
template.innerHTML = html;
extractImageSrcs(template.content).forEach(createImage);
2021-07-12 16:57:51 +02:00
}
2022-11-04 01:07:51 +01:00
/** Prevent flickering & layout shift on image load */
export function preloadImages(fragment: DocumentFragment): Promise<void>[] {
return extractImageSrcs(fragment).map(createImage).map(imageLoaded);
2022-11-04 01:07:51 +01:00
}