Fix: don't crash if an unselected element is selected (by not selecting it)

This commit is contained in:
Pieter Vander Vennet 2025-04-23 10:43:07 +02:00
parent b10d999b05
commit 981b412180
2 changed files with 16 additions and 3 deletions

View file

@ -32,11 +32,13 @@ export class WithSelectedElementState extends UserMapFeatureswitchState {
if (lastClick.mode !== "left") {
return
}
this.setSelectedElement(lastClick.nearestFeature)
const nearestFeature = lastClick.nearestFeature
this.setSelectedElement(nearestFeature)
})
// Add the selected element to the recently visited history
this.selectedElement.addCallbackD((selected) => {
console.trace("Selected element is", selected)
const [osm_type, osm_id] = selected.properties.id.split("/")
const [lon, lat] = GeoOperations.centerpointCoordinates(selected)
const layer = this.theme.getMatchingLayer(selected.properties)
@ -65,12 +67,21 @@ export class WithSelectedElementState extends UserMapFeatureswitchState {
}
protected setSelectedElement(feature: Feature) {
if (!feature) {
this.selectedElement.setData(undefined)
return
}
const layer = this.theme.getMatchingLayer(feature?.properties)
if (layer?.title === undefined) {
console.log("Not selecting feature", feature, ": no title (or no matching layer) found, unselectable element")
return
}
const current = this.selectedElement.data
if (
current?.properties?.id !== undefined &&
current.properties.id === feature?.properties?.id
) {
console.log("Not setting selected, same id", current, feature)
return // already set
}
this.selectedElement.setData(feature)

View file

@ -1,6 +1,6 @@
<script lang="ts">
import type { Feature } from "geojson"
import { ImmutableStore, Store, UIEventSource } from "../../Logic/UIEventSource"
import { Store, UIEventSource } from "../../Logic/UIEventSource"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import type { SpecialVisualizationState } from "../SpecialVisualization"
import TagRenderingAnswer from "../Popup/TagRendering/TagRenderingAnswer.svelte"
@ -35,7 +35,9 @@
<!-- Title element and title icons-->
<h3 class="m-0">
<a href={`#${$tags.id}`}>
{#if layer.title}
<TagRenderingAnswer config={layer.title} {selectedElement} {state} {tags} {layer} />
{/if}
</a>
</h3>
<div