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: 添加照片提供者组件并实现照片数据的上下文管理,添加实验性的大地图

Timothy Yin4da6ea592abb
diff --git a/.vscode/settings.json b/.vscode/settings.json
index e918e1a4..aa222ada 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,4 +1,5 @@
{
+ "eslint.useFlatConfig": true,
"files.associations": {
"*.css": "tailwindcss"
},
@@ -14,6 +15,5 @@
"ui:\\s*{([^)]*)\\s*}",
"(?:'|\"|`)([^']*)(?:'|\"|`)"
]
- ],
- "eslint.useFlatConfig": true
+ ]
}
\ No newline at end of file
diff --git a/app/app.vue b/app/app.vue
index 117a8d1c..2a275875 100644
--- a/app/app.vue
+++ b/app/app.vue
@@ -1,13 +1,20 @@
<script setup lang="ts">
+useHead({
+ titleTemplate: (title) => `${title} - TimoYin's mems`,
+})
+
+const { data } = useFetch('/api/photos')
+const photos = computed(() => (data.value as Photo[]) || [])
</script>
<template>
<UApp>
- <NuxtLayout>
- <NuxtPage />
- </NuxtLayout>
+ <PhotosProvider :photos="photos">
+ <NuxtLayout>
+ <NuxtPage />
+ </NuxtLayout>
+ </PhotosProvider>
</UApp>
</template>
-<style>
-</style>
+<style></style>
diff --git a/app/components/PhotosProvider.vue b/app/components/PhotosProvider.vue
new file mode 100644
index 00000000..1f176fc6
--- /dev/null
+++ b/app/components/PhotosProvider.vue
@@ -0,0 +1,16 @@
+<script lang="ts" setup>
+const props = defineProps<{
+ photos: Photo[]
+}>()
+
+const photosRef = toRef(props, 'photos')
+providePhotos(photosRef)
+</script>
+
+<template>
+ <slot />
+</template>
+
+<style scoped>
+
+</style>
diff --git a/app/components/map/GlassButton.vue b/app/components/map/GlassButton.vue
new file mode 100644
index 00000000..af6775ec
--- /dev/null
+++ b/app/components/map/GlassButton.vue
@@ -0,0 +1,38 @@
+<script lang="ts" setup>
+import { motion } from 'motion-v'
+import { twMerge } from 'tailwind-merge'
+
+defineEmits<{
+ (e: 'click'): void
+}>()
+
+withDefaults(
+ defineProps<{
+ class?: string
+ }>(),
+ {
+ class: '',
+ },
+)
+</script>
+
+<template>
+ <motion.button
+ :initial="{ scale: 1 }"
+ :while-hover="{ scale: 1.1 }"
+ :while-press="{ scale: 0.95 }"
+ :transition="{ type: 'spring', stiffness: 300, damping: 20 }"
+ type="button"
+ :class="
+ twMerge(
+ $props.class,
+ 'pointer-events-auto flex size-10 items-center justify-center rounded-xl bg-black/20 text-white backdrop-blur-md border border-white/10 shadow-lg shadow-black/25 overflow-hidden',
+ )
+ "
+ @click="$emit('click')"
+ >
+ <slot />
+ </motion.button>
+</template>
+
+<style scoped></style>
diff --git a/app/composables/usePhotos.ts b/app/composables/usePhotos.ts
new file mode 100644
index 00000000..6979de5b
--- /dev/null
+++ b/app/composables/usePhotos.ts
@@ -0,0 +1,35 @@
+interface PhotosContext {
+ photos: Ref<Photo[]>
+ getPhotoById: (id: string) => Photo | undefined
+ filterPhotos: (predicate: (photo: Photo) => boolean) => Photo[]
+ totalCount: ComputedRef<number>
+}
+
+const PhotosContextKey = Symbol('PhotosContext') as InjectionKey<PhotosContext>
+
+export function providePhotos(photos: Ref<Photo[]>) {
+ const context: PhotosContext = {
+ photos,
+ getPhotoById: (id: string) => {
+ return photos.value.find(photo => photo.id === id)
+ },
+ filterPhotos: (predicate: (photo: Photo) => boolean) => {
+ return photos.value.filter(predicate)
+ },
+ totalCount: computed(() => photos.value.length)
+ }
+
+ provide(PhotosContextKey, context)
+
+ return context
+}
+
+export function usePhotos(): PhotosContext {
+ const context = inject(PhotosContextKey)
+
+ if (!context) {
+ throw new Error('usePhotos must be used within a PhotosProvider')
+ }
+
+ return context
+}
diff --git a/app/layouts/default.vue b/app/layouts/masonry.vue
similarity index 96%
rename from app/layouts/default.vue
rename to app/layouts/masonry.vue
index 6218ee5c..53e5143a 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/masonry.vue
@@ -1,7 +1,6 @@
<script lang="ts" setup>
useHead({
title: 'Gallery',
- titleTemplate: (title) => `${title} - TimoYin's mems`,
})
const { data } = useFetch('/api/photos')
diff --git a/app/pages/[...slug].vue b/app/pages/[...slug].vue
index b9c9897c..8d66f6e4 100644
--- a/app/pages/[...slug].vue
+++ b/app/pages/[...slug].vue
@@ -1,5 +1,6 @@
<script lang="ts" setup>
definePageMeta({
+ layout: 'masonry',
// 固定 key 防止路径参数变化时创建新的实例
key: 'photo-viewer-route',
})
diff --git a/app/pages/explore.vue b/app/pages/explore.vue
new file mode 100644
index 00000000..41d30dcf
--- /dev/null
+++ b/app/pages/explore.vue
@@ -0,0 +1,161 @@
+<script lang="ts" setup>
+import { motion } from 'motion-v'
+
+useHead({
+ title: '地图探索',
+})
+
+const MAPID = 'explore-map'
+const { photos } = usePhotos()
+
+// 筛选出有GPS坐标的照片
+const photosWithLocation = computed(() => {
+ return photos.value.filter(
+ (photo) =>
+ photo.latitude !== null &&
+ photo.longitude !== null &&
+ photo.latitude !== undefined &&
+ photo.longitude !== undefined,
+ )
+})
+
+const mapViewState = computed(() => {
+ if (photosWithLocation.value.length === 0) {
+ return {
+ longitude: -122.4,
+ latitude: 37.8,
+ zoom: 2,
+ }
+ }
+
+ const latitudes = photosWithLocation.value.map((photo) => photo.latitude!)
+ const longitudes = photosWithLocation.value.map((photo) => photo.longitude!)
+
+ const minLat = Math.min(...latitudes)
+ const maxLat = Math.max(...latitudes)
+ const minLng = Math.min(...longitudes)
+ const maxLng = Math.max(...longitudes)
+
+ const centerLat = (minLat + maxLat) / 2
+ const centerLng = (minLng + maxLng) / 2
+
+ const latDiff = maxLat - minLat
+ const lngDiff = maxLng - minLng
+ const maxDiff = Math.max(latDiff, lngDiff)
+
+ let zoom = 8
+ if (maxDiff < 0.005) zoom = 16
+ else if (maxDiff < 0.02) zoom = 14
+ else if (maxDiff < 0.05) zoom = 12
+ else if (maxDiff < 0.2) zoom = 10
+ else if (maxDiff < 1) zoom = 8
+ else if (maxDiff < 5) zoom = 6
+ else if (maxDiff < 20) zoom = 5
+ else if (maxDiff < 50) zoom = 4
+ else zoom = 2
+
+ return {
+ longitude: centerLng,
+ latitude: centerLat,
+ zoom,
+ }
+})
+
+onMounted(() => {
+ console.log(`找到 ${photosWithLocation.value.length} 张有位置信息的照片`)
+})
+</script>
+
+<template>
+ <div class="w-full h-svh relative overflow-hidden">
+
+ <MapGlassButton
+ class="absolute top-4 left-4 z-10"
+ @click="$router.push('/')"
+ >
+ <Icon
+ name="tabler:home"
+ class="size-5"
+ />
+ </MapGlassButton>
+
+ <motion.div
+ :initial="{ opacity: 0, scale: 1.1 }"
+ :animate="{ opacity: 1, scale: 1 }"
+ :transition="{ duration: 0.6, delay: 1.5 }"
+ class="w-full h-full"
+ >
+ <MapboxMap
+ class="w-full h-full"
+ :map-id="MAPID"
+ :options="{
+ style: 'mapbox://styles/mapbox/standard',
+ zoom: mapViewState.zoom,
+ center: [mapViewState.longitude, mapViewState.latitude],
+ config: {
+ basemap: {
+ lightPreset: 'night',
+ colorThemes: 'faded',
+ },
+ },
+ language: 'zh-Hans',
+ }"
+ >
+ <!-- 为每张有位置信息的照片添加标记 -->
+ <MapboxDefaultMarker
+ v-for="photo in photosWithLocation"
+ :key="photo.id"
+ :marker-id="`photo-${photo.id}`"
+ :lnglat="[photo.longitude, photo.latitude]"
+ :options="{}"
+ >
+ <template #marker>
+ <HoverCardRoot>
+ <HoverCardTrigger as-child>
+ <div
+ class="photo-marker"
+ :style="{
+ width: '40px',
+ height: '40px',
+ borderRadius: '50%',
+ backgroundImage: `url(${photo.thumbnailUrl})`,
+ backgroundSize: 'cover',
+ backgroundPosition: 'center',
+ border: '3px solid white',
+ boxShadow: '0 2px 8px rgba(0,0,0,0.3)',
+ cursor: 'pointer',
+ transition: 'transform 0.2s ease',
+ }"
+ />
+ </HoverCardTrigger>
+ <HoverCardPortal>
+ <HoverCardContent
+ side="top"
+ :side-offset="8"
+ as-child
+ >
+ <motion.div
+ class="bg-white rounded-lg shadow-lg p-2 min-w-32 max-w-xs"
+ :initial="{ opacity: 0, y: 10 }"
+ :animate="{ opacity: 1, y: 0 }"
+ :exit="{ opacity: 0, y: 10 }"
+ :transition="{ duration: 0.2 }"
+ >
+ <div class="text-sm font-medium">
+ {{ photo.title || `照片 ${photo.id}` }}
+ </div>
+ <div class="text-xs text-gray-600">
+ {{ photo.city || photo.country || '未知位置' }}
+ </div>
+ </motion.div>
+ </HoverCardContent>
+ </HoverCardPortal>
+ </HoverCardRoot>
+ </template>
+ </MapboxDefaultMarker>
+ </MapboxMap>
+ </motion.div>
+ </div>
+</template>
+
+<style scoped></style>
diff --git a/nuxt.config.ts b/nuxt.config.ts
index ac0235a7..1df0ce13 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -6,6 +6,7 @@ export default defineNuxtConfig({
devtools: { enabled: true },
modules: [
+ 'reka-ui/nuxt',
'@nuxt/ui',
'@nuxt/eslint',
'@nuxt/fonts',
diff --git a/package.json b/package.json
index eea298bd..dfca137a 100644
--- a/package.json
+++ b/package.json
@@ -50,6 +50,7 @@
"nuxt-auth-utils": "0.5.23",
"nuxt-mapbox": "1.6.3",
"pinia": "^3.0.3",
+ "reka-ui": "^2.5.0",
"sharp": "^0.34.3",
"swiper": "^11.2.10",
"tailwind-merge": "^3.3.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index deec870b..ae1c0ec1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -89,6 +89,9 @@ importers:
pinia:
specifier: ^3.0.3
+ reka-ui:
+ specifier: ^2.5.0
sharp:
specifier: ^0.34.3
version: 0.34.3
@@ -6564,6 +6567,11 @@ packages:
peerDependencies:
vue: '>= 3.2.0'
+ resolution: {integrity: sha512-81aMAmJeVCy2k0E6x7n1kypDY6aM1ldLis5+zcdV1/JtoAlSDck5OBsyLRJU9CfgbrQp1ImnRnBSmC4fZ2fkZQ==}
+ peerDependencies:
+ vue: '>= 3.2.0'
+
resolution: {integrity: sha512-/hS+Y0u3aOfIETiaiirUFwDBDzmXPvO+jAfKTitUngIPzdKc6Z0LoFjM/CK5PL4C+eKwHohlHAb6H0VFfmmUsw==}
@@ -15162,6 +15170,23 @@ snapshots:
- '@vue/composition-api'
- typescript
+ dependencies:
+ '@floating-ui/dom': 1.7.4
+ '@floating-ui/vue': 1.1.9([email protected]([email protected]))
+ '@internationalized/date': 3.8.2
+ '@internationalized/number': 3.6.4
+ '@tanstack/vue-virtual': 3.13.12([email protected]([email protected]))
+ '@vueuse/core': 12.8.2([email protected])
+ '@vueuse/shared': 12.8.2([email protected])
+ aria-hidden: 1.2.6
+ defu: 6.1.4
+ ohash: 2.0.11
+ vue: 3.5.19([email protected])
+ transitivePeerDependencies:
+ - '@vue/composition-api'
+ - typescript
+