diff --git a/CHANGELOG.md b/CHANGELOG.md index a77e7f1c..97589517 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Add `font-faces`, `sky`, `roll` and `centerAltitude` to the style settings modal - The sidebar can now be resized, both as a whole and in the split between the layer list and the layer editor - Allow viewing and changing the map's max zoom in the debug modal +- Edit `text-variable-anchor-offset` as a list of anchors, each with an x and y offset; it previously showed no input - _...Add new stuff here..._ ### 🐞 Bug fixes diff --git a/e2e/layers-list.spec.ts b/e2e/layers-list.spec.ts index 1e3415f2..0455dd8a 100644 --- a/e2e/layers-list.spec.ts +++ b/e2e/layers-list.spec.ts @@ -228,6 +228,37 @@ describe("layers list", () => { await then(get.propertyArrayAddButton("spec-field:icon-padding")).shouldNotExist(); }); + + describe("text-variable-anchor-offset", () => { + let id: string; + + beforeEach(async () => { + id = await when.modal.fillLayers({ type: "symbol", layer: "example" }); + // the new pair's last input is its y offset + await when.addValueToPropertyArray("spec-field:text-variable-anchor-offset", "4"); + }); + + test("should save an anchor with its offset", async () => { + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + layers: [{ id, layout: { "text-variable-anchor-offset": ["center", [0, 4]] } }], + }); + }); + + test("should save a changed anchor", async () => { + await when.selectWithin("spec-field:text-variable-anchor-offset", "top"); + + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + layers: [{ id, layout: { "text-variable-anchor-offset": ["top", [0, 4]] } }], + }); + }); + + test("should remove an anchor row", async () => { + // the row's delete button shares the function stop's class + await when.deleteFunctionStop("text-variable-anchor-offset"); + + await then(get.element("[data-wd-key='spec-field:text-variable-anchor-offset'] select")).shouldNotExist(); + }); + }); }); describe("raster", () => { diff --git a/src/components/FieldFunction.tsx b/src/components/FieldFunction.tsx index 3a761520..55c0bcc5 100644 --- a/src/components/FieldFunction.tsx +++ b/src/components/FieldFunction.tsx @@ -5,7 +5,7 @@ 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 {findDefaultFromSpec, isVariableAnchorOffsetCollection} from "../libs/spec-helper"; import { type MappedLayerErrors } from "../libs/definitions"; @@ -100,7 +100,7 @@ function getDataType(value: any, fieldSpec={} as any) { else if (fieldSpec.type === "padding") { return "value"; } - else if (fieldSpec.type === "variableAnchorOffsetCollection") { + else if (fieldSpec.type === "variableAnchorOffsetCollection" && isVariableAnchorOffsetCollection(value)) { return "value"; } else if (isZoomField(value)) { diff --git a/src/components/InputDynamicArray.tsx b/src/components/InputDynamicArray.tsx index 1dc88960..2f457c13 100644 --- a/src/components/InputDynamicArray.tsx +++ b/src/components/InputDynamicArray.tsx @@ -150,7 +150,7 @@ type DeleteValueInputButtonProps = { onClick?(...args: unknown[]): unknown } & WithTranslation; -class DeleteValueInputButton extends React.Component { +export class DeleteValueInputButton extends React.Component { render() { const t = this.props.t; return { value={(Array.isArray(this.props.value) ? this.props.value : [this.props.value]) as (string | number | undefined)[]} /> ); + case "variableAnchorOffsetCollection": return ( + + ); default: console.warn(`No proper field input for ${this.props.fieldName} type: ${this.props.fieldSpec?.type}`); return null; diff --git a/src/components/InputVariableAnchorOffsetCollection.tsx b/src/components/InputVariableAnchorOffsetCollection.tsx new file mode 100644 index 00000000..c79068bf --- /dev/null +++ b/src/components/InputVariableAnchorOffsetCollection.tsx @@ -0,0 +1,102 @@ +import React from "react"; +import capitalize from "lodash.capitalize"; +import { type WithTranslation, withTranslation } from "react-i18next"; +import latest from "@maplibre/maplibre-gl-style-spec/dist/latest.json"; + +import { InputNumber } from "./InputNumber"; +import { InputEnum } from "./InputEnum"; +import { InputButton } from "./InputButton"; +import { DeleteValueInputButton } from "./InputDynamicArray"; +import { type VariableAnchorOffsetCollection } from "../libs/spec-helper"; + +const anchorOptions = Object.keys(latest.layout_symbol["text-variable-anchor"].values).map(v => [v, capitalize(v)]); + +export type InputVariableAnchorOffsetCollectionProps = { + value?: VariableAnchorOffsetCollection + onChange?(value: VariableAnchorOffsetCollection | undefined): unknown + "aria-label"?: string + label?: string +}; + +type InputVariableAnchorOffsetCollectionInternalProps = InputVariableAnchorOffsetCollectionProps & WithTranslation; + +/** Edits the anchor and offset pairs of a `text-variable-anchor-offset` value, one row per anchor */ +class InputVariableAnchorOffsetCollectionInternal extends React.Component { + get values(): VariableAnchorOffsetCollection { + return this.props.value || []; + } + + changeAnchor(pairIdx: number, anchor: string) { + const values = this.values.slice(0); + values[pairIdx * 2] = anchor; + this.props.onChange?.(values); + } + + changeOffset(pairIdx: number, axis: 0 | 1, newValue: number | undefined) { + const values = this.values.slice(0); + const offset = (values[pairIdx * 2 + 1] as [number, number]).slice(0) as [number, number]; + offset[axis] = newValue ?? 0; + values[pairIdx * 2 + 1] = offset; + this.props.onChange?.(values); + } + + addValue = () => { + this.props.onChange?.([...this.values, anchorOptions[0][0], [0, 0]]); + }; + + deleteValue(pairIdx: number) { + const values = this.values.slice(0); + values.splice(pairIdx * 2, 2); + this.props.onChange?.(values.length > 0 ? values : undefined); + } + + render() { + const t = this.props.t; + const i18nProps = { t, i18n: this.props.i18n, tReady: this.props.tReady }; + const label = this.props["aria-label"] || this.props.label; + const rows = []; + for (let i = 0; i < this.values.length / 2; i++) { + const anchor = this.values[i * 2] as string; + const [x, y] = this.values[i * 2 + 1] as [number, number]; + rows.push(
+
+ this.deleteValue(i)} {...i18nProps} /> +
+
+
+ this.changeAnchor(i, v as string)} + aria-label={label} + /> + this.changeOffset(i, 0, v)} + aria-label={label} + /> + this.changeOffset(i, 1, v)} + aria-label={label} + /> +
+
+
); + } + + return ( +
+ {rows} + + {t("Add value")} + +
+ ); + } +} + +export const InputVariableAnchorOffsetCollection = withTranslation()(InputVariableAnchorOffsetCollectionInternal); diff --git a/src/libs/spec-helper.test.ts b/src/libs/spec-helper.test.ts index 64bf3be2..a5ba9232 100644 --- a/src/libs/spec-helper.test.ts +++ b/src/libs/spec-helper.test.ts @@ -1,5 +1,18 @@ import { describe, it, expect } from "vitest"; -import { findDefaultFromSpec } from "./spec-helper"; +import { findDefaultFromSpec, isVariableAnchorOffsetCollection } from "./spec-helper"; + +describe("isVariableAnchorOffsetCollection", () => { + it("accepts anchors alternating with [x, y] offsets", () => { + expect(isVariableAnchorOffsetCollection(["top", [0, 4], "left", [3, 0]])).toBe(true); + }); + + it("rejects expressions and malformed lists", () => { + expect(isVariableAnchorOffsetCollection(["literal", ["top", [0, 4]]])).toBe(false); + expect(isVariableAnchorOffsetCollection(["case", ["has", "name"], ["literal", ["top", [0, 4]]], ["literal", ["left", [3, 0]]]])).toBe(false); + expect(isVariableAnchorOffsetCollection(["top", [0, 4], "left"])).toBe(false); + expect(isVariableAnchorOffsetCollection(["top", [0]])).toBe(false); + }); +}); describe("findDefaultFromSpec", () => { it("returns the spec default when present (even if falsy)", () => { diff --git a/src/libs/spec-helper.ts b/src/libs/spec-helper.ts index 304372cc..ecc85f58 100644 --- a/src/libs/spec-helper.ts +++ b/src/libs/spec-helper.ts @@ -1,3 +1,14 @@ +/** A `text-variable-anchor-offset` value: anchors alternating with their `[x, y]` offsets */ +export type VariableAnchorOffsetCollection = (string | [number, number])[]; + +/** Whether `value` is a literal anchor and offset list rather than an expression */ +export function isVariableAnchorOffsetCollection(value: unknown): value is VariableAnchorOffsetCollection { + return Array.isArray(value) && value.length % 2 === 0 && value.every((item, i) => i % 2 === 0 + ? typeof item === "string" + : Array.isArray(item) && item.length === 2 && item.every(n => typeof n === "number") + ); +} + /** * If we don't have a default value just make one up */ diff --git a/src/styles/_input.scss b/src/styles/_input.scss index df644a34..aae91f1c 100644 --- a/src/styles/_input.scss +++ b/src/styles/_input.scss @@ -95,6 +95,21 @@ display: inline-block; float: right; } + + // an anchor dropdown followed by its x and y offsets + .maputnik-variable-anchor-offset { + display: flex; + gap: vars.$margin-2; + + > * { + flex: 1; + min-width: 0; + } + + > :first-child { + flex: 2; + } + } } // SELECT