mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-25 02:00:10 +02:00
[web] Convert pug frontend helpers to typescript GitOrigin-RevId: e74527306ded6bc7db992cd8ceaff34d0d26b1b7
22 lines
658 B
TypeScript
22 lines
658 B
TypeScript
function loadingFailed(imgEl: HTMLImageElement) {
|
|
return imgEl.complete && imgEl.naturalWidth === 0
|
|
}
|
|
|
|
document.querySelectorAll('[data-ol-fallback-image]').forEach(element => {
|
|
if (!(element instanceof HTMLImageElement)) {
|
|
// The sprinkle only applies to image elements.
|
|
return
|
|
}
|
|
const imgEl = element as HTMLImageElement
|
|
function showFallback() {
|
|
imgEl.src = imgEl.getAttribute('data-ol-fallback-image')!
|
|
}
|
|
if (loadingFailed(imgEl)) {
|
|
// The image loading failed before the sprinkle ran.
|
|
showFallback()
|
|
} else {
|
|
// The image loading might fail in the future.
|
|
imgEl.addEventListener('error', showFallback)
|
|
}
|
|
})
|