From 7db7446647ea89dc042d42d7320163805b337a1e Mon Sep 17 00:00:00 2001 From: Sophia Mersmann Date: Thu, 12 Dec 2024 17:58:31 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20add=20empty=20arrays=20to=20the?= =?UTF-8?q?=20default=20grapher=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteClient/AbstractChartEditor.ts | 2 +- .../generate-default-object-from-schema.ts | 20 ++++++++++--------- devTools/schema/template | 10 ++++++++++ .../grapher/src/core/Grapher.jsdom.test.ts | 12 ----------- .../grapher/src/core/Grapher.tsx | 5 ++--- .../src/schema/defaultGrapherConfig.ts | 6 ++++++ .../src/schema/grapher-schema.006.yaml | 6 ++++++ 7 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 devTools/schema/template diff --git a/adminSiteClient/AbstractChartEditor.ts b/adminSiteClient/AbstractChartEditor.ts index c22baa71c8a..7c2bfc676ea 100644 --- a/adminSiteClient/AbstractChartEditor.ts +++ b/adminSiteClient/AbstractChartEditor.ts @@ -106,7 +106,7 @@ export abstract class AbstractChartEditor< /** patch config merged with parent config */ @computed get fullConfig(): GrapherInterface { if (!this.activeParentConfig) return this.liveConfig - return mergeGrapherConfigs(this.activeParentConfig, this.liveConfig) + return mergeGrapherConfigs(this.activeParentConfig, this.patchConfig) } /** parent config currently applied to grapher */ diff --git a/devTools/schema/generate-default-object-from-schema.ts b/devTools/schema/generate-default-object-from-schema.ts index b4f553a5744..9ef00ad59dd 100644 --- a/devTools/schema/generate-default-object-from-schema.ts +++ b/devTools/schema/generate-default-object-from-schema.ts @@ -4,6 +4,8 @@ import parseArgs from "minimist" import fs from "fs-extra" import { range } from "lodash" +const TEMPLATE_FILENAME = "./devTools/schema/template" + const schemaVersionRegex = /https:\/\/files\.ourworldindata\.org\/schemas\/grapher-schema\.(?\d{3})\.json/m const getSchemaVersion = (config: Record): string => @@ -58,6 +60,8 @@ async function main(parsedArgs: parseArgs.ParsedArgs) { // save as ts file if requested if (parsedArgs["save-ts"]) { + const template = fs.readFileSync(TEMPLATE_FILENAME, "utf8") + const latestVersion = getSchemaVersion(defaultConfig) const outdatedVersionsAsInts = range(1, parseInt(latestVersion)) const outdatedVersions = outdatedVersionsAsInts.map((versionNumber) => @@ -65,16 +69,14 @@ async function main(parsedArgs: parseArgs.ParsedArgs) { ) const out = parsedArgs["save-ts"] - const content = `// THIS IS A GENERATED FILE, DO NOT EDIT DIRECTLY - -// GENERATED BY devTools/schema/generate-default-object-from-schema.ts - -import { GrapherInterface } from "@ourworldindata/types" - -export const latestSchemaVersion = "${latestVersion}" as const -export const outdatedSchemaVersions = ${toArrayString(outdatedVersions)} as const + const content = template + .replace("{{LATEST_SCHEMA_VERSION}}", latestVersion.toString()) + .replace( + "{{OUTDATED_SCHEMA_VERSIONS}}", + toArrayString(outdatedVersions) + ) + .replace("{{DEFAULT_GRAPHER_CONFIG}}", defaultConfigJSON) -export const defaultGrapherConfig = ${defaultConfigJSON} as GrapherInterface` fs.outputFileSync(out, content) } diff --git a/devTools/schema/template b/devTools/schema/template new file mode 100644 index 00000000000..a1ba3e0eb1c --- /dev/null +++ b/devTools/schema/template @@ -0,0 +1,10 @@ +// THIS IS A GENERATED FILE, DO NOT EDIT DIRECTLY + +// GENERATED BY devTools/schema/generate-default-object-from-schema.ts + +import { GrapherInterface } from "@ourworldindata/types" + +export const latestSchemaVersion = "{{LATEST_SCHEMA_VERSION}}" as const +export const outdatedSchemaVersions = {{OUTDATED_SCHEMA_VERSIONS}} as const + +export const defaultGrapherConfig = {{DEFAULT_GRAPHER_CONFIG}} as GrapherInterface diff --git a/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts b/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts index 247950c185b..dba594860e5 100755 --- a/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts +++ b/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts @@ -79,10 +79,6 @@ it("can get dimension slots", () => { it("an empty Grapher serializes to an object that includes only the schema", () => { expect(new Grapher().toObject()).toEqual({ $schema: latestGrapherConfigSchema, - - // TODO: ideally, these are not serialised for an empty object - selectedEntityNames: [], - focusedSeriesNames: [], }) }) @@ -93,20 +89,12 @@ it("a bad chart type does not crash grapher", () => { expect(new Grapher(input).toObject()).toEqual({ ...input, $schema: latestGrapherConfigSchema, - - // TODO: ideally, these are not serialised for an empty object - selectedEntityNames: [], - focusedSeriesNames: [], }) }) it("does not preserve defaults in the object (except for the schema)", () => { expect(new Grapher({ tab: GRAPHER_TAB_OPTIONS.chart }).toObject()).toEqual({ $schema: latestGrapherConfigSchema, - - // TODO: ideally, these are not serialised for an empty object - selectedEntityNames: [], - focusedSeriesNames: [], }) }) diff --git a/packages/@ourworldindata/grapher/src/core/Grapher.tsx b/packages/@ourworldindata/grapher/src/core/Grapher.tsx index 726fd3ed455..ccb3353b48b 100644 --- a/packages/@ourworldindata/grapher/src/core/Grapher.tsx +++ b/packages/@ourworldindata/grapher/src/core/Grapher.tsx @@ -437,15 +437,14 @@ export class Grapher [entityName: string]: string | undefined } = {} - // Initializing arrays with `undefined` ensures that empty arrays get serialised - @observable selectedEntityNames?: EntityName[] = undefined + @observable selectedEntityNames: EntityName[] = [] + @observable focusedSeriesNames: SeriesName[] = [] @observable excludedEntities?: number[] = undefined /** IncludedEntities are usually empty which means use all available entities. When includedEntities is set it means "only use these entities". excludedEntities are evaluated afterwards and can still remove entities even if they were included before. */ @observable includedEntities?: number[] = undefined - @observable focusedSeriesNames?: SeriesName[] = undefined @observable comparisonLines?: ComparisonLineConfig[] = undefined // todo: Persistables? @observable relatedQuestions?: RelatedQuestionsConfig[] = undefined // todo: Persistables? diff --git a/packages/@ourworldindata/grapher/src/schema/defaultGrapherConfig.ts b/packages/@ourworldindata/grapher/src/schema/defaultGrapherConfig.ts index 79da7c8c82a..d9af04c43af 100644 --- a/packages/@ourworldindata/grapher/src/schema/defaultGrapherConfig.ts +++ b/packages/@ourworldindata/grapher/src/schema/defaultGrapherConfig.ts @@ -30,6 +30,8 @@ export const defaultGrapherConfig = { time: "latest", }, maxTime: "latest", + selectedEntityNames: [], + focusedSeriesNames: [], yAxis: { removePointsOutsideDomain: false, scaleType: "linear", @@ -54,11 +56,13 @@ export const defaultGrapherConfig = { selectedFacetStrategy: "none", invertColorScheme: false, hideRelativeToggle: true, + comparisonLines: [], logo: "owid", entityType: "country or region", facettingLabelByYVariables: "metric", addCountryMode: "add-country", compareEndPointsOnly: false, + relatedQuestions: [], chartTypes: ["LineChart"], hasMapTab: false, stackMode: "absolute", @@ -68,6 +72,7 @@ export const defaultGrapherConfig = { time: false, changeInPrefix: false, }, + excludedEntities: [], xAxis: { removePointsOutsideDomain: false, scaleType: "linear", @@ -85,6 +90,7 @@ export const defaultGrapherConfig = { sortBy: "total", sortOrder: "desc", hideFacetControl: true, + includedEntities: [], entityTypePlural: "countries and regions", missingDataStrategy: "auto", } as GrapherInterface diff --git a/packages/@ourworldindata/grapher/src/schema/grapher-schema.006.yaml b/packages/@ourworldindata/grapher/src/schema/grapher-schema.006.yaml index 14a7df9ded1..096173ab3df 100644 --- a/packages/@ourworldindata/grapher/src/schema/grapher-schema.006.yaml +++ b/packages/@ourworldindata/grapher/src/schema/grapher-schema.006.yaml @@ -101,6 +101,7 @@ properties: selectedEntityNames: type: array description: The initial selection of entities + default: [] items: type: - string @@ -109,6 +110,7 @@ properties: description: | The initially focused chart elements. Is either a list of entity or variable names. Only works for line and slope charts for now. + default: [] items: type: - string @@ -195,6 +197,7 @@ properties: comparisonLines: description: List of vertical comparison lines to draw type: array + default: [] items: type: object properties: @@ -361,6 +364,7 @@ properties: relatedQuestions: type: array description: Links to related questions + default: [] items: type: object properties: @@ -428,6 +432,7 @@ properties: excludedEntities: type: array description: Entities that should be excluded from the chart + default: [] items: type: integer minimum: 0 @@ -493,6 +498,7 @@ properties: includedEntities: type: array description: Entities to include. Opposite of excludedEntities. If this is set then all entities not specified here are excluded. + default: [] items: type: number entityTypePlural: