Skip to content

Commit

Permalink
refactor: improve update modale anchor with debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
AlitaBernachot committed Nov 19, 2024
1 parent 86f6339 commit 1cd311e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/components/draw/feature-sub-wrapper.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
<script setup lang="ts">
import { computed, inject } from 'vue'
import { inject, ref, watchEffect } from 'vue'
import MapPopup from '@/components/map/map-popup.vue'
import useDrawnFeatures from '@/composables/draw/drawn-features.composable'
import { DrawnFeature } from '@/services/draw/drawn-feature'
import { debounce } from '@/services/utils'
defineProps<{
isDocked: boolean
}>()
const { getFeatCoordinates } = useDrawnFeatures()
const emit = defineEmits(['closePopup'])
const feature: DrawnFeature | undefined = inject('feature')
const popupAnchor = computed(() =>
feature ? getFeatCoordinates(feature) : undefined
)
const popupAnchor: any = ref(getUnreactiveCoords())
function onClosePopup() {
emit('closePopup')
}
/**
* Update the anchor coords when user modfies the geom,
* but with a debounce, preventing the modale to moves at each update of the geom
*/
const onChangeGeom = debounce(() => {
popupAnchor.value = getUnreactiveCoords()
}, 1000)
/**
* Get an unreactive value for coords, otherwise, anchor is updated
* each time the geom is modified by the user
*/
function getUnreactiveCoords() {
return JSON.parse(JSON.stringify(getFeatCoordinates(feature!)))
}
watchEffect(() => {
if (feature?.getGeometry() && getFeatCoordinates(feature!)) {
onChangeGeom()
}
})
</script>

<template>
Expand Down

0 comments on commit 1cd311e

Please sign in to comment.