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 Yin657fb96954b7
diff --git a/app/components/masonry/Item.vue b/app/components/masonry/Item.vue
index fd1909b6..ff61c098 100644
--- a/app/components/masonry/Item.vue
+++ b/app/components/masonry/Item.vue
@@ -58,6 +58,7 @@ const itemVariants = {
<template>
<motion.div
:key="itemKey"
+ :data-photo-id="photo.id"
:variants="shouldAnimate ? itemVariants : undefined"
:initial="shouldAnimate ? 'hidden' : 'visible'"
:animate="'visible'"
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index cdbd34c6..9bc87835 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -20,6 +20,9 @@ const displayPhotos = computed(() => {
return hasActiveFilters.value ? filteredPhotos.value : sortedPhotos.value
})
+// Viewer state for scroll sync
+const { currentPhotoIndex, isViewerOpen } = storeToRefs(useViewerState())
+
// Constants
const FIRST_SCREEN_ITEMS_COUNT = 42
const COLUMN_GAP = 4
@@ -61,8 +64,10 @@ const columnCount = computed(() => {
const photoStats = computed(() => {
const totalPhotos = displayPhotos.value?.length || 0
- const photosWithDates = displayPhotos.value?.filter((p) => p.dateTaken).length || 0
- const photosWithTitles = displayPhotos.value?.filter((p) => p.title).length || 0
+ const photosWithDates =
+ displayPhotos.value?.filter((p) => p.dateTaken).length || 0
+ const photosWithTitles =
+ displayPhotos.value?.filter((p) => p.title).length || 0
const photosWithExif = displayPhotos.value?.filter((p) => p.exif).length || 0
// Get date range of all photos
@@ -109,7 +114,7 @@ const handleVisibilityChange = ({
visiblePhotos.value.delete(index)
}
updateDateRange()
-
+
// Process LivePhotos for visible photos
nextTick(() => {
processVisibleLivePhotos()
@@ -120,26 +125,29 @@ const handleVisibilityChange = ({
const processVisibleLivePhotos = async () => {
const visiblePhotosArray = Array.from(visiblePhotos.value)
const livePhotosToProcess = visiblePhotosArray
- .map(index => displayPhotos.value[index])
- .filter((photo): photo is Photo =>
- photo != null &&
- photo.isLivePhoto === 1 &&
- Boolean(photo.livePhotoVideoUrl) &&
- !processedBatch.value.has(photo.id)
+ .map((index) => displayPhotos.value[index])
+ .filter(
+ (photo): photo is Photo =>
+ photo != null &&
+ photo.isLivePhoto === 1 &&
+ Boolean(photo.livePhotoVideoUrl) &&
+ !processedBatch.value.has(photo.id),
)
-
+
if (livePhotosToProcess.length === 0) return
-
+
// Mark as processed to avoid reprocessing
- livePhotosToProcess.forEach(photo => {
+ livePhotosToProcess.forEach((photo) => {
processedBatch.value.add(photo.id)
})
-
+
// Start background processing
- batchProcessLivePhotos(livePhotosToProcess.map(photo => ({
- id: photo.id,
- livePhotoVideoUrl: photo.livePhotoVideoUrl!
- })))
+ batchProcessLivePhotos(
+ livePhotosToProcess.map((photo) => ({
+ id: photo.id,
+ livePhotoVideoUrl: photo.livePhotoVideoUrl!,
+ })),
+ )
}
const visibleCities = ref<string>()
@@ -233,6 +241,12 @@ onMounted(() => {
// Set up scroll listener
window.addEventListener('scroll', handleScroll, { passive: true })
+ nextTick(() => {
+ if (currentPhotoIndex.value) {
+ scrollToPhoto(currentPhotoIndex.value)
+ }
+ })
+
// Cleanup
onUnmounted(() => {
resizeObserver.disconnect()
@@ -243,6 +257,45 @@ onMounted(() => {
const handleOpenViewer = (index: number) => {
router.push(`/${displayPhotos.value[index]?.id}`)
}
+
+// 滚动到指定照片的位置
+const scrollToPhoto = (photoIndex: number) => {
+ if (!displayPhotos.value[photoIndex]) return
+
+ const photoId = displayPhotos.value[photoIndex].id
+ const photoElement = document.querySelector(`[data-photo-id="${photoId}"]`)
+
+ if (photoElement) {
+ // 计算滚动位置,让目标图片居中显示
+ const elementRect = photoElement.getBoundingClientRect()
+ const windowHeight = window.innerHeight
+ const currentScrollY = window.pageYOffset
+
+ // 计算目标滚动位置,让图片在视窗中央
+ const targetScrollY =
+ currentScrollY +
+ elementRect.top -
+ windowHeight / 2 +
+ elementRect.height / 2
+
+ // 平滑滚动到目标位置
+ window.scrollTo({
+ top: Math.max(0, targetScrollY),
+ behavior: 'smooth',
+ })
+ }
+}
+
+// 监听照片查看器的索引变化,滚动到对应图片
+watch(currentPhotoIndex, (newIndex) => {
+ // 只在查看器打开时才滚动
+ if (isViewerOpen.value && newIndex >= 0) {
+ // 延迟一下,确保DOM更新完成
+ nextTick(() => {
+ scrollToPhoto(newIndex)
+ })
+ }
+})
</script>
<template>
diff --git a/app/components/photo/Viewer.vue b/app/components/photo/Viewer.vue
index 007c06be..882efb66 100644
--- a/app/components/photo/Viewer.vue
+++ b/app/components/photo/Viewer.vue
@@ -4,8 +4,7 @@ import { Swiper, SwiperSlide } from 'swiper/vue'
import { Navigation, Keyboard, Virtual } from 'swiper/modules'
import type { Swiper as SwiperType } from 'swiper'
-import 'swiper/css'
-import 'swiper/css/navigation'
+import 'swiper/swiper-bundle.css'
import LoadingIndicator from './LoadingIndicator.vue'
import ProgressiveImage from './ProgressiveImage.vue'