Skip to content

Commit

Permalink
some architectural bugfixes:
Browse files Browse the repository at this point in the history
- move setRadius from composable useEdit to service to prevent creation of multiple listeners (useEdit, must remain singleton)
- fix selection, deselection of features between edit modes
  • Loading branch information
mki-c2c committed Nov 15, 2024
1 parent 66ef329 commit f7fde7c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/components/draw/feature-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function onToggleEditFeature() {
}
function onClickDelete() {
emit('clickDelete', props.feature.id)
emit('clickDelete', getUid(props.feature))
}
function onResetInfo(prevLabel: string, prevDescription: string) {
Expand Down
10 changes: 7 additions & 3 deletions src/components/draw/feature-measurements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ import {
getDebouncedElevation,
getElevation,
} from './feature-measurements-helper'
import useEdit from '@/composables/draw/edit.composable'
import { useDrawStore } from '@/stores/draw.store'
import { setRadius } from '@/services/draw/draw.helper'
defineProps<{
isEditingFeature?: boolean
}>()
const { t } = useTranslation()
const mapProjection: Projection = useMap().getOlMap().getView().getProjection()
const drawStore = useDrawStore()
const map = useMap().getOlMap()
const mapProjection: Projection = map.getView().getProjection()
const feature = ref<DrawnFeature | undefined>(inject('feature'))
const featureType = ref<string>(feature.value?.featureType || '')
Expand Down Expand Up @@ -87,7 +90,8 @@ watchEffect(() => {
function onClickValidateRadius(radius: number) {
if (feature.value) {
useEdit().setRadius(feature.value as DrawnFeature, Number(radius))
/* useEdit().setRadius(feature.value as DrawnFeature, Number(radius))*/
setRadius(feature.value as DrawnFeature, Number(radius), map, drawStore)
}
}
</script>
Expand Down
1 change: 1 addition & 0 deletions src/composables/draw/draw-interaction.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function useDrawInteraction(options: Options) {
function onDrawEnd(event: DrawEvent) {
drawTooltip.remove()
addFeature(event.feature)
event.stopPropagation()
}

return {
Expand Down
28 changes: 20 additions & 8 deletions src/composables/draw/draw-select.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import { useAppStore } from '@/stores/app.store'
export default function useDrawSelect() {
const map = useMap().getOlMap()
const appStore = useAppStore()
const { activeFeatureId, editingFeatureId, drawnFeatures } = storeToRefs(
useDrawStore()
)
const {
drawStateActive,
editStateActive,
activeFeatureId,
editingFeatureId,
drawnFeatures,
} = storeToRefs(useDrawStore())

listen(map, 'click', event => handleClick(event))

watch(activeFeatureId, (newId, oldId) => {
editingFeatureId.value = undefined
if (editingFeatureId.value !== newId) {
editingFeatureId.value = undefined
}
drawnFeatures.value
.filter(f => getUid(f) == oldId)
.forEach(oldFeature => {
Expand All @@ -37,25 +43,31 @@ export default function useDrawSelect() {
const handleClick = function (event: any) {
const pixel = event.pixel

if (
drawStateActive.value !== undefined ||
editStateActive.value !== undefined
) {
return true
}
activeFeatureId.value = undefined
const feature = map.forEachFeatureAtPixel(
const featureFound = map.forEachFeatureAtPixel(
pixel,
feature => {
const featureMatch = olArray.includes(drawnFeatures.value, feature)
if (featureMatch) {
appStore.toggleMyMapsOpen(true)
activeFeatureId.value = getUid(feature)

return
return true
}
},
{
hitTolerance: 5,
}
)

if (feature) {
return
if (featureFound) {
return false
}
}
}
22 changes: 6 additions & 16 deletions src/composables/draw/edit.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { useDrawStore } from '@/stores/draw.store'
import useMap from '../map/map.composable'
import { EditStateActive } from '@/stores/draw.store.model'

import { Circle } from 'ol/geom'
import { setCircleRadius } from '@/services/common/measurement.utils'
const DEFAULT_DRAW_EDIT_ZINDEX = 1001
const FEATURE_EDIT_LAYER_TYPE = 'featureEditLayer'

Expand Down Expand Up @@ -46,9 +44,11 @@ export default function useEdit() {
watch(editingFeatureId, editingFeatureId => {
if (!editingFeatureId) {
setEditActiveState(undefined)
editSource.getFeatures().forEach(feature => {
;(feature as DrawnFeature).editable = false
feature.changed()
drawnFeatures.value.forEach(feature => {
if ((feature as DrawnFeature).editable) {
;(feature as DrawnFeature).editable = false
feature.changed()
}
})
}
editSource.clear()
Expand Down Expand Up @@ -86,15 +86,5 @@ export default function useEdit() {
})
}

function setRadius(feature: DrawnFeature, radius: number) {
const geometry = feature.getGeometry()
if (geometry?.getType() === 'Circle') {
setCircleRadius(geometry as Circle, radius, map)
updateDrawnFeature(feature)
}
}

return {
setRadius,
}
return {}
}
18 changes: 18 additions & 0 deletions src/services/draw/draw.helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { setCircleRadius } from '@/services/common/measurement.utils'
import { DrawnFeature } from '@/services/draw/drawn-feature'
import { Circle } from 'ol/geom'
import { Map } from 'ol'

export const SYMBOLS_URL = import.meta.env.VITE_SYMBOLS_URL
export const SYMBOL_ICONS_URL = import.meta.env.VITE_SYMBOL_ICONS_URL

Expand All @@ -14,3 +19,16 @@ export async function getPublicSymbols(mySymbolsOnly = false) {
const json = await response.json()
return json.results
}

export function setRadius(
feature: DrawnFeature,
radius: number,
map: Map,
store: any
) {
const geometry = feature.getGeometry()
if (geometry?.getType() === 'Circle') {
setCircleRadius(geometry as Circle, radius, map)
store.updateDrawnFeature(feature)
}
}
9 changes: 6 additions & 3 deletions src/stores/draw.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ export const useDrawStore = defineStore('draw', () => {
drawnFeatures.value = features
}

function removeFeature(featureId: number) {
function removeFeature(featureId: String | undefined) {
drawnFeatures.value = drawnFeatures.value.filter(
feature => feature.id !== featureId
feature => getUid(feature) !== featureId
)
editingFeatureId.value = undefined
if (activeFeatureId.value === featureId) {
activeFeatureId.value = undefined
editingFeatureId.value = undefined
}
}

return {
Expand Down

0 comments on commit f7fde7c

Please sign in to comment.