@classic-homes/maps-svelte 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +432 -0
- package/dist/lib/components/base/MapContainer.svelte +68 -0
- package/dist/lib/components/base/MapContainer.svelte.d.ts +18 -0
- package/dist/lib/components/base/MapEmpty.svelte +42 -0
- package/dist/lib/components/base/MapEmpty.svelte.d.ts +8 -0
- package/dist/lib/components/base/MapError.svelte +54 -0
- package/dist/lib/components/base/MapError.svelte.d.ts +9 -0
- package/dist/lib/components/base/MapSkeleton.svelte +45 -0
- package/dist/lib/components/base/MapSkeleton.svelte.d.ts +7 -0
- package/dist/lib/components/base/index.d.ts +4 -0
- package/dist/lib/components/base/index.js +4 -0
- package/dist/lib/components/controls/CoordinateDisplay.svelte +163 -0
- package/dist/lib/components/controls/CoordinateDisplay.svelte.d.ts +6 -0
- package/dist/lib/components/controls/Legend.svelte +179 -0
- package/dist/lib/components/controls/Legend.svelte.d.ts +6 -0
- package/dist/lib/components/controls/LegendGroup.svelte +216 -0
- package/dist/lib/components/controls/LegendGroup.svelte.d.ts +6 -0
- package/dist/lib/components/controls/index.d.ts +3 -0
- package/dist/lib/components/controls/index.js +3 -0
- package/dist/lib/components/core/Map.svelte +434 -0
- package/dist/lib/components/core/Map.svelte.d.ts +11 -0
- package/dist/lib/components/core/Marker.svelte +185 -0
- package/dist/lib/components/core/Marker.svelte.d.ts +9 -0
- package/dist/lib/components/core/Popup.svelte +205 -0
- package/dist/lib/components/core/Popup.svelte.d.ts +11 -0
- package/dist/lib/components/core/PopupCard.svelte +17 -0
- package/dist/lib/components/core/PopupCard.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupContent.svelte +17 -0
- package/dist/lib/components/core/PopupContent.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupFooter.svelte +22 -0
- package/dist/lib/components/core/PopupFooter.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupHeader.svelte +45 -0
- package/dist/lib/components/core/PopupHeader.svelte.d.ts +14 -0
- package/dist/lib/components/core/Source.svelte +326 -0
- package/dist/lib/components/core/Source.svelte.d.ts +59 -0
- package/dist/lib/components/core/index.d.ts +8 -0
- package/dist/lib/components/core/index.js +8 -0
- package/dist/lib/components/deck/DeckOverlay.svelte +135 -0
- package/dist/lib/components/deck/DeckOverlay.svelte.d.ts +25 -0
- package/dist/lib/components/deck/index.d.ts +1 -0
- package/dist/lib/components/deck/index.js +1 -0
- package/dist/lib/components/index.d.ts +5 -0
- package/dist/lib/components/index.js +10 -0
- package/dist/lib/components/layers/CircleLayer.svelte +78 -0
- package/dist/lib/components/layers/CircleLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/FillExtrusionLayer.svelte +75 -0
- package/dist/lib/components/layers/FillExtrusionLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/FillLayer.svelte +70 -0
- package/dist/lib/components/layers/FillLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/HeatmapLayer.svelte +68 -0
- package/dist/lib/components/layers/HeatmapLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/LineLayer.svelte +81 -0
- package/dist/lib/components/layers/LineLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/SymbolLayer.svelte +108 -0
- package/dist/lib/components/layers/SymbolLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/index.d.ts +7 -0
- package/dist/lib/components/layers/index.js +6 -0
- package/dist/lib/components/layers/useLayer.svelte.d.ts +38 -0
- package/dist/lib/components/layers/useLayer.svelte.js +286 -0
- package/dist/lib/composables/index.d.ts +3 -0
- package/dist/lib/composables/index.js +3 -0
- package/dist/lib/composables/useMapContext.svelte.d.ts +62 -0
- package/dist/lib/composables/useMapContext.svelte.js +58 -0
- package/dist/lib/composables/useMapTheme.svelte.d.ts +11 -0
- package/dist/lib/composables/useMapTheme.svelte.js +66 -0
- package/dist/lib/composables/useReducedMotion.svelte.d.ts +6 -0
- package/dist/lib/composables/useReducedMotion.svelte.js +26 -0
- package/dist/lib/index.d.ts +12 -0
- package/dist/lib/index.js +29 -0
- package/dist/lib/utils.d.ts +2 -0
- package/dist/lib/utils.js +5 -0
- package/package.json +75 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy, tick } from 'svelte';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import maplibregl from 'maplibre-gl';
|
|
5
|
+
import type { PopupProps } from '@classic-homes/maps-core';
|
|
6
|
+
|
|
7
|
+
type Anchor =
|
|
8
|
+
| 'center'
|
|
9
|
+
| 'top'
|
|
10
|
+
| 'bottom'
|
|
11
|
+
| 'left'
|
|
12
|
+
| 'right'
|
|
13
|
+
| 'top-left'
|
|
14
|
+
| 'top-right'
|
|
15
|
+
| 'bottom-left'
|
|
16
|
+
| 'bottom-right';
|
|
17
|
+
|
|
18
|
+
interface MapContext {
|
|
19
|
+
readonly map: maplibregl.Map | null;
|
|
20
|
+
readonly loaded: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Props extends PopupProps {
|
|
24
|
+
/** Popup content */
|
|
25
|
+
children: Snippet;
|
|
26
|
+
/** Control popup open state */
|
|
27
|
+
open?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let {
|
|
31
|
+
coordinates,
|
|
32
|
+
closeButton = true,
|
|
33
|
+
closeOnClick = true,
|
|
34
|
+
closeOnMove = false,
|
|
35
|
+
anchor,
|
|
36
|
+
offset,
|
|
37
|
+
maxWidth = '300px',
|
|
38
|
+
focusAfterOpen = true,
|
|
39
|
+
autoPan = true,
|
|
40
|
+
autoPanPadding = 50,
|
|
41
|
+
open = true,
|
|
42
|
+
onOpen,
|
|
43
|
+
onClose,
|
|
44
|
+
children,
|
|
45
|
+
}: Props = $props();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Helper to normalize padding value into individual sides
|
|
49
|
+
*/
|
|
50
|
+
function normalizePadding(
|
|
51
|
+
padding: number | { top: number; right: number; bottom: number; left: number }
|
|
52
|
+
): { top: number; right: number; bottom: number; left: number } {
|
|
53
|
+
if (typeof padding === 'number') {
|
|
54
|
+
return { top: padding, right: padding, bottom: padding, left: padding };
|
|
55
|
+
}
|
|
56
|
+
return padding;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Check if user prefers reduced motion
|
|
61
|
+
*/
|
|
62
|
+
function prefersReducedMotion(): boolean {
|
|
63
|
+
if (typeof window === 'undefined') return false;
|
|
64
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Pan the map to ensure the popup is fully visible within the viewport
|
|
69
|
+
*/
|
|
70
|
+
function panToFitPopup(
|
|
71
|
+
map: maplibregl.Map,
|
|
72
|
+
popup: maplibregl.Popup,
|
|
73
|
+
padding: { top: number; right: number; bottom: number; left: number }
|
|
74
|
+
): void {
|
|
75
|
+
const popupEl = popup.getElement();
|
|
76
|
+
if (!popupEl) return;
|
|
77
|
+
|
|
78
|
+
const mapContainer = map.getContainer();
|
|
79
|
+
const mapRect = mapContainer.getBoundingClientRect();
|
|
80
|
+
const popupRect = popupEl.getBoundingClientRect();
|
|
81
|
+
|
|
82
|
+
let dx = 0;
|
|
83
|
+
let dy = 0;
|
|
84
|
+
|
|
85
|
+
// Calculate overflow on each side
|
|
86
|
+
if (popupRect.left < mapRect.left + padding.left) {
|
|
87
|
+
dx = popupRect.left - mapRect.left - padding.left;
|
|
88
|
+
} else if (popupRect.right > mapRect.right - padding.right) {
|
|
89
|
+
dx = popupRect.right - mapRect.right + padding.right;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (popupRect.top < mapRect.top + padding.top) {
|
|
93
|
+
dy = popupRect.top - mapRect.top - padding.top;
|
|
94
|
+
} else if (popupRect.bottom > mapRect.bottom - padding.bottom) {
|
|
95
|
+
dy = popupRect.bottom - mapRect.bottom + padding.bottom;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Pan if needed
|
|
99
|
+
if (dx !== 0 || dy !== 0) {
|
|
100
|
+
map.panBy([dx, dy], {
|
|
101
|
+
animate: !prefersReducedMotion(),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const mapContext = getContext<MapContext>('maplibre-map');
|
|
107
|
+
|
|
108
|
+
let popup: maplibregl.Popup | null = $state(null);
|
|
109
|
+
let containerElement: HTMLDivElement | null = $state(null);
|
|
110
|
+
let contentElement: HTMLDivElement | null = $state(null);
|
|
111
|
+
|
|
112
|
+
// Create container for content
|
|
113
|
+
$effect(() => {
|
|
114
|
+
if (!containerElement) {
|
|
115
|
+
containerElement = document.createElement('div');
|
|
116
|
+
containerElement.className = 'maplibregl-popup-content-wrapper';
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Create and manage popup
|
|
121
|
+
$effect(() => {
|
|
122
|
+
const { map, loaded } = mapContext;
|
|
123
|
+
|
|
124
|
+
if (!map || !loaded || !containerElement || popup) return;
|
|
125
|
+
|
|
126
|
+
const newPopup = new maplibregl.Popup({
|
|
127
|
+
closeButton,
|
|
128
|
+
closeOnClick,
|
|
129
|
+
closeOnMove,
|
|
130
|
+
anchor: anchor as Anchor | undefined,
|
|
131
|
+
offset: offset as number | maplibregl.PointLike | undefined,
|
|
132
|
+
maxWidth,
|
|
133
|
+
focusAfterOpen,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
newPopup.setDOMContent(containerElement);
|
|
137
|
+
popup = newPopup;
|
|
138
|
+
|
|
139
|
+
// Event handlers
|
|
140
|
+
newPopup.on('open', () => {
|
|
141
|
+
onOpen?.({
|
|
142
|
+
type: 'open',
|
|
143
|
+
lngLat: coordinates,
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
newPopup.on('close', () => {
|
|
148
|
+
onClose?.({
|
|
149
|
+
type: 'close',
|
|
150
|
+
lngLat: coordinates,
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Update popup position
|
|
156
|
+
$effect(() => {
|
|
157
|
+
if (popup && coordinates) {
|
|
158
|
+
popup.setLngLat(coordinates as [number, number]);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Open/close popup
|
|
163
|
+
$effect(() => {
|
|
164
|
+
const { map } = mapContext;
|
|
165
|
+
|
|
166
|
+
if (!map || !popup) return;
|
|
167
|
+
|
|
168
|
+
if (open) {
|
|
169
|
+
popup.setLngLat(coordinates as [number, number]).addTo(map);
|
|
170
|
+
|
|
171
|
+
// Auto-pan to fit popup after it renders
|
|
172
|
+
if (autoPan) {
|
|
173
|
+
// Wait for popup to render before measuring
|
|
174
|
+
requestAnimationFrame(() => {
|
|
175
|
+
if (popup && map) {
|
|
176
|
+
panToFitPopup(map, popup, normalizePadding(autoPanPadding));
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
popup.remove();
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Move content into container when it changes
|
|
186
|
+
$effect(() => {
|
|
187
|
+
if (containerElement && contentElement) {
|
|
188
|
+
// Use replaceChildren to safely clear and append content
|
|
189
|
+
// (avoids potential issues with innerHTML breaking event handlers)
|
|
190
|
+
containerElement.replaceChildren(contentElement);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Cleanup on destroy
|
|
195
|
+
onDestroy(() => {
|
|
196
|
+
if (popup) {
|
|
197
|
+
popup.remove();
|
|
198
|
+
popup = null;
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
</script>
|
|
202
|
+
|
|
203
|
+
<div bind:this={contentElement} style="display: contents;">
|
|
204
|
+
{@render children()}
|
|
205
|
+
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { PopupProps } from '@classic-homes/maps-core';
|
|
3
|
+
interface Props extends PopupProps {
|
|
4
|
+
/** Popup content */
|
|
5
|
+
children: Snippet;
|
|
6
|
+
/** Control popup open state */
|
|
7
|
+
open?: boolean;
|
|
8
|
+
}
|
|
9
|
+
declare const Popup: import("svelte").Component<Props, {}, "">;
|
|
10
|
+
type Popup = ReturnType<typeof Popup>;
|
|
11
|
+
export default Popup;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
/** Additional CSS classes */
|
|
7
|
+
class?: string;
|
|
8
|
+
/** Popup card content */
|
|
9
|
+
children: Snippet;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { class: className, children }: Props = $props();
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<div class={cn('bg-popover text-popover-foreground', className)}>
|
|
16
|
+
{@render children()}
|
|
17
|
+
</div>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Additional CSS classes */
|
|
4
|
+
class?: string;
|
|
5
|
+
/** Popup card content */
|
|
6
|
+
children: Snippet;
|
|
7
|
+
}
|
|
8
|
+
declare const PopupCard: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type PopupCard = ReturnType<typeof PopupCard>;
|
|
10
|
+
export default PopupCard;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
/** Additional CSS classes */
|
|
7
|
+
class?: string;
|
|
8
|
+
/** Content */
|
|
9
|
+
children: Snippet;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { class: className, children }: Props = $props();
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<div class={cn('px-4 py-3 text-sm', className)}>
|
|
16
|
+
{@render children()}
|
|
17
|
+
</div>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Additional CSS classes */
|
|
4
|
+
class?: string;
|
|
5
|
+
/** Content */
|
|
6
|
+
children: Snippet;
|
|
7
|
+
}
|
|
8
|
+
declare const PopupContent: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type PopupContent = ReturnType<typeof PopupContent>;
|
|
10
|
+
export default PopupContent;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { cn } from '../../utils.js';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
/** Additional CSS classes */
|
|
7
|
+
class?: string;
|
|
8
|
+
/** Footer content (typically buttons) */
|
|
9
|
+
children: Snippet;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { class: className, children }: Props = $props();
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
<div
|
|
16
|
+
class={cn(
|
|
17
|
+
'flex justify-end gap-2 border-t border-black dark:border-gray-400 bg-muted/30 px-4 py-3',
|
|
18
|
+
className
|
|
19
|
+
)}
|
|
20
|
+
>
|
|
21
|
+
{@render children()}
|
|
22
|
+
</div>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Additional CSS classes */
|
|
4
|
+
class?: string;
|
|
5
|
+
/** Footer content (typically buttons) */
|
|
6
|
+
children: Snippet;
|
|
7
|
+
}
|
|
8
|
+
declare const PopupFooter: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type PopupFooter = ReturnType<typeof PopupFooter>;
|
|
10
|
+
export default PopupFooter;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '../../utils.js';
|
|
3
|
+
|
|
4
|
+
type BadgeVariant = 'default' | 'secondary' | 'success' | 'warning' | 'info';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
/** Main heading text */
|
|
8
|
+
title: string;
|
|
9
|
+
/** Optional badge text */
|
|
10
|
+
badge?: string;
|
|
11
|
+
/** Badge color variant */
|
|
12
|
+
badgeVariant?: BadgeVariant;
|
|
13
|
+
/** Additional CSS classes */
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let { title, badge, badgeVariant = 'secondary', class: className }: Props = $props();
|
|
18
|
+
|
|
19
|
+
const badgeVariants: Record<BadgeVariant, string> = {
|
|
20
|
+
default: 'bg-primary text-primary-foreground',
|
|
21
|
+
secondary: 'bg-secondary text-secondary-foreground',
|
|
22
|
+
success: 'bg-success text-success-foreground',
|
|
23
|
+
warning: 'bg-warning text-warning-foreground',
|
|
24
|
+
info: 'bg-info text-info-foreground',
|
|
25
|
+
};
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<div
|
|
29
|
+
class={cn(
|
|
30
|
+
'flex items-start justify-between gap-2 border-b border-black dark:border-gray-400 bg-muted/30 px-4 py-3 pr-12',
|
|
31
|
+
className
|
|
32
|
+
)}
|
|
33
|
+
>
|
|
34
|
+
<h3 class="font-semibold text-sm leading-tight">{title}</h3>
|
|
35
|
+
{#if badge}
|
|
36
|
+
<span
|
|
37
|
+
class={cn(
|
|
38
|
+
'text-xs px-1.5 py-0.5 rounded font-medium flex-shrink-0',
|
|
39
|
+
badgeVariants[badgeVariant]
|
|
40
|
+
)}
|
|
41
|
+
>
|
|
42
|
+
{badge}
|
|
43
|
+
</span>
|
|
44
|
+
{/if}
|
|
45
|
+
</div>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type BadgeVariant = 'default' | 'secondary' | 'success' | 'warning' | 'info';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Main heading text */
|
|
4
|
+
title: string;
|
|
5
|
+
/** Optional badge text */
|
|
6
|
+
badge?: string;
|
|
7
|
+
/** Badge color variant */
|
|
8
|
+
badgeVariant?: BadgeVariant;
|
|
9
|
+
/** Additional CSS classes */
|
|
10
|
+
class?: string;
|
|
11
|
+
}
|
|
12
|
+
declare const PopupHeader: import("svelte").Component<Props, {}, "">;
|
|
13
|
+
type PopupHeader = ReturnType<typeof PopupHeader>;
|
|
14
|
+
export default PopupHeader;
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from 'svelte';
|
|
3
|
+
import type maplibregl from 'maplibre-gl';
|
|
4
|
+
import type {
|
|
5
|
+
GeoJSONFeatureCollection,
|
|
6
|
+
ClusterConfig,
|
|
7
|
+
GeoJSONFingerprint,
|
|
8
|
+
} from '@classic-homes/maps-core';
|
|
9
|
+
import {
|
|
10
|
+
calculateBounds,
|
|
11
|
+
createGeoJSONFingerprint,
|
|
12
|
+
fingerprintEqual,
|
|
13
|
+
} from '@classic-homes/maps-core';
|
|
14
|
+
|
|
15
|
+
interface MapContext {
|
|
16
|
+
readonly map: maplibregl.Map | null;
|
|
17
|
+
readonly loaded: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Source component props - extends MapLibre source specifications
|
|
22
|
+
*/
|
|
23
|
+
interface Props {
|
|
24
|
+
/** Unique source ID */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Source type */
|
|
27
|
+
type?: 'geojson' | 'vector' | 'raster' | 'raster-dem' | 'image' | 'video';
|
|
28
|
+
/** GeoJSON data (for geojson sources) */
|
|
29
|
+
data?: GeoJSONFeatureCollection | GeoJSON.GeoJSON | string;
|
|
30
|
+
/** TileJSON URL */
|
|
31
|
+
url?: string;
|
|
32
|
+
/** Tile URL templates */
|
|
33
|
+
tiles?: string[];
|
|
34
|
+
/** Tile size in pixels */
|
|
35
|
+
tileSize?: number;
|
|
36
|
+
/** Source min zoom */
|
|
37
|
+
minzoom?: number;
|
|
38
|
+
/** Source max zoom */
|
|
39
|
+
maxzoom?: number;
|
|
40
|
+
/** Attribution text */
|
|
41
|
+
attribution?: string;
|
|
42
|
+
/** Source bounds */
|
|
43
|
+
bounds?: [number, number, number, number];
|
|
44
|
+
/** Tile scheme */
|
|
45
|
+
scheme?: 'xyz' | 'tms';
|
|
46
|
+
/** Property to use as feature ID */
|
|
47
|
+
promoteId?: string;
|
|
48
|
+
/** Auto-generate feature IDs */
|
|
49
|
+
generateId?: boolean;
|
|
50
|
+
/** Enable clustering */
|
|
51
|
+
cluster?: boolean | ClusterConfig;
|
|
52
|
+
/** Cluster radius in pixels */
|
|
53
|
+
clusterRadius?: number;
|
|
54
|
+
/** Max zoom for clustering */
|
|
55
|
+
clusterMaxZoom?: number;
|
|
56
|
+
/** Min points to form a cluster */
|
|
57
|
+
clusterMinPoints?: number;
|
|
58
|
+
/** Aggregate properties for clusters */
|
|
59
|
+
clusterProperties?: Record<string, unknown>;
|
|
60
|
+
/** Enable line metrics */
|
|
61
|
+
lineMetrics?: boolean;
|
|
62
|
+
/** Simplification tolerance */
|
|
63
|
+
tolerance?: number;
|
|
64
|
+
/** Tile buffer size */
|
|
65
|
+
buffer?: number;
|
|
66
|
+
/** Fit map to source bounds when loaded */
|
|
67
|
+
fitBounds?: boolean;
|
|
68
|
+
/** Padding when fitting bounds */
|
|
69
|
+
fitBoundsPadding?: number;
|
|
70
|
+
/** Max zoom when fitting bounds */
|
|
71
|
+
fitBoundsMaxZoom?: number;
|
|
72
|
+
/** Animate when fitting bounds */
|
|
73
|
+
fitBoundsAnimate?: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let {
|
|
77
|
+
id,
|
|
78
|
+
type = 'geojson',
|
|
79
|
+
data,
|
|
80
|
+
url,
|
|
81
|
+
tiles,
|
|
82
|
+
tileSize,
|
|
83
|
+
minzoom,
|
|
84
|
+
maxzoom,
|
|
85
|
+
attribution,
|
|
86
|
+
bounds,
|
|
87
|
+
scheme,
|
|
88
|
+
promoteId,
|
|
89
|
+
generateId,
|
|
90
|
+
cluster,
|
|
91
|
+
clusterRadius,
|
|
92
|
+
clusterMaxZoom,
|
|
93
|
+
clusterMinPoints,
|
|
94
|
+
clusterProperties,
|
|
95
|
+
lineMetrics,
|
|
96
|
+
tolerance,
|
|
97
|
+
buffer,
|
|
98
|
+
fitBounds: shouldFitBounds = false,
|
|
99
|
+
fitBoundsPadding = 50,
|
|
100
|
+
fitBoundsMaxZoom,
|
|
101
|
+
fitBoundsAnimate = true,
|
|
102
|
+
}: Props = $props();
|
|
103
|
+
|
|
104
|
+
const mapContext = getContext<MapContext>('maplibre-map');
|
|
105
|
+
let sourceAdded = $state(false);
|
|
106
|
+
let initialFitDone = $state(false);
|
|
107
|
+
|
|
108
|
+
// Fingerprint tracking for efficient data updates
|
|
109
|
+
let lastDataFingerprint: GeoJSONFingerprint | null = null;
|
|
110
|
+
let pendingIdleCallback: number | null = null;
|
|
111
|
+
|
|
112
|
+
// Add source effect
|
|
113
|
+
$effect(() => {
|
|
114
|
+
const { map, loaded } = mapContext;
|
|
115
|
+
|
|
116
|
+
if (!map || !loaded || sourceAdded) return;
|
|
117
|
+
if (map.getSource(id)) {
|
|
118
|
+
sourceAdded = true;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
if (type === 'geojson') {
|
|
124
|
+
const sourceConfig: maplibregl.GeoJSONSourceSpecification = {
|
|
125
|
+
type: 'geojson',
|
|
126
|
+
data: (data || url || { type: 'FeatureCollection', features: [] }) as GeoJSON.GeoJSON,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
if (promoteId !== undefined) sourceConfig.promoteId = promoteId;
|
|
130
|
+
if (generateId !== undefined) sourceConfig.generateId = generateId;
|
|
131
|
+
// Handle cluster as boolean or ClusterConfig
|
|
132
|
+
if (cluster !== undefined) {
|
|
133
|
+
if (typeof cluster === 'boolean') {
|
|
134
|
+
sourceConfig.cluster = cluster;
|
|
135
|
+
} else {
|
|
136
|
+
sourceConfig.cluster = cluster.enabled;
|
|
137
|
+
if (cluster.radius !== undefined) sourceConfig.clusterRadius = cluster.radius;
|
|
138
|
+
if (cluster.maxZoom !== undefined) sourceConfig.clusterMaxZoom = cluster.maxZoom;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (clusterRadius !== undefined) sourceConfig.clusterRadius = clusterRadius;
|
|
142
|
+
if (clusterMaxZoom !== undefined) sourceConfig.clusterMaxZoom = clusterMaxZoom;
|
|
143
|
+
if (clusterMinPoints !== undefined) sourceConfig.clusterMinPoints = clusterMinPoints;
|
|
144
|
+
if (clusterProperties !== undefined)
|
|
145
|
+
sourceConfig.clusterProperties = clusterProperties as Record<
|
|
146
|
+
string,
|
|
147
|
+
maplibregl.ExpressionSpecification
|
|
148
|
+
>;
|
|
149
|
+
if (lineMetrics !== undefined) sourceConfig.lineMetrics = lineMetrics;
|
|
150
|
+
if (tolerance !== undefined) sourceConfig.tolerance = tolerance;
|
|
151
|
+
if (buffer !== undefined) sourceConfig.buffer = buffer;
|
|
152
|
+
if (attribution !== undefined) sourceConfig.attribution = attribution;
|
|
153
|
+
|
|
154
|
+
map.addSource(id, sourceConfig);
|
|
155
|
+
} else if (type === 'vector') {
|
|
156
|
+
const sourceConfig: maplibregl.VectorSourceSpecification = {
|
|
157
|
+
type: 'vector',
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
if (url) sourceConfig.url = url;
|
|
161
|
+
if (tiles) sourceConfig.tiles = tiles;
|
|
162
|
+
if (minzoom !== undefined) sourceConfig.minzoom = minzoom;
|
|
163
|
+
if (maxzoom !== undefined) sourceConfig.maxzoom = maxzoom;
|
|
164
|
+
if (attribution !== undefined) sourceConfig.attribution = attribution;
|
|
165
|
+
if (bounds !== undefined) sourceConfig.bounds = bounds as [number, number, number, number];
|
|
166
|
+
if (scheme !== undefined) sourceConfig.scheme = scheme;
|
|
167
|
+
if (promoteId !== undefined) sourceConfig.promoteId = promoteId;
|
|
168
|
+
|
|
169
|
+
map.addSource(id, sourceConfig);
|
|
170
|
+
} else if (type === 'raster') {
|
|
171
|
+
const sourceConfig: maplibregl.RasterSourceSpecification = {
|
|
172
|
+
type: 'raster',
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
if (url) sourceConfig.url = url;
|
|
176
|
+
if (tiles) sourceConfig.tiles = tiles;
|
|
177
|
+
if (tileSize !== undefined) sourceConfig.tileSize = tileSize;
|
|
178
|
+
if (minzoom !== undefined) sourceConfig.minzoom = minzoom;
|
|
179
|
+
if (maxzoom !== undefined) sourceConfig.maxzoom = maxzoom;
|
|
180
|
+
if (attribution !== undefined) sourceConfig.attribution = attribution;
|
|
181
|
+
if (bounds !== undefined) sourceConfig.bounds = bounds as [number, number, number, number];
|
|
182
|
+
if (scheme !== undefined) sourceConfig.scheme = scheme;
|
|
183
|
+
|
|
184
|
+
map.addSource(id, sourceConfig);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
sourceAdded = true;
|
|
188
|
+
} catch (err) {
|
|
189
|
+
console.error(`[maps-svelte] Failed to add source "${id}":`, err);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Update GeoJSON data with fingerprinting and deferred execution
|
|
194
|
+
$effect(() => {
|
|
195
|
+
const { map, loaded } = mapContext;
|
|
196
|
+
|
|
197
|
+
if (!map || !loaded || !sourceAdded || type !== 'geojson' || !data) return;
|
|
198
|
+
|
|
199
|
+
// Create fingerprint for current data
|
|
200
|
+
const newFingerprint = createGeoJSONFingerprint(data);
|
|
201
|
+
|
|
202
|
+
// Skip update if fingerprint matches (content unchanged)
|
|
203
|
+
if (fingerprintEqual(lastDataFingerprint, newFingerprint)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Cancel any pending idle callback
|
|
208
|
+
if (pendingIdleCallback !== null) {
|
|
209
|
+
if (typeof cancelIdleCallback !== 'undefined') {
|
|
210
|
+
cancelIdleCallback(pendingIdleCallback);
|
|
211
|
+
} else {
|
|
212
|
+
clearTimeout(pendingIdleCallback);
|
|
213
|
+
}
|
|
214
|
+
pendingIdleCallback = null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Capture data for closure
|
|
218
|
+
const dataToSet = data;
|
|
219
|
+
|
|
220
|
+
// Defer setData to idle time for better performance
|
|
221
|
+
const performUpdate = () => {
|
|
222
|
+
pendingIdleCallback = null;
|
|
223
|
+
try {
|
|
224
|
+
const source = map.getSource(id) as maplibregl.GeoJSONSource | undefined;
|
|
225
|
+
if (source) {
|
|
226
|
+
source.setData(dataToSet as GeoJSON.GeoJSON);
|
|
227
|
+
lastDataFingerprint = newFingerprint;
|
|
228
|
+
}
|
|
229
|
+
} catch (err) {
|
|
230
|
+
// Source might not exist yet
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
if (typeof requestIdleCallback !== 'undefined') {
|
|
235
|
+
// Use requestIdleCallback with 100ms timeout fallback
|
|
236
|
+
pendingIdleCallback = requestIdleCallback(performUpdate, { timeout: 100 });
|
|
237
|
+
} else {
|
|
238
|
+
// Fallback for browsers without requestIdleCallback
|
|
239
|
+
pendingIdleCallback = setTimeout(performUpdate, 0) as unknown as number;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Fit to bounds effect (runs once after source is added)
|
|
244
|
+
$effect(() => {
|
|
245
|
+
const { map, loaded } = mapContext;
|
|
246
|
+
|
|
247
|
+
// Only run for GeoJSON sources with inline data (not URL strings)
|
|
248
|
+
if (!map || !loaded || !sourceAdded || type !== 'geojson') return;
|
|
249
|
+
if (!shouldFitBounds || initialFitDone) return;
|
|
250
|
+
if (typeof data !== 'object' || !data) return;
|
|
251
|
+
|
|
252
|
+
const calculatedBounds = calculateBounds(data as GeoJSONFeatureCollection);
|
|
253
|
+
if (!calculatedBounds) return;
|
|
254
|
+
|
|
255
|
+
// Extract and validate bounds values
|
|
256
|
+
// Use Number() to ensure values are actual numbers, not proxies
|
|
257
|
+
const swLng = Number(calculatedBounds[0][0]);
|
|
258
|
+
const swLat = Number(calculatedBounds[0][1]);
|
|
259
|
+
const neLng = Number(calculatedBounds[1][0]);
|
|
260
|
+
const neLat = Number(calculatedBounds[1][1]);
|
|
261
|
+
|
|
262
|
+
// Validate that all values are finite numbers
|
|
263
|
+
if (!isFinite(swLng) || !isFinite(swLat) || !isFinite(neLng) || !isFinite(neLat)) {
|
|
264
|
+
console.warn('[maps-svelte] Source: Invalid bounds calculated, skipping fitBounds');
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Check for reduced motion preference
|
|
269
|
+
const prefersReducedMotion =
|
|
270
|
+
typeof window !== 'undefined' &&
|
|
271
|
+
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
272
|
+
|
|
273
|
+
// Build options object, only including defined values
|
|
274
|
+
const options: { padding?: number; maxZoom?: number; animate?: boolean } = {
|
|
275
|
+
animate: fitBoundsAnimate && !prefersReducedMotion,
|
|
276
|
+
};
|
|
277
|
+
if (fitBoundsPadding !== undefined) options.padding = fitBoundsPadding;
|
|
278
|
+
if (fitBoundsMaxZoom !== undefined) options.maxZoom = fitBoundsMaxZoom;
|
|
279
|
+
|
|
280
|
+
// Use nested array format [[sw_lng, sw_lat], [ne_lng, ne_lat]]
|
|
281
|
+
map.fitBounds(
|
|
282
|
+
[
|
|
283
|
+
[swLng, swLat],
|
|
284
|
+
[neLng, neLat],
|
|
285
|
+
],
|
|
286
|
+
options
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
initialFitDone = true;
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Cleanup on destroy
|
|
293
|
+
onDestroy(() => {
|
|
294
|
+
// Cancel any pending idle callback to prevent stale updates
|
|
295
|
+
if (pendingIdleCallback !== null) {
|
|
296
|
+
if (typeof cancelIdleCallback !== 'undefined') {
|
|
297
|
+
cancelIdleCallback(pendingIdleCallback);
|
|
298
|
+
} else {
|
|
299
|
+
clearTimeout(pendingIdleCallback);
|
|
300
|
+
}
|
|
301
|
+
pendingIdleCallback = null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const { map } = mapContext;
|
|
305
|
+
|
|
306
|
+
if (map && sourceAdded) {
|
|
307
|
+
try {
|
|
308
|
+
// Remove all layers using this source first
|
|
309
|
+
const style = map.getStyle();
|
|
310
|
+
if (style?.layers) {
|
|
311
|
+
style.layers.forEach((layer) => {
|
|
312
|
+
if ('source' in layer && layer.source === id) {
|
|
313
|
+
map.removeLayer(layer.id);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (map.getSource(id)) {
|
|
319
|
+
map.removeSource(id);
|
|
320
|
+
}
|
|
321
|
+
} catch (err) {
|
|
322
|
+
// Ignore cleanup errors
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
</script>
|