From 421bc13f39a57d6d987cf55102b25fa0b710af76 Mon Sep 17 00:00:00 2001 From: Simon Seyock Date: Tue, 19 Sep 2023 15:52:37 +0200 Subject: [PATCH] fix(DrawButton): remove text feature from layer on cancel --- src/Button/DrawButton/DrawButton.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Button/DrawButton/DrawButton.tsx b/src/Button/DrawButton/DrawButton.tsx index 5f5f3344d9..e139830493 100644 --- a/src/Button/DrawButton/DrawButton.tsx +++ b/src/Button/DrawButton/DrawButton.tsx @@ -1,4 +1,7 @@ import { useDraw, UseDrawProps } from '@terrestris/react-util/dist/Hooks/useDraw/useDraw'; +import useMap from '@terrestris/react-util/dist/Hooks/useMap/useMap'; +import {usePropOrDefault} from '@terrestris/react-util/dist/Hooks/usePropOrDefault/usePropOrDefault'; +import {DigitizeUtil} from '@terrestris/react-util/dist/Util/DigitizeUtil'; import OlFeature from 'ol/Feature'; import OlGeometry from 'ol/geom/Geometry'; import { @@ -70,6 +73,12 @@ const DrawButton: React.FC = ({ pressed, ...passThroughProps }) => { + const map = useMap(); + const layer = usePropOrDefault( + digitizeLayer, + () => map ? DigitizeUtil.getDigitizeLayer(map) : undefined, + [map] + ); /** * Currently drawn feature which should be represented as label or post-it. */ @@ -85,7 +94,7 @@ const DrawButton: React.FC = ({ useDraw({ onDrawEnd: onDrawEndInternal, - digitizeLayer, + digitizeLayer: layer, drawInteractionConfig, drawStyle, drawType: drawType === 'Text' ? 'Point' : drawType, @@ -107,6 +116,8 @@ const DrawButton: React.FC = ({ }; const onModalLabelCancelInternal = () => { + layer?.getSource()?.removeFeature(digitizeTextFeature); + setDigitizeTextFeature(null); onModalLabelCancel?.(); digitizeLayer?.getSource()?.removeFeature(digitizeTextFeature); setDigitizeTextFeature(null);