diff --git a/src/Models/ThemeConfig/Conversion/MiscTagRenderingChecks.ts b/src/Models/ThemeConfig/Conversion/MiscTagRenderingChecks.ts index 32f79c986..f96a3b2e5 100644 --- a/src/Models/ThemeConfig/Conversion/MiscTagRenderingChecks.ts +++ b/src/Models/ThemeConfig/Conversion/MiscTagRenderingChecks.ts @@ -215,6 +215,9 @@ export class MiscTagRenderingChecks extends DesugaringStep /** * question: Should a contributor be allowed to select multiple mappings? @@ -298,13 +299,17 @@ export interface QuestionableTagRenderingConfigJson extends TagRenderingConfigJs helperArgs?: any /** - * question: what units + * question: what units/denominations should be shown? + * + * If this is a string, the corresponding values from "unit.json" will be used, such as + * `pressure`, `power`, `speed`, `distance` + * See [the generated documentation](Docs/builtin_units.md) for more information * * group: hidden * * Note: this is actually a syntactic sugar and is translated to the unit-syntax on layer level */ - unit?: { + unit?: string | { /** * What is the quantity? E.g. 'voltage', 'speed', ... * See [builtin_units.md] for options diff --git a/src/Models/ThemeConfig/TagRenderingConfig.ts b/src/Models/ThemeConfig/TagRenderingConfig.ts index ff3c0c387..7f101bd26 100644 --- a/src/Models/ThemeConfig/TagRenderingConfig.ts +++ b/src/Models/ThemeConfig/TagRenderingConfig.ts @@ -5,10 +5,7 @@ import { TagUtils } from "../../Logic/Tags/TagUtils" import { And } from "../../Logic/Tags/And" import { Utils } from "../../Utils" import { Tag } from "../../Logic/Tags/Tag" -import { - MappingConfigJson, - QuestionableTagRenderingConfigJson, -} from "./Json/QuestionableTagRenderingConfigJson" +import { MappingConfigJson, QuestionableTagRenderingConfigJson } from "./Json/QuestionableTagRenderingConfigJson" import Validators, { ValidatorType } from "../../UI/InputElement/Validators" import { TagRenderingConfigJson } from "./Json/TagRenderingConfigJson" import { RegexTag } from "../../Logic/Tags/RegexTag" @@ -822,7 +819,7 @@ export default class TagRenderingConfig { () => currentProperties["_country"] ) // In general, we want a space between the amount and the unit - freeformValue = formatted + " " + denom.canonical + freeformValue = formatted + (denom.addSpace ? " " : "") + denom.canonical } else { freeformValue = validator.reformat( freeformValue, diff --git a/src/Models/UnitUtils.ts b/src/Models/UnitUtils.ts index 29dd07e41..17bf06cbd 100644 --- a/src/Models/UnitUtils.ts +++ b/src/Models/UnitUtils.ts @@ -145,12 +145,7 @@ export class UnitUtils { const loaded = this.allUnits.get(name) if (loaded === undefined) { throw ( - "No unit with quantity name " + - name + - " found (at " + - ctx + - "). Try one of: " + - Array.from(this.allUnits.keys()).join(", ") + `No unit with quantity name ${name} found (at ${ctx}). Try one of: ${Array.from(this.allUnits.keys()).join(", ")}. Alternatively, use \`{"quantity": "...", "denominations": ["...", "...", ...]}\`, e.g. \`{"quantity": "distance", "denominations":["m","cm"]}\`` ) } return loaded diff --git a/src/UI/InputElement/ValidatedInput.svelte b/src/UI/InputElement/ValidatedInput.svelte index ef817b147..bf5cefc71 100644 --- a/src/UI/InputElement/ValidatedInput.svelte +++ b/src/UI/InputElement/ValidatedInput.svelte @@ -14,6 +14,7 @@ import type { ValueRange } from "../../Models/ThemeConfig/TagRenderingConfig" import Translations from "../i18n/Translations" import BaseUIElement from "../BaseUIElement" + import { Denomination } from "../../Models/Denomination" export let type: ValidatorType export let feedback: UIEventSource | undefined = undefined @@ -91,6 +92,7 @@ if (!range) { return true } + const currentUnit: Denomination = unit.denominations.find(d => d.canonical === selectedUnit?.data) if (typeof canonicalValue === "string") { canonicalValue = Number(canonicalValue) } @@ -103,7 +105,11 @@ if (canonicalValue > range.max) { let max: number | string | BaseUIElement = range.max if (unit) { - max = unit.asHumanLongValue(max, getCountry) + if (currentUnit?.factorToCanonical !== undefined) { + max = unit.asHumanLongValue(Math.round(max / currentUnit.factorToCanonical) + currentUnit.canonical, getCountry) + } else { + max = unit.asHumanLongValue(max, getCountry) + } } feedback.set(t.tooHigh.Subs({ max })) return false @@ -111,7 +117,11 @@ if (canonicalValue < range.min) { let min: number | string | BaseUIElement = range.min if (unit) { + if (currentUnit?.factorToCanonical !== undefined) { + min = unit.asHumanLongValue(Math.round(min / currentUnit.factorToCanonical) + currentUnit.canonical, getCountry) + } else { min = unit.asHumanLongValue(min, getCountry) + } } feedback.set(t.tooLow.Subs({ min })) return false