mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
fix(web): use native image decoder (#3074)
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
parent
e5d083fe79
commit
696900228b
5 changed files with 26 additions and 48 deletions
|
|
@ -1,38 +0,0 @@
|
|||
import { tick } from 'svelte';
|
||||
import type { ActionReturn } from 'svelte/action';
|
||||
|
||||
interface Attributes {
|
||||
'on:image-error'?: (e: CustomEvent) => void;
|
||||
'on:image-load'?: (e: CustomEvent) => void;
|
||||
}
|
||||
|
||||
export function imageLoad(img: HTMLImageElement): ActionReturn<void, Attributes> {
|
||||
const onImageError = () => img.dispatchEvent(new CustomEvent('image-error'));
|
||||
const onImageLoaded = () => img.dispatchEvent(new CustomEvent('image-load'));
|
||||
|
||||
if (img.complete) {
|
||||
// Browser has fetched the image, naturalHeight is used to check
|
||||
// if any loading errors have occurred.
|
||||
const loadingError = img.naturalHeight === 0;
|
||||
|
||||
// Report status after a tick, to make sure event listeners are registered.
|
||||
if (loadingError) {
|
||||
tick().then(onImageError);
|
||||
} else {
|
||||
tick().then(onImageLoaded);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// Image has not been loaded yet, report status with event listeners.
|
||||
img.addEventListener('load', onImageLoaded, { once: true });
|
||||
img.addEventListener('error', onImageError, { once: true });
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
img.removeEventListener('load', onImageLoaded);
|
||||
img.removeEventListener('error', onImageError);
|
||||
},
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue