74 lines
2.1 KiB
Vue
74 lines
2.1 KiB
Vue
<script setup lang="ts">
|
|
import { NButton } from 'naive-ui'
|
|
import { computed, ref, watch } from 'vue'
|
|
import { ImageOff } from '@lucide/vue'
|
|
import { referenceImageUrl } from '../../lib/assets'
|
|
import RevealImage from './RevealImage.vue'
|
|
|
|
/** 图片统一铺满容器;原图预览按场景开启,历史选择按钮不触发预览。 */
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
src?: string | null
|
|
alt: string
|
|
emptyText?: string
|
|
retryable?: boolean
|
|
objectFit?: 'contain' | 'cover'
|
|
preview?: boolean
|
|
}>(),
|
|
{ retryable: true, objectFit: 'cover', preview: false }
|
|
)
|
|
const failed = ref(false)
|
|
const url = computed(() => (props.src ? referenceImageUrl(props.src) : null))
|
|
watch(
|
|
() => props.src,
|
|
() => {
|
|
failed.value = false
|
|
}
|
|
)
|
|
</script>
|
|
|
|
<template>
|
|
<div class="asset-image">
|
|
<RevealImage
|
|
v-if="url && !failed"
|
|
:src="url"
|
|
:alt="alt"
|
|
:preview="preview"
|
|
:object-fit="objectFit"
|
|
@error="failed = true"
|
|
/>
|
|
<span v-else class="asset-image-empty"
|
|
><ImageOff :size="22" :stroke-width="1.3" /><span>{{
|
|
src ? '图片无法加载' : emptyText || '尚未生成图片'
|
|
}}</span
|
|
><NButton v-if="failed && retryable" @click.stop="failed = false" text size="small">
|
|
重试加载
|
|
</NButton></span
|
|
>
|
|
</div>
|
|
</template>
|
|
|
|
<style>
|
|
/* 本组件专属布局与 Naive 内部结构覆盖。 */
|
|
@reference "../../styles/styles.css";
|
|
.asset-image {
|
|
@apply grid place-items-center w-full aspect-[4/3] overflow-hidden bg-(--app-subtle);
|
|
}
|
|
.asset-image img {
|
|
@apply w-full h-full min-h-0;
|
|
}
|
|
.asset-image-empty {
|
|
@apply flex items-center justify-center flex-col gap-2.5 p-5 text-muted text-[11px] text-center;
|
|
}
|
|
.asset-image .reveal-image,
|
|
.asset-image .n-image {
|
|
@apply w-full h-full min-h-0 min-w-0 flex justify-center;
|
|
}
|
|
.asset-image .n-image img {
|
|
@apply w-full h-full;
|
|
}
|
|
.asset-image.asset-image-preview {
|
|
@apply h-[min(42dvh,420px)] min-h-0 aspect-auto;
|
|
}
|
|
</style>
|