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.

feat(viewer): 重构照片查看器状态管理,添加当前索引和打开状态的支持

Timothy Yin31c16493c994
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index dfa1c676..149a322a 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -8,7 +8,6 @@ const props = withDefaults(defineProps<Props>(), {
columns: 'auto',
})
-const router = useRouter()
const { openViewer } = useViewerState()
// Constants
diff --git a/app/components/photo/Viewer.vue b/app/components/photo/Viewer.vue
index f65d924f..7ca72b10 100644
--- a/app/components/photo/Viewer.vue
+++ b/app/components/photo/Viewer.vue
@@ -15,12 +15,15 @@ import type { LoadingIndicatorRef } from './LoadingIndicator.vue'
interface Props {
photos: Photo[]
+ currentIndex: number
+ isOpen: boolean
}
const props = defineProps<Props>()
-
-const { closeViewer } = useViewerState()
-const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
+const emit = defineEmits<{
+ close: []
+ indexChange: [index: number]
+}>()
// Refs
const containerRef = ref<HTMLDivElement>()
@@ -33,12 +36,12 @@ const showExifPanel = ref(false)
const currentBlobSrc = ref<string | null>(null)
// Computed
-const currentPhoto = computed(() => props.photos[currentPhotoIndex.value])
+const currentPhoto = computed(() => props.photos[props.currentIndex])
const isMobile = useMediaQuery('(max-width: 768px)')
// 当 PhotoViewer 关闭时重置状态
watch(
- () => isViewerOpen,
+ () => props.isOpen,
(isOpen) => {
if (!isOpen) {
isImageZoomed.value = false
@@ -65,7 +68,7 @@ watch(
// 同步 Swiper 的索引
watch(
- () => currentPhotoIndex.value,
+ () => props.currentIndex,
(newIndex) => {
if (swiperRef.value && swiperRef.value.activeIndex !== newIndex) {
swiperRef.value.slideTo(newIndex, 300)
@@ -84,17 +87,15 @@ watch(isImageZoomed, (isZoomed) => {
// Navigation methods
const handlePrevious = () => {
- if (currentPhotoIndex.value > 0) {
- // emit('indexChange', currentPhotoIndex.value - 1)
- currentPhotoIndex.value -= 1
+ if (props.currentIndex > 0) {
+ emit('indexChange', props.currentIndex - 1)
swiperRef.value?.slidePrev()
}
}
const handleNext = () => {
- if (currentPhotoIndex.value < props.photos.length - 1) {
- // emit('indexChange', currentPhotoIndex.value + 1)
- currentPhotoIndex.value += 1
+ if (props.currentIndex < props.photos.length - 1) {
+ emit('indexChange', props.currentIndex + 1)
swiperRef.value?.slideNext()
}
}
@@ -106,8 +107,7 @@ const handleSwiperInit = (swiper: SwiperType) => {
}
const handleSlideChange = (swiper: SwiperType) => {
- // emit('indexChange', swiper.activeIndex)
- currentPhotoIndex.value = swiper.activeIndex
+ emit('indexChange', swiper.activeIndex)
}
// Handle image events
@@ -121,8 +121,7 @@ const handleBlobSrcChange = (blobSrc: string | null) => {
defineShortcuts({
escape: () => {
- // emit('close')
- closeViewer()
+ emit('close')
},
})
@@ -135,20 +134,20 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 背景层 -->
<AnimatePresence>
<motion.div
- v-if="isViewerOpen"
+ v-if="isOpen"
:initial="{ opacity: 0 }"
:animate="{ opacity: 1 }"
:exit="{ opacity: 0 }"
:transition="{ duration: 0.3 }"
class="fixed inset-0 bg-white/50 dark:bg-black/50 backdrop-blur-sm z-50"
- @click="closeViewer"
+ @click="emit('close')"
/>
</AnimatePresence>
<!-- 交叉溶解的 Thumbhash 背景 -->
<AnimatePresence mode="sync">
<motion.div
- v-if="isViewerOpen && currentPhoto?.thumbnailHash"
+ v-if="isOpen && currentPhoto?.thumbnailHash"
:key="currentPhoto.id"
:initial="{ opacity: 0 }"
:animate="{ opacity: 1 }"
@@ -166,7 +165,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 主内容区域 -->
<AnimatePresence>
<motion.div
- v-if="isViewerOpen"
+ v-if="isOpen"
:initial="{ opacity: 0 }"
:animate="{ opacity: 1 }"
:exit="{ opacity: 0 }"
@@ -174,7 +173,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
ref="containerRef"
class="fixed inset-0 z-50 flex items-center justify-center"
:style="{ touchAction: isMobile ? 'manipulation' : 'none' }"
- @click.self="closeViewer"
+ @click.self="emit('close')"
>
<div
class="flex w-full h-full"
@@ -218,7 +217,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<button
type="button"
class="pointer-events-auto flex size-8 items-center justify-center rounded-full text-white backdrop-blur-xl duration-200 bg-black/30 hover:bg-black/40"
- @click="closeViewer"
+ @click="emit('close')"
>
<Icon name="tabler:x" />
</button>
@@ -233,7 +232,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
:modules="swiperModules"
:space-between="0"
:slides-per-view="1"
- :initial-slide="currentPhotoIndex"
+ :initial-slide="currentIndex"
:virtual="true"
:keyboard="{
enabled: true,
@@ -260,35 +259,31 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<ProgressiveImage
class="h-full w-full object-contain"
:loading-indicator-ref="loadingIndicatorRef || null"
- :is-current-image="index === currentPhotoIndex"
+ :is-current-image="index === currentIndex"
:src="photo.originalUrl!"
:thumbnail-src="photo.thumbnailUrl!"
:alt="photo.title || ''"
:width="
- index === currentPhotoIndex
+ index === currentIndex
? (currentPhoto?.width ?? undefined)
: undefined
"
:height="
- index === currentPhotoIndex
+ index === currentIndex
? (currentPhoto?.height ?? undefined)
: undefined
"
:enable-pan="
- index === currentPhotoIndex
+ index === currentIndex
? !isMobile || isImageZoomed
: true
"
:enable-zoom="true"
:on-zoom-change="
- index === currentPhotoIndex
- ? handleZoomChange
- : undefined
+ index === currentIndex ? handleZoomChange : undefined
"
:on-blob-src-change="
- index === currentPhotoIndex
- ? handleBlobSrcChange
- : undefined
+ index === currentIndex ? handleBlobSrcChange : undefined
"
/>
</motion.div>
@@ -298,7 +293,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 自定义导航按钮 (桌面端) -->
<template v-if="!isMobile">
<button
- v-if="currentPhotoIndex > 0"
+ v-if="currentIndex > 0"
type="button"
class="absolute top-1/2 left-4 z-20 flex size-8 -translate-y-1/2 items-center justify-center rounded-full text-white opacity-0 backdrop-blur-sm duration-200 group-hover:opacity-100 bg-black/30 hover:bg-black/40"
@click="handlePrevious"
@@ -310,7 +305,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
</button>
<button
- v-if="currentPhotoIndex < photos.length - 1"
+ v-if="currentIndex < photos.length - 1"
type="button"
class="absolute top-1/2 right-4 z-20 flex size-8 -translate-y-1/2 items-center justify-center rounded-full text-white opacity-0 backdrop-blur-sm duration-200 group-hover:opacity-100 bg-black/30 hover:bg-black/40"
@click="handleNext"
@@ -326,9 +321,9 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 缩略图导航 -->
<Suspense>
<GalleryThumbnail
- :current-index="currentPhotoIndex"
+ :current-index="currentIndex"
:photos="photos"
- @index-change="currentPhotoIndex = $event"
+ @index-change="emit('indexChange', $event)"
/>
</Suspense>
</div>
diff --git a/app/layouts/default.vue b/app/layouts/default.vue
index 596c0cab..76a65712 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/default.vue
@@ -1,13 +1,32 @@
<script lang="ts" setup>
-
+const { data } = useFetch('/api/photos')
+const photos = computed(() => (data.value as Photo[]) || [])
</script>
<template>
<div class="relative h-screen">
- <slot />
+ <div class="h-svh px-1">
+ <ClientOnly>
+ <MasonryRoot
+ :photos="photos"
+ columns="auto"
+ />
+ <slot />
+ <template #fallback>
+ <div
+ class="fixed inset-0 flex flex-col gap-4 items-center justify-center"
+ >
+ <LoaderAvatar />
+ <span class="loading-scan-wrapper">
+ <span class="text-base font-medium loading-scan-text"
+ >LOADING</span
+ >
+ </span>
+ </div>
+ </template>
+ </ClientOnly>
+ </div>
</div>
</template>
-<style scoped>
-
-</style>
+<style scoped></style>
diff --git a/app/pages/index.vue b/app/pages/index.vue
index 2f20c24c..fec4e7d0 100644
--- a/app/pages/index.vue
+++ b/app/pages/index.vue
@@ -1,50 +1,24 @@
<script lang="ts" setup>
-const { data } = useFetch('/api/photos')
-
-const route = useRoute()
-const router = useRouter()
-const { openViewer } = useViewerState()
+const { switchToIndex, closeViewer } = useViewerState()
+const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
useHead({
title: 'Gallery',
titleTemplate: (title) => `${title} - TimoYin's mems`,
})
+const { data } = useFetch('/api/photos')
const photos = computed(() => (data.value as Photo[]) || [])
-
-// watch(
-// () => route.params.photoId,
-// (newPhotoId) => {
-// if (newPhotoId) {
-// const photo = photos.value.find((p) => p.id === newPhotoId)
-// if (photo) {
-// openViewer(photos.value.indexOf(photo))
-// }
-// }
-// },
-// )
</script>
<template>
- <div class="h-svh px-1">
- <ClientOnly>
- <MasonryRoot
- :photos="photos"
- columns="auto"
- />
- <PhotoViewer :photos />
- <template #fallback>
- <div
- class="fixed inset-0 flex flex-col gap-4 items-center justify-center"
- >
- <LoaderAvatar />
- <span class="loading-scan-wrapper">
- <span class="text-base font-medium loading-scan-text">LOADING</span>
- </span>
- </div>
- </template>
- </ClientOnly>
- </div>
+ <PhotoViewer
+ :photos="photos"
+ :current-index="currentPhotoIndex"
+ :is-open="isViewerOpen"
+ @close="closeViewer"
+ @index-change="switchToIndex"
+ />
</template>
<style scoped>
diff --git a/app/stores/viewer.ts b/app/stores/viewer.ts
index c796cfdd..99675c40 100644
--- a/app/stores/viewer.ts
+++ b/app/stores/viewer.ts
@@ -7,6 +7,10 @@ export const useViewerState = defineStore('photo-viewer-state', () => {
isViewerOpen.value = true
}
+ const switchToIndex = (index: number) => {
+ currentPhotoIndex.value = index
+ }
+
const closeViewer = () => {
isViewerOpen.value = false
}
@@ -15,6 +19,7 @@ export const useViewerState = defineStore('photo-viewer-state', () => {
currentPhotoIndex,
isViewerOpen,
openViewer,
+ switchToIndex,
closeViewer,
}
-})
\ No newline at end of file
+})