diff --git a/web/src/lib/components/asset-viewer/detail-panel.svelte b/web/src/lib/components/asset-viewer/detail-panel.svelte index 1962e08256..05575035e0 100644 --- a/web/src/lib/components/asset-viewer/detail-panel.svelte +++ b/web/src/lib/components/asset-viewer/detail-panel.svelte @@ -4,60 +4,23 @@ import ImageOutline from 'svelte-material-icons/ImageOutline.svelte'; import CameraIris from 'svelte-material-icons/CameraIris.svelte'; import MapMarkerOutline from 'svelte-material-icons/MapMarkerOutline.svelte'; - import { createEventDispatcher, onMount } from 'svelte'; - import { browser } from '$app/environment'; + import { createEventDispatcher } from 'svelte'; import { AssetResponseDto, AlbumResponseDto } from '@api'; import { asByteUnitString } from '../../utils/byte-units'; import { locale } from '$lib/stores/preferences.store'; - - type Leaflet = typeof import('leaflet'); - type LeafletMap = import('leaflet').Map; - type LeafletMarker = import('leaflet').Marker; - - // Map Property - let map: LeafletMap; - let leaflet: Leaflet; - let marker: LeafletMarker; + import type { LatLngTuple } from 'leaflet'; export let asset: AssetResponseDto; - $: if (asset.exifInfo?.latitude != null && asset.exifInfo?.longitude != null) { - drawMap(asset.exifInfo.latitude, asset.exifInfo.longitude); - } - export let albums: AlbumResponseDto[] = []; - onMount(async () => { - if (browser) { - if (asset.exifInfo?.latitude != null && asset.exifInfo?.longitude != null) { - await drawMap(asset.exifInfo.latitude, asset.exifInfo.longitude); - } + $: latlng = (() => { + const lat = asset.exifInfo?.latitude; + const lng = asset.exifInfo?.longitude; + + if (lat && lng) { + return [lat, lng] as LatLngTuple; } - }); - - async function drawMap(lat: number, lon: number) { - if (!leaflet) { - leaflet = await import('leaflet'); - } - - if (!map) { - map = leaflet.map('map'); - leaflet - .tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { - attribution: '© OpenStreetMap' - }) - .addTo(map); - } - - if (marker) { - map.removeLayer(marker); - } - - map.setView([lat || 0, lon || 0], 17); - - marker = leaflet.marker([lat || 0, lon || 0]); - marker.bindPopup(`${(lat || 0).toFixed(7)},${(lon || 0).toFixed(7)}`); - map.addLayer(marker); - } + })(); const dispatch = createEventDispatcher(); @@ -186,9 +149,22 @@ -
-
-
+{#if latlng} +
+ {#await import('../shared-components/leaflet') then { Map, TileLayer, Marker }} + + OpenStreetMap' + }} + /> + + + {/await} +
+{/if}
@@ -225,7 +201,3 @@ {/each}
- - diff --git a/web/src/lib/components/shared-components/leaflet/index.ts b/web/src/lib/components/shared-components/leaflet/index.ts new file mode 100644 index 0000000000..ae216f5012 --- /dev/null +++ b/web/src/lib/components/shared-components/leaflet/index.ts @@ -0,0 +1,3 @@ +export { default as Map } from './map.svelte'; +export { default as Marker } from './marker.svelte'; +export { default as TileLayer } from './tile-layer.svelte'; diff --git a/web/src/lib/components/shared-components/leaflet/map.svelte b/web/src/lib/components/shared-components/leaflet/map.svelte new file mode 100644 index 0000000000..b7afca2a63 --- /dev/null +++ b/web/src/lib/components/shared-components/leaflet/map.svelte @@ -0,0 +1,42 @@ + + + + +
+ {#if map} + + {/if} +
diff --git a/web/src/lib/components/shared-components/leaflet/marker.svelte b/web/src/lib/components/shared-components/leaflet/marker.svelte new file mode 100644 index 0000000000..598bb9f111 --- /dev/null +++ b/web/src/lib/components/shared-components/leaflet/marker.svelte @@ -0,0 +1,46 @@ + diff --git a/web/src/lib/components/shared-components/leaflet/tile-layer.svelte b/web/src/lib/components/shared-components/leaflet/tile-layer.svelte new file mode 100644 index 0000000000..c050d1c1d4 --- /dev/null +++ b/web/src/lib/components/shared-components/leaflet/tile-layer.svelte @@ -0,0 +1,19 @@ + diff --git a/web/src/lib/utils/context.ts b/web/src/lib/utils/context.ts new file mode 100644 index 0000000000..05394097de --- /dev/null +++ b/web/src/lib/utils/context.ts @@ -0,0 +1,8 @@ +import { getContext, setContext } from 'svelte'; + +export function createContext(key: string | symbol = Symbol()) { + return { + get: () => getContext(key), + set: (context: T) => setContext(key, context) + }; +}