|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <slot></slot> |
| 4 | + </div> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script setup lang="ts"> |
| 8 | +import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue"; |
| 9 | +import VectorTileLayer from "ol/layer/VectorTile"; |
| 10 | +import type Map from "ol/Map"; |
| 11 | +import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties"; |
| 12 | +import { |
| 13 | + layersCommonDefaultProps, |
| 14 | + type LayersCommonProps, |
| 15 | +} from "@/components/layers/LayersCommonProps"; |
| 16 | +import type { StyleLike } from "ol/style/Style"; |
| 17 | +import type { FlatStyleLike } from "ol/style/flat"; |
| 18 | +import type LayerGroup from "ol/layer/Group"; |
| 19 | +
|
| 20 | +const props = withDefaults( |
| 21 | + defineProps< |
| 22 | + LayersCommonProps & { |
| 23 | + renderBuffer?: number; |
| 24 | + updateWhileAnimating?: boolean; |
| 25 | + styles?: StyleLike | FlatStyleLike | null; |
| 26 | + updateWhileInteracting?: boolean; |
| 27 | + } |
| 28 | + >(), |
| 29 | + { |
| 30 | + ...layersCommonDefaultProps, |
| 31 | + renderBuffer: 100, |
| 32 | + updateWhileAnimating: false, |
| 33 | + updateWhileInteracting: false, |
| 34 | + } |
| 35 | +); |
| 36 | +
|
| 37 | +const map = inject<Map>("map"); |
| 38 | +const layerGroup = inject<LayerGroup | null>("layerGroup", null); |
| 39 | +
|
| 40 | +const { properties } = usePropsAsObjectProperties(props); |
| 41 | +
|
| 42 | +const vectorTileLayer = computed(() => new VectorTileLayer(properties)); |
| 43 | +
|
| 44 | +watch(properties, () => { |
| 45 | + vectorTileLayer.value.setProperties(properties); |
| 46 | +}); |
| 47 | +
|
| 48 | +onMounted(() => { |
| 49 | + map?.addLayer(vectorTileLayer.value); |
| 50 | + if (layerGroup) { |
| 51 | + const layerCollection = layerGroup.getLayers(); |
| 52 | + layerCollection.push(vectorTileLayer.value); |
| 53 | + layerGroup.setLayers(layerCollection); |
| 54 | + } |
| 55 | +}); |
| 56 | +
|
| 57 | +onUnmounted(() => { |
| 58 | + map?.removeLayer(vectorTileLayer.value); |
| 59 | +}); |
| 60 | +
|
| 61 | +provide("vectorTileLayer", vectorTileLayer); |
| 62 | +provide("stylable", vectorTileLayer); |
| 63 | +
|
| 64 | +defineExpose({ |
| 65 | + vectorTileLayer, |
| 66 | +}); |
| 67 | +</script> |
0 commit comments