Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

refactor(photo-item): 优化图片加载和容器高度计算逻辑,适配亮色模式下加载屏的样式

Timothy Yin0e0cfc417870
diff --git a/app/components/masonry/PhotoItem.client.vue b/app/components/masonry/PhotoItem.client.vue
index 8d62dcca..8024dd65 100644
--- a/app/components/masonry/PhotoItem.client.vue
+++ b/app/components/masonry/PhotoItem.client.vue
@@ -17,7 +17,6 @@ const ITEM_GAP = 4
// Reactive state
const isLoading = ref(true)
-const actualHeight = ref(0)
const photoRef = ref<HTMLElement>()
const mainImageRef = ref<HTMLImageElement>()
const isVisible = ref(false)
@@ -28,36 +27,29 @@ const resizeObserverRef = ref<ResizeObserver | null>(null)
const intersectionObserverRef = ref<IntersectionObserver | null>(null)
// Computed
-const containerHeight = computed(() => {
- // Get the actual container width to calculate proper aspect ratio
- const currentWidth = containerWidth.value || 280
-
- // Use actual dimensions from photo data if available
+const aspectRatio = computed(() => {
+ // Priority 1: Use aspectRatio from photo data if available
+ if (props.photo.aspectRatio) {
+ return props.photo.aspectRatio
+ }
+
+ // Priority 2: Calculate from width and height if available
if (props.photo.width && props.photo.height) {
- const aspectRatio = props.photo.height / props.photo.width
- return Math.round(currentWidth * aspectRatio)
+ return props.photo.height / props.photo.width
}
- // Fallback to calculated height if image has loaded
- if (actualHeight.value > 0) {
- return actualHeight.value
- }
+ // Fallback: Default aspect ratio
+ return 1.2
+})
- // Default fallback height based on current width
- return Math.round(currentWidth * 1.2) // 1.2 is a reasonable default aspect ratio
+const containerHeight = computed(() => {
+ const currentWidth = containerWidth.value || 280
+ return Math.round(currentWidth * aspectRatio.value)
})
// Methods
const handleImageLoad = (event: Event) => {
- const img = event.target as HTMLImageElement
isLoading.value = false
-
- // Update actual height based on loaded image dimensions
- if (img.naturalWidth && img.naturalHeight) {
- const aspectRatio = img.naturalHeight / img.naturalWidth
- const currentWidth = containerWidth.value || 280
- actualHeight.value = Math.round(currentWidth * aspectRatio)
- }
}
const handleImageError = () => {
@@ -68,13 +60,7 @@ const handleImageError = () => {
// Check if image is already loaded (cached)
const checkImageLoaded = (img: HTMLImageElement) => {
if (img.complete && img.naturalHeight !== 0) {
- // Create a synthetic event for consistency
- const syntheticEvent = new Event('load')
- Object.defineProperty(syntheticEvent, 'target', {
- value: img,
- enumerable: true,
- })
- handleImageLoad(syntheticEvent)
+ isLoading.value = false
}
}
@@ -145,13 +131,6 @@ onMounted(() => {
if (props.photo.thumbnailUrl) {
const img = new Image()
img.onload = () => {
- if (img.naturalWidth && img.naturalHeight) {
- const aspectRatio = img.naturalHeight / img.naturalWidth
- const currentWidth = containerWidth.value || 280
- actualHeight.value = Math.round(
- currentWidth * (props.photo.aspectRatio || aspectRatio),
- )
- }
// Update loading state after preload completes
isLoading.value = false
}
@@ -220,37 +199,41 @@ onUnmounted(() => {
@click="emit('openViewer', props.index)"
>
<div class="relative group overflow-hidden transition-all duration-300">
- <!-- Thumbhash 组件占位符 -->
+ <!-- Container with fixed aspect ratio -->
<div
- v-if="isLoading && photo.thumbnailHash"
- class="w-full absolute inset-0"
- :style="{ aspectRatio: `${photo.width || 1} / ${photo.height || 1}` }"
+ class="w-full relative"
+ :style="{ aspectRatio: `1 / ${aspectRatio}` }"
>
- <Thumbhash
- :thumbhash="photo.thumbnailHash"
- class="w-full h-full object-cover scale-110"
- :alt="photo.title || 'Photo placeholder'"
- />
- </div>
+ <!-- Thumbhash placeholder -->
+ <div
+ v-if="isLoading && photo.thumbnailHash"
+ class="absolute inset-0 w-full h-full"
+ >
+ <Thumbhash
+ :thumbhash="photo.thumbnailHash"
+ class="w-full h-full object-cover scale-110"
+ :alt="photo.title || 'Photo placeholder'"
+ />
+ </div>
- <!-- Loading placeholder (fallback when no thumbhash) -->
- <div
- v-if="isLoading && !photo.thumbnailHash"
- class="w-full bg-gray-200 dark:bg-gray-700 animate-pulse"
- :style="{ aspectRatio: `${photo.width || 1} / ${photo.height || 1.2}` }"
- />
+ <!-- Loading placeholder (fallback when no thumbhash) -->
+ <div
+ v-if="isLoading && !photo.thumbnailHash"
+ class="absolute inset-0 w-full h-full bg-gray-200 dark:bg-gray-700 animate-pulse"
+ />
- <!-- Main image -->
- <img
- ref="mainImageRef"
- v-show="!isLoading"
- :src="photo.thumbnailUrl || ''"
- :alt="photo.title || photo.description || 'Photo'"
- class="w-full h-auto object-cover transition-transform duration-300 group-hover:scale-105 block"
- @load="handleImageLoad"
- @error="handleImageError"
- loading="lazy"
- />
+ <!-- Main image -->
+ <img
+ ref="mainImageRef"
+ v-show="!isLoading"
+ :src="photo.thumbnailUrl || ''"
+ :alt="photo.title || photo.description || 'Photo'"
+ class="absolute inset-0 w-full h-full object-cover transition-transform duration-300 group-hover:scale-105"
+ @load="handleImageLoad"
+ @error="handleImageError"
+ loading="lazy"
+ />
+ </div>
<!-- Overlay -->
<div
diff --git a/app/pages/index.vue b/app/pages/index.vue
index 294bf913..0c071555 100644
--- a/app/pages/index.vue
+++ b/app/pages/index.vue
@@ -3,16 +3,18 @@ const { data } = useFetch('/api/photos')
useHead({
title: 'Gallery',
- titleTemplate: (title) => `${title} - TimoYin\'s mems`,
+ titleTemplate: (title) => `${title} - TimoYin's mems`,
})
+
+const photos = computed(() => (data.value as Photo[]) || [])
</script>
<template>
<div class="h-svh px-1">
<ClientOnly>
<MasonryRoot
- :photos="data as Photo[]"
- :columns="'auto'"
+ :photos="photos"
+ columns="auto"
/>
<template #fallback>
<div
@@ -33,15 +35,16 @@ useHead({
display: inline-block;
position: relative;
}
+
.loading-scan-text {
display: inline-block;
background: linear-gradient(
90deg,
- #fff 0%,
- #fff 30%,
- rgba(255, 255, 255, 0.3) 50%,
- #fff 70%,
- #fff 100%
+ var(--ui-text-highlighted) 0%,
+ var(--ui-text-highlighted) 30%,
+ var(--ui-text-muted) 50%,
+ var(--ui-text-highlighted) 70%,
+ var(--ui-text-highlighted) 100%
);
background-size: 200% 100%;
background-position: 200% 0;