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.
This commit is contained in:
SHAIK KHADEER BASHA authored and GitHub committed 2026-12-04 10:24:16 +02:00
1 parent e1a1af0226
commit 1f90a7de65
5 files changed
+149 -1

No files matched your search

+1
View File
@@ -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
+19
View File
@@ -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<string, string>) => {
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
+34
View File
@@ -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({
@@ -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<ModalSourcesTypeEditorInternalProps> {
render() {
renderEditor() {
const t = this.props.t;
const commonProps = {
source: this.props.source,
@@ -373,6 +374,15 @@ class ModalSourcesTypeEditorInternal extends React.Component<ModalSourcesTypeEdi
default: return null;
}
}
render() {
const { source, onChange } = this.props;
return <>
{this.renderEditor()}
{(source.type === "geojson" || source.type === "vector") &&
<SourceFeatureIdFields source={source} onChange={onChange} />}
</>;
}
}
export const ModalSourcesTypeEditor = withTranslation()(ModalSourcesTypeEditorInternal);
@@ -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<string, string> {
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" && <Block label={t("Generate feature IDs")} fieldSpec={latest.source_geojson.generateId}>
<InputCheckbox
value={source.generateId}
onChange={(enabled: boolean) => {
const updated = { ...source, generateId: enabled || undefined };
if (enabled) delete updated.promoteId;
onChange(updated);
}}
/>
</Block>}
<FieldString
label={t("Feature ID property")}
aria-label={t("Feature ID property")}
fieldSpec={fieldSpec}
value={property}
onChange={value => {
setInvalidMapping(false);
updatePromoteId(value || undefined);
}}
/>
{source.type === "vector" && <Block
label={t("Feature ID properties by source layer")}
fieldSpec={fieldSpec}
data-wd-key="source-promote-id-mapping"
error={mappingError ? { message: mappingError } : undefined}
>
<FieldJson
value={mapping}
lintType="json"
optional
onChange={value => {
if (value === undefined) {
setInvalidMapping(false);
if (mapping !== undefined) updatePromoteId(undefined);
} else if (isSourceLayerMapping(value)) {
setInvalidMapping(false);
updatePromoteId(value);
} else {
setInvalidMapping(true);
}
}}
/>
{mappingError && <div role="alert">{mappingError}</div>}
</Block>}
</>;
}