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.

fix: 重写 Masonry 相关组件,修复初次加载 Header 位置异常,优化图片信息叠层样式

HoshinoSuzumif6ab80008f4d
diff --git a/app/components/DateRangeIndicator.vue b/app/components/DateRangeIndicator.vue
new file mode 100644
index 00000000..f3cee69c
--- /dev/null
+++ b/app/components/DateRangeIndicator.vue
@@ -0,0 +1,55 @@
+<script lang="ts" setup>
+import { AnimatePresence, motion } from 'motion-v'
+
+const props = defineProps<{
+ dateRange?: string
+ locations?: string
+ isVisible: boolean
+ class?: string
+}>()
+
+const shouldShow = computed(() => {
+ return props.isVisible && !!props.dateRange
+})
+
+const variants = {
+ initial: {
+ opacity: 0,
+ x: -20,
+ scale: 0.95,
+ },
+ animate: {
+ opacity: 1,
+ x: 0,
+ scale: 1,
+ },
+}
+</script>
+
+<template>
+ <div>
+ <AnimatePresence>
+ <motion.div
+ v-if="shouldShow"
+ class="fixed top-4 left-4 z-50 lg:top-6 lg:left-6"
+ :initial="variants.initial"
+ :animate="variants.animate"
+ :exit="variants.initial"
+ :transition="{
+ type: 'spring',
+ duration: 0.4,
+ bounce: 0.15,
+ }"
+ >
+ <div
+ class="flex flex-col gap-1 bg-black/60 backdrop-blur-[70px] rounded-xl border border-white/10 px-4 py-2 shadow-2xl"
+ >
+ <span class="text-white text-4xl font-black">{{ dateRange }}</span>
+ <span class="text-white/60 text-xl font-bold">{{ locations }}</span>
+ </div>
+ </motion.div>
+ </AnimatePresence>
+ </div>
+</template>
+
+<style scoped></style>
diff --git a/app/components/masonry/HeaderItem.vue b/app/components/masonry/HeaderItem.vue
new file mode 100644
index 00000000..208aa5d1
--- /dev/null
+++ b/app/components/masonry/HeaderItem.vue
@@ -0,0 +1,74 @@
+<script lang="ts" setup>
+defineProps<{
+ stats: {
+ total: number
+ withDates: number
+ withTitles: number
+ withExif: number
+ dateRange: {
+ start: Date | undefined
+ end: Date | undefined
+ } | null
+ }
+ dateRangeText?: string
+}>()
+</script>
+
+<template>
+ <div
+ class="inline-block w-full align-top break-inside-avoid mb-4 select-auto"
+ >
+ <div class="text-center py-8">
+ <h1 class="text-4xl font-bold text-gray-900 dark:text-white mb-2">
+ Photo Gallery
+ </h1>
+ <div class="text-gray-600 dark:text-gray-400 space-y-1">
+ <p class="text-lg">{{ stats.total }} 张照片</p>
+ <div class="text-sm space-y-1">
+ <p v-if="dateRangeText">时间跨度: {{ dateRangeText }}</p>
+ <p>
+ {{ stats.withDates }} 张包含拍摄时间 ·
+ {{ stats.withExif }} 张包含 EXIF 数据
+ </p>
+ </div>
+ </div>
+ <AuthState>
+ <template #default="{ loggedIn, user, clear }">
+ <div
+ v-if="loggedIn"
+ class="text-gray-600 dark:text-gray-400 flex flex-col items-center"
+ >
+ Logged in as {{ user?.username || user?.email }}
+ <div class="gap-3">
+ <NuxtLink
+ to="/dashboard"
+ class="ml-2 text-blue-500"
+ >
+ Dashboard
+ </NuxtLink>
+ <button
+ @click="clear"
+ class="ml-2 text-blue-500"
+ >
+ Logout
+ </button>
+ </div>
+ </div>
+ <div v-else>
+ <a
+ href="/api/auth/github"
+ class="text-blue-500"
+ >
+ Login
+ </a>
+ </div>
+ </template>
+ <template #placeholder>
+ <button disabled>Loading...</button>
+ </template>
+ </AuthState>
+ </div>
+ </div>
+</template>
+
+<style scoped></style>
diff --git a/app/components/masonry/Item.vue b/app/components/masonry/Item.vue
new file mode 100644
index 00000000..029f39cf
--- /dev/null
+++ b/app/components/masonry/Item.vue
@@ -0,0 +1,77 @@
+<script lang="ts" setup>
+import { motion } from 'motion-v'
+
+const props = withDefaults(
+ defineProps<{
+ photo: Photo
+ index: number
+ hasAnimated: boolean
+ firstScreenItems?: number
+ }>(),
+ {
+ firstScreenItems: 300,
+ },
+)
+
+const emit = defineEmits<{
+ animationComplete: []
+ 'visibility-change': [
+ { index: number; isVisible: boolean; date: string | Date },
+ ]
+}>()
+
+const itemKey = computed(() => {
+ return props.photo.id
+})
+
+const shouldAnimate = computed(() => {
+ return !props.hasAnimated && props.index < props.firstScreenItems
+})
+
+const animateDelay = computed(() => {
+ return props.index * 0.02
+})
+
+const itemVariants = {
+ hidden: {
+ opacity: 0,
+ y: 30,
+ scale: 0.95,
+ filter: 'blur(6px)',
+ },
+ visible: {
+ opacity: 1,
+ y: 0,
+ scale: 1,
+ filter: 'blur(0px)',
+ transition: {
+ type: 'spring' as const,
+ duration: 0.3,
+ bounce: 0,
+ delay: animateDelay.value,
+ },
+ },
+}
+</script>
+
+<template>
+ <motion.div
+ :key="itemKey"
+ :variants="shouldAnimate ? itemVariants : undefined"
+ :initial="shouldAnimate ? 'hidden' : 'visible'"
+ :animate="'visible'"
+ @animation-complete="
+ () => {
+ if (shouldAnimate) emit('animationComplete')
+ }
+ "
+ >
+ <MasonryPhotoItem
+ :photo="photo"
+ :index="index"
+ @visibility-change="emit('visibility-change', $event)"
+ />
+ </motion.div>
+</template>
+
+<style scoped></style>
diff --git a/app/components/MasonryPhotoItem.client.vue b/app/components/masonry/PhotoItem.client.vue
similarity index 63%
rename from app/components/MasonryPhotoItem.client.vue
rename to app/components/masonry/PhotoItem.client.vue
index 744f1843..25f7a8df 100644
--- a/app/components/MasonryPhotoItem.client.vue
+++ b/app/components/masonry/PhotoItem.client.vue
@@ -1,17 +1,11 @@
<script setup lang="ts">
-import Thumbhash from './Thumbhash.vue'
-
interface Props {
photo: Photo
index: number
- hasAnimated: boolean
- shouldAnimate: boolean
- animationDelay: number
}
const props = defineProps<Props>()
const emit = defineEmits<{
- 'animation-complete': []
'visibility-change': [
{ index: number; isVisible: boolean; date: string | Date },
]
@@ -55,13 +49,6 @@ const handleImageLoad = (event: Event) => {
const aspectRatio = img.naturalHeight / img.naturalWidth
actualHeight.value = Math.round(DEFAULT_WIDTH * aspectRatio)
}
-
- // Trigger animation after image loads
- if (props.shouldAnimate) {
- setTimeout(() => {
- emit('animation-complete')
- }, props.animationDelay * 1000)
- }
}
const handleImageError = () => {
@@ -76,7 +63,7 @@ const checkImageLoaded = (img: HTMLImageElement) => {
const syntheticEvent = new Event('load')
Object.defineProperty(syntheticEvent, 'target', {
value: img,
- enumerable: true
+ enumerable: true,
})
handleImageLoad(syntheticEvent)
}
@@ -145,7 +132,9 @@ onMounted(() => {
img.onload = () => {
if (img.naturalWidth && img.naturalHeight) {
const aspectRatio = img.naturalHeight / img.naturalWidth
- actualHeight.value = Math.round(DEFAULT_WIDTH * (props.photo.aspectRatio || aspectRatio))
+ actualHeight.value = Math.round(
+ DEFAULT_WIDTH * (props.photo.aspectRatio || aspectRatio),
+ )
}
// Update loading state after preload completes
isLoading.value = false
@@ -202,15 +191,9 @@ onMounted(() => {
<template>
<div
ref="photoRef"
- class="masonry-item break-inside-avoid transition-all duration-300"
+ class="inline-block w-full align-top break-inside-avoid transition-all duration-300 cursor-pointer"
:style="{
marginBottom: `${ITEM_GAP}px`,
- opacity: shouldAnimate ? (hasAnimated ? 1 : 0) : 1,
- transform: shouldAnimate
- ? hasAnimated
- ? 'translateY(0) scale(1)'
- : 'translateY(30px) scale(0.95)'
- : 'none',
}"
>
<div
@@ -254,47 +237,116 @@ onMounted(() => {
<!-- Photo info overlay (bottom) -->
<div
- class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent p-4 translate-y-full group-hover:translate-y-0 transition-transform duration-300"
+ class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/60 to-transparent p-3 translate-y-full group-hover:translate-y-0 transition-transform duration-300"
>
- <div class="text-white">
- <p
- v-if="photo.title"
- class="text-sm font-medium mb-1"
- >
- {{ photo.title }}
- </p>
- <p
- v-if="photo.description"
- class="text-xs opacity-80 mb-1"
- >
- {{ photo.description }}
- </p>
- <p
- v-if="photo.dateTaken"
- class="text-xs opacity-80"
- >
- {{ formatDate(photo.dateTaken) }}
- </p>
- <!-- Camera info from EXIF if available -->
- <div
- v-if="photo.exif && (photo.exif.Make || photo.exif.Model)"
- class="text-xs opacity-70 mt-1"
- >
- {{ [photo.exif.Make, photo.exif.Model].filter(Boolean).join(' ') }}
+ <div class="text-white flex flex-col gap-1">
+ <div class="flex flex-col">
+ <p
+ v-if="photo.title"
+ class="text-base font-medium"
+ >
+ {{ photo.title }}
+ </p>
+ <p
+ v-if="photo.description"
+ class="text-xs opacity-80"
+ >
+ {{ photo.description }}
+ </p>
+ <p
+ v-if="photo.dateTaken"
+ class="text-xs opacity-80"
+ >
+ {{ formatDate(photo.dateTaken) }}
+ </p>
</div>
- <!-- Photo specs from EXIF -->
<div
- v-if="
- photo.exif &&
- (photo.exif.FNumber || photo.exif.ExposureTime || photo.exif.ISO)
- "
- class="text-xs opacity-70 mt-1 flex gap-2"
+ v-if="photo.tags?.length"
+ class="opacity-80 mt-1 flex items-center gap-1"
>
- <span v-if="photo.exif.FNumber">f/{{ photo.exif.FNumber }}</span>
- <span v-if="photo.exif.ExposureTime">{{
- formatExposureTime(photo.exif.ExposureTime)
- }}</span>
- <span v-if="photo.exif.ISO">ISO {{ photo.exif.ISO }}</span>
+ <UBadge
+ v-for="tag in photo.tags"
+ :key="tag"
+ size="sm"
+ color="neutral"
+ class="bg-white/20 backdrop-blur-3xl"
+ >
+ {{ tag }}
+ </UBadge>
+ </div>
+ <div>
+ <!-- Camera info from EXIF if available -->
+ <div
+ v-if="photo.exif && (photo.exif.Make || photo.exif.Model)"
+ class="text-sm opacity-70 mt-1 flex items-center gap-0.5"
+ >
+ <Icon
+ name="tabler:camera"
+ class="mt-[1px]"
+ />
+ <span class="text-xs font-medium">
+ {{
+ [photo.exif.Make, photo.exif.Model].filter(Boolean).join(' ')
+ }}
+ </span>
+ </div>
+ <!-- Photo specs from EXIF -->
+ <div
+ v-if="
+ photo.exif &&
+ (photo.exif.FNumber ||
+ photo.exif.ExposureTime ||
+ photo.exif.ISO)
+ "
+ class="text-sm opacity-70 mt-1 flex gap-2"
+ >
+ <div
+ v-if="photo.exif.FocalLength"
+ class="flex items-center gap-0.5"
+ >
+ <Icon
+ name="streamline:image-accessories-lenses-photos-camera-shutter-picture-photography-pictures-photo-lens"
+ class="mt-[1px]"
+ />
+ <span class="text-xs font-medium">{{
+ photo.exif.FocalLengthIn35mmFormat
+ }}</span>
+ </div>
+ <div
+ v-if="photo.exif.FNumber"
+ class="flex items-center gap-0.5"
+ >
+ <Icon
+ name="tabler:aperture"
+ class="mt-[1px]"
+ />
+ <span class="text-xs font-medium"
+ >f/{{ photo.exif.FNumber }}</span
+ >
+ </div>
+ <div
+ v-if="photo.exif.ExposureTime"
+ class="flex items-center gap-0.5"
+ >
+ <Icon
+ name="material-symbols:shutter-speed"
+ class="mt-[1px]"
+ />
+ <span class="text-xs font-medium">
+ {{ formatExposureTime(photo.exif.ExposureTime) }}
+ </span>
+ </div>
+ <div
+ v-if="photo.exif.ISO"
+ class="flex items-center gap-0.5"
+ >
+ <Icon
+ name="carbon:iso-outline"
+ class="mt-[1px]"
+ />
+ <span class="text-xs font-medium">{{ photo.exif.ISO }}</span>
+ </div>
+ </div>
</div>
</div>
</div>
@@ -302,10 +354,4 @@ onMounted(() => {
</div>
</template>
-<style scoped>
-.masonry-item {
- display: inline-block;
- width: 100%;
- vertical-align: top;
-}
-</style>
+<style scoped></style>
diff --git a/app/components/MasonryRoot.vue b/app/components/masonry/Root.vue
similarity index 52%
rename from app/components/MasonryRoot.vue
rename to app/components/masonry/Root.vue
index 3af1480e..bee17e6e 100644
--- a/app/components/MasonryRoot.vue
+++ b/app/components/masonry/Root.vue
@@ -46,47 +46,40 @@ const columnCount = computed(() => {
const photoStats = computed(() => {
const totalPhotos = props.photos.length
- const photosWithDates = props.photos.filter(p => p.dateTaken).length
- const photosWithTitles = props.photos.filter(p => p.title).length
- const photosWithExif = props.photos.filter(p => p.exif).length
-
+ const photosWithDates = props.photos.filter((p) => p.dateTaken).length
+ const photosWithTitles = props.photos.filter((p) => p.title).length
+ const photosWithExif = props.photos.filter((p) => p.exif).length
+
// Get date range of all photos
const allDates = props.photos
- .map(p => p.dateTaken)
+ .map((p) => p.dateTaken)
.filter((date): date is string => Boolean(date))
- .map(date => new Date(date))
+ .map((date) => new Date(date))
.sort((a, b) => a.getTime() - b.getTime())
-
- const dateRange = allDates.length > 0 ? {
- start: allDates[0],
- end: allDates[allDates.length - 1]
- } : null
-
+
+ const dateRange =
+ allDates.length > 0
+ ? {
+ start: allDates[0],
+ end: allDates[allDates.length - 1],
+ }
+ : null
+
return {
total: totalPhotos,
withDates: photosWithDates,
withTitles: photosWithTitles,
withExif: photosWithExif,
- dateRange
+ dateRange,
}
})
const dateRangeText = computed(() => {
- const range = photoStats.value.dateRange
+ const range = photoStats.value?.dateRange
if (!range || !range.start || !range.end) return ''
return `${range.start.toLocaleDateString('zh-CN')} - ${range.end.toLocaleDateString('zh-CN')}`
})
-// Methods
-const calculateAnimationDelay = (index: number): number => {
- if (hasAnimated.value || index >= FIRST_SCREEN_ITEMS_COUNT) return 0
- return Math.min(index * 0.05, 0.3)
-}
-
-const handleAnimationComplete = () => {
- hasAnimated.value = true
-}
-
const handleVisibilityChange = ({
index,
isVisible,
@@ -147,7 +140,10 @@ const updateDateRange = () => {
startDate.getDate() === endDate.getDate()
) {
// Same day
- dateRange.value = startDate.toLocaleDateString('zh-CN', formatOptionsWithDay)
+ dateRange.value = startDate.toLocaleDateString(
+ 'zh-CN',
+ formatOptionsWithDay,
+ )
} else if (
startDate.getFullYear() === endDate.getFullYear() &&
startDate.getMonth() === endDate.getMonth()
@@ -204,48 +200,11 @@ onMounted(() => {
</script>
<template>
- <div class="masonry-container">
- <!-- Date Range Indicator for Desktop -->
- <div
- v-if="!isMobile && dateRange && showFloatingActions"
- class="fixed top-4 left-4 z-50 lg:top-6 lg:left-6"
- >
- <div
- class="flex flex-col gap-1 bg-black/60 backdrop-blur-[70px] rounded-xl border border-white/10 px-4 py-2 shadow-2xl"
- >
- <span class="text-white text-4xl font-black">{{ dateRange }}</span>
- <span class="text-white/60 text-xl font-bold">重庆、新加坡</span>
- </div>
- </div>
-
- <!-- Floating Action Bar for Desktop -->
- <div
- v-if="!isMobile && showFloatingActions"
- class="fixed top-4 right-4 z-50 lg:top-6 lg:right-6"
- >
- <div
- class="bg-black/60 backdrop-blur-[70px] rounded-xl border border-white/10 p-3 shadow-2xl"
- >
- <div class="flex gap-2">
- <button
- class="p-2 rounded-lg bg-white/10 hover:bg-white/20 transition-colors"
- >
- <Icon
- name="mdi:grid"
- class="w-5 h-5 text-white"
- />
- </button>
- <button
- class="p-2 rounded-lg bg-white/10 hover:bg-white/20 transition-colors"
- >
- <Icon
- name="mdi:filter"
- class="w-5 h-5 text-white"
- />
- </button>
- </div>
- </div>
- </div>
+ <div class="relative w-full">
+ <DateRangeIndicator
+ :date-range="dateRange"
+ :is-visible="!!dateRange && showFloatingActions"
+ />
<!-- Mobile Date Range Indicator -->
<div
@@ -279,43 +238,6 @@ onMounted(() => {
class="p-1 lg:px-0 lg:pb-0"
:style="{ userSelect: 'none' }"
>
- <!-- Mobile Header -->
- <div
- v-if="isMobile"
- class="mb-4 text-center"
- >
- <h1 class="text-2xl font-bold text-gray-900 dark:text-white">
- Photo Gallery
- </h1>
- <AuthState>
- <template #default="{ loggedIn, user, clear }">
- <div
- v-if="loggedIn"
- class="text-gray-600 dark:text-gray-400"
- >
- Logged in as {{ user?.username || user?.email }}
- <button
- @click="clear"
- class="ml-2 text-blue-500"
- >
- Logout
- </button>
- </div>
- <div v-else>
- <a
- href="/api/auth/github"
- class="text-blue-500"
- >
- Login
- </a>
- </div>
- </template>
- <template #placeholder>
- <button disabled>Loading...</button>
- </template>
- </AuthState>
- </div>
-
<!-- Masonry Grid -->
<div
ref="masonryContainer"
@@ -325,73 +247,19 @@ onMounted(() => {
columnGap: `${COLUMN_GAP}px`,
}"
>
- <!-- Desktop Header -->
- <div
- v-if="!isMobile"
- class="masonry-item break-inside-avoid mb-4 select-auto"
- :style="{ width: '100%' }"
- >
- <div class="text-center py-8">
- <h1 class="text-4xl font-bold text-gray-900 dark:text-white mb-2">
- Photo Gallery
- </h1>
- <div class="text-gray-600 dark:text-gray-400 space-y-1">
- <p class="text-lg">{{ photoStats.total }} 张照片</p>
- <div class="text-sm space-y-1">
- <p v-if="dateRangeText">
- 时间跨度: {{ dateRangeText }}
- </p>
- <p>{{ photoStats.withDates }} 张包含拍摄时间 · {{ photoStats.withExif }} 张包含 EXIF 数据</p>
- </div>
- </div>
- <AuthState>
- <template #default="{ loggedIn, user, clear }">
- <div
- v-if="loggedIn"
- class="text-gray-600 dark:text-gray-400 flex flex-col items-center"
- >
- Logged in as {{ user?.username || user?.email }}
- <div class="gap-3">
- <NuxtLink
- to="/dashboard"
- class="ml-2 text-blue-500"
- >
- Dashboard
- </NuxtLink>
- <button
- @click="clear"
- class="ml-2 text-blue-500"
- >
- Logout
- </button>
- </div>
- </div>
- <div v-else>
- <a
- href="/api/auth/github"
- class="text-blue-500"
- >
- Login
- </a>
- </div>
- </template>
- <template #placeholder>
- <button disabled>Loading...</button>
- </template>
- </AuthState>
- </div>
- </div>
+ <MasonryHeaderItem
+ :stats="photoStats"
+ :date-range-text
+ />
<!-- Photo Items -->
- <MasonryPhotoItem
+ <MasonryItem
v-for="(photo, index) in photos"
:key="photo.id || index"
:photo="photo"
:index="index"
- :has-animated="hasAnimated"
- :should-animate="!hasAnimated && index < FIRST_SCREEN_ITEMS_COUNT"
- :animation-delay="calculateAnimationDelay(index)"
- @animation-complete="handleAnimationComplete"
+ :hasAnimated
+ :first-screen-items="FIRST_SCREEN_ITEMS_COUNT"
@visibility-change="handleVisibilityChange"
/>
</div>
@@ -409,10 +277,4 @@ onMounted(() => {
width: 100%;
column-fill: balance;
}
-
-.masonry-item {
- display: inline-block;
- width: 100%;
- vertical-align: top;
-}
</style>
diff --git a/app/pages/index.vue b/app/pages/index.vue
index 12b395bc..7db5dc8c 100644
--- a/app/pages/index.vue
+++ b/app/pages/index.vue
@@ -4,9 +4,11 @@ const { data } = useFetch('/api/photos')
<template>
<div class="h-svh p-2 pt-1 bg-gray-50 dark:bg-gray-900">
- <MasonryRoot
- :photos="data as Photo[]"
- :columns="'auto'"
- />
+ <ClientOnly>
+ <MasonryRoot
+ :photos="data as Photo[]"
+ :columns="'auto'"
+ />
+ </ClientOnly>
</div>
</template>