@m3ui-vue/m3ui-vue 1.0.2 → 1.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/dist/API.md +666 -12
- package/dist/MMenuItem-BOkcVtEe.js +809 -0
- package/dist/MMenuItem-BOkcVtEe.js.map +1 -0
- package/dist/MSlider-BXsBv0ox.js +1402 -0
- package/dist/MSlider-BXsBv0ox.js.map +1 -0
- package/dist/MTextField-rML9jnXs.js +671 -0
- package/dist/MTextField-rML9jnXs.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MBottomSheet.vue.d.ts +20 -1
- package/dist/components/MDialog.vue.d.ts +17 -8
- package/dist/components/MDragDropList.vue.d.ts +1 -1
- package/dist/components/MIconButton.vue.d.ts +1 -1
- package/dist/components/MMap.vue.d.ts +74 -0
- package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
- package/dist/components/MMapControl.vue.d.ts +40 -0
- package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
- package/dist/components/MMapFilter.vue.d.ts +19 -0
- package/dist/components/MMapGeoJson.vue.d.ts +34 -0
- package/dist/components/MMapHover.vue.d.ts +17 -0
- package/dist/components/MMapIdentify.vue.d.ts +37 -0
- package/dist/components/MMapLayers.vue.d.ts +31 -0
- package/dist/components/MMapMarker.vue.d.ts +45 -0
- package/dist/components/MMapMeasure.vue.d.ts +43 -0
- package/dist/components/MMapPopup.vue.d.ts +45 -0
- package/dist/components/MMapSearch.vue.d.ts +28 -0
- package/dist/components/MMenuItem.vue.d.ts +2 -2
- package/dist/components/MSeekBar.vue.d.ts +122 -0
- package/dist/components/MSplitButton.vue.d.ts +2 -2
- package/dist/components/_MMapAttribution.vue.d.ts +7 -0
- package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
- package/dist/composables/_mapGeometry.d.ts +13 -0
- package/dist/composables/_mapPin.d.ts +5 -0
- package/dist/composables/_popupPosition.d.ts +12 -0
- package/dist/composables/_ssrState.d.ts +30 -0
- package/dist/composables/_storageKeys.d.ts +3 -0
- package/dist/composables/_useCalendarMonth.d.ts +24 -0
- package/dist/composables/_useEscapeStack.d.ts +3 -0
- package/dist/composables/_useScrollLock.d.ts +8 -0
- package/dist/composables/_useSheetMotion.d.ts +16 -1
- package/dist/composables/_useThemeTokens.d.ts +7 -0
- package/dist/composables/mapAdapters.d.ts +111 -0
- package/dist/composables/mapSearch.d.ts +31 -0
- package/dist/composables/themeInitScript.d.ts +4 -3
- package/dist/composables/useColorPalette.d.ts +173 -7
- package/dist/composables/useLocale.d.ts +38 -0
- package/dist/composables/useMap.d.ts +47 -0
- package/dist/composables/useMapFilter.d.ts +10 -0
- package/dist/composables/useMapLayers.d.ts +30 -0
- package/dist/composables/useTheme.d.ts +2 -1
- package/dist/index.d.ts +4 -1
- package/dist/locales/index.js +273 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +7913 -8625
- package/dist/m3ui.js.map +1 -1
- package/dist/map.d.ts +24 -0
- package/dist/map.js +2067 -0
- package/dist/map.js.map +1 -0
- package/dist/plugin.d.ts +18 -2
- package/dist/rich-text-editor.js +26 -25
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-CuvWRZ1h.js → useLocale-CqQrhWFl.js} +40 -2
- package/dist/useLocale-CqQrhWFl.js.map +1 -0
- package/package.json +11 -2
- package/src/__tests__/MDialog.test.ts +30 -0
- package/src/__tests__/MFab.test.ts +52 -6
- package/src/__tests__/MMap.test.ts +422 -0
- package/src/__tests__/MMapLayers.test.ts +257 -0
- package/src/__tests__/MMapTools.test.ts +233 -0
- package/src/__tests__/MSeekBar.test.ts +170 -0
- package/src/__tests__/MTooltip.test.ts +110 -0
- package/src/__tests__/_fakeMaplibre.ts +161 -0
- package/src/__tests__/calendarMonth.test.ts +59 -0
- package/src/__tests__/hydration.test.ts +33 -0
- package/src/__tests__/pickerModals.test.ts +128 -0
- package/src/__tests__/popupPosition.test.ts +224 -0
- package/src/__tests__/scrollLock.test.ts +110 -0
- package/src/__tests__/sheets.test.ts +158 -0
- package/src/__tests__/ssr.test.ts +63 -1
- package/src/__tests__/ssrState.test.ts +155 -0
- package/src/__tests__/themeInitScript.test.ts +18 -0
- package/src/components/MAutocomplete.vue +2 -4
- package/src/components/MBottomSheet.vue +211 -13
- package/src/components/MCommandPalette.vue +4 -6
- package/src/components/MContextMenu.vue +19 -5
- package/src/components/MDatePicker.vue +29 -70
- package/src/components/MDatePickerModal.vue +116 -172
- package/src/components/MDateRangePicker.vue +29 -51
- package/src/components/MDateRangePickerModal.vue +84 -162
- package/src/components/MDialog.vue +87 -63
- package/src/components/MFab.vue +29 -7
- package/src/components/MIconButton.vue +1 -1
- package/src/components/MLightbox.vue +12 -5
- package/src/components/MMap.vue +568 -0
- package/src/components/MMapBaseLayers.vue +67 -0
- package/src/components/MMapControl.vue +175 -0
- package/src/components/MMapFeatureInfo.vue +41 -0
- package/src/components/MMapFilter.vue +80 -0
- package/src/components/MMapGeoJson.vue +136 -0
- package/src/components/MMapHover.vue +131 -0
- package/src/components/MMapIdentify.vue +104 -0
- package/src/components/MMapLayers.vue +111 -0
- package/src/components/MMapMarker.vue +109 -0
- package/src/components/MMapMeasure.vue +452 -0
- package/src/components/MMapPopup.vue +103 -0
- package/src/components/MMapSearch.vue +162 -0
- package/src/components/MMenu.vue +31 -12
- package/src/components/MMenuItem.vue +21 -4
- package/src/components/MMultiAutocomplete.vue +2 -4
- package/src/components/MMultiSelect.vue +2 -4
- package/src/components/MNavigationDrawer.vue +2 -8
- package/src/components/MNavigationRail.vue +2 -3
- package/src/components/MSearchBar.vue +2 -5
- package/src/components/MSeekBar.vue +593 -0
- package/src/components/MSelect.vue +2 -8
- package/src/components/MSplitButton.vue +23 -7
- package/src/components/MSpotlightSearch.vue +4 -6
- package/src/components/MTimePicker.vue +23 -6
- package/src/components/MTimePickerModal.vue +213 -226
- package/src/components/MTooltip.vue +99 -3
- package/src/components/_MMapAttribution.vue +81 -0
- package/src/components/_MMapLayerItem.vue +77 -0
- package/src/composables/_mapGeometry.ts +75 -0
- package/src/composables/_mapPin.ts +25 -0
- package/src/composables/_popupPosition.ts +39 -0
- package/src/composables/_ssrState.ts +80 -0
- package/src/composables/_storageKeys.ts +6 -0
- package/src/composables/_useCalendarMonth.ts +74 -0
- package/src/composables/_useEscapeStack.ts +33 -0
- package/src/composables/_useScrollLock.ts +41 -0
- package/src/composables/_useSheetMotion.ts +82 -20
- package/src/composables/_useThemeTokens.ts +42 -0
- package/src/composables/mapAdapters.ts +208 -0
- package/src/composables/mapSearch.ts +56 -0
- package/src/composables/themeInitScript.ts +6 -4
- package/src/composables/useColorPalette.ts +23 -9
- package/src/composables/useLocale.ts +78 -0
- package/src/composables/useMap.ts +98 -0
- package/src/composables/useMapFilter.ts +45 -0
- package/src/composables/useMapLayers.ts +124 -0
- package/src/composables/useTheme.ts +8 -3
- package/src/composables/useWindowSizeClass.ts +14 -5
- package/src/index.ts +4 -1
- package/src/locales/de.ts +39 -1
- package/src/locales/es.ts +39 -1
- package/src/locales/fr.ts +39 -1
- package/src/locales/ja.ts +39 -1
- package/src/locales/ko.ts +39 -1
- package/src/locales/pt.ts +39 -1
- package/src/locales/zh.ts +39 -1
- package/src/map.ts +32 -0
- package/src/plugin.ts +54 -3
- package/dist/MTextField-BPbFeI9M.js +0 -1368
- package/dist/MTextField-BPbFeI9M.js.map +0 -1
- package/dist/useLocale-CuvWRZ1h.js.map +0 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* Click-to-identify for an `MMap` with an `adapter`: a click on the map lists the features of the layers marked to
|
|
4
|
+
* identify under it (with their formatted properties) in a popup, with a marker where the user clicked. The pointer
|
|
5
|
+
* turns into a hand over those layers. The `identify` event carries the results, to show them in a panel of your own
|
|
6
|
+
* instead (`:popup="false"`, then `MMapFeatureInfo` in an `MWindow`). Available from the `/map` entry point.
|
|
7
|
+
*
|
|
8
|
+
* @slot default The popup's content instead of `MMapFeatureInfo` (scope: `{ results, lngLat, close }`)
|
|
9
|
+
* @event identify The user clicked the map: what was found there (empty when nothing) and where (`[lng, lat]`)
|
|
10
|
+
*/
|
|
11
|
+
import { onBeforeUnmount, ref, shallowRef, watch } from 'vue'
|
|
12
|
+
import type * as Maplibre from 'maplibre-gl'
|
|
13
|
+
import MMapPopup from './MMapPopup.vue'
|
|
14
|
+
import MMapMarker from './MMapMarker.vue'
|
|
15
|
+
import MMapFeatureInfo from './MMapFeatureInfo.vue'
|
|
16
|
+
import { useMap } from '../composables/useMap'
|
|
17
|
+
import { useMapLayers } from '../composables/useMapLayers'
|
|
18
|
+
import { identifyFeatures, type MapIdentifyResult } from '../composables/mapAdapters'
|
|
19
|
+
import { PIN_POPUP_OFFSET } from '../composables/_mapPin'
|
|
20
|
+
|
|
21
|
+
// Attributes (class, style…) go on the popup's content
|
|
22
|
+
defineOptions({ inheritAttrs: false })
|
|
23
|
+
|
|
24
|
+
const props = withDefaults(
|
|
25
|
+
defineProps<{
|
|
26
|
+
/** Listen to clicks. It also pauses by itself while another tool owns them (`MMapMeasure` measuring) */
|
|
27
|
+
enabled?: boolean
|
|
28
|
+
/** Show the results in a popup at the click */
|
|
29
|
+
popup?: boolean
|
|
30
|
+
/** Mark where the user clicked while there are results */
|
|
31
|
+
marker?: boolean
|
|
32
|
+
}>(),
|
|
33
|
+
{ enabled: true, popup: true, marker: true },
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
const emit = defineEmits<{ identify: [results: MapIdentifyResult[], lngLat: [number, number]] }>()
|
|
37
|
+
|
|
38
|
+
const { on, onReady, activeTool } = useMap()
|
|
39
|
+
const { model } = useMapLayers()
|
|
40
|
+
|
|
41
|
+
const results = shallowRef<MapIdentifyResult[]>([])
|
|
42
|
+
const lngLat = ref<[number, number]>([0, 0])
|
|
43
|
+
const open = ref(false)
|
|
44
|
+
|
|
45
|
+
on('click', (e) => {
|
|
46
|
+
// Another tool (measuring…) owns the clicks
|
|
47
|
+
if (!props.enabled || activeTool.value) return
|
|
48
|
+
const map = e.target as Maplibre.Map
|
|
49
|
+
results.value = identifyFeatures(map, e.point, model.value)
|
|
50
|
+
lngLat.value = [e.lngLat.lng, e.lngLat.lat]
|
|
51
|
+
open.value = results.value.length > 0
|
|
52
|
+
emit('identify', results.value, lngLat.value)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
function close() {
|
|
56
|
+
open.value = false
|
|
57
|
+
}
|
|
58
|
+
watch(() => props.enabled, (enabled) => { if (!enabled) close() })
|
|
59
|
+
|
|
60
|
+
// A hand over the layers that answer to a click
|
|
61
|
+
let map: Maplibre.Map | undefined
|
|
62
|
+
let hovered: string[] = []
|
|
63
|
+
const enter = () => { if (props.enabled && !activeTool.value && map) map.getCanvas().style.cursor = 'pointer' }
|
|
64
|
+
const leave = () => { if (map) map.getCanvas().style.cursor = '' }
|
|
65
|
+
function bindCursor() {
|
|
66
|
+
if (!map) return
|
|
67
|
+
for (const id of hovered) {
|
|
68
|
+
map.off('mouseenter', id, enter)
|
|
69
|
+
map.off('mouseleave', id, leave)
|
|
70
|
+
}
|
|
71
|
+
hovered = (model.value?.layers ?? []).filter((l) => l.identify).map((l) => l.id)
|
|
72
|
+
for (const id of hovered) {
|
|
73
|
+
map.on('mouseenter', id, enter)
|
|
74
|
+
map.on('mouseleave', id, leave)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
onReady((m) => {
|
|
78
|
+
map = m
|
|
79
|
+
bindCursor()
|
|
80
|
+
})
|
|
81
|
+
watch(model, bindCursor)
|
|
82
|
+
onBeforeUnmount(() => {
|
|
83
|
+
hovered.forEach((id) => {
|
|
84
|
+
map?.off('mouseenter', id, enter)
|
|
85
|
+
map?.off('mouseleave', id, leave)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
defineExpose({ close })
|
|
90
|
+
</script>
|
|
91
|
+
|
|
92
|
+
<template>
|
|
93
|
+
<!-- One root (the marker and the popup move their content into the map) -->
|
|
94
|
+
<div class="hidden">
|
|
95
|
+
<MMapMarker v-if="marker && open" :lng-lat="lngLat" />
|
|
96
|
+
<MMapPopup v-if="popup && results.length" v-model:open="open" :lng-lat="lngLat" max-width="360px" :offset="marker ? PIN_POPUP_OFFSET : undefined">
|
|
97
|
+
<div class="max-h-80 overflow-y-auto" v-bind="$attrs">
|
|
98
|
+
<slot :results="results" :lng-lat="lngLat" :close="close">
|
|
99
|
+
<MMapFeatureInfo :results="results" />
|
|
100
|
+
</slot>
|
|
101
|
+
</div>
|
|
102
|
+
</MMapPopup>
|
|
103
|
+
</div>
|
|
104
|
+
</template>
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The layer list of an `MMap` with an `adapter`: a search field, the style's groups (collapsible, with a visibility
|
|
4
|
+
* toggle for the whole group and a count) and its layers, each with a visibility toggle and, expanded, its
|
|
5
|
+
* description, an opacity slider, its legend and a download link. Only the content: put it in an `MWindow`, an
|
|
6
|
+
* `MSideSheet`, an `MBottomSheet` or an `MMapControl`. Available from the `/map` entry point.
|
|
7
|
+
*
|
|
8
|
+
* @slot actions Extra actions in an expanded layer (scope: `{ layer, visible }`)
|
|
9
|
+
*/
|
|
10
|
+
import { computed, ref } from 'vue'
|
|
11
|
+
import MIcon from './MIcon.vue'
|
|
12
|
+
import MTextField from './MTextField.vue'
|
|
13
|
+
import MMapLayerItem from './_MMapLayerItem.vue'
|
|
14
|
+
import { useLocale } from '../composables/useLocale'
|
|
15
|
+
import { useMapLayers } from '../composables/useMapLayers'
|
|
16
|
+
import type { MapLayerInfo } from '../composables/mapAdapters'
|
|
17
|
+
|
|
18
|
+
const props = withDefaults(
|
|
19
|
+
defineProps<{
|
|
20
|
+
/** Show the search field */
|
|
21
|
+
search?: boolean
|
|
22
|
+
/** Groups start collapsed */
|
|
23
|
+
collapsed?: boolean
|
|
24
|
+
}>(),
|
|
25
|
+
{ search: true, collapsed: false },
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const t = useLocale()
|
|
29
|
+
const { model, layersOf, isVisible, groupState, setGroupVisible } = useMapLayers()
|
|
30
|
+
|
|
31
|
+
const query = ref('')
|
|
32
|
+
const matches = (layer: MapLayerInfo) => !query.value || layer.title.toLowerCase().includes(query.value.toLowerCase())
|
|
33
|
+
|
|
34
|
+
const groups = computed(() =>
|
|
35
|
+
(model.value?.groups ?? [])
|
|
36
|
+
.map((g) => ({ ...g, all: layersOf(g.name), layers: layersOf(g.name).filter(matches) }))
|
|
37
|
+
.filter((g) => g.layers.length),
|
|
38
|
+
)
|
|
39
|
+
const ungrouped = computed(() => layersOf(null).filter(matches))
|
|
40
|
+
|
|
41
|
+
// Collapsed groups and expanded layers (by default the other way around, with `collapsed`)
|
|
42
|
+
const toggledGroups = ref(new Set<string>())
|
|
43
|
+
const isOpen = (group: string) => toggledGroups.value.has(group) === props.collapsed
|
|
44
|
+
function toggleGroup(group: string) {
|
|
45
|
+
const next = new Set(toggledGroups.value)
|
|
46
|
+
if (!next.delete(group)) next.add(group)
|
|
47
|
+
toggledGroups.value = next
|
|
48
|
+
}
|
|
49
|
+
const expanded = ref(new Set<string>())
|
|
50
|
+
function toggleExpanded(id: string) {
|
|
51
|
+
const next = new Set(expanded.value)
|
|
52
|
+
if (!next.delete(id)) next.add(id)
|
|
53
|
+
expanded.value = next
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const visibleCount = (layers: MapLayerInfo[]) => layers.filter((l) => isVisible(l.id)).length
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<template>
|
|
60
|
+
<div class="flex flex-col gap-3">
|
|
61
|
+
<MTextField v-if="search" v-model="query" :label="t.mapSearchLayers" leading-icon="search" variant="outlined" clearable />
|
|
62
|
+
|
|
63
|
+
<section v-for="group in groups" :key="group.name" class="flex flex-col">
|
|
64
|
+
<div class="m3-map-layers-row flex cursor-pointer items-center gap-2 rounded-xl px-2 py-1.5" @click="toggleGroup(group.name)">
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
class="grid size-8 shrink-0 cursor-pointer place-items-center rounded-full transition-colors duration-200"
|
|
68
|
+
:class="groupState(group.name) === 'visible' ? 'bg-primary-container text-on-primary-container'
|
|
69
|
+
: groupState(group.name) === 'partial' ? 'bg-secondary-container text-on-secondary-container'
|
|
70
|
+
: 'bg-on-surface/8 text-on-surface-variant'"
|
|
71
|
+
:aria-label="groupState(group.name) === 'hidden' ? t.mapShowLayer : t.mapHideLayer"
|
|
72
|
+
:aria-pressed="groupState(group.name) !== 'hidden'"
|
|
73
|
+
@click.stop="setGroupVisible(group.name, groupState(group.name) !== 'visible')"
|
|
74
|
+
>
|
|
75
|
+
<MIcon :name="groupState(group.name) === 'hidden' ? 'visibility_off' : 'visibility'" :size="18" />
|
|
76
|
+
</button>
|
|
77
|
+
<span class="flex-1 select-none truncate text-label-large text-on-surface">{{ group.title }}</span>
|
|
78
|
+
<span class="rounded-full bg-surface-container-highest px-2 py-0.5 text-label-small tabular-nums text-on-surface-variant">
|
|
79
|
+
{{ visibleCount(group.all) }}/{{ group.all.length }}
|
|
80
|
+
</span>
|
|
81
|
+
<MIcon name="expand_more" :size="18" class="text-on-surface-variant transition-transform duration-300" :class="isOpen(group.name) ? 'rotate-180' : ''" />
|
|
82
|
+
</div>
|
|
83
|
+
<div class="grid transition-[grid-template-rows] duration-300 ease-[cubic-bezier(0.2,0,0,1)]" :style="{ gridTemplateRows: isOpen(group.name) ? '1fr' : '0fr' }">
|
|
84
|
+
<div class="flex min-h-0 flex-col gap-1.5 overflow-hidden" :class="isOpen(group.name) ? 'pt-1.5' : ''">
|
|
85
|
+
<MMapLayerItem v-for="layer in group.layers" :key="layer.id" :layer="layer" :expanded="expanded.has(layer.id)" @toggle-expanded="toggleExpanded(layer.id)">
|
|
86
|
+
<template v-if="$slots.actions" #actions="scope"><slot name="actions" v-bind="scope" /></template>
|
|
87
|
+
</MMapLayerItem>
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
</section>
|
|
91
|
+
|
|
92
|
+
<div v-if="ungrouped.length" class="flex flex-col gap-1.5">
|
|
93
|
+
<MMapLayerItem v-for="layer in ungrouped" :key="layer.id" :layer="layer" :expanded="expanded.has(layer.id)" @toggle-expanded="toggleExpanded(layer.id)">
|
|
94
|
+
<template v-if="$slots.actions" #actions="scope"><slot name="actions" v-bind="scope" /></template>
|
|
95
|
+
</MMapLayerItem>
|
|
96
|
+
</div>
|
|
97
|
+
|
|
98
|
+
<div v-if="!groups.length && !ungrouped.length" class="flex flex-col items-center gap-3 py-8">
|
|
99
|
+
<div class="grid size-14 place-items-center rounded-full bg-surface-container-highest">
|
|
100
|
+
<MIcon name="layers_clear" :size="28" class="text-on-surface-variant" />
|
|
101
|
+
</div>
|
|
102
|
+
<p class="text-body-medium text-on-surface-variant">{{ t.mapNoLayers }}</p>
|
|
103
|
+
</div>
|
|
104
|
+
</div>
|
|
105
|
+
</template>
|
|
106
|
+
|
|
107
|
+
<style>
|
|
108
|
+
.m3-map-layers-row:hover {
|
|
109
|
+
background-color: color-mix(in srgb, var(--color-on-surface) 6%, transparent);
|
|
110
|
+
}
|
|
111
|
+
</style>
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* A marker on an `MMap`, at `lngLat`: MapLibre's pin in the theme's primary (or `color`), or your own content from the
|
|
4
|
+
* default slot. With `draggable`, `lngLat` is a v-model. Content in `#popup` opens in a popup when the marker is
|
|
5
|
+
* clicked. Available from the `/map` entry point.
|
|
6
|
+
*
|
|
7
|
+
* @slot default The marker itself, instead of the pin (any element, anchored by `anchor`)
|
|
8
|
+
* @slot popup Content of a popup that opens when the marker is clicked
|
|
9
|
+
* @event update:lngLat Dragged to a new position (`draggable`): `[lng, lat]`
|
|
10
|
+
* @event click The marker was clicked
|
|
11
|
+
*/
|
|
12
|
+
import { onBeforeUnmount, ref, useSlots, watch } from 'vue'
|
|
13
|
+
import type * as Maplibre from 'maplibre-gl'
|
|
14
|
+
import { useMap } from '../composables/useMap'
|
|
15
|
+
import { PIN_OFFSET } from '../composables/_mapPin'
|
|
16
|
+
|
|
17
|
+
// Attributes (class, style…) go on what's shown on the map, not on the hidden wrapper it's rendered in
|
|
18
|
+
defineOptions({ inheritAttrs: false })
|
|
19
|
+
|
|
20
|
+
const props = withDefaults(
|
|
21
|
+
defineProps<{
|
|
22
|
+
/** Position, `[lng, lat]` (v-model with `draggable`) */
|
|
23
|
+
lngLat: [number, number]
|
|
24
|
+
/** The pin's color (any CSS color). Defaults to the theme's primary; ignored with custom content */
|
|
25
|
+
color?: string
|
|
26
|
+
/** Let the user drag it */
|
|
27
|
+
draggable?: boolean
|
|
28
|
+
/**
|
|
29
|
+
* Which part of the marker sits on `lngLat`. By default the pin's tip (MapLibre's placement: its center, offset
|
|
30
|
+
* `[0, -14]`), and the center of custom content
|
|
31
|
+
*/
|
|
32
|
+
anchor?: Maplibre.PositionAnchor
|
|
33
|
+
/** Offset in pixels, `[x, y]`. The pin's default is MapLibre's `[0, -14]`, which puts its tip on `lngLat` */
|
|
34
|
+
offset?: [number, number]
|
|
35
|
+
/** Rotation in degrees */
|
|
36
|
+
rotation?: number
|
|
37
|
+
}>(),
|
|
38
|
+
{ draggable: false, rotation: 0 },
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
const emit = defineEmits<{
|
|
42
|
+
'update:lngLat': [lngLat: [number, number]]
|
|
43
|
+
click: [event: MouseEvent]
|
|
44
|
+
}>()
|
|
45
|
+
|
|
46
|
+
const slots = useSlots()
|
|
47
|
+
const content = ref<HTMLElement>()
|
|
48
|
+
const popupContent = ref<HTMLElement>()
|
|
49
|
+
const { onReady } = useMap()
|
|
50
|
+
|
|
51
|
+
let marker: Maplibre.Marker | undefined
|
|
52
|
+
let custom = false
|
|
53
|
+
|
|
54
|
+
onReady((map, ml) => {
|
|
55
|
+
custom = !!slots.default && !!content.value
|
|
56
|
+
marker = new ml.Marker({
|
|
57
|
+
...(custom ? { element: content.value } : { color: props.color ?? 'currentColor' }),
|
|
58
|
+
draggable: props.draggable,
|
|
59
|
+
// Left out for the pin unless given: MapLibre's own placement puts its tip on the point (see _mapPin.ts)
|
|
60
|
+
...(props.anchor ? { anchor: props.anchor } : custom ? { anchor: 'center' as const } : {}),
|
|
61
|
+
...(props.offset ? { offset: props.offset } : {}),
|
|
62
|
+
rotation: props.rotation,
|
|
63
|
+
})
|
|
64
|
+
if (!custom && !props.color) marker.getElement().classList.add('m3-map-marker--default')
|
|
65
|
+
marker.setLngLat(props.lngLat).addTo(map)
|
|
66
|
+
marker.getElement().addEventListener('click', onClick)
|
|
67
|
+
marker.on('dragend', () => {
|
|
68
|
+
const { lng, lat } = marker!.getLngLat()
|
|
69
|
+
emit('update:lngLat', [lng, lat])
|
|
70
|
+
})
|
|
71
|
+
if (slots.popup && popupContent.value) {
|
|
72
|
+
// The pin's popup gets MapLibre's per-side offsets, which it only applies when `offset` isn't set at all
|
|
73
|
+
marker.setPopup(new ml.Popup({ ...(custom ? { offset: 16 } : {}), maxWidth: '320px' }).setDOMContent(popupContent.value))
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
function onClick(e: MouseEvent) {
|
|
78
|
+
emit('click', e)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Compared by value: an inline `:lng-lat="[lng, lat]"` is a new array on every parent render
|
|
82
|
+
watch(() => props.lngLat.join(','), () => marker?.setLngLat(props.lngLat))
|
|
83
|
+
watch(() => props.draggable, (draggable) => marker?.setDraggable(draggable))
|
|
84
|
+
watch(() => props.rotation, (rotation) => marker?.setRotation(rotation))
|
|
85
|
+
watch(() => props.offset?.join(','), () => marker?.setOffset(props.offset ?? (custom ? [0, 0] : PIN_OFFSET)))
|
|
86
|
+
|
|
87
|
+
onBeforeUnmount(() => {
|
|
88
|
+
marker?.getElement().removeEventListener('click', onClick)
|
|
89
|
+
marker?.getPopup()?.remove()
|
|
90
|
+
marker?.remove()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
defineExpose({
|
|
94
|
+
/** The MapLibre marker */
|
|
95
|
+
marker: () => marker,
|
|
96
|
+
})
|
|
97
|
+
</script>
|
|
98
|
+
|
|
99
|
+
<template>
|
|
100
|
+
<!-- Rendered here, then moved into the map by MapLibre (the marker element and the popup's content) -->
|
|
101
|
+
<div class="hidden">
|
|
102
|
+
<div v-if="$slots.default" ref="content" class="m3-map-marker" v-bind="$attrs">
|
|
103
|
+
<slot />
|
|
104
|
+
</div>
|
|
105
|
+
<div v-if="$slots.popup" ref="popupContent">
|
|
106
|
+
<slot name="popup" />
|
|
107
|
+
</div>
|
|
108
|
+
</div>
|
|
109
|
+
</template>
|