mirror of
https://github.com/maputnik/editor.git
synced 2026-11-27 04:23:45 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user