Line data Source code
1 : <template>
2 817 : <div
3 : ref="containerRef"
4 : class="relative flex size-full items-center justify-center overflow-hidden"
5 : :class="containerClass"
6 : >
7 : <Skeleton v-if="!isImageLoaded" class="absolute inset-0 size-full" />
8 : <img
9 : v-if="cachedSrc"
10 : :src="cachedSrc"
11 : :alt="alt"
12 : draggable="false"
13 : :class="imageClass"
14 : :style="imageStyle"
15 : @load="onImageLoad"
16 : @error="onImageError"
17 : />
18 : <div
19 : v-if="hasError"
20 : class="absolute inset-0 flex items-center justify-center"
21 : >
22 : <img
23 : src="/assets/images/default-template.png"
24 : :alt="alt"
25 : draggable="false"
26 : :class="imageClass"
27 : :style="imageStyle"
28 : />
29 : </div>
30 : </div>
31 : </template>
32 :
33 : <script setup lang="ts">
34 : import { computed, onUnmounted, ref, watch } from 'vue'
35 : import type { StyleValue } from 'vue'
36 :
37 : import Skeleton from '@comfyorg/design-system/skeleton/Skeleton'
38 : import { useIntersectionObserver } from '@/composables/useIntersectionObserver'
39 : import { useMediaCache } from '@/services/mediaCacheService'
40 :
41 : type ClassValue = string | Record<string, boolean> | ClassValue[]
42 :
43 : const {
44 : src,
45 : alt = '',
46 : containerClass = '',
47 : imageClass = '',
48 : imageStyle,
49 : rootMargin = '300px'
50 : } = defineProps<{
51 : src: string
52 : alt?: string
53 : containerClass?: ClassValue
54 : imageClass?: ClassValue
55 : imageStyle?: StyleValue
56 : rootMargin?: string
57 : }>()
58 :
59 499 : const containerRef = ref<HTMLElement | null>(null)
60 499 : const isIntersecting = ref(false)
61 499 : const isImageLoaded = ref(false)
62 499 : const hasError = ref(false)
63 499 : const cachedSrc = ref<string | undefined>(undefined)
64 :
65 499 : const { getCachedMedia, acquireUrl, releaseUrl } = useMediaCache()
66 :
67 : // Use intersection observer to detect when the image container comes into view
68 499 : useIntersectionObserver(
69 : containerRef,
70 : (entries) => {
71 521 : const entry = entries[0]
72 521 : isIntersecting.value = entry?.isIntersecting ?? false
73 : },
74 : {
75 : rootMargin,
76 : threshold: 0.1
77 : }
78 : )
79 :
80 : // Only start loading the image when it's in view
81 499 : const shouldLoad = computed(() => isIntersecting.value)
82 :
83 499 : watch(
84 : shouldLoad,
85 : async (shouldLoadVal) => {
86 680 : if (shouldLoadVal && src && !cachedSrc.value && !hasError.value) {
87 162 : try {
88 162 : const cachedMedia = await getCachedMedia(src)
89 154 : if (cachedMedia.error) {
90 0 : hasError.value = true
91 149 : } else if (cachedMedia.objectUrl) {
92 149 : const acquiredUrl = acquireUrl(src)
93 149 : cachedSrc.value = acquiredUrl || cachedMedia.objectUrl
94 : } else {
95 0 : cachedSrc.value = src
96 : }
97 : } catch (error) {
98 0 : console.warn('Failed to load cached media:', error)
99 0 : cachedSrc.value = src
100 : }
101 518 : } else if (!shouldLoadVal) {
102 514 : if (cachedSrc.value?.startsWith('blob:')) {
103 11 : releaseUrl(src)
104 : }
105 : // Hide image when out of view
106 514 : isImageLoaded.value = false
107 514 : cachedSrc.value = undefined
108 514 : hasError.value = false
109 : }
110 : },
111 : { immediate: true }
112 : )
113 :
114 499 : const onImageLoad = () => {
115 145 : isImageLoaded.value = true
116 145 : hasError.value = false
117 : }
118 :
119 499 : const onImageError = () => {
120 0 : hasError.value = true
121 0 : isImageLoaded.value = false
122 : }
123 :
124 499 : onUnmounted(() => {
125 214 : if (cachedSrc.value?.startsWith('blob:')) {
126 74 : releaseUrl(src)
127 : }
128 : })
129 : </script>
|