Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix that drawings on top of each other were glued #228

Merged
merged 9 commits into from
Feb 6, 2025
1 change: 1 addition & 0 deletions packages/plugins/Draw/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Feature: Add new configuration parameter `measureOptions` to allow users to select a measurement mode when drawing a feature. This way, a length / area in the selected unit is added to the drawn feature.
- Fix: Update initial value of `drawMode` to a selectable value if the default `Point` is not a drawable option.
- Fix: Adjust type `DrawGetters` regarding its keys `selectableDrawModes` and `selectableModes` to correctly reflect that they represent objects.
- Fix: Stacked geometries can be separated with the "Edit" operation again. [Thanks to mike-000](https://github.com/openlayers/openlayers/issues/16593#issuecomment-2624257614).
- Chore: Add `@polar/core` as a dependency as the component `RadioCard.vue` has been moved from this package to `@polar/core`.

## 2.0.0
Expand Down
6 changes: 5 additions & 1 deletion packages/plugins/Draw/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ export const makeActions = () => {
getters: { selectedFeature },
rootGetters: { map },
}) {
interactions.forEach((interaction) => map.removeInteraction(interaction))
interactions.forEach(
(interaction) =>
// @ts-expect-error | "un on removal" riding piggyback as _onRemove
map.removeInteraction(interaction) && interaction._onRemove?.()
)
if (interactions.some((interaction) => interaction instanceof Select)) {
if (selectedFeature && selectedFeature.get('text') === '') {
// text nodes without text are considered deleted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
import { Modify, Select, Snap } from 'ol/interaction'
import Interaction from 'ol/interaction/Interaction'
import { PolarActionContext } from '@polar/lib-custom-types'
import { Collection, Feature, Map, MapBrowserEvent } from 'ol'
import { CreateInteractionsPayload, DrawGetters, DrawState } from '../../types'

const createModify = (
map: Map,
drawLayer: CreateInteractionsPayload['drawLayer']
) => {
let active = false
const features: Collection<Feature> = new Collection()
const modify = new Modify({ features })
modify.on('modifystart', () => {
active = true
})
modify.on('modifyend', () => {
active = false
})

const localSelector = (e: MapBrowserEvent<UIEvent>) => {
if (!active) {
map.forEachFeatureAtPixel(
e.pixel,
(f) => {
if (f !== features.item(0)) {
features.setAt(0, f as Feature)
}
return true
},
{
layerFilter: (l) => l === drawLayer,
}
)
}
}

map.on('pointermove', localSelector)
// @ts-expect-error | "un on removal" riding piggyback as _onRemove
modify._onRemove = () => map.un('pointermove', localSelector)

return modify
}

export default function (
{ commit, dispatch, getters }: PolarActionContext<DrawState, DrawGetters>,
{
commit,
dispatch,
getters,
rootGetters,
}: PolarActionContext<DrawState, DrawGetters>,
{ drawSource, drawLayer }: CreateInteractionsPayload
): Interaction[] {
const { drawMode } = getters
Expand Down Expand Up @@ -40,7 +84,7 @@ export default function (
})

return [
new Modify({ source: drawSource }),
createModify(rootGetters.map, drawLayer),
new Snap({ source: drawSource }),
select,
]
Expand Down