@x33025/sveltely 0.1.25 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
const loadedImageSources = new Set<string>();
|
|
3
|
+
</script>
|
|
4
|
+
|
|
1
5
|
<script lang="ts">
|
|
2
6
|
import { ImageOffIcon } from '@lucide/svelte';
|
|
3
7
|
import type { Snippet } from 'svelte';
|
|
@@ -29,12 +33,28 @@
|
|
|
29
33
|
|
|
30
34
|
let imageLoaded = $state(false);
|
|
31
35
|
let imageFailed = $state(false);
|
|
36
|
+
let imageElement = $state<HTMLImageElement | null>(null);
|
|
37
|
+
let previousSrc = $state<string | null>(null);
|
|
32
38
|
|
|
33
39
|
$effect(() => {
|
|
34
|
-
src;
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
const nextSrc = src ?? null;
|
|
41
|
+
if (nextSrc === previousSrc) return;
|
|
42
|
+
|
|
43
|
+
previousSrc = nextSrc;
|
|
37
44
|
imageFailed = false;
|
|
45
|
+
imageLoaded = nextSrc ? loadedImageSources.has(nextSrc) : false;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const markImageLoaded = (loadedSrc: string | null | undefined) => {
|
|
49
|
+
if (loadedSrc) loadedImageSources.add(loadedSrc);
|
|
50
|
+
imageLoaded = true;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
$effect(() => {
|
|
54
|
+
if (!imageElement || !src || imageLoaded || imageFailed) return;
|
|
55
|
+
if (imageElement.complete && imageElement.naturalWidth > 0) {
|
|
56
|
+
markImageLoaded(src);
|
|
57
|
+
}
|
|
38
58
|
});
|
|
39
59
|
|
|
40
60
|
const canRenderImage = $derived(Boolean(src) && !busy && !imageFailed);
|
|
@@ -53,6 +73,7 @@
|
|
|
53
73
|
>
|
|
54
74
|
{#if canRenderImage}
|
|
55
75
|
<img
|
|
76
|
+
bind:this={imageElement}
|
|
56
77
|
{src}
|
|
57
78
|
{alt}
|
|
58
79
|
{loading}
|
|
@@ -63,7 +84,7 @@
|
|
|
63
84
|
: 'object-contain'} {imageLoaded
|
|
64
85
|
? 'opacity-100'
|
|
65
86
|
: 'opacity-0'} transition-opacity duration-150"
|
|
66
|
-
onload={() => (
|
|
87
|
+
onload={() => markImageLoaded(src)}
|
|
67
88
|
onerror={() => (imageFailed = true)}
|
|
68
89
|
/>
|
|
69
90
|
{/if}
|