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(map): impl `MapProvider` to adapt both MapBox and MapLibre

HoshinoSuzumi82193b9b6f06
diff --git a/app/assets/mapStyles/maplibre_basic.json b/app/assets/mapStyles/chronoframe_light.json
similarity index 92%
rename from app/assets/mapStyles/maplibre_basic.json
rename to app/assets/mapStyles/chronoframe_light.json
index 287c2eb9..5b8b2d81 100644
--- a/app/assets/mapStyles/maplibre_basic.json
+++ b/app/assets/mapStyles/chronoframe_light.json
@@ -1,6 +1,6 @@
{
"version": 8,
- "name": "ChronoFrame Basic",
+ "name": "ChronoFrame Light",
"metadata": {
"mapbox:autocomposite": false,
"mapbox:type": "template",
@@ -9,7 +9,8 @@
"openmaptiles:mapbox:owner": "openmaptiles",
"openmaptiles:mapbox:source:url": "mapbox://openmaptiles.4qljc88t"
},
- "light": {"anchor": "viewport"},
+ "center": [116.4074, 39.9042],
+ "zoom": 2,
"projection": {"type": "globe"},
"sources": {
"openmaptiles": {
@@ -727,7 +728,7 @@
],
"layout": {
"text-anchor": "center",
- "text-field": "{name:latin}\n{name:nonlatin}",
+ "text-field": "{name:nonlatin}",
"text-font": ["Noto Sans Regular"],
"text-max-width": 6,
"text-size": {"stops": [[6, 10], [12, 14]]},
@@ -740,18 +741,79 @@
"text-halo-width": 2
}
},
+ {
+ "id": "place_label_state",
+ "type": "symbol",
+ "source": "openmaptiles",
+ "source-layer": "place",
+ "minzoom": 3,
+ "maxzoom": 16,
+ "filter": [
+ "all",
+ ["==", "$type", "Point"],
+ ["in", "class", "state", "region", "province"]
+ ],
+ "layout": {
+ "text-field": "{name:nonlatin}",
+ "text-font": ["Noto Sans Regular"],
+ "text-max-width": 10,
+ "text-size": {"stops": [[3, 14], [8, 20]]},
+ "symbol-sort-key": 10
+ },
+ "paint": {
+ "text-color": "hsl(0, 0%, 0%)",
+ "text-halo-blur": 0,
+ "text-halo-color": "hsla(0, 0%, 100%, 0.75)",
+ "text-halo-width": 2
+ }
+ },
+ {
+ "id": "place_label_state_tw",
+ "type": "symbol",
+ "source": "openmaptiles",
+ "source-layer": "place",
+ "minzoom": 3,
+ "maxzoom": 16,
+ "filter": [
+ "all",
+ ["==", "$type", "Point"],
+ ["==", "class", "country"],
+ [
+ "any",
+ ["==", "iso_a2", "TW"],
+ ["==", "wikidata", "Q865"],
+ ["==", "name:en", "Taiwan"],
+ ["==", "name", "Taiwan"]
+ ]
+ ],
+ "layout": {
+ "text-field": "台湾省",
+ "text-font": ["Noto Sans Regular"],
+ "text-max-width": 10,
+ "text-size": {"stops": [[3, 14], [8, 20]]},
+ "symbol-sort-key": 10
+ },
+ "paint": {
+ "text-color": "hsl(0, 0%, 0%)",
+ "text-halo-blur": 0,
+ "text-halo-color": "hsla(0, 0%, 100%, 0.75)",
+ "text-halo-width": 2
+ }
+ },
{
"id": "place_label_city",
"type": "symbol",
"source": "openmaptiles",
"source-layer": "place",
+ "minzoom": 5,
"maxzoom": 16,
"filter": ["all", ["==", "$type", "Point"], ["==", "class", "city"]],
"layout": {
- "text-field": "{name:latin}\n{name:nonlatin}",
+ "text-field": "{name:nonlatin}",
"text-font": ["Noto Sans Regular"],
"text-max-width": 10,
- "text-size": {"stops": [[3, 12], [8, 16]]}
+ "text-size": {"stops": [[6, 12], [10, 16]]},
+ "symbol-sort-key": 1
},
"paint": {
"text-color": "hsl(0, 0%, 0%)",
@@ -796,7 +858,8 @@
"all",
["==", "$type", "Point"],
["==", "class", "country"],
- ["has", "iso_a2"]
+ ["has", "iso_a2"],
+ ["!=", "iso_a2", "TW"]
],
"layout": {
"text-field": "{name:latin}",
@@ -813,5 +876,6 @@
}
}
],
- "id": "basic"
+ "id": "basic",
+ "owner": "ChronoFrame"
}
\ No newline at end of file
diff --git a/app/components/map/ClusterPin.vue b/app/components/map/ClusterPin.vue
index 38939ea7..a658b355 100644
--- a/app/components/map/ClusterPin.vue
+++ b/app/components/map/ClusterPin.vue
@@ -46,11 +46,10 @@ const sizeDelta = computed(() => {
</script>
<template>
- <MapboxDefaultMarker
+ <MapProviderMarker
:key="`marker-cluster-${representativePhoto.id}`"
:marker-id="`marker-cluster-${markerId || representativePhoto.id}`"
:lnglat="props.clusterPoint.geometry.coordinates"
- :options="{}"
>
<template #marker>
<HoverCardRoot
@@ -261,7 +260,7 @@ const sizeDelta = computed(() => {
</HoverCardPortal>
</HoverCardRoot>
</template>
- </MapboxDefaultMarker>
+ </MapProviderMarker>
</template>
<style scoped></style>
diff --git a/app/components/map/PhotoPin.vue b/app/components/map/PhotoPin.vue
index 144ce5ef..e0c85bb0 100644
--- a/app/components/map/PhotoPin.vue
+++ b/app/components/map/PhotoPin.vue
@@ -30,12 +30,11 @@ const onClick = () => {
</script>
<template>
- <MapboxDefaultMarker
+ <MapProviderMarker
ref="markerRef"
:key="`marker-single-${marker.id}`"
:marker-id="`marker-single-${markerId || marker.id}`"
:lnglat="props.clusterPoint.geometry.coordinates"
- :options="{}"
>
<template #marker>
<HoverCardRoot
@@ -244,7 +243,7 @@ const onClick = () => {
</HoverCardPortal>
</HoverCardRoot>
</template>
- </MapboxDefaultMarker>
+ </MapProviderMarker>
</template>
<style scoped></style>
diff --git a/app/components/map/Provider.vue b/app/components/map/Provider.vue
index ef75940a..2fc736c8 100644
--- a/app/components/map/Provider.vue
+++ b/app/components/map/Provider.vue
@@ -1,14 +1,35 @@
<script lang="ts" setup>
-import type { StyleSpecification } from 'maplibre-gl'
+import type { AttributionControlOptions, StyleSpecification } from 'maplibre-gl'
import { twMerge } from 'tailwind-merge'
-import MaplibreBasicStyle from '~/assets/mapStyles/maplibre_basic.json'
+import ChronoFrameLightStyle from '~/assets/mapStyles/chronoframe_light.json'
+import type { MapboxMap, MapInstance, MaplibreMap } from '~~/shared/types/map'
-defineProps<{
- class?: string
-}>()
+const props = withDefaults(
+ defineProps<{
+ class?: string
+ mapId?: string
+ style?: StyleSpecification | string
+ center?: [number, number]
+ zoom?: number
+ interactive?: boolean
+ attributionControl?: false | AttributionControlOptions
+ language?: string
+ }>(),
+ {
+ class: undefined,
+ mapId: undefined,
+ style: undefined,
+ center: undefined,
+ zoom: 2,
+ interactive: true,
+ attributionControl: false,
+ language: undefined,
+ },
+)
const emit = defineEmits<{
- load: [map: any]
+ load: [map: MapInstance]
+ zoom: []
}>()
const mapConfig = useRuntimeConfig().public.map
@@ -16,9 +37,9 @@ const mapConfig = useRuntimeConfig().public.map
const provider = computed(() => mapConfig.provider)
const mapStyle = computed(() => {
if (provider.value === 'mapbox') {
- return `mapbox://styles/mapbox/standard`
+ return props.style || `mapbox://styles/mapbox/standard`
} else {
- return MaplibreBasicStyle as StyleSpecification
+ return props.style || (ChronoFrameLightStyle as StyleSpecification)
}
})
</script>
@@ -28,17 +49,32 @@ const mapStyle = computed(() => {
<ClientOnly>
<MglMap
v-if="provider === 'maplibre'"
- :map-style="mapStyle"
- @map:load="emit('load', $event.map)"
+ class="w-full h-full"
+ :map-key="mapId"
+ :map-style
+ :center
+ :zoom
+ :interactive
+ :attribution-control
+ @map:load="emit('load', $event.map as MaplibreMap)"
+ @map:zoom="emit('zoom')"
>
<slot />
</MglMap>
<MapboxMap
v-else
+ class="w-full h-full"
+ :map-id="mapId || 'cframe-mapbox-map'"
:options="{
style: mapStyle,
+ center: center,
+ zoom: zoom,
+ interactive: interactive,
+ attributionControl: attributionControl,
+ language: language,
}"
- @load="emit('load', $event)"
+ @load="emit('load', $event as MapboxMap)"
+ @zoom="emit('zoom')"
>
<slot />
</MapboxMap>
diff --git a/app/components/map/ProviderMarker.vue b/app/components/map/ProviderMarker.vue
new file mode 100644
index 00000000..ff2109c7
--- /dev/null
+++ b/app/components/map/ProviderMarker.vue
@@ -0,0 +1,38 @@
+<script lang="ts" setup>
+withDefaults(
+ defineProps<{
+ markerId?: string
+ lnglat?: [number, number]
+ }>(),
+ {
+ markerId: undefined,
+ lnglat: undefined,
+ },
+)
+
+const mapConfig = useRuntimeConfig().public.map
+
+const provider = computed(() => mapConfig.provider)
+</script>
+
+<template>
+ <MapboxDefaultMarker
+ v-if="provider === 'mapbox'"
+ :marker-id
+ :lnglat
+ >
+ <template #marker>
+ <slot name="marker" />
+ </template>
+ </MapboxDefaultMarker>
+ <MglMarker
+ v-else
+ :coordinates="lnglat"
+ >
+ <template #marker>
+ <slot name="marker" />
+ </template>
+ </MglMarker>
+</template>
+
+<style scoped></style>
diff --git a/app/components/photo/MiniMap.vue b/app/components/photo/MiniMap.vue
index 694ed0d5..34bbf127 100644
--- a/app/components/photo/MiniMap.vue
+++ b/app/components/photo/MiniMap.vue
@@ -1,5 +1,6 @@
<script lang="ts" setup>
import { motion } from 'motion-v'
+import { isMapboxMap, type MapInstance } from '~~/shared/types/map'
const props = defineProps<{
photo: Photo
@@ -9,12 +10,14 @@ const props = defineProps<{
const MAPID = 'minimap-info-panel'
+const config = useRuntimeConfig()
+
const loaded = ref(false)
const isAnimating = ref(false)
-const mapInstance = ref<any>(null)
+const mapInstance = ref<MapInstance | null>(null)
let animationTimer: ReturnType<typeof setTimeout> | null = null
-const onMapLoad = (map: any) => {
+const onMapLoad = (map: MapInstance) => {
mapInstance.value = map
map.setCenter([props.longitude, props.latitude])
setTimeout(() => {
@@ -50,18 +53,20 @@ const moveMapTo = (newLat: number, newLng: number) => {
watch([() => props.latitude, () => props.longitude], ([newLat, newLng]) => {
moveMapTo(newLat, newLng)
- if (mapInstance.value) {
+ const map = mapInstance.value
+ // Only available with Mapbox
+ if (map && isMapboxMap(map as any, config.public.map.provider)) {
// 根据拍摄时间决定四种 lightPreset: dawn / day / dusk / night
const photoDate = new Date(props.photo.dateTaken || 0)
const hours = photoDate.getHours()
if (hours >= 5 && hours < 7) {
- mapInstance.value.setConfigProperty('basemap', 'lightPreset', 'dawn')
+ ;(map as any).setConfigProperty('basemap', 'lightPreset', 'dawn')
} else if (hours >= 7 && hours < 17) {
- mapInstance.value.setConfigProperty('basemap', 'lightPreset', 'day')
+ ;(map as any).setConfigProperty('basemap', 'lightPreset', 'day')
} else if (hours >= 17 && hours < 21) {
- mapInstance.value.setConfigProperty('basemap', 'lightPreset', 'dusk')
+ ;(map as any).setConfigProperty('basemap', 'lightPreset', 'dusk')
} else {
- mapInstance.value.setConfigProperty('basemap', 'lightPreset', 'night')
+ ;(map as any).setConfigProperty('basemap', 'lightPreset', 'night')
}
}
})
@@ -71,10 +76,6 @@ onUnmounted(() => {
clearTimeout(animationTimer)
animationTimer = null
}
- if (mapInstance.value) {
- mapInstance.value.remove()
- mapInstance.value = null
- }
})
</script>
@@ -82,21 +83,12 @@ onUnmounted(() => {
<div
class="relative w-full h-44 overflow-hidden rounded-lg border border-white/10 dark:border-white/10"
>
- <!-- mapbox://styles/hoshinosuzumi/cmev0eujf01dw01pje3g9cmlg -->
- <MapboxMap
+ <MapProvider
class="w-full h-full relative overflow-hidden"
:map-id="MAPID"
- :options="{
- style: 'mapbox://styles/mapbox/standard',
- zoom: 12,
- config: {
- basemap: {
- colorThemes: 'faded',
- },
- },
- interactive: false,
- language: $i18n.locale,
- }"
+ :zoom="12"
+ :interactive="false"
+ :language="$i18n.locale"
@load="onMapLoad"
>
<AnimatePresence>
@@ -125,7 +117,7 @@ onUnmounted(() => {
{{ $t('minimap.loading') }}
</p>
</div>
- </MapboxMap>
+ </MapProvider>
</div>
</template>
diff --git a/app/pages/globe.vue b/app/pages/globe.vue
index 1764db8b..95038fb5 100644
--- a/app/pages/globe.vue
+++ b/app/pages/globe.vue
@@ -243,22 +243,13 @@ onBeforeRouteLeave(() => {
>
<ClientOnly>
<!-- mapbox://styles/hoshinosuzumi/cmev0eujf01dw01pje3g9cmlg -->
- <MapboxMap
+ <MapProvider
class="w-full h-full"
:map-id="generateRandomKey()"
- :options="{
- style: 'mapbox://styles/mapbox/standard',
- zoom: mapViewState.zoom,
- center: [mapViewState.longitude, mapViewState.latitude],
- config: {
- basemap: {
- lightPreset: $colorMode.value === 'dark' ? 'night' : 'day',
- colorThemes: 'faded',
- },
- },
- attributionControl: false,
- language: $i18n.locale,
- }"
+ :zoom="mapViewState.zoom"
+ :center="[mapViewState.longitude, mapViewState.latitude]"
+ :attribution-control="false"
+ :language="$i18n.locale"
@load="onMapLoaded"
@zoom="onMapZoom"
>
@@ -288,7 +279,7 @@ onBeforeRouteLeave(() => {
@close="onMarkerPinClose"
/>
</template>
- </MapboxMap>
+ </MapProvider>
<template #fallback>
<div class="w-full h-full flex items-center justify-center">
diff --git a/app/pages/map-test.vue b/app/pages/map-test.vue
index ceacd297..9aeebd26 100644
--- a/app/pages/map-test.vue
+++ b/app/pages/map-test.vue
@@ -1,15 +1,7 @@
<script lang="ts" setup>
-import type { Map as MaplibreMap } from 'maplibre-gl'
-import type { Map as MapboxMap } from 'mapbox-gl'
+import type { MapInstance } from '~~/shared/types/map'
-const mapInstance = ref<MaplibreMap | MapboxMap | null>(null)
-
-// function isMaplibreMap(map: any): map is MaplibreMap {
-// return map && typeof map.getStyle === 'function' && typeof map.setStyle === 'function'
-// }
-// function isMapboxMap(map: any): map is MapboxMap {
-// return map && typeof map.getStyle === 'function' && typeof map.setStyle === 'function'
-// }
+const mapInstance = ref<MapInstance | null>(null)
watch(mapInstance, (newMap) => {
if (newMap) {
@@ -32,7 +24,9 @@ const onMapLoaded = (map: any) => {
<template>
<div class="w-full h-screen">
- <MapProvider @load="onMapLoaded"></MapProvider>
+ <MapProvider @load="onMapLoaded">
+ <slot />
+ </MapProvider>
</div>
</template>
diff --git a/shared/types/map.ts b/shared/types/map.ts
index 586e376e..ebda99dd 100644
--- a/shared/types/map.ts
+++ b/shared/types/map.ts
@@ -1,3 +1,25 @@
+import type { Map as _MaplibreMap } from 'maplibre-gl'
+import type { Map as _MapboxMap } from 'mapbox-gl'
+
+export type MaplibreMap = _MaplibreMap
+export type MapboxMap = _MapboxMap
+
+export type MapInstance = MaplibreMap | MapboxMap
+
+export const isMaplibreMap = (
+ map: MapInstance,
+ provider: string,
+): map is MaplibreMap => {
+ return provider === 'maplibre'
+}
+
+export const isMapboxMap = (
+ map: MapInstance,
+ provider: string,
+): map is MapboxMap => {
+ return provider === 'mapbox'
+}
+
export interface PhotoMarker {
id: string
latitude: number