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)
This commit is contained in:
zstadler authored and GitHub committed 2026-11-24 23:25:26 +02:00
1 parent 8e73bb64c9
commit 129111357c
3 files changed
+55 -2

No files matched your search

+21 -2
View File
@@ -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<ModalGlobalStateInternalProps> = (props) => {
const getGlobalStateVariables = (): GlobalStateVariable[] => {
const style = props.mapStyle;
@@ -77,7 +96,7 @@ const ModalGlobalStateInternal: React.FC<ModalGlobalStateInternalProps> = (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<ModalGlobalStateInternalProps> = (props
<td>
<FieldString
label={props.t("Value")}
value={variable.value}
value={formatValue(variable.value)}
onChange={(value) => onChangeVariableValue(index, value || "")}
data-wd-key={"global-state-variable-value:" + index}
/>