farmOS / farmOS-map

farmOS-map is an OpenLayers wrapper library designed for agricultural mapping needs. It can be used in any project that has similar requirements.
https://farmOS.github.io/farmOS-map
MIT License
40 stars 22 forks source link

Add ability to determine if an edit interaction is active #184

Open paul121 opened 2 years ago

paul121 commented 2 years ago

Right now I'm not sure if there is an easy way to determine if any of the edit interactions are currently "active". This would be useful when adding custom behaviors that should/should not do certain things when a user is using interactions from the edit control, like drawing on the map.

An interaction object is added to instance.edit the first time it is turned on, but is never removed. This can be used to see if an interaction has been used by looking on the instance.edit control eg: instance.edit.drawInteraction or instance.edit.modifyInteraction, but because it is never removed, it cannot be used to determine if the interaction is "active".

Interestingly, interaction objects do have a getActive() method, but our edit control does not use this: https://openlayers.org/en/latest/apidoc/module-ol_interaction_Interaction-Interaction.html#getActive Instead of making the interaction "inactive" it removes the interaction from the map, but keeps the interaction object itself at instance.edit.*. The interaction object continues to report getActive() == true even after the interaction was removed from the map.

After a quick look at this I see two possible approaches:

  1. Remove the interaction object from instance.edit after removing the interaction from the map. This way the presence of the interaction object can be used to determine if it is active. https://github.com/farmOS/farmOS-map/blob/65f13b2cd9a36f4b211ec9211e2532b4fcf29d48/src/control/Edit/Edit.js#L423-L444

  2. Refactor the edit control to use the "active" state of interactions instead of adding/removing interactions from the map. At first glance it seems like this might be feasible, but I'm not 100% sure. See also #183

Finally, it would be nice if the edit control provided some helper methods to determine if any part of the edit control "is active". This could be a convenience method wrapped around one of the approaches described above. For example:

# Determine if any edit interaction is being used.
let editing = instance.edit.getActive();

# Determine if the draw interaction is being used.
let drawing = instance.edit.getActive('draw');

# Determine if the modify interaction is being used.
let modifying = instance.edit.getActive('modify');
symbioquine commented 2 years ago

I feel that we should follow the patterns that OpenLayers has established and keep farmOS-map's behavior as unsurprising as possible when OL functionality is used programmatically.

In practice I think that means you're highlighting some real issues here @paul121.

IMHO we should;