From 9050c5cf259db862d58f6360cc1940be78092635 Mon Sep 17 00:00:00 2001 From: Chad Burt Date: Wed, 18 Oct 2023 11:28:56 -0700 Subject: [PATCH] Fixes #647 (try 3) --- packages/client/src/MeasureControl.tsx | 11 ++++++++--- packages/client/src/draw/useMapboxGLDraw.ts | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/client/src/MeasureControl.tsx b/packages/client/src/MeasureControl.tsx index ae9d5b3d0..fc0010175 100644 --- a/packages/client/src/MeasureControl.tsx +++ b/packages/client/src/MeasureControl.tsx @@ -187,10 +187,14 @@ class MeasureControl extends EventEmitter { private addSourcesAndLayers() { const sources = this.getSources(); for (const id in sources) { - this.map.addSource(id, sources[id]); + if (!this.map.getSource(id)) { + this.map.addSource(id, sources[id]); + } } for (const layer of measureLayers) { - this.map.addLayer(layer); + if (!this.map.getLayer(layer.id)) { + this.map.addLayer(layer); + } } } @@ -311,12 +315,13 @@ class MeasureControl extends EventEmitter { // unregister all event handlers this.removeEventListeners(this.map); } + document.body.removeEventListener("keydown", this.onKeyDown); this.isDestroyed = true; }; onKeyDown = (e: KeyboardEvent) => { if (this.isDestroyed) { - throw new Error("MeasureControl is destroyed"); + return; } if (e.key === "Escape" && this.state === "drawing") { this.stopEditing(); diff --git a/packages/client/src/draw/useMapboxGLDraw.ts b/packages/client/src/draw/useMapboxGLDraw.ts index 521e6102e..e9fcbb567 100644 --- a/packages/client/src/draw/useMapboxGLDraw.ts +++ b/packages/client/src/draw/useMapboxGLDraw.ts @@ -134,6 +134,7 @@ export default function useMapboxGLDraw( useEffect(() => { if ( mapContext.manager?.map && + // mapContext.manager.map.loaded() && geometryType && !disabled && !handlerState.current.draw @@ -357,6 +358,7 @@ export default function useMapboxGLDraw( disabled, preprocessingEndpoint, preprocessingResults, + // mapContext.manager?.map?.loaded(), ]); useEffect(() => {