|
| 1 | +<template> |
| 2 | + <slot></slot> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script setup lang="ts"> |
| 6 | +import { inject, watch, onMounted, onUnmounted, shallowRef } from "vue"; |
| 7 | +import Pointer, { type Options } from "ol/interaction/Pointer"; |
| 8 | +import type Map from "ol/Map"; |
| 9 | +import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties"; |
| 10 | +import { useOpenLayersEvents } from "@/composables/useOpenLayersEvents"; |
| 11 | +import type { MapBrowserEvent } from "ol"; |
| 12 | +
|
| 13 | +// prevent warnings caused by event pass-through via useOpenLayersEvents composable |
| 14 | +defineOptions({ |
| 15 | + inheritAttrs: false, |
| 16 | +}); |
| 17 | +
|
| 18 | +const emit = defineEmits<{ |
| 19 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 20 | + (e: "down", data: MapBrowserEvent<any>): void; |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 22 | + (e: "move", data: MapBrowserEvent<any>): void; |
| 23 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 24 | + (e: "up", data: MapBrowserEvent<any>): void; |
| 25 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 26 | + (e: "drag", data: MapBrowserEvent<any>): void; |
| 27 | +}>(); |
| 28 | +
|
| 29 | +const props = |
| 30 | + defineProps< |
| 31 | + Omit< |
| 32 | + Options, |
| 33 | + | "handleDownEvent" |
| 34 | + | "handleDragEvent" |
| 35 | + | "handleMoveEvent" |
| 36 | + | "handleUpEvent" |
| 37 | + > |
| 38 | + >(); |
| 39 | +
|
| 40 | +const map = inject<Map>("map"); |
| 41 | +const properties = usePropsAsObjectProperties(props); |
| 42 | +
|
| 43 | +const pointer = shallowRef( |
| 44 | + new Pointer({ |
| 45 | + ...properties, |
| 46 | + handleDownEvent: (e) => { |
| 47 | + emit("down", e); |
| 48 | + return true; |
| 49 | + }, |
| 50 | + handleDragEvent: (e) => { |
| 51 | + emit("drag", e); |
| 52 | + return true; |
| 53 | + }, |
| 54 | + handleMoveEvent: (e) => { |
| 55 | + emit("move", e); |
| 56 | + }, |
| 57 | + handleUpEvent: (e) => { |
| 58 | + emit("up", e); |
| 59 | + return true; |
| 60 | + }, |
| 61 | + }), |
| 62 | +); |
| 63 | +
|
| 64 | +useOpenLayersEvents(pointer, ["change:active"]); |
| 65 | +
|
| 66 | +watch(pointer, (newVal, oldVal) => { |
| 67 | + map?.removeInteraction(oldVal); |
| 68 | + map?.addInteraction(newVal); |
| 69 | + map?.changed(); |
| 70 | +}); |
| 71 | +
|
| 72 | +onMounted(() => { |
| 73 | + map?.addInteraction(pointer.value); |
| 74 | +}); |
| 75 | +
|
| 76 | +onUnmounted(() => { |
| 77 | + map?.removeInteraction(pointer.value); |
| 78 | +}); |
| 79 | +
|
| 80 | +defineExpose({ |
| 81 | + pointer, |
| 82 | +}); |
| 83 | +</script> |
0 commit comments