diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df361ed..9c2e07b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## main ### ✨ Features and improvements +- Edit generated or promoted feature IDs for GeoJSON sources and promoted feature IDs for vector sources. - Add Polish to the language selector - Preview sprites in all sprite-backed dropdowns, including icon-image and pattern properties, with searchable names and keyboard selection - Replace Cypress with Playwright for end-to-end tests and Vitest browser mode (Playwright provider) for component tests; drop the `@shellygo/cypress-test-utils` helper in favour of a `MaputnikDriver` page object diff --git a/e2e/modal-driver.ts b/e2e/modal-driver.ts index 015ce9e5..3c0fce39 100644 --- a/e2e/modal-driver.ts +++ b/e2e/modal-driver.ts @@ -56,6 +56,25 @@ export class ModalDriver { await this.helper.get.element(".maputnik-active-source-type-editor-header-delete").first().click(); }, + enableGeneratedFeatureIds: async () => { + await this.helper.get.element(".maputnik-add-source") + .getByRole("checkbox", { name: "Generate feature IDs" }).click(); + }, + + setFeatureIdProperty: async (sourceId: string, value: string) => { + const source = this.helper.get.element(".maputnik-active-source-type-editor") + .filter({ has: this.helper.get.element(".maputnik-active-source-type-editor-header").getByText("#" + sourceId, { exact: true }) }); + const input = source.getByRole("textbox", { name: "Feature ID property" }); + await input.fill(value); + await input.blur(); + }, + + setNewSourceFeatureIdMapping: async (mapping: Record) => { + await this.helper.get.element(".maputnik-add-source [data-wd-key='source-promote-id-mapping'] .cm-content").click(); + await this.helper.when.typeKeys("{selectall}"); + await this.helper.when.typeText(JSON.stringify(mapping)); + }, + /** Fills one number box of a coordinate pair in the image/video source editor. */ setCoordinateValue: async (index: number, value: string) => { const input = this.helper.get diff --git a/e2e/modals.spec.ts b/e2e/modals.spec.ts index 36a84889..6643923f 100644 --- a/e2e/modals.spec.ts +++ b/e2e/modals.spec.ts @@ -209,6 +209,40 @@ describe("modals", () => { }); }); + test("generate feature IDs for a GeoJSON source", async () => { + const sourceId = "generated-feature-ids"; + await when.setValue("modal:sources.add.source_id", sourceId); + await when.select("modal:sources.add.source_type", "geojson_url"); + await when.modal.enableGeneratedFeatureIds(); + await when.click("modal:sources.add.add_source"); + + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + sources: { [sourceId]: { type: "geojson", generateId: true } }, + }); + }); + + test("promote a vector source feature property to its ID", async () => { + const sourceId = "promoted-feature-ids"; + await when.modal.addSource(sourceId, "tilejson_vector"); + await when.modal.setFeatureIdProperty(sourceId, "feature_id"); + + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + sources: { [sourceId]: { type: "vector", promoteId: "feature_id" } }, + }); + }); + + test("promote different ID properties for different vector source layers", async () => { + const sourceId = "layer-feature-ids"; + await when.setValue("modal:sources.add.source_id", sourceId); + await when.select("modal:sources.add.source_type", "tilejson_vector"); + await when.modal.setNewSourceFeatureIdMapping({ roads: "road_id", places: "place_id" }); + await when.click("modal:sources.add.add_source"); + + await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ + sources: { [sourceId]: { type: "vector", promoteId: { roads: "road_id", places: "place_id" } } }, + }); + }); + test("add new tilejson vector source", async () => { await when.modal.addSource("tilejsonvector", "tilejson_vector"); await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ diff --git a/src/components/modals/ModalSourcesTypeEditor.tsx b/src/components/modals/ModalSourcesTypeEditor.tsx index 4b1bddd5..7a08b815 100644 --- a/src/components/modals/ModalSourcesTypeEditor.tsx +++ b/src/components/modals/ModalSourcesTypeEditor.tsx @@ -11,6 +11,7 @@ import { FieldDynamicArray } from "../FieldDynamicArray"; import { FieldArray } from "../FieldArray"; import { FieldJson } from "../FieldJson"; import { FieldCheckbox } from "../FieldCheckbox"; +import { SourceFeatureIdFields } from "./SourceFeatureIdFields"; export type EditorMode = "video" | "image" | "tilejson_vector" | "tile_raster" | "tilejson_raster" | "tilexyz_raster-dem" | "tilejson_raster-dem" | "pmtiles_vector" | "tile_vector" | "geojson_url" | "geojson_json" | null; @@ -317,7 +318,7 @@ type ModalSourcesTypeEditorInternalProps = { } & WithTranslation; class ModalSourcesTypeEditorInternal extends React.Component { - render() { + renderEditor() { const t = this.props.t; const commonProps = { source: this.props.source, @@ -373,6 +374,15 @@ class ModalSourcesTypeEditorInternal extends React.Component + {this.renderEditor()} + {(source.type === "geojson" || source.type === "vector") && + } + ; + } } export const ModalSourcesTypeEditor = withTranslation()(ModalSourcesTypeEditorInternal); diff --git a/src/components/modals/SourceFeatureIdFields.tsx b/src/components/modals/SourceFeatureIdFields.tsx new file mode 100644 index 00000000..e0553750 --- /dev/null +++ b/src/components/modals/SourceFeatureIdFields.tsx @@ -0,0 +1,84 @@ +import { useState } from "react"; +import { latest } from "@maplibre/maplibre-gl-style-spec"; +import type { GeoJSONSourceSpecification, VectorSourceSpecification } from "maplibre-gl"; +import { useTranslation } from "react-i18next"; + +import { Block } from "../Block"; +import { FieldJson } from "../FieldJson"; +import { InputCheckbox } from "../InputCheckbox"; +import { FieldString } from "../FieldString"; + +type IdSource = GeoJSONSourceSpecification | VectorSourceSpecification; + +type SourceFeatureIdFieldsProps = { + source: IdSource + onChange(source: IdSource): void +}; + +function isSourceLayerMapping(value: unknown): value is Record { + return value !== null && typeof value === "object" && !Array.isArray(value) && + Object.values(value).every(property => typeof property === "string"); +} + +export function SourceFeatureIdFields({ source, onChange }: SourceFeatureIdFieldsProps) { + const { t } = useTranslation(); + const [invalidMapping, setInvalidMapping] = useState(false); + const { promoteId } = source; + const fieldSpec = source.type === "vector" ? latest.source_vector.promoteId : latest.source_geojson.promoteId; + const property = typeof promoteId === "string" ? promoteId : undefined; + const mapping = isSourceLayerMapping(promoteId) ? promoteId : undefined; + const mappingError = invalidMapping ? t("Enter a JSON object mapping source layer names to property names.") : undefined; + + function updatePromoteId(value: IdSource["promoteId"]) { + const updated = { ...source, promoteId: value }; + if (updated.type === "geojson" && value !== undefined) delete updated.generateId; + onChange(updated); + } + + return <> + {source.type === "geojson" && + { + const updated = { ...source, generateId: enabled || undefined }; + if (enabled) delete updated.promoteId; + onChange(updated); + }} + /> + } + { + setInvalidMapping(false); + updatePromoteId(value || undefined); + }} + /> + {source.type === "vector" && + { + if (value === undefined) { + setInvalidMapping(false); + if (mapping !== undefined) updatePromoteId(undefined); + } else if (isSourceLayerMapping(value)) { + setInvalidMapping(false); + updatePromoteId(value); + } else { + setInvalidMapping(true); + } + }} + /> + {mappingError &&
{mappingError}
} +
} + ; +}