Line data Source code
1 : <template>
2 59 : <template v-if="imageUrl">
3 : <div
4 : v-if="imageError"
5 : class="flex size-full flex-col items-center justify-center text-center text-white"
6 : >
7 : <i-lucide:image-off class="mb-1 size-8 text-smoke-500" />
8 : <p class="text-muted-foreground">{{ $t('g.imageFailedToLoad') }}</p>
9 : </div>
10 : <img
11 : v-else
12 : :src="imageUrl"
13 : :alt="$t('g.liveSamplingPreview')"
14 : class="pointer-events-none min-h-55 w-full flex-1 object-contain contain-size"
15 : @load="handleImageLoad"
16 : @error="handleImageError"
17 : />
18 : <div class="mt-1 text-center text-foreground">
19 : {{
20 : imageError
21 : ? $t('g.errorLoadingImage')
22 : : actualDimensions || $t('g.calculatingDimensions')
23 : }}
24 : </div>
25 : </template>
26 : </template>
27 :
28 : <script setup lang="ts">
29 : import { ref, watch } from 'vue'
30 :
31 : interface LivePreviewProps {
32 : imageUrl: string
33 : }
34 :
35 21 : const props = defineProps<LivePreviewProps>()
36 :
37 21 : const actualDimensions = ref<string | null>(null)
38 21 : const imageError = ref(false)
39 :
40 21 : watch(
41 : () => props.imageUrl,
42 : () => {
43 : // Reset error state when URL changes, but keep previous dimensions
44 : // to avoid flickering "Calculating dimensions" text during live preview
45 13 : imageError.value = false
46 : }
47 : )
48 :
49 21 : const handleImageLoad = (event: Event) => {
50 33 : if (!event.target || !(event.target instanceof HTMLImageElement)) return
51 33 : const img = event.target
52 33 : imageError.value = false
53 33 : if (img.naturalWidth && img.naturalHeight) {
54 33 : actualDimensions.value = `${img.naturalWidth} x ${img.naturalHeight}`
55 : }
56 : }
57 :
58 21 : const handleImageError = () => {
59 0 : imageError.value = true
60 0 : actualDimensions.value = null
61 : }
62 : </script>
|