forked from MapComplete/MapComplete
Themes: allow to easily import tagrenderings and add a prefix key to all tags
This commit is contained in:
parent
4d9bdaf877
commit
01680f236c
9 changed files with 201 additions and 31 deletions
|
|
@ -0,0 +1,134 @@
|
|||
import { DesugaringStep } from "./Conversion"
|
||||
import { ConversionContext } from "./ConversionContext"
|
||||
import SpecialVisualizations from "../../../UI/SpecialVisualizations"
|
||||
import { Translatable } from "../Json/Translatable"
|
||||
import { TagConfigJson } from "../Json/TagConfigJson"
|
||||
import { MappingConfigJson, QuestionableTagRenderingConfigJson } from "../Json/QuestionableTagRenderingConfigJson"
|
||||
|
||||
export default class AddPrefixToTagRenderingConfig extends DesugaringStep<QuestionableTagRenderingConfigJson> {
|
||||
|
||||
|
||||
private readonly _prefix: string
|
||||
|
||||
constructor(prefix: string) {
|
||||
super("Adds `prefix` to _all_ keys. Used to add information about a subamenity withing a bigger amenity (e.g. toilets in a restaurant, a sauna in a water park, ...)", ["*"], "AddPrefixToTagRenderingConfig")
|
||||
this._prefix = prefix
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* const edit = new AddPrefixToTagRenderingConfig("PREFIX")
|
||||
* edit.updateString("Some string") // => "Some string"
|
||||
* edit.updateString("Some string {key0}") // => "Some string {PREFIX:key0}"
|
||||
*
|
||||
* // Should prefix a key in a special visualisation
|
||||
* new AddPrefixToTagRenderingConfig("PREFIX").updateString("{opening_hours_table(opening_hours)}") // => "{opening_hours_table(PREFIX:opening_hours,,)}"
|
||||
*
|
||||
* // Should prefix the default key in a special visualisation
|
||||
* new AddPrefixToTagRenderingConfig("PREFIX").updateString("{opening_hours_table()}") // => "{opening_hours_table(PREFIX:opening_hours,,)}"
|
||||
*/
|
||||
private updateString(str: string): string {
|
||||
const parsed = SpecialVisualizations.constructSpecification(str)
|
||||
const fixedSpec: string[] = []
|
||||
for (const spec of parsed) {
|
||||
if (typeof spec === "string") {
|
||||
const part = spec.replace(/{([a-zA-Z0-9:_-]+)}/g, `{${this._prefix}:$1}`)
|
||||
fixedSpec.push(part)
|
||||
} else {
|
||||
const newArgs: string[] = []
|
||||
for (let i = 0; i < spec.func.args.length; i++) {
|
||||
const argDoc = spec.func.args[i]
|
||||
const argV = spec.args[i]
|
||||
if (argDoc.type === "key") {
|
||||
newArgs.push(this._prefix + ":" + (argV ?? argDoc.defaultValue ?? ""))
|
||||
} else {
|
||||
newArgs.push(argV ?? "")
|
||||
}
|
||||
}
|
||||
fixedSpec.push("{" + spec.func.funcName + "(" + newArgs.join(",") + ")}")
|
||||
}
|
||||
}
|
||||
return fixedSpec.join("")
|
||||
|
||||
}
|
||||
|
||||
private updateTranslatable(val: Translatable | undefined): Translatable | undefined {
|
||||
if (!val) {
|
||||
return val
|
||||
}
|
||||
if (typeof val === "string") {
|
||||
return this.updateString(val)
|
||||
}
|
||||
const newTranslations: Record<string, string> = {}
|
||||
for (const lng in val) {
|
||||
newTranslations[lng] = this.updateString(val[lng])
|
||||
}
|
||||
return newTranslations
|
||||
}
|
||||
|
||||
private updateTag(tags: string): string;
|
||||
private updateTag(tags: TagConfigJson): TagConfigJson;
|
||||
private updateTag(tags: TagConfigJson): TagConfigJson {
|
||||
if (!tags) {
|
||||
return tags
|
||||
}
|
||||
if (tags["and"]) {
|
||||
return { and: this.updateTags(tags["and"]) }
|
||||
}
|
||||
if (tags["or"]) {
|
||||
return { or: this.updateTags(tags["or"]) }
|
||||
}
|
||||
return this._prefix + ":" + tags
|
||||
}
|
||||
|
||||
private updateTags(tags: ReadonlyArray<string>): string[] {
|
||||
return tags?.map(tag => this.updateTag(tag))
|
||||
}
|
||||
|
||||
private updateMapping(mapping: Readonly<MappingConfigJson>): MappingConfigJson {
|
||||
return {
|
||||
...mapping,
|
||||
addExtraTags: this.updateTags(mapping.addExtraTags),
|
||||
if: this.updateTag(mapping.if),
|
||||
then: this.updateTranslatable(mapping.then),
|
||||
alsoShowIf: this.updateTag(mapping.alsoShowIf),
|
||||
ifnot: this.updateTag(mapping.ifnot),
|
||||
priorityIf: this.updateTag(mapping.priorityIf),
|
||||
hideInAnswer: mapping.hideInAnswer === true || mapping.hideInAnswer === false ? mapping.hideInAnswer : this.updateTag(mapping.hideInAnswer)
|
||||
}
|
||||
}
|
||||
|
||||
public convert(json: Readonly<QuestionableTagRenderingConfigJson>, context: ConversionContext): QuestionableTagRenderingConfigJson {
|
||||
let freeform = json.freeform
|
||||
if (freeform) {
|
||||
const ff = json.freeform
|
||||
freeform = {
|
||||
...ff,
|
||||
key: this._prefix + ":" + ff.key,
|
||||
addExtraTags: this.updateTags(ff.addExtraTags)
|
||||
}
|
||||
}
|
||||
|
||||
return <QuestionableTagRenderingConfigJson>{
|
||||
...json,
|
||||
id: this._prefix + "_" + json.id,
|
||||
|
||||
question: this.updateTranslatable(json.question),
|
||||
questionHint: this.updateTranslatable(json.questionHint),
|
||||
|
||||
render: this.updateTranslatable(<Translatable>json.render),
|
||||
freeform,
|
||||
editButtonAriaLabel: json.editButtonAriaLabel,
|
||||
onSoftDelete: this.updateTags(json.onSoftDelete),
|
||||
invalidValues: this.updateTag(json.invalidValues),
|
||||
mappings: json.mappings?.map(mapping => this.updateMapping(mapping)),
|
||||
|
||||
condition: this.updateTag(json.condition),
|
||||
metacondition: json.metacondition, // no update here
|
||||
filter: json.filter === true, // We break references to filters, as those references won't have the updated tags
|
||||
_appliedPrefix: this._prefix
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -6,6 +6,8 @@ import { QuestionableTagRenderingConfigJson } from "../Json/QuestionableTagRende
|
|||
import { TagUtils } from "../../../Logic/Tags/TagUtils"
|
||||
import { Utils } from "../../../Utils"
|
||||
import { AddContextToTranslations } from "./AddContextToTranslations"
|
||||
import AddPrefixToTagRenderingConfig from "./AddPrefixToTagRenderingConfig"
|
||||
import { Translatable } from "../Json/Translatable"
|
||||
|
||||
export class ExpandTagRendering extends Conversion<
|
||||
| string
|
||||
|
|
@ -208,6 +210,21 @@ export class ExpandTagRendering extends Conversion<
|
|||
let matchingTrs: (TagRenderingConfigJson & { id: string })[]
|
||||
if (id === "*") {
|
||||
matchingTrs = layerTrs
|
||||
} else if (id === "title") {
|
||||
const title = layer.title
|
||||
if (title["render"] || title["mappings"]) {
|
||||
const titleTr = <TagRenderingConfigJson>layer.title
|
||||
return [{
|
||||
...titleTr,
|
||||
id: layer.id + "_title"
|
||||
}]
|
||||
} else {
|
||||
const transl = <Translatable>layer.title
|
||||
return [{
|
||||
render: transl,
|
||||
id: layer.id + "_title"
|
||||
}]
|
||||
}
|
||||
} else if (id.startsWith("*")) {
|
||||
const id_ = id.substring(1)
|
||||
matchingTrs = layerTrs.filter((tr) => tr["labels"]?.indexOf(id_) >= 0)
|
||||
|
|
@ -249,6 +266,25 @@ export class ExpandTagRendering extends Conversion<
|
|||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a variation of 'tr' where every key has been prefixed by the given 'prefix'-key.
|
||||
* If the given key is undefined, returns the original tagRendering.
|
||||
*
|
||||
* Note: metacondition will _not_ be prefixed
|
||||
* @param key
|
||||
* @param tr
|
||||
* @private
|
||||
*/
|
||||
private static applyKeyPrefix(key: string | undefined, tr: Readonly<QuestionableTagRenderingConfigJson>, ctx: ConversionContext): QuestionableTagRenderingConfigJson {
|
||||
if (key === undefined || key === null) {
|
||||
return tr
|
||||
}
|
||||
if (key.endsWith(":")) {
|
||||
ctx.err("A 'prefix'-key should not end with a colon. The offending prefix value is: " + key)
|
||||
}
|
||||
return new AddPrefixToTagRenderingConfig(key).convert(tr, ctx.enter("prefix"))
|
||||
}
|
||||
|
||||
private convertOnce(
|
||||
tr: string | { builtin: string | string[] } | TagRenderingConfigJson,
|
||||
ctx: ConversionContext
|
||||
|
|
@ -310,6 +346,7 @@ export class ExpandTagRendering extends Conversion<
|
|||
if (
|
||||
key === "builtin" ||
|
||||
key === "override" ||
|
||||
key === "prefix" ||
|
||||
key === "id" ||
|
||||
key.startsWith("#")
|
||||
) {
|
||||
|
|
@ -379,6 +416,7 @@ export class ExpandTagRendering extends Conversion<
|
|||
}
|
||||
for (let foundTr of lookup) {
|
||||
foundTr = Utils.Clone(foundTr)
|
||||
foundTr = ExpandTagRendering.applyKeyPrefix(tr["prefix"], foundTr, ctx)
|
||||
ctx.MergeObjectsForOverride(tr["override"] ?? {}, foundTr)
|
||||
if (names.length == 1) {
|
||||
foundTr["id"] = tr["id"] ?? foundTr["id"]
|
||||
|
|
|
|||
|
|
@ -1,14 +1,4 @@
|
|||
import {
|
||||
Concat,
|
||||
Conversion,
|
||||
DesugaringContext,
|
||||
DesugaringStep,
|
||||
Each,
|
||||
Fuse,
|
||||
On,
|
||||
Pass,
|
||||
SetDefault,
|
||||
} from "./Conversion"
|
||||
import { Concat, Conversion, DesugaringContext, DesugaringStep, Each, Fuse, On, Pass, SetDefault } from "./Conversion"
|
||||
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
|
||||
import { PrepareLayer, RewriteSpecial } from "./PrepareLayer"
|
||||
import { LayerConfigJson } from "../Json/LayerConfigJson"
|
||||
|
|
@ -163,9 +153,8 @@ class SubstituteLayer extends Conversion<string | LayerConfigJson, LayerConfigJs
|
|||
const unused = Array.from(hideLabels).filter((l) => !usedLabels.has(l))
|
||||
if (unused.length > 0) {
|
||||
context.err(
|
||||
"This theme specifies that certain tagrenderings have to be removed based on forbidden layers. One or more of these layers did not match any tagRenderings and caused no deletions: " +
|
||||
unused.join(", ") +
|
||||
"\n This means that this label can be removed or that the original tagRendering that should be deleted does not have this label anymore"
|
||||
`You are attempting to import layer '${found.id}' in this theme. This layer import specifies that certain tagrenderings have to be removed based on forbidden ids and/or labels. One or more of these forbidden ids did not match any tagRenderings and caused no deletions: ${unused.join(", ")}
|
||||
This means that this label can be removed or that the original tagRendering that should be deleted does not have this label anymore`
|
||||
)
|
||||
}
|
||||
found.tagRenderings = filtered
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue