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(viewer): enhance photo viewer functionality with direct access handling

Timothy Yin84e9f2f760f5
diff --git a/app/app.vue b/app/app.vue
index b0bc23d2..fc27911f 100644
--- a/app/app.vue
+++ b/app/app.vue
@@ -2,9 +2,11 @@
import dayjsLocale_zhCN from 'dayjs/locale/zh-cn'
import dayjsLocale_zhTW from 'dayjs/locale/zh-tw'
import dayjsLocale_zhHK from 'dayjs/locale/zh-hk'
+
+const router = useRouter()
const config = useRuntimeConfig()
-const { locale } = useI18n()
const dayjs = useDayjs()
+const { locale } = useI18n()
useHead({
titleTemplate: (title) =>
@@ -14,6 +16,37 @@ useHead({
const { data, refresh, status } = useFetch('/api/photos')
const photos = computed(() => (data.value as Photo[]) || [])
+const { switchToIndex, closeViewer, clearReturnRoute } = useViewerState()
+const { currentPhotoIndex, isViewerOpen, returnRoute, isDirectAccess } =
+ storeToRefs(useViewerState())
+
+const handleIndexChange = (newIndex: number) => {
+ switchToIndex(newIndex)
+ router.replace(`/${photos.value[newIndex]?.id}`)
+}
+
+const handleClose = () => {
+ closeViewer()
+
+ // 如果是直接访问详情页面,关闭时返回首页
+ if (isDirectAccess.value) {
+ isDirectAccess.value = false
+ router.replace('/')
+ } else if (returnRoute.value) {
+ // 如果有指定的返回路由,返回到该路由
+ const destination = returnRoute.value
+ clearReturnRoute()
+ router.replace(destination)
+ } else {
+ // 否则使用历史记录或默认返回首页
+ if (window.history.length > 1) {
+ router.back()
+ } else {
+ router.replace('/')
+ }
+ }
+}
+
watchEffect(() => {
dayjs.locale('zh-Hans', dayjsLocale_zhCN)
dayjs.locale('zh-Hant-TW', dayjsLocale_zhTW)
@@ -46,6 +79,15 @@ provide(
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
+ <ClientOnly>
+ <PhotoViewer
+ :photos="photos"
+ :current-index="currentPhotoIndex"
+ :is-open="isViewerOpen"
+ @close="handleClose"
+ @index-change="handleIndexChange"
+ />
+ </ClientOnly>
</PhotosProvider>
</UApp>
</template>
diff --git a/app/pages/[...slug].vue b/app/pages/[...slug].vue
index 891758ad..81e3684f 100644
--- a/app/pages/[...slug].vue
+++ b/app/pages/[...slug].vue
@@ -9,8 +9,8 @@ const route = useRoute()
const router = useRouter()
const config = useRuntimeConfig()
-const { switchToIndex, closeViewer, openViewer, clearReturnRoute } = useViewerState()
-const { currentPhotoIndex, isViewerOpen, returnRoute } = storeToRefs(useViewerState())
+const { switchToIndex, closeViewer, openViewer } = useViewerState()
+const { isViewerOpen } = storeToRefs(useViewerState())
const { photos } = usePhotos()
@@ -62,7 +62,8 @@ watch(
title: currentPhotos[foundIndex]?.title || $t('title.fallback.photo'),
})
if (!isViewerOpen.value) {
- openViewer(foundIndex)
+ // 直接访问照片详情页时,不设置 returnRoute(传入 null)
+ openViewer(foundIndex, null)
} else {
switchToIndex(foundIndex)
}
@@ -76,39 +77,10 @@ watch(
},
{ immediate: true },
)
-
-const handleIndexChange = (newIndex: number) => {
- switchToIndex(newIndex)
- router.replace(`/${photos.value[newIndex]?.id}`)
-}
-
-const handleClose = () => {
- closeViewer()
-
- if (returnRoute.value) {
- const destination = returnRoute.value
- clearReturnRoute()
- router.replace(destination)
- } else {
- if (window.history.length > 1) {
- router.back()
- } else {
- router.replace('/')
- }
- }
-}
</script>
<template>
- <ClientOnly>
- <PhotoViewer
- :photos="photos"
- :current-index="currentPhotoIndex"
- :is-open="isViewerOpen"
- @close="handleClose"
- @index-change="handleIndexChange"
- />
- </ClientOnly>
+ <div />
</template>
<style scoped></style>
diff --git a/app/stores/viewer.ts b/app/stores/viewer.ts
index dd47c2af..64cde15a 100644
--- a/app/stores/viewer.ts
+++ b/app/stores/viewer.ts
@@ -2,12 +2,16 @@ export const useViewerState = defineStore('photo-viewer-state', () => {
const currentPhotoIndex = ref(0)
const isViewerOpen = ref(false)
const returnRoute = ref<string | null>(null)
+ const isDirectAccess = ref(false)
const openViewer = (index: number, route?: string | null) => {
currentPhotoIndex.value = index
isViewerOpen.value = true
if (route) {
returnRoute.value = route
+ isDirectAccess.value = false
+ } else {
+ isDirectAccess.value = true
}
}
@@ -27,6 +31,7 @@ export const useViewerState = defineStore('photo-viewer-state', () => {
currentPhotoIndex,
isViewerOpen,
returnRoute,
+ isDirectAccess,
openViewer,
switchToIndex,
closeViewer,