From d488f8c9b2e295b7879d5dcee03f242139caed4e Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 3 May 2024 18:44:37 +0200 Subject: [PATCH] Fix #1935 --- src/UI/Popup/TagRendering/Questionbox.svelte | 2 +- .../TagRendering/TagRenderingQuestion.svelte | 53 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/UI/Popup/TagRendering/Questionbox.svelte b/src/UI/Popup/TagRendering/Questionbox.svelte index adebc53b9..585f1d00a 100644 --- a/src/UI/Popup/TagRendering/Questionbox.svelte +++ b/src/UI/Popup/TagRendering/Questionbox.svelte @@ -79,7 +79,7 @@ console.log("Applying questions to ask") const qta = questionsToAsk.data firstQuestion.setData(undefined) - allQuestionsToAsk.setData([]) + //allQuestionsToAsk.setData([]) await Utils.awaitAnimationFrame() firstQuestion.setData(qta[0]) allQuestionsToAsk.setData(qta) diff --git a/src/UI/Popup/TagRendering/TagRenderingQuestion.svelte b/src/UI/Popup/TagRendering/TagRenderingQuestion.svelte index 0f3faa293..83d1b8e38 100644 --- a/src/UI/Popup/TagRendering/TagRenderingQuestion.svelte +++ b/src/UI/Popup/TagRendering/TagRenderingQuestion.svelte @@ -35,6 +35,7 @@ export let config: TagRenderingConfig export let tags: UIEventSource> + export let selectedElement: Feature export let state: SpecialVisualizationState export let layer: LayerConfig | undefined @@ -69,7 +70,10 @@ /** * Prepares and fills the checkedMappings */ + console.log("Initing ", config.id) + function initialize(tgs: Record, confg: TagRenderingConfig): void { + console.trace("Initing question state for", confg.id, config.id) mappings = confg.mappings?.filter((m) => { if (typeof m.hideInAnswer === "boolean") { return !m.hideInAnswer @@ -77,7 +81,7 @@ return !m.hideInAnswer.matchesProperties(tgs) }) selectedMapping = mappings?.findIndex(mapping => mapping.if.matchesProperties(tgs) || mapping.alsoShowIf?.matchesProperties(tgs)) - if(selectedMapping < 0){ + if (selectedMapping < 0) { selectedMapping = undefined } // We received a new config -> reinit @@ -137,11 +141,35 @@ } - $: { - // Even though 'config' is not declared as a store, Svelte uses it as one to update the component - // We want to (re)-initialize whenever the 'tags' or 'config' change - but not when 'checkedConfig' changes - initialize($tags, config) - } + let usedKeys: string[] = config.usedTags().flatMap(t => t.usedKeys()) + /** + * The 'minimalTags' is a subset of the tags of the feature, only containing the values relevant for this object. + * The main goal is to be stable and only 'ping' when an actual change is relevant + */ + let minimalTags = new UIEventSource>(undefined) + tags.addCallbackAndRunD(tags => { + const previousMinimal = minimalTags.data + const newMinimal: Record = {} + let somethingChanged = previousMinimal === undefined + for (const key of usedKeys) { + const newValue = tags[key] + somethingChanged ||= previousMinimal?.[key] !== newValue + if (newValue !== undefined && newValue !== null) { + newMinimal[key] = newValue + } + + } + if (somethingChanged) { + console.log("Updating minimal tags to", newMinimal,"of",config.id) + minimalTags.setData(newMinimal) + } + }) + + minimalTags.addCallbackAndRunD(tgs => { + initialize(tgs, config) + }) + + onDestroy( freeformInput.subscribe((freeformValue) => { if (!mappings || mappings?.length == 0 || config.freeform?.key === undefined) { @@ -178,8 +206,13 @@ checkedMappings, tags.data ) - if(state.featureSwitches.featureSwitchIsDebugging.data){ - console.log("Constructing change spec from", {freeform: $freeformInput, selectedMapping, checkedMappings, currentTags: tags.data}, " --> ", selectedTags) + if (state.featureSwitches.featureSwitchIsDebugging.data) { + console.log("Constructing change spec from", { + freeform: $freeformInput, + selectedMapping, + checkedMappings, + currentTags: tags.data + }, " --> ", selectedTags) } } catch (e) { console.error("Could not calculate changeSpecification:", e) @@ -194,7 +227,7 @@ } if (layer === undefined || (layer?.source === null && layer.id !== "favourite")) { /** - * This is a special, priviliged layer. + * This is a special, privileged layer. * We simply apply the tags onto the records */ const kv = selectedTags.asChange(tags.data) @@ -277,7 +310,7 @@ feature={selectedElement} /> - {/if} + {/if} {/if}