maplibrex-svelte 0.1.0
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/LICENSE +21 -0
- package/README.md +300 -0
- package/dist/components/MapLibre.svelte +258 -0
- package/dist/components/MapLibre.svelte.d.ts +42 -0
- package/dist/components/controls/AttributionControl.svelte +21 -0
- package/dist/components/controls/AttributionControl.svelte.d.ts +11 -0
- package/dist/components/controls/Control.svelte +58 -0
- package/dist/components/controls/Control.svelte.d.ts +33 -0
- package/dist/components/controls/CustomControl.svelte +46 -0
- package/dist/components/controls/CustomControl.svelte.d.ts +13 -0
- package/dist/components/controls/FullscreenControl.svelte +32 -0
- package/dist/components/controls/FullscreenControl.svelte.d.ts +12 -0
- package/dist/components/controls/GeolocateControl.svelte +32 -0
- package/dist/components/controls/GeolocateControl.svelte.d.ts +12 -0
- package/dist/components/controls/GlobeControl.svelte +16 -0
- package/dist/components/controls/GlobeControl.svelte.d.ts +11 -0
- package/dist/components/controls/LogoControl.svelte +21 -0
- package/dist/components/controls/LogoControl.svelte.d.ts +11 -0
- package/dist/components/controls/NavigationControl.svelte +21 -0
- package/dist/components/controls/NavigationControl.svelte.d.ts +11 -0
- package/dist/components/controls/ScaleControl.svelte +21 -0
- package/dist/components/controls/ScaleControl.svelte.d.ts +11 -0
- package/dist/components/controls/TerrainControl.svelte +21 -0
- package/dist/components/controls/TerrainControl.svelte.d.ts +11 -0
- package/dist/components/data/FeatureState.svelte +56 -0
- package/dist/components/data/FeatureState.svelte.d.ts +13 -0
- package/dist/components/data/Protocol.svelte +24 -0
- package/dist/components/data/Protocol.svelte.d.ts +9 -0
- package/dist/components/layers/BackgroundLayer.svelte +21 -0
- package/dist/components/layers/BackgroundLayer.svelte.d.ts +17 -0
- package/dist/components/layers/CircleLayer.svelte +25 -0
- package/dist/components/layers/CircleLayer.svelte.d.ts +21 -0
- package/dist/components/layers/ColorReliefLayer.svelte +25 -0
- package/dist/components/layers/ColorReliefLayer.svelte.d.ts +21 -0
- package/dist/components/layers/CustomLayer.svelte +35 -0
- package/dist/components/layers/CustomLayer.svelte.d.ts +13 -0
- package/dist/components/layers/FillExtrusionLayer.svelte +25 -0
- package/dist/components/layers/FillExtrusionLayer.svelte.d.ts +21 -0
- package/dist/components/layers/FillLayer.svelte +25 -0
- package/dist/components/layers/FillLayer.svelte.d.ts +21 -0
- package/dist/components/layers/HeatmapLayer.svelte +25 -0
- package/dist/components/layers/HeatmapLayer.svelte.d.ts +21 -0
- package/dist/components/layers/HillshadeLayer.svelte +25 -0
- package/dist/components/layers/HillshadeLayer.svelte.d.ts +21 -0
- package/dist/components/layers/Layer.svelte +204 -0
- package/dist/components/layers/Layer.svelte.d.ts +29 -0
- package/dist/components/layers/LineLayer.svelte +25 -0
- package/dist/components/layers/LineLayer.svelte.d.ts +21 -0
- package/dist/components/layers/RasterLayer.svelte +25 -0
- package/dist/components/layers/RasterLayer.svelte.d.ts +21 -0
- package/dist/components/layers/SymbolLayer.svelte +25 -0
- package/dist/components/layers/SymbolLayer.svelte.d.ts +21 -0
- package/dist/components/overlays/Marker.svelte +204 -0
- package/dist/components/overlays/Marker.svelte.d.ts +27 -0
- package/dist/components/overlays/Popup.svelte +184 -0
- package/dist/components/overlays/Popup.svelte.d.ts +24 -0
- package/dist/components/sources/CanvasSource.svelte +16 -0
- package/dist/components/sources/CanvasSource.svelte.d.ts +13 -0
- package/dist/components/sources/GeoJSONSource.svelte +16 -0
- package/dist/components/sources/GeoJSONSource.svelte.d.ts +13 -0
- package/dist/components/sources/ImageSource.svelte +16 -0
- package/dist/components/sources/ImageSource.svelte.d.ts +13 -0
- package/dist/components/sources/RasterDEMSource.svelte +16 -0
- package/dist/components/sources/RasterDEMSource.svelte.d.ts +13 -0
- package/dist/components/sources/RasterSource.svelte +16 -0
- package/dist/components/sources/RasterSource.svelte.d.ts +13 -0
- package/dist/components/sources/Source.svelte +88 -0
- package/dist/components/sources/Source.svelte.d.ts +14 -0
- package/dist/components/sources/VectorSource.svelte +16 -0
- package/dist/components/sources/VectorSource.svelte.d.ts +13 -0
- package/dist/components/sources/VideoSource.svelte +16 -0
- package/dist/components/sources/VideoSource.svelte.d.ts +13 -0
- package/dist/components/style/GlobalState.svelte +67 -0
- package/dist/components/style/GlobalState.svelte.d.ts +8 -0
- package/dist/components/style/Image.svelte +92 -0
- package/dist/components/style/Image.svelte.d.ts +14 -0
- package/dist/components/style/Light.svelte +31 -0
- package/dist/components/style/Light.svelte.d.ts +4 -0
- package/dist/components/style/Projection.svelte +31 -0
- package/dist/components/style/Projection.svelte.d.ts +4 -0
- package/dist/components/style/Sky.svelte +31 -0
- package/dist/components/style/Sky.svelte.d.ts +4 -0
- package/dist/components/style/Terrain.svelte +50 -0
- package/dist/components/style/Terrain.svelte.d.ts +9 -0
- package/dist/context.d.ts +41 -0
- package/dist/context.js +31 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +48 -0
- package/dist/internal/bind-events.svelte.d.ts +26 -0
- package/dist/internal/bind-events.svelte.js +49 -0
- package/dist/internal/camera.d.ts +15 -0
- package/dist/internal/camera.js +33 -0
- package/dist/internal/deep-equal.d.ts +6 -0
- package/dist/internal/deep-equal.js +25 -0
- package/dist/internal/diff.d.ts +15 -0
- package/dist/internal/diff.js +29 -0
- package/dist/internal/events.d.ts +22 -0
- package/dist/internal/events.js +40 -0
- package/dist/internal/interactions.d.ts +22 -0
- package/dist/internal/interactions.js +36 -0
- package/dist/internal/source.d.ts +25 -0
- package/dist/internal/source.js +25 -0
- package/dist/internal/stable-key.d.ts +9 -0
- package/dist/internal/stable-key.js +17 -0
- package/dist/internal/style-ops.d.ts +26 -0
- package/dist/internal/style-ops.js +56 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +1 -0
- package/package.json +92 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Snippet } from 'svelte';
|
|
2
|
+
import { type AnySourceSpecification } from '../../internal/source.js';
|
|
3
|
+
type Props = {
|
|
4
|
+
/** Unique within the style. Changing it recreates the source. */
|
|
5
|
+
id: string;
|
|
6
|
+
spec: AnySourceSpecification;
|
|
7
|
+
/** Layers nested here inherit this source id automatically. */
|
|
8
|
+
children?: Snippet<[{
|
|
9
|
+
id: string;
|
|
10
|
+
}]>;
|
|
11
|
+
};
|
|
12
|
+
declare const Source: import("svelte").Component<Props, {}, "">;
|
|
13
|
+
type Source = ReturnType<typeof Source>;
|
|
14
|
+
export default Source;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { VectorSourceSpecification } from 'maplibre-gl';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import Source from './Source.svelte';
|
|
5
|
+
|
|
6
|
+
type Props = Omit<VectorSourceSpecification, 'type'> & {
|
|
7
|
+
/** Unique within the style. Changing it recreates the source. */
|
|
8
|
+
id: string;
|
|
9
|
+
/** Layers nested here inherit this source id automatically. */
|
|
10
|
+
children?: Snippet<[{ id: string }]>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let { id, children, ...spec }: Props = $props();
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<Source {id} spec={{ type: 'vector', ...spec }} {children} />
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { VectorSourceSpecification } from 'maplibre-gl';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type Props = Omit<VectorSourceSpecification, 'type'> & {
|
|
4
|
+
/** Unique within the style. Changing it recreates the source. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** Layers nested here inherit this source id automatically. */
|
|
7
|
+
children?: Snippet<[{
|
|
8
|
+
id: string;
|
|
9
|
+
}]>;
|
|
10
|
+
};
|
|
11
|
+
declare const VectorSource: import("svelte").Component<Props, {}, "">;
|
|
12
|
+
type VectorSource = ReturnType<typeof VectorSource>;
|
|
13
|
+
export default VectorSource;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { VideoSourceSpecification } from 'maplibre-gl';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import Source from './Source.svelte';
|
|
5
|
+
|
|
6
|
+
type Props = Omit<VideoSourceSpecification, 'type'> & {
|
|
7
|
+
/** Unique within the style. Changing it recreates the source. */
|
|
8
|
+
id: string;
|
|
9
|
+
/** Layers nested here inherit this source id automatically. */
|
|
10
|
+
children?: Snippet<[{ id: string }]>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let { id, children, ...spec }: Props = $props();
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<Source {id} spec={{ type: 'video', ...spec }} {children} />
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { VideoSourceSpecification } from 'maplibre-gl';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
type Props = Omit<VideoSourceSpecification, 'type'> & {
|
|
4
|
+
/** Unique within the style. Changing it recreates the source. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** Layers nested here inherit this source id automatically. */
|
|
7
|
+
children?: Snippet<[{
|
|
8
|
+
id: string;
|
|
9
|
+
}]>;
|
|
10
|
+
};
|
|
11
|
+
declare const VideoSource: import("svelte").Component<Props, {}, "">;
|
|
12
|
+
type VideoSource = ReturnType<typeof VideoSource>;
|
|
13
|
+
export default VideoSource;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { getMapContext } from '../../context.js';
|
|
4
|
+
import { diffRecords } from '../../internal/diff.js';
|
|
5
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Every prop becomes one global state property, reachable from style
|
|
9
|
+
* expressions through `["global-state", "<name>"]`.
|
|
10
|
+
*/
|
|
11
|
+
type Props = Record<string, unknown>;
|
|
12
|
+
|
|
13
|
+
let values: Props = $props();
|
|
14
|
+
|
|
15
|
+
const context = getMapContext();
|
|
16
|
+
|
|
17
|
+
let mounted = $state(false);
|
|
18
|
+
let applied = $state<Record<string, unknown> | undefined>();
|
|
19
|
+
|
|
20
|
+
// Global state belongs to the style, so unmounting puts back what the style
|
|
21
|
+
// declared rather than leaving the map with this component's values.
|
|
22
|
+
$effect(() => {
|
|
23
|
+
const map = context.map;
|
|
24
|
+
const ready = context.ready;
|
|
25
|
+
void context.styleEpoch;
|
|
26
|
+
|
|
27
|
+
if (!map || !ready) return;
|
|
28
|
+
|
|
29
|
+
const previous = untrack(() => map.getGlobalState());
|
|
30
|
+
const touched = untrack(() => Object.keys(values));
|
|
31
|
+
|
|
32
|
+
untrack(() => {
|
|
33
|
+
const next = { ...values };
|
|
34
|
+
for (const [name, value] of Object.entries(next)) {
|
|
35
|
+
map.setGlobalStateProperty(name, value);
|
|
36
|
+
}
|
|
37
|
+
applied = next;
|
|
38
|
+
mounted = true;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return () => {
|
|
42
|
+
mounted = false;
|
|
43
|
+
applied = undefined;
|
|
44
|
+
if (!isStyleAlive(map)) return;
|
|
45
|
+
// `null` is how MapLibre resets a global state property to its default.
|
|
46
|
+
for (const name of touched) map.setGlobalStateProperty(name, previous[name] ?? null);
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
$effect(() => {
|
|
51
|
+
const map = context.map;
|
|
52
|
+
const next = { ...values };
|
|
53
|
+
if (!map || !mounted) return;
|
|
54
|
+
|
|
55
|
+
const { changed, removed } = diffRecords<unknown>(
|
|
56
|
+
untrack(() => applied),
|
|
57
|
+
next
|
|
58
|
+
);
|
|
59
|
+
if (changed.length === 0 && removed.length === 0) return;
|
|
60
|
+
|
|
61
|
+
untrack(() => {
|
|
62
|
+
for (const [name, value] of changed) map.setGlobalStateProperty(name, value);
|
|
63
|
+
for (const name of removed) map.setGlobalStateProperty(name, null);
|
|
64
|
+
applied = next;
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
</script>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every prop becomes one global state property, reachable from style
|
|
3
|
+
* expressions through `["global-state", "<name>"]`.
|
|
4
|
+
*/
|
|
5
|
+
type Props = Record<string, unknown>;
|
|
6
|
+
declare const GlobalState: import("svelte").Component<Props, {}, "">;
|
|
7
|
+
type GlobalState = ReturnType<typeof GlobalState>;
|
|
8
|
+
export default GlobalState;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { StyleImageMetadata, StyleImageSource } from 'maplibre-gl';
|
|
3
|
+
import { untrack } from 'svelte';
|
|
4
|
+
import { getMapContext } from '../../context.js';
|
|
5
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
6
|
+
|
|
7
|
+
type Props = Partial<StyleImageMetadata> & {
|
|
8
|
+
/** The name symbol layers refer to through `icon-image`. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Loaded with `map.loadImage`. Give either this or `image`, not both. */
|
|
11
|
+
url?: string;
|
|
12
|
+
/** An already-decoded image: canvas output, generated pixels, an SVG blob. */
|
|
13
|
+
image?: StyleImageSource;
|
|
14
|
+
/** Called when `url` fails to load, instead of leaving the map silent. */
|
|
15
|
+
onerror?: (error: unknown) => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
let {
|
|
19
|
+
id,
|
|
20
|
+
url,
|
|
21
|
+
image,
|
|
22
|
+
onerror,
|
|
23
|
+
pixelRatio,
|
|
24
|
+
sdf,
|
|
25
|
+
stretchX,
|
|
26
|
+
stretchY,
|
|
27
|
+
content,
|
|
28
|
+
textFitWidth,
|
|
29
|
+
textFitHeight
|
|
30
|
+
}: Props = $props();
|
|
31
|
+
|
|
32
|
+
const context = getMapContext();
|
|
33
|
+
|
|
34
|
+
const metadata = $derived({
|
|
35
|
+
...(pixelRatio !== undefined ? { pixelRatio } : {}),
|
|
36
|
+
...(sdf !== undefined ? { sdf } : {}),
|
|
37
|
+
...(stretchX !== undefined ? { stretchX } : {}),
|
|
38
|
+
...(stretchY !== undefined ? { stretchY } : {}),
|
|
39
|
+
...(content !== undefined ? { content } : {}),
|
|
40
|
+
...(textFitWidth !== undefined ? { textFitWidth } : {}),
|
|
41
|
+
...(textFitHeight !== undefined ? { textFitHeight } : {})
|
|
42
|
+
} satisfies Partial<StyleImageMetadata>);
|
|
43
|
+
|
|
44
|
+
// A style swap drops registered images along with everything else, so this
|
|
45
|
+
// tracks the style epoch like sources and layers do.
|
|
46
|
+
$effect(() => {
|
|
47
|
+
const map = context.map;
|
|
48
|
+
const ready = context.ready;
|
|
49
|
+
void context.styleEpoch;
|
|
50
|
+
|
|
51
|
+
const imageId = id;
|
|
52
|
+
const source = image;
|
|
53
|
+
const href = url;
|
|
54
|
+
|
|
55
|
+
if (!map || !ready) return;
|
|
56
|
+
|
|
57
|
+
if (source === undefined && href === undefined) {
|
|
58
|
+
throw new Error(`[maplibrex] <Image id="${imageId}"> needs either \`url\` or \`image\`.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let cancelled = false;
|
|
62
|
+
|
|
63
|
+
const register = (resolved: StyleImageSource) => {
|
|
64
|
+
if (cancelled) return;
|
|
65
|
+
// `addImage` throws on a duplicate id, and a re-run after a prop change
|
|
66
|
+
// legitimately arrives with the image already present.
|
|
67
|
+
if (map.hasImage(imageId)) map.updateImage(imageId, resolved);
|
|
68
|
+
else
|
|
69
|
+
map.addImage(
|
|
70
|
+
imageId,
|
|
71
|
+
resolved,
|
|
72
|
+
untrack(() => metadata)
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (source !== undefined) {
|
|
77
|
+
register(source);
|
|
78
|
+
} else {
|
|
79
|
+
map
|
|
80
|
+
.loadImage(href as string)
|
|
81
|
+
.then((response) => register(response.data))
|
|
82
|
+
.catch((error: unknown) => {
|
|
83
|
+
if (!cancelled) onerror?.(error);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return () => {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
if (isStyleAlive(map) && map.hasImage(imageId)) map.removeImage(imageId);
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
</script>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { StyleImageMetadata, StyleImageSource } from 'maplibre-gl';
|
|
2
|
+
type Props = Partial<StyleImageMetadata> & {
|
|
3
|
+
/** The name symbol layers refer to through `icon-image`. */
|
|
4
|
+
id: string;
|
|
5
|
+
/** Loaded with `map.loadImage`. Give either this or `image`, not both. */
|
|
6
|
+
url?: string;
|
|
7
|
+
/** An already-decoded image: canvas output, generated pixels, an SVG blob. */
|
|
8
|
+
image?: StyleImageSource;
|
|
9
|
+
/** Called when `url` fails to load, instead of leaving the map silent. */
|
|
10
|
+
onerror?: (error: unknown) => void;
|
|
11
|
+
};
|
|
12
|
+
declare const Image: import("svelte").Component<Props, {}, "">;
|
|
13
|
+
type Image = ReturnType<typeof Image>;
|
|
14
|
+
export default Image;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { LightSpecification } from 'maplibre-gl';
|
|
3
|
+
import { untrack } from 'svelte';
|
|
4
|
+
import { getMapContext } from '../../context.js';
|
|
5
|
+
import { deepEqual } from '../../internal/deep-equal.js';
|
|
6
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
7
|
+
|
|
8
|
+
type Props = LightSpecification;
|
|
9
|
+
|
|
10
|
+
let light: Props = $props();
|
|
11
|
+
|
|
12
|
+
const context = getMapContext();
|
|
13
|
+
|
|
14
|
+
$effect(() => {
|
|
15
|
+
const map = context.map;
|
|
16
|
+
const ready = context.ready;
|
|
17
|
+
void context.styleEpoch;
|
|
18
|
+
const next = { ...light };
|
|
19
|
+
|
|
20
|
+
if (!map || !ready) return;
|
|
21
|
+
|
|
22
|
+
const previous = untrack(() => map.getLight());
|
|
23
|
+
if (deepEqual(previous, next)) return;
|
|
24
|
+
|
|
25
|
+
map.setLight(next);
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
if (isStyleAlive(map)) map.setLight(previous);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
</script>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ProjectionSpecification } from 'maplibre-gl';
|
|
3
|
+
import { untrack } from 'svelte';
|
|
4
|
+
import { getMapContext } from '../../context.js';
|
|
5
|
+
import { deepEqual } from '../../internal/deep-equal.js';
|
|
6
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
7
|
+
|
|
8
|
+
type Props = ProjectionSpecification;
|
|
9
|
+
|
|
10
|
+
let projection: Props = $props();
|
|
11
|
+
|
|
12
|
+
const context = getMapContext();
|
|
13
|
+
|
|
14
|
+
$effect(() => {
|
|
15
|
+
const map = context.map;
|
|
16
|
+
const ready = context.ready;
|
|
17
|
+
void context.styleEpoch;
|
|
18
|
+
const next = { ...projection };
|
|
19
|
+
|
|
20
|
+
if (!map || !ready) return;
|
|
21
|
+
|
|
22
|
+
const previous = untrack(() => map.getProjection());
|
|
23
|
+
if (deepEqual(previous, next)) return;
|
|
24
|
+
|
|
25
|
+
map.setProjection(next);
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
if (isStyleAlive(map)) map.setProjection(previous);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
</script>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { SkySpecification } from 'maplibre-gl';
|
|
3
|
+
import { untrack } from 'svelte';
|
|
4
|
+
import { getMapContext } from '../../context.js';
|
|
5
|
+
import { deepEqual } from '../../internal/deep-equal.js';
|
|
6
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
7
|
+
|
|
8
|
+
type Props = SkySpecification;
|
|
9
|
+
|
|
10
|
+
let sky: Props = $props();
|
|
11
|
+
|
|
12
|
+
const context = getMapContext();
|
|
13
|
+
|
|
14
|
+
$effect(() => {
|
|
15
|
+
const map = context.map;
|
|
16
|
+
const ready = context.ready;
|
|
17
|
+
void context.styleEpoch;
|
|
18
|
+
const next = { ...sky };
|
|
19
|
+
|
|
20
|
+
if (!map || !ready) return;
|
|
21
|
+
|
|
22
|
+
const previous = untrack(() => map.getSky());
|
|
23
|
+
if (deepEqual(previous, next)) return;
|
|
24
|
+
|
|
25
|
+
map.setSky(next);
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
if (isStyleAlive(map)) map.setSky(previous);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
</script>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { TerrainSpecification } from 'maplibre-gl';
|
|
3
|
+
import { untrack } from 'svelte';
|
|
4
|
+
import { getMapContext, getSourceContext } from '../../context.js';
|
|
5
|
+
import { isStyleAlive } from '../../internal/style-ops.js';
|
|
6
|
+
|
|
7
|
+
type Props = {
|
|
8
|
+
/** A raster-dem source id. Defaults to the enclosing source component. */
|
|
9
|
+
source?: string;
|
|
10
|
+
/** @defaultValue 1 */
|
|
11
|
+
exaggeration?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
let { source, exaggeration }: Props = $props();
|
|
15
|
+
|
|
16
|
+
const context = getMapContext();
|
|
17
|
+
const sourceContext = getSourceContext();
|
|
18
|
+
const resolvedSource = $derived(source ?? sourceContext?.id);
|
|
19
|
+
|
|
20
|
+
// Terrain is style-level state rather than a layer, so unmounting restores
|
|
21
|
+
// whatever the style itself declared instead of blindly disabling it.
|
|
22
|
+
$effect(() => {
|
|
23
|
+
const map = context.map;
|
|
24
|
+
const ready = context.ready;
|
|
25
|
+
void context.styleEpoch;
|
|
26
|
+
void sourceContext?.epoch;
|
|
27
|
+
|
|
28
|
+
const sourceId = resolvedSource;
|
|
29
|
+
const next = exaggeration;
|
|
30
|
+
|
|
31
|
+
if (!map || !ready) return;
|
|
32
|
+
|
|
33
|
+
if (!sourceId) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
'[maplibrex] <Terrain> needs a raster-dem source: pass `source` or nest it inside <RasterDEMSource>.'
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const previous = untrack(() => map.getTerrain());
|
|
40
|
+
|
|
41
|
+
map.setTerrain({
|
|
42
|
+
source: sourceId,
|
|
43
|
+
...(next !== undefined ? { exaggeration: next } : {})
|
|
44
|
+
} satisfies TerrainSpecification);
|
|
45
|
+
|
|
46
|
+
return () => {
|
|
47
|
+
if (isStyleAlive(map)) map.setTerrain(previous);
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
</script>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type Props = {
|
|
2
|
+
/** A raster-dem source id. Defaults to the enclosing source component. */
|
|
3
|
+
source?: string;
|
|
4
|
+
/** @defaultValue 1 */
|
|
5
|
+
exaggeration?: number;
|
|
6
|
+
};
|
|
7
|
+
declare const Terrain: import("svelte").Component<Props, {}, "">;
|
|
8
|
+
type Terrain = ReturnType<typeof Terrain>;
|
|
9
|
+
export default Terrain;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Map as MapLibreMap, Marker } from 'maplibre-gl';
|
|
2
|
+
/**
|
|
3
|
+
* What every descendant of `<MapLibre>` needs in order to touch the map safely.
|
|
4
|
+
*
|
|
5
|
+
* Properties are declared readonly and are backed by getters over `$state`, so
|
|
6
|
+
* consumers read them inside `$effect` and re-run when the map appears, becomes
|
|
7
|
+
* usable, or swaps its style.
|
|
8
|
+
*/
|
|
9
|
+
export interface MapContext {
|
|
10
|
+
/** The live instance, or `null` before mount and after teardown. */
|
|
11
|
+
readonly map: MapLibreMap | null;
|
|
12
|
+
/** `true` once the style is loaded and sources/layers can be added. */
|
|
13
|
+
readonly ready: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Increments on every completed style load. `setStyle` wipes user-added
|
|
16
|
+
* sources and layers, so descendants track this to re-add themselves.
|
|
17
|
+
*/
|
|
18
|
+
readonly styleEpoch: number;
|
|
19
|
+
}
|
|
20
|
+
/** Lets a nested `<Layer>` inherit the id of its enclosing source component. */
|
|
21
|
+
export interface SourceContext {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
/** Increments when the source is recreated, so child layers re-add. */
|
|
24
|
+
readonly epoch: number;
|
|
25
|
+
}
|
|
26
|
+
/** Lets a nested `<Popup>` attach itself to its enclosing `<Marker>`. */
|
|
27
|
+
export interface MarkerContext {
|
|
28
|
+
readonly marker: Marker | null;
|
|
29
|
+
}
|
|
30
|
+
export declare function setMapContext(context: MapContext): MapContext;
|
|
31
|
+
/**
|
|
32
|
+
* @throws if used outside a `<MapLibre>` subtree, which is a wiring mistake
|
|
33
|
+
* rather than a runtime condition worth handling.
|
|
34
|
+
*/
|
|
35
|
+
export declare function getMapContext(): MapContext;
|
|
36
|
+
export declare function setSourceContext(context: SourceContext): SourceContext;
|
|
37
|
+
/** Returns `null` when the component is not nested inside a source. */
|
|
38
|
+
export declare function getSourceContext(): SourceContext | null;
|
|
39
|
+
export declare function setMarkerContext(context: MarkerContext): MarkerContext;
|
|
40
|
+
/** Returns `null` when the component is not nested inside a marker. */
|
|
41
|
+
export declare function getMarkerContext(): MarkerContext | null;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getContext, hasContext, setContext } from 'svelte';
|
|
2
|
+
const MAP_CONTEXT = Symbol.for('maplibrex.map');
|
|
3
|
+
const SOURCE_CONTEXT = Symbol.for('maplibrex.source');
|
|
4
|
+
const MARKER_CONTEXT = Symbol.for('maplibrex.marker');
|
|
5
|
+
export function setMapContext(context) {
|
|
6
|
+
return setContext(MAP_CONTEXT, context);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @throws if used outside a `<MapLibre>` subtree, which is a wiring mistake
|
|
10
|
+
* rather than a runtime condition worth handling.
|
|
11
|
+
*/
|
|
12
|
+
export function getMapContext() {
|
|
13
|
+
if (!hasContext(MAP_CONTEXT)) {
|
|
14
|
+
throw new Error('[maplibrex] This component must be rendered inside <MapLibre>.');
|
|
15
|
+
}
|
|
16
|
+
return getContext(MAP_CONTEXT);
|
|
17
|
+
}
|
|
18
|
+
export function setSourceContext(context) {
|
|
19
|
+
return setContext(SOURCE_CONTEXT, context);
|
|
20
|
+
}
|
|
21
|
+
/** Returns `null` when the component is not nested inside a source. */
|
|
22
|
+
export function getSourceContext() {
|
|
23
|
+
return hasContext(SOURCE_CONTEXT) ? getContext(SOURCE_CONTEXT) : null;
|
|
24
|
+
}
|
|
25
|
+
export function setMarkerContext(context) {
|
|
26
|
+
return setContext(MARKER_CONTEXT, context);
|
|
27
|
+
}
|
|
28
|
+
/** Returns `null` when the component is not nested inside a marker. */
|
|
29
|
+
export function getMarkerContext() {
|
|
30
|
+
return hasContext(MARKER_CONTEXT) ? getContext(MARKER_CONTEXT) : null;
|
|
31
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* maplibrex-svelte — component-oriented Svelte 5 bindings for MapLibre GL JS.
|
|
3
|
+
*
|
|
4
|
+
* Remember to load the upstream stylesheet once in your app:
|
|
5
|
+
* `import 'maplibre-gl/dist/maplibre-gl.css';`
|
|
6
|
+
*/
|
|
7
|
+
export { default as MapLibre } from './components/MapLibre.svelte';
|
|
8
|
+
export { default as Control } from './components/controls/Control.svelte';
|
|
9
|
+
export { default as CustomControl } from './components/controls/CustomControl.svelte';
|
|
10
|
+
export { default as AttributionControl } from './components/controls/AttributionControl.svelte';
|
|
11
|
+
export { default as FullscreenControl } from './components/controls/FullscreenControl.svelte';
|
|
12
|
+
export { default as GeolocateControl } from './components/controls/GeolocateControl.svelte';
|
|
13
|
+
export { default as GlobeControl } from './components/controls/GlobeControl.svelte';
|
|
14
|
+
export { default as LogoControl } from './components/controls/LogoControl.svelte';
|
|
15
|
+
export { default as NavigationControl } from './components/controls/NavigationControl.svelte';
|
|
16
|
+
export { default as ScaleControl } from './components/controls/ScaleControl.svelte';
|
|
17
|
+
export { default as TerrainControl } from './components/controls/TerrainControl.svelte';
|
|
18
|
+
export { default as Source } from './components/sources/Source.svelte';
|
|
19
|
+
export { default as GeoJSONSource } from './components/sources/GeoJSONSource.svelte';
|
|
20
|
+
export { default as ImageSource } from './components/sources/ImageSource.svelte';
|
|
21
|
+
export { default as RasterDEMSource } from './components/sources/RasterDEMSource.svelte';
|
|
22
|
+
export { default as RasterSource } from './components/sources/RasterSource.svelte';
|
|
23
|
+
export { default as VectorSource } from './components/sources/VectorSource.svelte';
|
|
24
|
+
export { default as CanvasSource } from './components/sources/CanvasSource.svelte';
|
|
25
|
+
export { default as VideoSource } from './components/sources/VideoSource.svelte';
|
|
26
|
+
export { default as Layer } from './components/layers/Layer.svelte';
|
|
27
|
+
export { default as CustomLayer } from './components/layers/CustomLayer.svelte';
|
|
28
|
+
export { default as BackgroundLayer } from './components/layers/BackgroundLayer.svelte';
|
|
29
|
+
export { default as CircleLayer } from './components/layers/CircleLayer.svelte';
|
|
30
|
+
export { default as ColorReliefLayer } from './components/layers/ColorReliefLayer.svelte';
|
|
31
|
+
export { default as FillExtrusionLayer } from './components/layers/FillExtrusionLayer.svelte';
|
|
32
|
+
export { default as FillLayer } from './components/layers/FillLayer.svelte';
|
|
33
|
+
export { default as HeatmapLayer } from './components/layers/HeatmapLayer.svelte';
|
|
34
|
+
export { default as HillshadeLayer } from './components/layers/HillshadeLayer.svelte';
|
|
35
|
+
export { default as LineLayer } from './components/layers/LineLayer.svelte';
|
|
36
|
+
export { default as RasterLayer } from './components/layers/RasterLayer.svelte';
|
|
37
|
+
export { default as SymbolLayer } from './components/layers/SymbolLayer.svelte';
|
|
38
|
+
export { default as Marker } from './components/overlays/Marker.svelte';
|
|
39
|
+
export { default as Popup } from './components/overlays/Popup.svelte';
|
|
40
|
+
export { default as GlobalState } from './components/style/GlobalState.svelte';
|
|
41
|
+
export { default as Image } from './components/style/Image.svelte';
|
|
42
|
+
export { default as Light } from './components/style/Light.svelte';
|
|
43
|
+
export { default as Projection } from './components/style/Projection.svelte';
|
|
44
|
+
export { default as Sky } from './components/style/Sky.svelte';
|
|
45
|
+
export { default as Terrain } from './components/style/Terrain.svelte';
|
|
46
|
+
export { default as FeatureState } from './components/data/FeatureState.svelte';
|
|
47
|
+
export { default as Protocol } from './components/data/Protocol.svelte';
|
|
48
|
+
export { getMapContext, getMarkerContext, getSourceContext, setMapContext, setMarkerContext, setSourceContext } from './context.js';
|
|
49
|
+
export type { MapContext, MarkerContext, SourceContext } from './context.js';
|
|
50
|
+
export type { InteractionName, InteractionRequest } from './internal/interactions.js';
|
|
51
|
+
export type { AnySourceSpecification } from './internal/source.js';
|
|
52
|
+
export type { CameraMode, LayerEventProps, MapEventProps, MarkerEventProps, PopupEventProps } from './types.js';
|
|
53
|
+
/**
|
|
54
|
+
* Re-exported so consumers can type their own code without a direct dependency
|
|
55
|
+
* on the underlying package.
|
|
56
|
+
*/
|
|
57
|
+
export type { CustomLayerInterface, FilterSpecification, LayerSpecification, LightSpecification, LngLatBoundsLike, LngLatLike, Map as MapLibreMap, MapOptions, PaddingOptions, ProjectionSpecification, SkySpecification, SourceSpecification, StyleImageMetadata, StyleImageSource, StyleSpecification, TerrainSpecification } from 'maplibre-gl';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* maplibrex-svelte — component-oriented Svelte 5 bindings for MapLibre GL JS.
|
|
3
|
+
*
|
|
4
|
+
* Remember to load the upstream stylesheet once in your app:
|
|
5
|
+
* `import 'maplibre-gl/dist/maplibre-gl.css';`
|
|
6
|
+
*/
|
|
7
|
+
export { default as MapLibre } from './components/MapLibre.svelte';
|
|
8
|
+
export { default as Control } from './components/controls/Control.svelte';
|
|
9
|
+
export { default as CustomControl } from './components/controls/CustomControl.svelte';
|
|
10
|
+
export { default as AttributionControl } from './components/controls/AttributionControl.svelte';
|
|
11
|
+
export { default as FullscreenControl } from './components/controls/FullscreenControl.svelte';
|
|
12
|
+
export { default as GeolocateControl } from './components/controls/GeolocateControl.svelte';
|
|
13
|
+
export { default as GlobeControl } from './components/controls/GlobeControl.svelte';
|
|
14
|
+
export { default as LogoControl } from './components/controls/LogoControl.svelte';
|
|
15
|
+
export { default as NavigationControl } from './components/controls/NavigationControl.svelte';
|
|
16
|
+
export { default as ScaleControl } from './components/controls/ScaleControl.svelte';
|
|
17
|
+
export { default as TerrainControl } from './components/controls/TerrainControl.svelte';
|
|
18
|
+
export { default as Source } from './components/sources/Source.svelte';
|
|
19
|
+
export { default as GeoJSONSource } from './components/sources/GeoJSONSource.svelte';
|
|
20
|
+
export { default as ImageSource } from './components/sources/ImageSource.svelte';
|
|
21
|
+
export { default as RasterDEMSource } from './components/sources/RasterDEMSource.svelte';
|
|
22
|
+
export { default as RasterSource } from './components/sources/RasterSource.svelte';
|
|
23
|
+
export { default as VectorSource } from './components/sources/VectorSource.svelte';
|
|
24
|
+
export { default as CanvasSource } from './components/sources/CanvasSource.svelte';
|
|
25
|
+
export { default as VideoSource } from './components/sources/VideoSource.svelte';
|
|
26
|
+
export { default as Layer } from './components/layers/Layer.svelte';
|
|
27
|
+
export { default as CustomLayer } from './components/layers/CustomLayer.svelte';
|
|
28
|
+
export { default as BackgroundLayer } from './components/layers/BackgroundLayer.svelte';
|
|
29
|
+
export { default as CircleLayer } from './components/layers/CircleLayer.svelte';
|
|
30
|
+
export { default as ColorReliefLayer } from './components/layers/ColorReliefLayer.svelte';
|
|
31
|
+
export { default as FillExtrusionLayer } from './components/layers/FillExtrusionLayer.svelte';
|
|
32
|
+
export { default as FillLayer } from './components/layers/FillLayer.svelte';
|
|
33
|
+
export { default as HeatmapLayer } from './components/layers/HeatmapLayer.svelte';
|
|
34
|
+
export { default as HillshadeLayer } from './components/layers/HillshadeLayer.svelte';
|
|
35
|
+
export { default as LineLayer } from './components/layers/LineLayer.svelte';
|
|
36
|
+
export { default as RasterLayer } from './components/layers/RasterLayer.svelte';
|
|
37
|
+
export { default as SymbolLayer } from './components/layers/SymbolLayer.svelte';
|
|
38
|
+
export { default as Marker } from './components/overlays/Marker.svelte';
|
|
39
|
+
export { default as Popup } from './components/overlays/Popup.svelte';
|
|
40
|
+
export { default as GlobalState } from './components/style/GlobalState.svelte';
|
|
41
|
+
export { default as Image } from './components/style/Image.svelte';
|
|
42
|
+
export { default as Light } from './components/style/Light.svelte';
|
|
43
|
+
export { default as Projection } from './components/style/Projection.svelte';
|
|
44
|
+
export { default as Sky } from './components/style/Sky.svelte';
|
|
45
|
+
export { default as Terrain } from './components/style/Terrain.svelte';
|
|
46
|
+
export { default as FeatureState } from './components/data/FeatureState.svelte';
|
|
47
|
+
export { default as Protocol } from './components/data/Protocol.svelte';
|
|
48
|
+
export { getMapContext, getMarkerContext, getSourceContext, setMapContext, setMarkerContext, setSourceContext } from './context.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Map as MapLibreMap, Subscription } from 'maplibre-gl';
|
|
2
|
+
/**
|
|
3
|
+
* MapLibre's `on` is a family of overloads with no single structural type:
|
|
4
|
+
* `Map` adds layer-scoped variants, while `Marker` and `Popup` narrow the
|
|
5
|
+
* payload to their own event maps. Components already type their `on<event>`
|
|
6
|
+
* props against those maps, so the erasure stays confined to this module.
|
|
7
|
+
*/
|
|
8
|
+
interface EventSource {
|
|
9
|
+
on(type: never, listener: never): Subscription;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Keeps MapLibre subscriptions in sync with a component's `on<event>` props.
|
|
13
|
+
*
|
|
14
|
+
* Subscriptions are rebuilt only when the SET OF EVENT NAMES changes. Handler
|
|
15
|
+
* identity is deliberately not tracked: inline arrow functions get a new
|
|
16
|
+
* identity on every render, and re-subscribing on each of them would churn
|
|
17
|
+
* listeners on a hot path. The indirection below always calls the current
|
|
18
|
+
* handler, so the latest closure still wins.
|
|
19
|
+
*/
|
|
20
|
+
export declare function bindEvents(getTarget: () => EventSource | null | undefined, getProps: () => Record<string, unknown>): void;
|
|
21
|
+
/**
|
|
22
|
+
* The layer-scoped variant: MapLibre filters these events by rendered features,
|
|
23
|
+
* so the layer id is part of the subscription and a layer rename must rebuild it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function bindLayerEvents(getMap: () => MapLibreMap | null | undefined, getLayerId: () => string | null | undefined, getProps: () => Record<string, unknown>): void;
|
|
26
|
+
export {};
|