From 3e6b7c668e3c01913976ae80253e9e907ef2e10d Mon Sep 17 00:00:00 2001 From: keiya sasaki Date: Wed, 15 Nov 2023 15:18:00 +0900 Subject: [PATCH] chore(web): fix feature unselection (#812) --- web/src/beta/lib/core/Map/Layers/hooks.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/web/src/beta/lib/core/Map/Layers/hooks.ts b/web/src/beta/lib/core/Map/Layers/hooks.ts index 0e3cec34f2..8c34a19b73 100644 --- a/web/src/beta/lib/core/Map/Layers/hooks.ts +++ b/web/src/beta/lib/core/Map/Layers/hooks.ts @@ -821,7 +821,8 @@ function useSelection({ layerId?: string; featureId?: string[]; }[], - ) => { + ): boolean => { + let shouldUpdate = false; for (const { layerId, featureId } of layers) { if (!layerId || !featureId) continue; @@ -838,13 +839,14 @@ function useSelection({ if (featureId.length) { engineRef?.current?.selectFeatures(layerId, featureId); - updateStyle(layerId); selectedFeatureIds.current[selectedFeatureIdsIndex].featureIds = selectedFeatureIds.current[selectedFeatureIdsIndex].featureIds.concat(featureId); + shouldUpdate = true; } } + return shouldUpdate; }, - [engineRef, updateStyle], + [engineRef], ); const selectFeatures = useCallback( @@ -856,16 +858,26 @@ function useSelection({ options?: LayerSelectionReason, info?: SelectedFeatureInfo, ) => { + let shouldUpdate = false; selectedFeatureIds.current.forEach(id => { engineRef?.current?.unselectFeatures(id.layerId, id.featureIds); - updateStyle(id.layerId); + shouldUpdate = true; }); + const prevSelectedFeatureIds = selectedFeatureIds.current; selectedFeatureIds.current = []; updateSelectedLayerForFeature(layers, options, info); - updateEngineFeatures(layers); + shouldUpdate = updateEngineFeatures(layers) || shouldUpdate; + + if (!shouldUpdate) return; + + for (const { layerId } of [...layers, ...prevSelectedFeatureIds]) { + if (!layerId) continue; + // Wait 1 frame for cesium to synchronize the updated value. + requestAnimationFrame(() => updateStyle(layerId)); + } }, [engineRef, updateStyle, updateEngineFeatures, updateSelectedLayerForFeature], );