From 1f90a7de651eeb6a8983550e172ffa6954737d45 Mon Sep 17 00:00:00 2001 From: SHAIK KHADEER BASHA <120262074+KhadeerBasha1232@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:54:16 +0530 Subject: [PATCH] Edit generated and promoted feature IDs in Data Sources (#2397) Fixes #805. The Data Sources editor had no way to set `generateId` or `promoteId`, so these had to be added by hand in the style JSON. This adds a `SourceFeatureIdFields` block under the existing fields for GeoJSON and vector sources: - GeoJSON (inline and URL): a **Generate feature IDs** checkbox and a **Feature ID property** field. As the issue points out, the two are mutually exclusive, so turning one on clears the other. - Vector (TileJSON, tile URLs and PMTiles): a **Feature ID property** field for a single property, and a **Feature ID properties by source layer** JSON field for the per-layer form (`{"roads": "road_id"}`). An object that doesn't map names to strings shows an error and isn't saved. Edits keep the rest of the source configuration. | Before | After | |---|---| | ![before](https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-2397/before.png) | ![after](https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-2397/after.png) | Both show the Data Sources modal with an inline GeoJSON source and a vector tile source, in Chromium. **Open question:** the issue asked how the UI should look, and nobody had decided yet. For vector sources I used two fields because `promoteId` can be either a string or an object. Setting the single property replaces a per-layer mapping, and the other way round. If you'd prefer one field, for example a single JSON field, I can change that. **Performance:** none. The fields only render inside the Data Sources modal, for GeoJSON and vector sources. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues: fixes #805. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality: three e2e tests in `modals.spec.ts` (generate IDs for a new GeoJSON source, set a vector source's ID property, save a per-layer mapping). I ran the GeoJSON one against `main` and it fails; the other two look for fields that don't exist on `main`. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Tested locally: `npm run lint` clean, `npx tsc --noEmit` clean, `npx vitest run` (57 passed) and `npx playwright test e2e/modals.spec.ts` (64 passed). I also checked that focusing and leaving the empty "Feature ID property" field keeps an existing per-layer mapping. I didn't run the full e2e suite locally (it times out on Windows with several workers); it passes in CI on this PR. Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the change, the tests and this description. I've reviewed and tested the change and can answer questions about it. --- CHANGELOG.md | 1 + e2e/modal-driver.ts | 19 +++++ e2e/modals.spec.ts | 34 ++++++++ .../modals/ModalSourcesTypeEditor.tsx | 12 ++- .../modals/SourceFeatureIdFields.tsx | 84 +++++++++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/components/modals/SourceFeatureIdFields.tsx 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}
} +
} + ; +}