mirror of
https://github.com/maputnik/editor.git
synced 2026-11-27 04:23:45 +00:00
## Launch Checklist <!-- Thanks for the PR! Feel free to add or remove items from the checklist. --> When a fixed paint value is converted to a zoom function, Maputnik creates exactly two stops. <img width="362" height="226" alt="image" src="https://github.com/user-attachments/assets/209aebbf-7bb3-4422-b0c7-1019e20a9ddb" /> Deleting either one of those two stops leaves a single remaining value. During the transition back to the ordinary field editor, `ZoomProperty` can render with `stops` undefined. It previously called `.map` unconditionally, causing the editor to crash. <img width="2551" height="1248" alt="image" src="https://github.com/user-attachments/assets/f5c2af59-dcae-4ffe-8245-f175eea9c843" /> This PR guards the stop list with optional chaining. The change only protects the render path and does not alter zoom-function conversion or value persistence. Now, the remaining stop's value will be restored after deleting the other stop: <img width="367" height="168" alt="image" src="https://github.com/user-attachments/assets/8d13236b-6140-451b-8613-0bc5f11893de" /> The regression test covers `circle-opacity`: 1. Convert a fixed value to a zoom function. 2. Change the second stop's value. 3. Delete the first of the two stops. 4. Verify that the ordinary field editor reappears. 5. Verify that the remaining stop's value is restored. 6. Edit the restored field and verify that the style updates successfully. - [x] Briefly describe the changes in this PR. - [ ] Link to related issues. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality. - [x] Add an entry to `CHANGELOG.md` under the `## main` section.
399 lines
10 KiB
TypeScript
399 lines
10 KiB
TypeScript
import React from "react";
|
|
|
|
import { SpecProperty } from "./SpecProperty";
|
|
import { DataProperty, type Stop } from "./DataProperty";
|
|
import { ZoomProperty } from "./ZoomProperty";
|
|
import { ExpressionProperty } from "./ExpressionProperty";
|
|
import {function as styleFunction} from "@maplibre/maplibre-gl-style-spec";
|
|
import {findDefaultFromSpec} from "../libs/spec-helper";
|
|
import { type MappedLayerErrors } from "../libs/definitions";
|
|
|
|
|
|
function isLiteralExpression(value: any) {
|
|
return (Array.isArray(value) && value.length === 2 && value[0] === "literal");
|
|
}
|
|
|
|
function isGetExpression(value: any) {
|
|
return (
|
|
Array.isArray(value) &&
|
|
value.length === 2 &&
|
|
value[0] === "get"
|
|
);
|
|
}
|
|
|
|
function isZoomField(value: any) {
|
|
return (
|
|
typeof(value) === "object" &&
|
|
value.stops &&
|
|
typeof(value.property) === "undefined" &&
|
|
Array.isArray(value.stops) &&
|
|
value.stops.length > 1 &&
|
|
value.stops.every((stop: Stop) => {
|
|
return (
|
|
Array.isArray(stop) &&
|
|
stop.length === 2
|
|
);
|
|
})
|
|
);
|
|
}
|
|
|
|
function isIdentityProperty(value: any) {
|
|
return (
|
|
typeof(value) === "object" &&
|
|
value.type === "identity" &&
|
|
Object.prototype.hasOwnProperty.call(value, "property")
|
|
);
|
|
}
|
|
|
|
function isDataStopProperty(value: any) {
|
|
return (
|
|
typeof(value) === "object" &&
|
|
value.stops &&
|
|
typeof(value.property) !== "undefined" &&
|
|
value.stops.length > 1 &&
|
|
Array.isArray(value.stops) &&
|
|
value.stops.every((stop: Stop) => {
|
|
return (
|
|
Array.isArray(stop) &&
|
|
stop.length === 2 &&
|
|
typeof(stop[0]) === "object"
|
|
);
|
|
})
|
|
);
|
|
}
|
|
|
|
function isDataField(value: any) {
|
|
return (
|
|
isIdentityProperty(value) ||
|
|
isDataStopProperty(value)
|
|
);
|
|
}
|
|
|
|
function isPrimative(value: any): value is string | boolean | number {
|
|
const valid = ["string", "boolean", "number"];
|
|
return valid.includes(typeof(value));
|
|
}
|
|
|
|
function isArrayOfPrimatives(values: any): values is Array<string | boolean | number> {
|
|
if (Array.isArray(values)) {
|
|
return values.every(isPrimative);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function getDataType(value: any, fieldSpec={} as any) {
|
|
if (value === undefined) {
|
|
return "value";
|
|
}
|
|
else if (isPrimative(value)) {
|
|
return "value";
|
|
}
|
|
else if (fieldSpec.type === "array" && isArrayOfPrimatives(value)) {
|
|
return "value";
|
|
}
|
|
else if (fieldSpec.type === "numberArray" && isArrayOfPrimatives(value)) {
|
|
return "value";
|
|
}
|
|
else if (fieldSpec.type === "colorArray") {
|
|
return "value";
|
|
}
|
|
else if (fieldSpec.type === "padding") {
|
|
return "value";
|
|
}
|
|
else if (fieldSpec.type === "variableAnchorOffsetCollection") {
|
|
return "value";
|
|
}
|
|
else if (isZoomField(value)) {
|
|
return "zoom_function";
|
|
}
|
|
else if (isDataField(value)) {
|
|
return "data_function";
|
|
}
|
|
else {
|
|
return "expression";
|
|
}
|
|
}
|
|
|
|
|
|
type FieldFunctionProps = {
|
|
onChange(fieldName: string, value: any): unknown
|
|
fieldName: string
|
|
fieldType: string
|
|
fieldSpec: any
|
|
errors?: MappedLayerErrors
|
|
value?: any
|
|
};
|
|
|
|
|
|
/** Supports displaying spec field for zoom function objects
|
|
* https://www.mapbox.com/mapbox-gl-style-spec/#types-function-zoom-property
|
|
*/
|
|
export const FieldFunction: React.FC<FieldFunctionProps> = (props) => {
|
|
const [isEditing, setIsEditing] = React.useState(false);
|
|
// Keep the expression editor mounted while typing, but otherwise select the
|
|
// editor from the current value so a collapsed function never renders as stops.
|
|
const dataType = isEditing ? "expression" : getDataType(props.value, props.fieldSpec);
|
|
|
|
const getFieldFunctionType = (fieldSpec: any) => {
|
|
if (fieldSpec.expression.interpolated) {
|
|
return "exponential";
|
|
}
|
|
if (fieldSpec.type === "number") {
|
|
return "interval";
|
|
}
|
|
return "categorical";
|
|
};
|
|
|
|
const addStop = () => {
|
|
const stops = props.value.stops.slice(0);
|
|
const lastStop = stops[stops.length - 1];
|
|
if (typeof lastStop[0] === "object") {
|
|
stops.push([
|
|
{ zoom: lastStop[0].zoom + 1, value: lastStop[0].value },
|
|
lastStop[1],
|
|
]);
|
|
} else {
|
|
stops.push([lastStop[0] + 1, lastStop[1]]);
|
|
}
|
|
|
|
const changedValue = {
|
|
...props.value,
|
|
stops: stops,
|
|
};
|
|
|
|
props.onChange(props.fieldName, changedValue);
|
|
};
|
|
|
|
const deleteExpression = () => {
|
|
const { fieldSpec, fieldName } = props;
|
|
props.onChange(fieldName, fieldSpec.default);
|
|
setIsEditing(false);
|
|
};
|
|
|
|
const deleteStop = (stopIdx: number) => {
|
|
const stops = props.value.stops.slice(0);
|
|
stops.splice(stopIdx, 1);
|
|
|
|
let changedValue: any = {
|
|
...props.value,
|
|
stops: stops,
|
|
};
|
|
|
|
if (stops.length === 1) {
|
|
changedValue = stops[0][1];
|
|
}
|
|
|
|
props.onChange(props.fieldName, changedValue);
|
|
};
|
|
|
|
const makeZoomFunction = () => {
|
|
const { value } = props;
|
|
|
|
let zoomFunc: any;
|
|
if (typeof value === "object") {
|
|
if (value.stops) {
|
|
zoomFunc = {
|
|
base: value.base,
|
|
stops: value.stops.map((stop: Stop) => {
|
|
return [stop[0].zoom, stop[1] || findDefaultFromSpec(props.fieldSpec)];
|
|
}),
|
|
};
|
|
} else {
|
|
zoomFunc = {
|
|
base: value.base,
|
|
stops: [
|
|
[6, findDefaultFromSpec(props.fieldSpec)],
|
|
[10, findDefaultFromSpec(props.fieldSpec)],
|
|
],
|
|
};
|
|
}
|
|
} else {
|
|
zoomFunc = {
|
|
stops: [
|
|
[6, value || findDefaultFromSpec(props.fieldSpec)],
|
|
[10, value || findDefaultFromSpec(props.fieldSpec)],
|
|
],
|
|
};
|
|
}
|
|
|
|
props.onChange(props.fieldName, zoomFunc);
|
|
};
|
|
|
|
const undoExpression = () => {
|
|
const { value, fieldName } = props;
|
|
|
|
if (isGetExpression(value)) {
|
|
props.onChange(fieldName, {
|
|
type: "identity",
|
|
property: value[1],
|
|
});
|
|
setIsEditing(false);
|
|
} else if (isLiteralExpression(value)) {
|
|
props.onChange(fieldName, value[1]);
|
|
setIsEditing(false);
|
|
}
|
|
};
|
|
|
|
const canUndo = () => {
|
|
const { value, fieldSpec } = props;
|
|
return (
|
|
isGetExpression(value) ||
|
|
isLiteralExpression(value) ||
|
|
isPrimative(value) ||
|
|
(Array.isArray(value) && fieldSpec.type === "array")
|
|
);
|
|
};
|
|
|
|
const makeExpression = () => {
|
|
const { value, fieldSpec } = props;
|
|
let expression;
|
|
|
|
if (typeof value === "object" && "stops" in value) {
|
|
expression = styleFunction.convertFunction(value, fieldSpec);
|
|
} else if (isIdentityProperty(value)) {
|
|
expression = ["get", value.property];
|
|
} else {
|
|
expression = ["literal", value || props.fieldSpec.default];
|
|
}
|
|
props.onChange(props.fieldName, expression);
|
|
};
|
|
|
|
const makeDataFunction = () => {
|
|
const functionType = getFieldFunctionType(props.fieldSpec);
|
|
const stopValue = functionType === "categorical" ? "" : 0;
|
|
const { value } = props;
|
|
let dataFunc;
|
|
|
|
if (typeof value === "object") {
|
|
if (value.stops) {
|
|
dataFunc = {
|
|
property: "",
|
|
type: functionType,
|
|
base: value.base,
|
|
stops: value.stops.map((stop: Stop) => {
|
|
return [{ zoom: stop[0], value: stopValue }, stop[1] || findDefaultFromSpec(props.fieldSpec)];
|
|
}),
|
|
};
|
|
} else {
|
|
dataFunc = {
|
|
property: "",
|
|
type: functionType,
|
|
base: value.base,
|
|
stops: [
|
|
[{ zoom: 6, value: stopValue }, findDefaultFromSpec(props.fieldSpec)],
|
|
[{ zoom: 10, value: stopValue }, findDefaultFromSpec(props.fieldSpec)],
|
|
],
|
|
};
|
|
}
|
|
} else {
|
|
dataFunc = {
|
|
property: "",
|
|
type: functionType,
|
|
base: value.base,
|
|
stops: [
|
|
[{ zoom: 6, value: stopValue }, props.value || findDefaultFromSpec(props.fieldSpec)],
|
|
[{ zoom: 10, value: stopValue }, props.value || findDefaultFromSpec(props.fieldSpec)],
|
|
],
|
|
};
|
|
}
|
|
|
|
props.onChange(props.fieldName, dataFunc);
|
|
};
|
|
|
|
const makeElevationFunction = () => {
|
|
const expression = [
|
|
"interpolate",
|
|
["linear"],
|
|
["elevation"],
|
|
0,
|
|
"black",
|
|
2000,
|
|
"white"
|
|
];
|
|
|
|
props.onChange(props.fieldName, expression);
|
|
};
|
|
|
|
const onMarkEditing = () => {
|
|
setIsEditing(true);
|
|
};
|
|
|
|
const onUnmarkEditing = () => {
|
|
setIsEditing(false);
|
|
};
|
|
|
|
const propClass =
|
|
props.fieldSpec.default === props.value ? "maputnik-default-property" : "maputnik-modified-property";
|
|
|
|
let specField;
|
|
|
|
if (dataType === "expression") {
|
|
specField = (
|
|
<ExpressionProperty
|
|
errors={props.errors}
|
|
onChange={props.onChange.bind(null, props.fieldName)}
|
|
canUndo={canUndo}
|
|
onUndo={undoExpression}
|
|
onDelete={deleteExpression}
|
|
fieldType={props.fieldType}
|
|
fieldName={props.fieldName}
|
|
fieldSpec={props.fieldSpec}
|
|
value={props.value}
|
|
onFocus={onMarkEditing}
|
|
onBlur={onUnmarkEditing}
|
|
/>
|
|
);
|
|
} else if (dataType === "zoom_function") {
|
|
specField = (
|
|
<ZoomProperty
|
|
errors={props.errors}
|
|
onChange={props.onChange.bind(null)}
|
|
fieldType={props.fieldType}
|
|
fieldName={props.fieldName}
|
|
fieldSpec={props.fieldSpec}
|
|
value={props.value}
|
|
onDeleteStop={deleteStop}
|
|
onAddStop={addStop}
|
|
onChangeToDataFunction={makeDataFunction}
|
|
onExpressionClick={makeExpression}
|
|
/>
|
|
);
|
|
} else if (dataType === "data_function") {
|
|
specField = (
|
|
<DataProperty
|
|
errors={props.errors}
|
|
onChange={props.onChange.bind(null)}
|
|
fieldType={props.fieldType}
|
|
fieldName={props.fieldName}
|
|
fieldSpec={props.fieldSpec}
|
|
value={props.value}
|
|
onDeleteStop={deleteStop}
|
|
onAddStop={addStop}
|
|
onChangeToZoomFunction={makeZoomFunction}
|
|
onExpressionClick={makeExpression}
|
|
/>
|
|
);
|
|
} else {
|
|
specField = (
|
|
<SpecProperty
|
|
errors={props.errors}
|
|
onChange={props.onChange.bind(null)}
|
|
fieldType={props.fieldType}
|
|
fieldName={props.fieldName}
|
|
fieldSpec={props.fieldSpec}
|
|
value={props.value}
|
|
onZoomClick={makeZoomFunction}
|
|
onDataClick={makeDataFunction}
|
|
onExpressionClick={makeExpression}
|
|
onElevationClick={makeElevationFunction}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className={propClass} data-wd-key={"spec-field-container:" + props.fieldName}>
|
|
{specField}
|
|
</div>
|
|
);
|
|
};
|