From 129111357c5a504f52c758b2cd90df9bbd85b8b5 Mon Sep 17 00:00:00 2001 From: zstadler Date: Tue, 24 Nov 2026 23:25:26 +0200 Subject: [PATCH] Save global state values as JSON instead of strings, if possible (#2363) To keep numbers, booleans, arrays and objects working in styles, the Global State dialog now shows each non-string value as JSON. It also saves entered values as JSON instead of a string, if possible. Text that isn't valid JSON (like "value" or "#ff0000" without the quotes) is still saved as a string. String values that look like JSON will be shown and can be entered with quotes, e.g. `"9"`. Resolves #2362 Created with [Claude AI](https://claude.ai) --- CHANGELOG.md | 1 + e2e/modals.spec.ts | 33 ++++++++++++++++++++++ src/components/modals/ModalGlobalState.tsx | 23 +++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cce86011..dd87111f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - The map no longer stays blank on load until it is moved, when the style loads before the map is ready - The debug modal now shows the MapLibre options for any renderer other than OpenLayers (e.g. `mbgljs`) - Checkboxes for properties that default to `true` (e.g. `fill-antialias`, `text-keep-upright`) are now shown checked when the property is unset +- Global state values are now saved as JSON (numbers, booleans, arrays, objects) instead of always as strings - _...Add new stuff here..._ ## 3.1.0 diff --git a/e2e/modals.spec.ts b/e2e/modals.spec.ts index a3b3c6d5..36a84889 100644 --- a/e2e/modals.spec.ts +++ b/e2e/modals.spec.ts @@ -610,6 +610,39 @@ describe("modals", () => { state: { key1: { default: "myvalue" } }, }); }); + + test("edit variable value as number", async () => { + await when.click("global-state-add-variable"); + await when.wait(100); + await when.setValue("global-state-variable-value:0", "9"); + await when.typeKeys("{enter}"); + await when.wait(100); + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + state: { key1: { default: 9 } }, + }); + }); + + test("edit variable value as array", async () => { + await when.click("global-state-add-variable"); + await when.wait(100); + await when.setValue("global-state-variable-value:0", "[\"CCS\",\"Type2\"]"); + await when.typeKeys("{enter}"); + await when.wait(100); + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + state: { key1: { default: ["CCS", "Type2"] } }, + }); + }); + + test("edit variable value as quoted string", async () => { + await when.click("global-state-add-variable"); + await when.wait(100); + await when.setValue("global-state-variable-value:0", "\"9\""); + await when.typeKeys("{enter}"); + await when.wait(100); + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + state: { key1: { default: "9" } }, + }); + }); }); describe("error panel", () => { diff --git a/src/components/modals/ModalGlobalState.tsx b/src/components/modals/ModalGlobalState.tsx index 17a77ea0..67954a27 100644 --- a/src/components/modals/ModalGlobalState.tsx +++ b/src/components/modals/ModalGlobalState.tsx @@ -23,6 +23,25 @@ type GlobalStateVariable = { value: any; }; +const formatValue = (value: any): string => { + if (typeof value === "string") { + try { + JSON.parse(value); + } catch { + return value; + } + } + return JSON.stringify(value); +}; + +const parseValue = (text: string): any => { + try { + return JSON.parse(text); + } catch { + return text; + } +}; + const ModalGlobalStateInternal: React.FC = (props) => { const getGlobalStateVariables = (): GlobalStateVariable[] => { const style = props.mapStyle; @@ -77,7 +96,7 @@ const ModalGlobalStateInternal: React.FC = (props const onChangeVariableValue = (index: number, newValue: string) => { const variables = getGlobalStateVariables(); - variables[index].value = newValue || ""; + variables[index].value = parseValue(newValue || ""); setGlobalStateVariables(variables); }; @@ -96,7 +115,7 @@ const ModalGlobalStateInternal: React.FC = (props onChangeVariableValue(index, value || "")} data-wd-key={"global-state-variable-value:" + index} />