Refactoring: use theme instead of layout in a lot of places

This commit is contained in:
Pieter Vander Vennet 2024-10-17 04:06:03 +02:00
parent 9427083939
commit bdc9ba52a6
104 changed files with 445 additions and 449 deletions

View file

@ -1,5 +1,5 @@
import { Conversion, DesugaringStep } from "./Conversion"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import { Utils } from "../../../Utils"
import metapaths from "../../../assets/schemas/layoutconfigmeta.json"
import tagrenderingmetapaths from "../../../assets/schemas/questionabletagrenderingconfigmeta.json"
@ -9,7 +9,7 @@ import { parse as parse_html } from "node-html-parser"
import { ConversionContext } from "./ConversionContext"
export class ExtractImages extends Conversion<
LayoutConfigJson,
ThemeConfigJson,
{ path: string; context: string }[]
> {
private static readonly layoutMetaPaths = metapaths.filter((mp) => {
@ -109,7 +109,7 @@ export class ExtractImages extends Conversion<
*
*/
convert(
json: LayoutConfigJson,
json: ThemeConfigJson,
context: ConversionContext
): { path: string; context: string }[] {
const allFoundImages: { path: string; context: string }[] = []
@ -243,7 +243,7 @@ export class ExtractImages extends Conversion<
}
}
export class FixImages extends DesugaringStep<LayoutConfigJson> {
export class FixImages extends DesugaringStep<ThemeConfigJson> {
private readonly _knownImages: Set<string>
constructor(knownImages: Set<string>) {
@ -289,7 +289,7 @@ export class FixImages extends DesugaringStep<LayoutConfigJson> {
* fixed.layers[0]["pointRendering"][0].marker[0].icon // => "https://raw.githubusercontent.com/seppesantens/MapComplete-Themes/main/VerkeerdeBordenDatabank/TS_bolt.svg"
* fixed.layers[0]["pointRendering"][0].iconBadges[0].then.mappings[0].then // => "https://raw.githubusercontent.com/seppesantens/MapComplete-Themes/main/VerkeerdeBordenDatabank/Something.svg"
*/
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
let url: URL
try {
url = new URL(json.id)

View file

@ -1,4 +1,4 @@
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import { Utils } from "../../../Utils"
import LineRenderingConfigJson from "../Json/LineRenderingConfigJson"
import { LayerConfigJson } from "../Json/LayerConfigJson"
@ -257,12 +257,12 @@ export class UpdateLegacyLayer extends DesugaringStep<
}
}
class UpdateLegacyTheme extends DesugaringStep<LayoutConfigJson> {
class UpdateLegacyTheme extends DesugaringStep<ThemeConfigJson> {
constructor() {
super("Small fixes in the theme config", ["roamingRenderings"], "UpdateLegacyTheme")
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const oldThemeConfig = { ...json }
if (oldThemeConfig.socialImage === "") {
@ -311,7 +311,7 @@ class UpdateLegacyTheme extends DesugaringStep<LayoutConfigJson> {
}
}
export class FixLegacyTheme extends Fuse<LayoutConfigJson> {
export class FixLegacyTheme extends Fuse<ThemeConfigJson> {
constructor() {
super(
"Fixes a legacy theme to the modern JSON format geared to humans. Syntactic sugars are kept (i.e. no tagRenderings are expandend, no dependencies are automatically gathered)",

View file

@ -1,5 +1,5 @@
import { Concat, Conversion, DesugaringContext, DesugaringStep, Each, Fuse, On, Pass, SetDefault } from "./Conversion"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import { PrepareLayer } from "./PrepareLayer"
import { LayerConfigJson } from "../Json/LayerConfigJson"
import { Utils } from "../../../Utils"
@ -165,7 +165,7 @@ class SubstituteLayer extends Conversion<string | LayerConfigJson, LayerConfigJs
}
}
class AddDefaultLayers extends DesugaringStep<LayoutConfigJson> {
class AddDefaultLayers extends DesugaringStep<ThemeConfigJson> {
private readonly _state: DesugaringContext
constructor(state: DesugaringContext) {
@ -177,7 +177,7 @@ class AddDefaultLayers extends DesugaringStep<LayoutConfigJson> {
this._state = state
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const state = this._state
json.layers = Utils.NoNull([...(json.layers ?? [])])
const alreadyLoaded = new Set(json.layers.map((l) => l["id"]))
@ -209,7 +209,7 @@ class AddDefaultLayers extends DesugaringStep<LayoutConfigJson> {
}
}
class AddContextToTranslationsInLayout extends DesugaringStep<LayoutConfigJson> {
class AddContextToTranslationsInLayout extends DesugaringStep<ThemeConfigJson> {
constructor() {
super(
"Adds context to translations, including the prefix 'themes:json.id'; this is to make sure terms in an 'overrides' or inline layer are linkable too",
@ -218,8 +218,8 @@ class AddContextToTranslationsInLayout extends DesugaringStep<LayoutConfigJson>
)
}
convert(json: LayoutConfigJson): LayoutConfigJson {
const conversion = new AddContextToTranslations<LayoutConfigJson>("themes:")
convert(json: ThemeConfigJson): ThemeConfigJson {
const conversion = new AddContextToTranslations<ThemeConfigJson>("themes:")
// The context is used to generate the 'context' in the translation .It _must_ be `json.id` to correctly link into weblate
return conversion.convert(
json,
@ -228,7 +228,7 @@ class AddContextToTranslationsInLayout extends DesugaringStep<LayoutConfigJson>
}
}
class ApplyOverrideAll extends DesugaringStep<LayoutConfigJson> {
class ApplyOverrideAll extends DesugaringStep<ThemeConfigJson> {
constructor() {
super(
"Applies 'overrideAll' onto every 'layer'. The 'overrideAll'-field is removed afterwards",
@ -237,7 +237,7 @@ class ApplyOverrideAll extends DesugaringStep<LayoutConfigJson> {
)
}
convert(json: LayoutConfigJson, ctx: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, ctx: ConversionContext): ThemeConfigJson {
const overrideAll = json.overrideAll
if (overrideAll === undefined) {
return json
@ -278,7 +278,7 @@ class ApplyOverrideAll extends DesugaringStep<LayoutConfigJson> {
}
}
class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
class AddDependencyLayersToTheme extends DesugaringStep<ThemeConfigJson> {
private readonly _state: DesugaringContext
constructor(state: DesugaringContext) {
@ -390,7 +390,7 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
return dependenciesToAdd
}
convert(theme: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(theme: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const state = this._state
const allKnownLayers: Map<string, LayerConfigJson> = state.sharedLayers
const knownTagRenderings: Map<string, TagRenderingConfigJson> = state.tagRenderings
@ -428,7 +428,7 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> {
}
}
class PreparePersonalTheme extends DesugaringStep<LayoutConfigJson> {
class PreparePersonalTheme extends DesugaringStep<ThemeConfigJson> {
private readonly _state: DesugaringContext
constructor(state: DesugaringContext) {
@ -436,7 +436,7 @@ class PreparePersonalTheme extends DesugaringStep<LayoutConfigJson> {
this._state = state
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
if (json.id !== "personal") {
return json
}
@ -452,7 +452,7 @@ class PreparePersonalTheme extends DesugaringStep<LayoutConfigJson> {
}
}
class WarnForUnsubstitutedLayersInTheme extends DesugaringStep<LayoutConfigJson> {
class WarnForUnsubstitutedLayersInTheme extends DesugaringStep<ThemeConfigJson> {
constructor() {
super(
"Generates a warning if a theme uses an unsubstituted layer",
@ -461,7 +461,7 @@ class WarnForUnsubstitutedLayersInTheme extends DesugaringStep<LayoutConfigJson>
)
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
if (json.hideFromOverview === true) {
return json
}
@ -503,7 +503,7 @@ class WarnForUnsubstitutedLayersInTheme extends DesugaringStep<LayoutConfigJson>
}
}
class PostvalidateTheme extends DesugaringStep<LayoutConfigJson> {
class PostvalidateTheme extends DesugaringStep<ThemeConfigJson> {
private readonly _state: DesugaringContext
constructor(state: DesugaringContext) {
@ -511,7 +511,7 @@ class PostvalidateTheme extends DesugaringStep<LayoutConfigJson> {
this._state = state
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
for (const l of json.layers) {
const layer = <LayerConfigJson>l
const basedOn = <string>layer["_basedOn"]
@ -582,7 +582,7 @@ class PostvalidateTheme extends DesugaringStep<LayoutConfigJson> {
}
}
export class PrepareTheme extends Fuse<LayoutConfigJson> {
export class PrepareTheme extends Fuse<ThemeConfigJson> {
private state: DesugaringContext
constructor(
@ -616,7 +616,7 @@ export class PrepareTheme extends Fuse<LayoutConfigJson> {
this.state = state
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const result = super.convert(json, context)
if ((this.state.publicLayers?.size ?? 0) === 0) {
// THis is a bootstrapping run, no need to already set this flag

View file

@ -1,13 +1,13 @@
import { DesugaringStep } from "./Conversion"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import { AvailableRasterLayers } from "../../RasterLayers"
import { ExtractImages } from "./FixImages"
import { ConversionContext } from "./ConversionContext"
import LayoutConfig from "../LayoutConfig"
import ThemeConfig from "../ThemeConfig"
import { Utils } from "../../../Utils"
import { DetectDuplicatePresets, DoesImageExist, ValidateLanguageCompleteness } from "./Validation"
export class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
export class ValidateTheme extends DesugaringStep<ThemeConfigJson> {
/**
* The paths where this layer is originally saved. Triggers some extra checks
* @private
@ -33,8 +33,8 @@ export class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
}
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
const theme = new LayoutConfig(json, this._isBuiltin)
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const theme = new ThemeConfig(json, this._isBuiltin)
{
// Legacy format checks
if (this._isBuiltin) {

View file

@ -1,10 +1,10 @@
import { Bypass, Each, Fuse, On } from "./Conversion"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import Constants from "../../Constants"
import { DoesImageExist, ValidateLayerConfig } from "./Validation"
import { ValidateTheme } from "./ValidateTheme"
export class ValidateThemeAndLayers extends Fuse<LayoutConfigJson> {
export class ValidateThemeAndLayers extends Fuse<ThemeConfigJson> {
constructor(
doesImageExist: DoesImageExist,
path: string,

View file

@ -4,8 +4,8 @@ import LayerConfig from "../LayerConfig"
import { Utils } from "../../../Utils"
import Constants from "../../Constants"
import { Translation } from "../../../UI/i18n/Translation"
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import LayoutConfig from "../LayoutConfig"
import { ThemeConfigJson } from "../Json/ThemeConfigJson"
import ThemeConfig from "../ThemeConfig"
import { TagRenderingConfigJson } from "../Json/TagRenderingConfigJson"
import { TagUtils } from "../../../Logic/Tags/TagUtils"
import { And } from "../../../Logic/Tags/And"
@ -23,7 +23,7 @@ import { PrevalidateLayer } from "./PrevalidateLayer"
import { AvailableRasterLayers } from "../../RasterLayers"
import { eliCategory } from "../../RasterLayerProperties"
export class ValidateLanguageCompleteness extends DesugaringStep<LayoutConfig> {
export class ValidateLanguageCompleteness extends DesugaringStep<ThemeConfig> {
private readonly _languages: string[]
constructor(...languages: string[]) {
@ -35,7 +35,7 @@ export class ValidateLanguageCompleteness extends DesugaringStep<LayoutConfig> {
this._languages = languages ?? ["en"]
}
convert(obj: LayoutConfig, context: ConversionContext): LayoutConfig {
convert(obj: ThemeConfig, context: ConversionContext): ThemeConfig {
const origLayers = obj.layers
obj.layers = [...obj.layers].filter((l) => l["id"] !== "favourite")
const translations = Translation.ExtractAllTranslationsFrom(obj)
@ -128,7 +128,7 @@ export class DoesImageExist extends DesugaringStep<string> {
}
}
class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson> {
class OverrideShadowingCheck extends DesugaringStep<ThemeConfigJson> {
constructor() {
super(
"Checks that an 'overrideAll' does not override a single override",
@ -137,7 +137,7 @@ class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson> {
)
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
const overrideAll = json.overrideAll
if (overrideAll === undefined) {
return json
@ -170,12 +170,12 @@ class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson> {
}
}
class MiscThemeChecks extends DesugaringStep<LayoutConfigJson> {
class MiscThemeChecks extends DesugaringStep<ThemeConfigJson> {
constructor() {
super("Miscelleanous checks on the theme", [], "MiscThemesChecks")
}
convert(json: LayoutConfigJson, context: ConversionContext): LayoutConfigJson {
convert(json: ThemeConfigJson, context: ConversionContext): ThemeConfigJson {
if (json.id !== "personal" && (json.layers === undefined || json.layers.length === 0)) {
context.err("The theme " + json.id + " has no 'layers' defined")
}
@ -240,7 +240,7 @@ class MiscThemeChecks extends DesugaringStep<LayoutConfigJson> {
}
}
export class PrevalidateTheme extends Fuse<LayoutConfigJson> {
export class PrevalidateTheme extends Fuse<ThemeConfigJson> {
constructor() {
super(
"Various consistency checks on the raw JSON",
@ -905,7 +905,7 @@ export class ValidateFilter extends DesugaringStep<FilterConfigJson> {
export class DetectDuplicateFilters extends DesugaringStep<{
layers: LayerConfigJson[]
themes: LayoutConfigJson[]
themes: ThemeConfigJson[]
}> {
constructor() {
super(
@ -916,15 +916,15 @@ export class DetectDuplicateFilters extends DesugaringStep<{
}
convert(
json: { layers: LayerConfigJson[]; themes: LayoutConfigJson[] },
json: { layers: LayerConfigJson[]; themes: ThemeConfigJson[] },
context: ConversionContext,
): { layers: LayerConfigJson[]; themes: LayoutConfigJson[] } {
): { layers: LayerConfigJson[]; themes: ThemeConfigJson[] } {
const { layers, themes } = json
const perOsmTag = new Map<
string,
{
layer: LayerConfigJson
layout: LayoutConfigJson | undefined
theme: ThemeConfigJson | undefined
filter: FilterConfigJson
}[]
>()
@ -955,10 +955,10 @@ export class DetectDuplicateFilters extends DesugaringStep<{
return
}
let msg = "Possible duplicate filter: " + key
for (const { filter, layer, layout } of value) {
for (const { filter, layer, theme } of value) {
let id = ""
if (layout !== undefined) {
id = layout.id + ":"
if (theme !== undefined) {
id = theme.id + ":"
}
msg += `\n - ${id}${layer.id}.${filter.id}`
}
@ -977,11 +977,11 @@ export class DetectDuplicateFilters extends DesugaringStep<{
string,
{
layer: LayerConfigJson
layout: LayoutConfigJson | undefined
theme: ThemeConfigJson | undefined
filter: FilterConfigJson
}[]
>,
layout?: LayoutConfigJson | undefined,
theme?: ThemeConfigJson | undefined,
): void {
if (layer.filter === undefined || layer.filter === null) {
return
@ -1009,14 +1009,14 @@ export class DetectDuplicateFilters extends DesugaringStep<{
perOsmTag.get(key).push({
layer,
filter,
layout,
theme,
})
}
}
}
}
export class DetectDuplicatePresets extends DesugaringStep<LayoutConfig> {
export class DetectDuplicatePresets extends DesugaringStep<ThemeConfig> {
constructor() {
super(
"Detects mappings which have identical (english) names or identical mappings.",
@ -1025,7 +1025,7 @@ export class DetectDuplicatePresets extends DesugaringStep<LayoutConfig> {
)
}
convert(json: LayoutConfig, context: ConversionContext): LayoutConfig {
convert(json: ThemeConfig, context: ConversionContext): ThemeConfig {
const presets: PresetConfig[] = [].concat(...json.layers.map((l) => l.presets))
const enNames = presets.map((p) => p.title.textFor("en"))
@ -1074,7 +1074,7 @@ export class DetectDuplicatePresets extends DesugaringStep<LayoutConfig> {
}
export class ValidateThemeEnsemble extends Conversion<
LayoutConfig[],
ThemeConfig[],
Map<
string,
{
@ -1093,7 +1093,7 @@ export class ValidateThemeEnsemble extends Conversion<
}
convert(
json: LayoutConfig[],
json: ThemeConfig[],
context: ConversionContext,
): Map<
string,