55</template >
66
77<script setup lang="ts">
8- import { ref , provide , onMounted , onUnmounted , watch } from " vue" ;
8+ import { onMounted , onUnmounted , provide , ref , watch } from " vue" ;
99import type { AtPixelOptions } from " ol/Map" ;
1010import Map , { type MapOptions } from " ol/Map" ;
1111import type { FeatureLike } from " ol/Feature" ;
@@ -15,8 +15,9 @@ import type { Pixel } from "ol/pixel";
1515import type { Source } from " ol/source" ;
1616import type { Coordinate } from " ol/coordinate" ;
1717import usePropsAsObjectProperties from " @/composables/usePropsAsObjectProperties" ;
18+ import { mergeProperties } from " @/helpers/properties" ;
1819
19- const props = defineProps <MapOptions >();
20+ const props = defineProps <MapOptions & { instance ? : Map } >();
2021
2122const emit = defineEmits ([
2223 " change:layerGroup" ,
@@ -36,21 +37,37 @@ const emit = defineEmits([
3637 " rendercomplete" ,
3738]);
3839
39- const properties = usePropsAsObjectProperties (props );
40+ const properties = usePropsAsObjectProperties ({
41+ ... props ,
42+ instance: undefined ,
43+ });
4044
4145const mapRef = ref <string | HTMLElement | undefined >(undefined );
42- let map: Map | undefined = new Map (properties );
46+ let map: Map =
47+ props .instance || new Map ({ ... properties , instance: undefined });
4348
44- watch (properties , () => {
45- map ?.setProperties (properties );
46- });
49+ watch (
50+ properties ,
51+ () => {
52+ const p = props .instance
53+ ? mergeProperties (properties , props .instance .getProperties ())
54+ : properties ;
55+ map ?.setProperties (p );
56+ },
57+ { immediate: true },
58+ );
4759
4860onMounted (() => {
49- map ?.setTarget (mapRef .value );
61+ // bind the map to the component template if not re-using an existing one passed via prop.
62+ if (! props .instance ) {
63+ map ?.setTarget (mapRef .value );
64+ }
5065});
5166
5267onUnmounted (() => {
53- map ?.setTarget (undefined );
68+ if (! props .instance ) {
69+ map ?.setTarget (undefined );
70+ }
5471 map = undefined ;
5572});
5673
0 commit comments