Skip to content

Commit

Permalink
fix(draw): recreate modify interaction instead of de/-activating
Browse files Browse the repository at this point in the history
this prevents once edited feature geometries to stay editable when another feature geometry should only be editable
  • Loading branch information
tkohr committed Sep 27, 2024
1 parent 3f7f77b commit 39a3bfe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
8 changes: 6 additions & 2 deletions cypress/e2e/draw/draw-feat.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export function checkDrawInteractionActive(
})
}

export function checkModifyInteractionActive(active: boolean) {
export function checkModifyInteractionActive(exists: boolean) {
cy.getModifyInteraction().then(modifyInteraction => {
expect(modifyInteraction.getActive()).to.equal(active)
if (exists) {
expect(modifyInteraction).to.exist
} else {
expect(modifyInteraction).to.equal(undefined)
}
})
}
7 changes: 4 additions & 3 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ Cypress.Commands.add('getDrawInteractions', () => {
Cypress.Commands.add('getModifyInteraction', () => {
cy.window().then(win => {
const map = win.olMap
return map
const modifyInteraction = map
.getInteractions()
.getArray()
.filter(interaction => {
.find(interaction => {
return (
'removePoint' in interaction &&
typeof interaction.removePoint === 'function'
)
})[0]
})
cy.wrap(modifyInteraction)
})
})

Expand Down
35 changes: 20 additions & 15 deletions src/composables/draw/edit.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function useEdit() {
)
const { updateDrawnFeature, setEditActiveState } = useDrawStore()

let modifyInteraction: Modify
const map = useMap().getOlMap()
const editSource = new VectorSource({
features: [] as DrawnFeature[],
Expand All @@ -31,9 +32,9 @@ export default function useEdit() {

watch(editStateActive, editStateActive => {
if (editStateActive) {
modifyInteraction.setActive(true)
createModifyInteraction()
} else {
modifyInteraction.setActive(false)
map.removeInteraction(modifyInteraction)
}
})

Expand Down Expand Up @@ -62,18 +63,22 @@ export default function useEdit() {
}
})

const modifyInteraction = new Modify({
source: editSource,
pixelTolerance: 20,
deleteCondition: function (event) {
return noModifierKeys(event) && singleClick(event)
},
})
modifyInteraction.setActive(false)
map.addInteraction(modifyInteraction)
function createModifyInteraction() {
if (modifyInteraction) {
map.removeInteraction(modifyInteraction)
}
modifyInteraction = new Modify({
source: editSource,
pixelTolerance: 20,
deleteCondition: function (event) {
return noModifierKeys(event) && singleClick(event)
},
})
map.addInteraction(modifyInteraction)

listen(modifyInteraction, 'modifyend', event => {
const feature = (event as ModifyEvent).features.getArray()[0]
updateDrawnFeature(feature as DrawnFeature)
})
listen(modifyInteraction, 'modifyend', event => {
const feature = (event as ModifyEvent).features.getArray()[0]
updateDrawnFeature(feature as DrawnFeature)
})
}
}

0 comments on commit 39a3bfe

Please sign in to comment.