Skip to content

Commit 0363578

Browse files
committed
fix(ol-interaction-draw): emit events on changed geometry
closes #361
1 parent ee8fb7f commit 0363578

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/components/interaction/OlInteractionDraw.vue

+19-15
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
</template>
44

55
<script setup lang="ts">
6-
import type { Ref } from "vue";
76
import {
8-
provide,
97
inject,
10-
watch,
118
onMounted,
129
onUnmounted,
10+
provide,
11+
type Ref,
12+
shallowRef,
1313
toRefs,
14-
ref,
14+
watch,
1515
} from "vue";
16+
import type { GeometryFunction } from "ol/interaction/Draw";
1617
import Draw from "ol/interaction/Draw";
1718
import type Map from "ol/Map";
1819
import type VectorSource from "ol/source/Vector";
1920
import type { Type as GeometryType } from "ol/geom/Geometry";
20-
import type { GeometryFunction } from "ol/interaction/Draw";
2121
import type { Condition } from "ol/events/condition";
2222
import { useOpenLayersEvents } from "@/composables/useOpenLayersEvents";
2323
@@ -93,9 +93,12 @@ function createDraw() {
9393
});
9494
}
9595
96-
let draw = createDraw();
96+
const draw = shallowRef(createDraw());
9797
98-
useOpenLayersEvents(draw, ["drawstart", "drawend"]);
98+
const { updateOpenLayersEventHandlers } = useOpenLayersEvents(draw, [
99+
"drawstart",
100+
"drawend",
101+
]);
99102
100103
watch(
101104
[
@@ -115,25 +118,26 @@ watch(
115118
wrapX,
116119
],
117120
() => {
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);
122126
map?.changed();
123127
},
124128
);
125129
126130
onMounted(() => {
127-
map?.addInteraction(draw);
131+
map?.addInteraction(draw.value);
128132
});
129133
130134
onUnmounted(() => {
131-
map?.removeInteraction(draw);
135+
map?.removeInteraction(draw.value);
132136
});
133137
134-
provide("stylable", ref(draw));
138+
provide("stylable", draw);
135139
136140
defineExpose({
137-
draw,
141+
draw: draw.value,
138142
});
139143
</script>

src/composables/useOpenLayersEvents.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function useOpenLayersEvents(
5959
const instance = getCurrentInstance();
6060
const globalOptions = inject("ol-options");
6161

62-
onMounted(() => {
62+
function updateOpenLayersEventHandlers() {
6363
([...COMMON_EVENTS, ...eventNames] as EventTypes[]).forEach((eventName) => {
6464
let unwrappedFeature: Pick<BaseObject, "on">;
6565

@@ -81,5 +81,13 @@ export function useOpenLayersEvents(
8181
instance?.emit(eventName, ...args);
8282
});
8383
});
84+
}
85+
86+
onMounted(() => {
87+
updateOpenLayersEventHandlers();
8488
});
89+
90+
return {
91+
updateOpenLayersEventHandlers,
92+
};
8593
}

0 commit comments

Comments
 (0)