diff --git a/src/components/Map/EditingToolbar.vue b/src/components/Map/EditingToolbar.vue index cd76c5b679b88813992ac0ed88a1adff907f4fa5..9de7a5fe75a4b5693a39b996f4919f090a466b26 100644 --- a/src/components/Map/EditingToolbar.vue +++ b/src/components/Map/EditingToolbar.vue @@ -47,7 +47,6 @@ <span class="sr-only">Modifier {{ toolbarEditGeomTitle }}</span> </a> <a - :class="{ active: isDeleting }" :title="`Supprimer ${toolbarEditGeomTitle}`" @click="deleteObj" > @@ -77,7 +76,6 @@ export default { return { editionService: editionService, isEditing: false, - isDeleting: false, isSnapEnabled: false, }; }, @@ -103,13 +101,11 @@ export default { methods: { update() { editionService.activeUpdateFeature(); - this.isDeleting = false; this.isEditing = true; }, deleteObj() { - editionService.activeDeleteFeature(); + editionService.removeFeatureFromMap(); this.isEditing = false; - this.isDeleting = true; }, toggleSnap() { if (this.isSnapEnabled) { diff --git a/src/services/edition-service.js b/src/services/edition-service.js index 79483775db6b488df3ef61b5a0648cab8c4d3733..62f3d9bfc1d892d80bdb0c284403350f6495d12e 100644 --- a/src/services/edition-service.js +++ b/src/services/edition-service.js @@ -169,11 +169,6 @@ const editionService = { if (this.draw) { this.draw.setActive(false); } - - if (this.selectForDeletion) { - this.removeSelectInteraction(this.selectForDeletion); - - } if (this.selectForUpdate) { this.removeSelectInteraction(this.selectForUpdate); } @@ -192,63 +187,32 @@ const editionService = { this.selectForUpdate.getFeatures().push(this.editing_feature); }, - activeDeleteFeature() { + /** + * Deletes the currently displayed feature from the map. + * This method removes the feature directly from the source without additional selection steps. + * It assumes that there is only one feature present in the source. + */ + removeFeatureFromMap() { + // Reset all other tools to ensure only the delete feature functionality is active this.resetAllTools(); - if (!this.selectForDeletion) { - const style = new Style({ - fill: new Fill({ - color: 'rgba(255, 0, 0, 0.2)' - }), - stroke: new Stroke({ - color: 'rgba(255, 0, 0, 0.5)', - width: 2 - }), - image: new Circle({ - radius: 7, - fill: new Fill({ - color: 'rgba(255, 0, 0, 0.5)' - }) - }), - text: new Text({ - font: '12px Calibri,sans-serif', - fill: new Fill({ color: 'rgba(255, 0, 0, 0.5)' }), - }), - zIndex: 50 - }); - this.selectForDeletion = new Select({ - style: style, - filter: (feature) => { - if (this.featureToEdit && feature.id_ === this.featureToEdit.id) { - return true; - } else if (this.drawnFeature && feature.ol_uid === this.drawnFeature.ol_uid) { - return true; - } else if (!this.drawnFeature && !this.featureToEdit) { - return true; - } - return false; - } - }); - mapService.getMap().addInteraction(this.selectForDeletion); - // Lorsque de nouvelles features sont sélectionnées - const selected_features = this.selectForDeletion.getFeatures(); - this.listenerKey = selected_features.on('add', (evt) => { - var feature = evt.element; - if (feature) { - setTimeout(() => { - if (confirm('Etes-vous sur de vouloir supprimer cet objet ?')) { - // supprimer l'edition de la sélection - this.selectForDeletion.getFeatures().clear(); - // supprimer l'edition de la carte - this.drawSource.removeFeature(feature); - this.editing_feature = undefined; - this.draw.setActive(true); - this.selectForDeletion.setActive(false); - } - }, 300); - } - }); - } else { - this.selectForDeletion.setActive(true); + // Access the source where the features are stored + const source = this.drawSource; // Replace with the correct reference to your OpenLayers source + // Get all features from the source + const features = source.getFeatures(); + // Check if there is a feature to delete + if (features.length > 0 && confirm('Etes-vous sur de vouloir supprimer cet objet ?')) { + try { + // Remove the feature from the source + const featureToRemove = features[0]; + source.removeFeature(featureToRemove); + // Reinitialize the feature edited on the map + this.editing_feature = undefined; + // Toggle draw mode to create a new feature + this.draw.setActive(true); + } catch (error) { + // Log an error if the feature cannot be removed + console.error('Error while deleting the feature: ', error); + } } },