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 Yin9d779f2cec88
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index acc1157c..dfa1c676 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -8,6 +8,9 @@ const props = withDefaults(defineProps<Props>(), {
columns: 'auto',
})
+const router = useRouter()
+const { openViewer } = useViewerState()
+
// Constants
const FIRST_SCREEN_ITEMS_COUNT = 30
const COLUMN_GAP = 4
@@ -49,11 +52,12 @@ const photoStats = computed(() => {
const photosWithExif = props.photos?.filter((p) => p.exif).length || 0
// Get date range of all photos
- const allDates = props.photos
- ?.map((p) => p?.dateTaken)
- .filter((date): date is string => Boolean(date))
- .map((date) => new Date(date))
- .sort((a, b) => a.getTime() - b.getTime()) || []
+ const allDates =
+ props.photos
+ ?.map((p) => p?.dateTaken)
+ .filter((date): date is string => Boolean(date))
+ .map((date) => new Date(date))
+ .sort((a, b) => a.getTime() - b.getTime()) || []
const dateRange =
allDates.length > 0
@@ -196,8 +200,10 @@ onMounted(() => {
})
})
-const viewerIndex = ref(0)
-const isViewerOpen = ref(false)
+const handleOpenViewer = (index: number) => {
+ openViewer(index)
+ // router.replace(`/${props.photos[index]?.id}`)
+}
</script>
<template>
@@ -249,23 +255,10 @@ const isViewerOpen = ref(false)
:hasAnimated
:first-screen-items="FIRST_SCREEN_ITEMS_COUNT"
@visibility-change="handleVisibilityChange"
- @open-viewer="
- (idx) => {
- viewerIndex = idx
- isViewerOpen = true
- }
- "
+ @open-viewer="handleOpenViewer($event)"
/>
</div>
</div>
-
- <PhotoViewer
- :photos="photos"
- :current-index="viewerIndex"
- :is-open="isViewerOpen"
- @close="isViewerOpen = false"
- @index-change="viewerIndex = $event"
- />
</div>
</template>
diff --git a/app/components/photo/Viewer.vue b/app/components/photo/Viewer.vue
index 58d22636..f65d924f 100644
--- a/app/components/photo/Viewer.vue
+++ b/app/components/photo/Viewer.vue
@@ -15,18 +15,12 @@ import type { LoadingIndicatorRef } from './LoadingIndicator.vue'
interface Props {
photos: Photo[]
- currentIndex: number
- isOpen: boolean
}
const props = defineProps<Props>()
-const emit = defineEmits<{
- close: []
- indexChange: [index: number]
-}>()
-
-const router = useRouter()
+const { closeViewer } = useViewerState()
+const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
// Refs
const containerRef = ref<HTMLDivElement>()
@@ -39,12 +33,12 @@ const showExifPanel = ref(false)
const currentBlobSrc = ref<string | null>(null)
// Computed
-const currentPhoto = computed(() => props.photos[props.currentIndex])
+const currentPhoto = computed(() => props.photos[currentPhotoIndex.value])
const isMobile = useMediaQuery('(max-width: 768px)')
// 当 PhotoViewer 关闭时重置状态
watch(
- () => props.isOpen,
+ () => isViewerOpen,
(isOpen) => {
if (!isOpen) {
isImageZoomed.value = false
@@ -71,7 +65,7 @@ watch(
// 同步 Swiper 的索引
watch(
- () => props.currentIndex,
+ () => currentPhotoIndex.value,
(newIndex) => {
if (swiperRef.value && swiperRef.value.activeIndex !== newIndex) {
swiperRef.value.slideTo(newIndex, 300)
@@ -90,15 +84,17 @@ watch(isImageZoomed, (isZoomed) => {
// Navigation methods
const handlePrevious = () => {
- if (props.currentIndex > 0) {
- emit('indexChange', props.currentIndex - 1)
+ if (currentPhotoIndex.value > 0) {
+ // emit('indexChange', currentPhotoIndex.value - 1)
+ currentPhotoIndex.value -= 1
swiperRef.value?.slidePrev()
}
}
const handleNext = () => {
- if (props.currentIndex < props.photos.length - 1) {
- emit('indexChange', props.currentIndex + 1)
+ if (currentPhotoIndex.value < props.photos.length - 1) {
+ // emit('indexChange', currentPhotoIndex.value + 1)
+ currentPhotoIndex.value += 1
swiperRef.value?.slideNext()
}
}
@@ -110,7 +106,8 @@ const handleSwiperInit = (swiper: SwiperType) => {
}
const handleSlideChange = (swiper: SwiperType) => {
- emit('indexChange', swiper.activeIndex)
+ // emit('indexChange', swiper.activeIndex)
+ currentPhotoIndex.value = swiper.activeIndex
}
// Handle image events
@@ -124,7 +121,8 @@ const handleBlobSrcChange = (blobSrc: string | null) => {
defineShortcuts({
escape: () => {
- emit('close')
+ // emit('close')
+ closeViewer()
},
})
@@ -137,20 +135,20 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 背景层 -->
<AnimatePresence>
<motion.div
- v-if="isOpen"
+ v-if="isViewerOpen"
: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="emit('close')"
+ @click="closeViewer"
/>
</AnimatePresence>
<!-- 交叉溶解的 Thumbhash 背景 -->
<AnimatePresence mode="sync">
<motion.div
- v-if="isOpen && currentPhoto?.thumbnailHash"
+ v-if="isViewerOpen && currentPhoto?.thumbnailHash"
:key="currentPhoto.id"
:initial="{ opacity: 0 }"
:animate="{ opacity: 1 }"
@@ -168,7 +166,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 主内容区域 -->
<AnimatePresence>
<motion.div
- v-if="isOpen"
+ v-if="isViewerOpen"
:initial="{ opacity: 0 }"
:animate="{ opacity: 1 }"
:exit="{ opacity: 0 }"
@@ -176,7 +174,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="emit('close')"
+ @click.self="closeViewer"
>
<div
class="flex w-full h-full"
@@ -220,7 +218,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="emit('close')"
+ @click="closeViewer"
>
<Icon name="tabler:x" />
</button>
@@ -235,7 +233,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
:modules="swiperModules"
:space-between="0"
:slides-per-view="1"
- :initial-slide="currentIndex"
+ :initial-slide="currentPhotoIndex"
:virtual="true"
:keyboard="{
enabled: true,
@@ -262,31 +260,35 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<ProgressiveImage
class="h-full w-full object-contain"
:loading-indicator-ref="loadingIndicatorRef || null"
- :is-current-image="index === currentIndex"
+ :is-current-image="index === currentPhotoIndex"
:src="photo.originalUrl!"
:thumbnail-src="photo.thumbnailUrl!"
:alt="photo.title || ''"
:width="
- index === currentIndex
+ index === currentPhotoIndex
? (currentPhoto?.width ?? undefined)
: undefined
"
:height="
- index === currentIndex
+ index === currentPhotoIndex
? (currentPhoto?.height ?? undefined)
: undefined
"
:enable-pan="
- index === currentIndex
+ index === currentPhotoIndex
? !isMobile || isImageZoomed
: true
"
:enable-zoom="true"
:on-zoom-change="
- index === currentIndex ? handleZoomChange : undefined
+ index === currentPhotoIndex
+ ? handleZoomChange
+ : undefined
"
:on-blob-src-change="
- index === currentIndex ? handleBlobSrcChange : undefined
+ index === currentPhotoIndex
+ ? handleBlobSrcChange
+ : undefined
"
/>
</motion.div>
@@ -296,7 +298,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 自定义导航按钮 (桌面端) -->
<template v-if="!isMobile">
<button
- v-if="currentIndex > 0"
+ v-if="currentPhotoIndex > 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"
@@ -308,7 +310,7 @@ const swiperModules = [Navigation, Keyboard, Virtual]
</button>
<button
- v-if="currentIndex < photos.length - 1"
+ v-if="currentPhotoIndex < 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"
@@ -324,9 +326,9 @@ const swiperModules = [Navigation, Keyboard, Virtual]
<!-- 缩略图导航 -->
<Suspense>
<GalleryThumbnail
- :current-index="currentIndex"
+ :current-index="currentPhotoIndex"
:photos="photos"
- @index-change="(index: number) => emit('indexChange', index)"
+ @index-change="currentPhotoIndex = $event"
/>
</Suspense>
</div>
diff --git a/app/pages/index.vue b/app/pages/index.vue
index 0c071555..2f20c24c 100644
--- a/app/pages/index.vue
+++ b/app/pages/index.vue
@@ -1,12 +1,28 @@
<script lang="ts" setup>
const { data } = useFetch('/api/photos')
+const route = useRoute()
+const router = useRouter()
+const { openViewer } = useViewerState()
+
useHead({
title: 'Gallery',
titleTemplate: (title) => `${title} - TimoYin's mems`,
})
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>
@@ -16,6 +32,7 @@ const photos = computed(() => (data.value as Photo[]) || [])
:photos="photos"
columns="auto"
/>
+ <PhotoViewer :photos />
<template #fallback>
<div
class="fixed inset-0 flex flex-col gap-4 items-center justify-center"
diff --git a/app/stores/viewer.ts b/app/stores/viewer.ts
new file mode 100644
index 00000000..c796cfdd
--- /dev/null
+++ b/app/stores/viewer.ts
@@ -0,0 +1,20 @@
+export const useViewerState = defineStore('photo-viewer-state', () => {
+ const currentPhotoIndex = ref(0)
+ const isViewerOpen = ref(false)
+
+ const openViewer = (index: number) => {
+ currentPhotoIndex.value = index
+ isViewerOpen.value = true
+ }
+
+ const closeViewer = () => {
+ isViewerOpen.value = false
+ }
+
+ return {
+ currentPhotoIndex,
+ isViewerOpen,
+ openViewer,
+ closeViewer,
+ }
+})
\ No newline at end of file
diff --git a/nuxt.config.ts b/nuxt.config.ts
index d8fbbc53..ac0235a7 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -12,6 +12,7 @@ export default defineNuxtConfig({
'@nuxt/icon',
'@nuxt/image',
'@nuxt/test-utils',
+ '@pinia/nuxt',
'motion-v/nuxt',
'nuxt-auth-utils',
'@vueuse/nuxt',
diff --git a/package.json b/package.json
index 2abafc48..5e44e780 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"@nuxt/image": "1.11.0",
"@nuxt/ui": "^3.3.2",
"@nuxthub/core": "0.9.0",
+ "@pinia/nuxt": "^0.11.2",
"@tailwindcss/vite": "^4.1.12",
"@vingle/bmp-js": "^0.2.5",
"@vueuse/nuxt": "13.8.0",
@@ -47,6 +48,7 @@
"nuxt": "^4.0.3",
"nuxt-auth-utils": "0.5.23",
"nuxt-mapbox": "1.6.3",
+ "pinia": "^3.0.3",
"sharp": "^0.34.3",
"swiper": "^11.2.10",
"tailwind-merge": "^3.3.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 68dd47f4..e43e0a73 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -32,6 +32,9 @@ importers:
'@nuxthub/core':
specifier: 0.9.0
+ '@pinia/nuxt':
+ specifier: ^0.11.2
'@tailwindcss/vite':
specifier: ^4.1.12
@@ -86,6 +89,9 @@ importers:
nuxt-mapbox:
specifier: 1.6.3
+ pinia:
+ specifier: ^3.0.3
sharp:
specifier: ^0.34.3
version: 0.34.3
@@ -2285,6 +2291,11 @@ packages:
'@photostructure/[email protected]':
resolution: {integrity: sha512-DwrvodcXHNSdGdeSF7SBL5o8aBlsaeuCuG7633F04nYsL3hn5Hxe3z/5kCqxv61J1q7ggKZ27GPylR3x0cPNXQ==}
+ '@pinia/[email protected]':
+ resolution: {integrity: sha512-CgvSWpbktxxWBV7ModhAcsExsQZqpPq6vMYEe9DexmmY6959ev8ukL4iFhr/qov2Nb9cQAWd7niFDnaWkN+FHg==}
+ peerDependencies:
+ pinia: ^3.0.3
+
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
engines: {node: '>=14'}
@@ -3276,6 +3287,9 @@ packages:
resolution: {integrity: sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==}
+ resolution: {integrity: sha512-lwOnNBH2e7x1fIIbVT7yF5D+YWhqELm55/4ZKf45R9T8r9dE2AIOy8HKjfqzGsoTHFbWbr337O4E0A0QADnjBg==}
+
resolution: {integrity: sha512-9z9TLbfC+AjAi1PQyWX+OErjIaJmdFlbDHcD+cAMYKY6Bh5VlsAtCeGyRMrXwIlMEQPukvnWt3gZBLwTAIMKzQ==}
peerDependencies:
@@ -6178,6 +6192,15 @@ packages:
resolution: {integrity: sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==}
engines: {node: '>=12'}
+ resolution: {integrity: sha512-ttXO/InUULUXkMHpTdp9Fj4hLpD/2AoJdmAbAeW2yu1iy1k+pkFekQXw5VpC0/5p51IOR/jDaDRfRWRnMMsGOA==}
+ peerDependencies:
+ typescript: '>=4.4.4'
+ vue: ^2.7.0 || ^3.5.11
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==}
@@ -10350,6 +10373,13 @@ snapshots:
'@photostructure/[email protected]': {}
+ dependencies:
+ '@nuxt/kit': 3.18.1([email protected])
+ transitivePeerDependencies:
+ - magicast
+
optional: true
@@ -11505,6 +11535,10 @@ snapshots:
'@vue/[email protected]': {}
+ dependencies:
+ '@vue/devtools-kit': 7.7.7
+
dependencies:
'@vue/devtools-kit': 7.7.7
@@ -14728,6 +14762,13 @@ snapshots:
+ dependencies:
+ '@vue/devtools-api': 7.7.7
+ vue: 3.5.19([email protected])
+ optionalDependencies:
+ typescript: 5.9.2
+
dependencies:
confbox: 0.1.8