refactoring: move logic of lastclick into special layer, fix labels, fix anchoring

This commit is contained in:
Pieter Vander Vennet 2023-04-02 02:59:20 +02:00
parent 25a98af057
commit 52a0810ea9
47 changed files with 682 additions and 197 deletions

View file

@ -50,6 +50,9 @@ export default class SelectedElementTagsUpdater {
const state = this.state
state.selectedElement.addCallbackAndRunD(async (s) => {
let id = s.properties?.id
if (!id) {
return
}
const backendUrl = state.osmConnection._oauth_config.url
if (id.startsWith(backendUrl)) {

View file

@ -20,32 +20,24 @@ export default class TitleHandler {
(selected) => {
const defaultTitle = state.layout?.title?.txt ?? "MapComplete"
if (selected === undefined) {
if (selected === undefined || selectedLayer.data === undefined) {
return defaultTitle
}
const tags = selected.properties
for (const layer of state.layout?.layers ?? []) {
if (layer.title === undefined) {
continue
}
if (layer.source.osmTags.matchesProperties(tags)) {
const tagsSource =
allElements.getStore(tags.id) ??
new UIEventSource<Record<string, string>>(tags)
const title = new SvelteUIElement(TagRenderingAnswer, {
tags: tagsSource,
state,
selectedElement: selectedElement.data,
layer: selectedLayer.data,
})
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()
?.textContent ?? defaultTitle
)
}
}
return defaultTitle
const layer = selectedLayer.data
const tagsSource =
allElements.getStore(tags.id) ?? new UIEventSource<Record<string, string>>(tags)
const title = new SvelteUIElement(TagRenderingAnswer, {
tags: tagsSource,
state,
selectedElement: selectedElement.data,
layer,
})
return (
new Combine([defaultTitle, " | ", title]).ConstructElement()?.textContent ??
defaultTitle
)
},
[Locale.language, selectedLayer]
)

View file

@ -43,6 +43,10 @@ export default class FeaturePropertiesStore {
return this._elements.get(id)
}
public addSpecial(id: string, store: UIEventSource<Record<string, string>>) {
this._elements.set(id, store)
}
/**
* Overwrites the tags of the old properties object, returns true if a change was made.
* Metatags are overriden if they are in the new properties, but not removed

View file

@ -0,0 +1,57 @@
import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig"
import FeatureSource from "../FeatureSource"
import { ImmutableStore, Store } from "../../UIEventSource"
import { Feature, Point } from "geojson"
import { TagUtils } from "../../Tags/TagUtils"
import BaseUIElement from "../../../UI/BaseUIElement"
import { Utils } from "../../../Utils"
import { regex_not_newline_characters } from "svelte/types/compiler/utils/patterns"
import { render } from "sass"
/**
* Highly specialized feature source.
* Based on a lon/lat UIEVentSource, will generate the corresponding feature with the correct properties
*/
export class LastClickFeatureSource implements FeatureSource {
features: Store<Feature[]>
public properties: Record<string, string>
constructor(location: Store<{ lon: number; lat: number }>, layout: LayoutConfig) {
const allPresets: BaseUIElement[] = []
for (const layer of layout.layers)
for (let i = 0; i < (layer.presets ?? []).length; i++) {
const preset = layer.presets[i]
const tags = new ImmutableStore(TagUtils.KVtoProperties(preset.tags))
const { html } = layer.mapRendering[0].RenderIcon(tags, false, {
noSize: true,
includeBadges: false,
})
allPresets.push(html)
}
const renderings = Utils.Dedup(
allPresets.map((uiElem) => uiElem.ConstructElement().innerHTML)
)
const properties = {
lastclick: "yes",
id: "last_click",
has_note_layer: layout.layers.some((l) => l.id === "note") ? "yes" : "no",
has_presets: layout.layers.some((l) => l.presets?.length > 0) ? "yes" : "no",
renderings: renderings.join(""),
number_of_presets: "" + renderings.length,
first_preset: renderings[0],
}
this.properties = properties
this.features = location.mapD(({ lon, lat }) => [
<Feature<Point>>{
type: "Feature",
properties,
geometry: {
type: "Point",
coordinates: [lon, lat],
},
},
])
}
}

View file

@ -21,16 +21,16 @@ export class TagUtils {
[">", (a, b) => a > b],
]
static KVtoProperties(tags: Tag[]): any {
const properties = {}
static KVtoProperties(tags: Tag[]): Record<string, string> {
const properties : Record<string, string> = {}
for (const tag of tags) {
properties[tag.key] = tag.value
}
return properties
}
static changeAsProperties(kvs: { k: string; v: string }[]): any {
const tags = {}
static changeAsProperties(kvs: { k: string; v: string }[]): Record<string, string> {
const tags: Record<string, string> = {}
for (const kv of kvs) {
tags[kv.k] = kv.v
}