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): 添加照片查看器 catch-all 路由,优化照片索引切换逻辑

Timothy Yin60e7a3ce2900
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index b3fd1347..b22b3d92 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -9,7 +9,7 @@ const props = withDefaults(defineProps<Props>(), {
})
const dayjs = useDayjs()
-const { openViewer } = useViewerState()
+const router = useRouter()
// Constants
const FIRST_SCREEN_ITEMS_COUNT = 30
@@ -107,7 +107,7 @@ const updateDateRange = () => {
}
const visiblePhotosArray = Array.from(visiblePhotos.value)
-
+
// Calculate visible dates
const visibleDates = visiblePhotosArray
.map((index) => props.photos[index]?.dateTaken)
@@ -119,9 +119,9 @@ const updateDateRange = () => {
const cities = visiblePhotosArray
.map((index) => props.photos[index]?.city)
.filter((city): city is string => Boolean(city))
-
+
const uniqueCities = [...new Set(cities)]
-
+
if (uniqueCities.length === 0) {
visibleCities.value = undefined
} else if (uniqueCities.length === 1) {
@@ -196,8 +196,7 @@ onMounted(() => {
})
const handleOpenViewer = (index: number) => {
- openViewer(index)
- // router.replace(`/${props.photos[index]?.id}`)
+ router.push(`/${props.photos[index]?.id}`)
}
</script>
diff --git a/app/components/photo/Viewer.vue b/app/components/photo/Viewer.vue
index 03b94b55..f9035927 100644
--- a/app/components/photo/Viewer.vue
+++ b/app/components/photo/Viewer.vue
@@ -61,16 +61,15 @@ watch(
} else {
document.body.style.overflow = 'hidden'
}
-
- if (isOpen && currentPhoto.value) {
- useHead({
- title: currentPhoto.value.title,
- })
- } else {
- useHead({
- title: 'Gallery',
- })
- }
+ // if (isOpen && currentPhoto.value) {
+ // useHead({
+ // title: currentPhoto.value.title,
+ // })
+ // } else {
+ // useHead({
+ // title: 'Gallery',
+ // })
+ // }
},
{ immediate: true },
)
diff --git a/app/pages/[...slug].vue b/app/pages/[...slug].vue
new file mode 100644
index 00000000..18b5b670
--- /dev/null
+++ b/app/pages/[...slug].vue
@@ -0,0 +1,99 @@
+<script lang="ts" setup>
+definePageMeta({
+ // 固定 key 防止路径参数变化时创建新的实例
+ key: 'photo-viewer-route',
+})
+
+const route = useRoute()
+const router = useRouter()
+
+const { switchToIndex, closeViewer, openViewer } = useViewerState()
+const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
+
+const { data } = useFetch('/api/photos')
+const photos = computed(() => (data.value as Photo[]) || [])
+
+const slug = computed(() => (route.params.slug as string[]) || [])
+const photoId = computed(() => slug.value[0] || null)
+
+watch(
+ [photoId, photos],
+ ([currentPhotoId, currentPhotos]) => {
+ if (currentPhotoId && currentPhotos.length > 0) {
+ const foundIndex = currentPhotos.findIndex(
+ (photo) => photo.id === currentPhotoId,
+ )
+ if (foundIndex !== -1) {
+ useHead({
+ title: currentPhotos[foundIndex]?.title || 'Photo',
+ })
+ if (!isViewerOpen.value) {
+ openViewer(foundIndex)
+ } else {
+ switchToIndex(foundIndex)
+ }
+ }
+ } else if (!currentPhotoId) {
+ closeViewer()
+ useHead({
+ title: 'Gallery',
+ })
+ }
+ },
+ { immediate: true },
+)
+
+const handleIndexChange = (newIndex: number) => {
+ switchToIndex(newIndex)
+ router.replace(`/${photos.value[newIndex]?.id}`)
+}
+
+const handleClose = () => {
+ closeViewer()
+ router.replace('/')
+}
+</script>
+
+<template>
+ <PhotoViewer
+ :photos="photos"
+ :current-index="currentPhotoIndex"
+ :is-open="isViewerOpen"
+ @close="handleClose"
+ @index-change="handleIndexChange"
+ />
+</template>
+
+<style scoped>
+.loading-scan-wrapper {
+ display: inline-block;
+ position: relative;
+}
+
+.loading-scan-text {
+ display: inline-block;
+ background: linear-gradient(
+ 90deg,
+ 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;
+ -webkit-background-clip: text;
+ -webkit-text-fill-color: transparent;
+ background-clip: text;
+ color: transparent;
+ animation: scan-x-text 1.2s linear infinite;
+}
+@keyframes scan-x-text {
+ 0% {
+ background-position: 200% 0;
+ }
+ 100% {
+ background-position: -200% 0;
+ }
+}
+</style>
diff --git a/app/pages/index.vue b/app/pages/index.vue
deleted file mode 100644
index cc7ef847..00000000
--- a/app/pages/index.vue
+++ /dev/null
@@ -1,51 +0,0 @@
-<script lang="ts" setup>
-const { switchToIndex, closeViewer } = useViewerState()
-const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
-
-const { data } = useFetch('/api/photos')
-const photos = computed(() => (data.value as Photo[]) || [])
-</script>
-
-<template>
- <PhotoViewer
- :photos="photos"
- :current-index="currentPhotoIndex"
- :is-open="isViewerOpen"
- @close="closeViewer"
- @index-change="switchToIndex"
- />
-</template>
-
-<style scoped>
-.loading-scan-wrapper {
- display: inline-block;
- position: relative;
-}
-
-.loading-scan-text {
- display: inline-block;
- background: linear-gradient(
- 90deg,
- 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;
- -webkit-background-clip: text;
- -webkit-text-fill-color: transparent;
- background-clip: text;
- color: transparent;
- animation: scan-x-text 1.2s linear infinite;
-}
-@keyframes scan-x-text {
- 0% {
- background-position: 200% 0;
- }
- 100% {
- background-position: -200% 0;
- }
-}
-</style>