Refactoring: allow to reuse units, move all units into central file

This commit is contained in:
Pieter Vander Vennet 2023-12-12 03:46:51 +01:00
parent 067fb549c1
commit 94e07d5b13
30 changed files with 1495 additions and 1307 deletions

View file

@ -517,7 +517,10 @@ export interface LayerConfigJson {
*
* group: editing
*/
units?: UnitConfigJson[]
units?: (
| UnitConfigJson
| Record<string, string | { quantity: string; denominations: string[]; canonical?: string }>
)[]
/**
* If set, synchronizes whether or not this layer is enabled.

View file

@ -57,12 +57,16 @@
*
*/
export default interface UnitConfigJson {
/**
* What is quantified? E.g. 'speed', 'length' (including width, diameter, ...), 'electric tension', 'electric current', 'duration'
*/
quantity?: string
/**
* Every key from this list will be normalized.
*
* To render the value properly (with a human readable denomination), use `{canonical(<key>)}`
*/
appliesToKey: string[]
appliesToKey?: string[]
/**
* If set, invalid values will be erased in the MC application (but not in OSM of course!)
* Be careful with setting this
@ -143,4 +147,11 @@ export interface DenominationConfigJson {
* Note that if all values use 'prefix', the dropdown might move to before the text field
*/
prefix?: boolean
/**
* If set, add a space between the quantity and the denomination.
*
* E.g.: `50 mph` instad of `50mph`
*/
addSpace?: boolean
}

View file

@ -105,8 +105,10 @@ export default class LayerConfig extends WithContextLoader {
".units: the 'units'-section should be a list; you probably have an object there"
)
}
this.units = (json.units ?? []).map((unitJson, i) =>
Unit.fromJson(unitJson, `${context}.unit[${i}]`)
this.units = [].concat(
...(json.units ?? []).map((unitJson, i) =>
Unit.fromJson(unitJson, `${context}.unit[${i}]`)
)
)
if (json.description !== undefined) {