Skip to content

Commit

Permalink
fix(ol-interaction-draw): emit events on changed geometry
Browse files Browse the repository at this point in the history
closes #361
  • Loading branch information
d-koppenhagen committed Jul 7, 2024
1 parent ee8fb7f commit 0363578
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/components/interaction/OlInteractionDraw.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
</template>

<script setup lang="ts">
import type { Ref } from "vue";
import {
provide,
inject,
watch,
onMounted,
onUnmounted,
provide,
type Ref,
shallowRef,
toRefs,
ref,
watch,
} from "vue";
import type { GeometryFunction } from "ol/interaction/Draw";
import Draw from "ol/interaction/Draw";
import type Map from "ol/Map";
import type VectorSource from "ol/source/Vector";
import type { Type as GeometryType } from "ol/geom/Geometry";
import type { GeometryFunction } from "ol/interaction/Draw";
import type { Condition } from "ol/events/condition";
import { useOpenLayersEvents } from "@/composables/useOpenLayersEvents";
Expand Down Expand Up @@ -93,9 +93,12 @@ function createDraw() {
});
}
let draw = createDraw();
const draw = shallowRef(createDraw());
useOpenLayersEvents(draw, ["drawstart", "drawend"]);
const { updateOpenLayersEventHandlers } = useOpenLayersEvents(draw, [
"drawstart",
"drawend",
]);
watch(
[
Expand All @@ -115,25 +118,26 @@ watch(
wrapX,
],
() => {
draw.abortDrawing();
map?.removeInteraction(draw);
draw = createDraw();
map?.addInteraction(draw);
draw.value.abortDrawing();
map?.removeInteraction(draw.value);
draw.value = createDraw();
updateOpenLayersEventHandlers();
map?.addInteraction(draw.value);
map?.changed();
},
);
onMounted(() => {
map?.addInteraction(draw);
map?.addInteraction(draw.value);
});
onUnmounted(() => {
map?.removeInteraction(draw);
map?.removeInteraction(draw.value);
});
provide("stylable", ref(draw));
provide("stylable", draw);
defineExpose({
draw,
draw: draw.value,
});
</script>
10 changes: 9 additions & 1 deletion src/composables/useOpenLayersEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function useOpenLayersEvents(
const instance = getCurrentInstance();
const globalOptions = inject("ol-options");

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

Expand All @@ -81,5 +81,13 @@ export function useOpenLayersEvents(
instance?.emit(eventName, ...args);
});
});
}

onMounted(() => {
updateOpenLayersEventHandlers();
});

return {
updateOpenLayersEventHandlers,
};
}

0 comments on commit 0363578

Please sign in to comment.