3
3
</template >
4
4
5
5
<script setup lang="ts">
6
- import type { Ref } from " vue" ;
7
6
import {
8
- provide ,
9
7
inject ,
10
- watch ,
11
8
onMounted ,
12
9
onUnmounted ,
10
+ provide ,
11
+ type Ref ,
12
+ shallowRef ,
13
13
toRefs ,
14
- ref ,
14
+ watch ,
15
15
} from " vue" ;
16
+ import type { GeometryFunction } from " ol/interaction/Draw" ;
16
17
import Draw from " ol/interaction/Draw" ;
17
18
import type Map from " ol/Map" ;
18
19
import type VectorSource from " ol/source/Vector" ;
19
20
import type { Type as GeometryType } from " ol/geom/Geometry" ;
20
- import type { GeometryFunction } from " ol/interaction/Draw" ;
21
21
import type { Condition } from " ol/events/condition" ;
22
22
import { useOpenLayersEvents } from " @/composables/useOpenLayersEvents" ;
23
23
@@ -93,9 +93,12 @@ function createDraw() {
93
93
});
94
94
}
95
95
96
- let draw = createDraw ();
96
+ const draw = shallowRef ( createDraw () );
97
97
98
- useOpenLayersEvents (draw , [" drawstart" , " drawend" ]);
98
+ const { updateOpenLayersEventHandlers } = useOpenLayersEvents (draw , [
99
+ " drawstart" ,
100
+ " drawend" ,
101
+ ]);
99
102
100
103
watch (
101
104
[
@@ -115,25 +118,26 @@ watch(
115
118
wrapX ,
116
119
],
117
120
() => {
118
- draw .abortDrawing ();
119
- map ?.removeInteraction (draw );
120
- draw = createDraw ();
121
- map ?.addInteraction (draw );
121
+ draw .value .abortDrawing ();
122
+ map ?.removeInteraction (draw .value );
123
+ draw .value = createDraw ();
124
+ updateOpenLayersEventHandlers ();
125
+ map ?.addInteraction (draw .value );
122
126
map ?.changed ();
123
127
},
124
128
);
125
129
126
130
onMounted (() => {
127
- map ?.addInteraction (draw );
131
+ map ?.addInteraction (draw . value );
128
132
});
129
133
130
134
onUnmounted (() => {
131
- map ?.removeInteraction (draw );
135
+ map ?.removeInteraction (draw . value );
132
136
});
133
137
134
- provide (" stylable" , ref ( draw ) );
138
+ provide (" stylable" , draw );
135
139
136
140
defineExpose ({
137
- draw ,
141
+ draw: draw . value ,
138
142
});
139
143
</script >
0 commit comments