Change a layer's type from the layer editor (#2404)

Fixes #738.

The layer editor's Type field was a read-only text box
(`disabled={true}` on `FieldType`), so changing a layer's type meant
editing the JSON. It's a dropdown again, the same one the Add Layer
dialog uses. The `onChange` was already wired to `changeType()`, which
keeps the paint and layout properties the new type supports and drops
the rest. That's the behaviour agreed in the thread: keep what's shared,
and the rest falls back to the spec defaults. Undo brings back the old
layer.

`changeType()` now also drops `source`, `source-layer` and `filter` when
the new type is `background`, since a background layer can't have them
and the style would otherwise fail validation. Other types keep all
three. For example, a heatmap on a vector source still needs its
`source-layer`.

**Before / after** (layer editor, Type field):

| Before | After |
|---|---|
| <img
src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-738/before-layer-type.png"
width="400"> | <img
src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-738/after-layer-type.png"
width="400"> |

**Performance:** no impact. The change only runs when you pick a new
type.

## Launch Checklist

 - [x] Briefly describe the changes in this PR.
 - [x] Link to related issues: fixes #738.
- [x] Include before/after visuals or gifs if this PR includes visual
changes.
- [x] Write tests for all new functionality: `should change the layer
type` in `layer-editor.spec.ts` (fails on `main`, where the field is a
disabled text box), plus two `changeType` unit tests in `layer.test.ts`
for the background case and for keeping source keys between other types.
 - [x] Add an entry to `CHANGELOG.md` under the `## main` section.

Tested locally: `npm run lint` clean, `npx tsc --noEmit` clean, `npx
vitest run` (59 passed) and the full `npx playwright test` suite (221
passed). With `main`'s `LayerEditor.tsx` and `layer.ts` put back, the
new e2e test and the background unit test both fail.

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.

Co-authored-by: Harel M <harel.mazor@gmail.com>
This commit is contained in:
SHAIK KHADEER BASHAandHarel M authored and GitHub committed 2026-12-04 18:21:34 +00:00
1 parent 07bc2bcc3b
commit 11c7daa2bb
5 files changed
+51 -3

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
- 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
- Set the min and max zoom of a PMTiles source, for example to overzoom past the last zoom level in the file
- Change a layer's type from the layer editor; properties the new type doesn't support are removed
- Add a "Line metrics" checkbox to GeoJSON sources, which `line-gradient` needs to render
- _...Add new stuff here..._
+10
View File
@@ -57,6 +57,16 @@ describe("layer editor", () => {
});
});
describe("type", () => {
test("should change the layer type", async () => {
const id = await when.modal.fillLayers({ type: "circle", layer: "example" });
await when.select("layer-editor.layer-type.select", "line");
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
layers: [{ id, type: "line", source: "example" }],
});
});
});
describe("source", () => {
test("should show error when the source is invalid", async () => {
await when.modal.fillLayers({
+1 -1
View File
@@ -222,7 +222,7 @@ class LayerEditorInternal extends React.Component<LayerEditorInternalProps, Laye
onChange={newId => this.props.onLayerIdChange(this.props.layerIndex, this.props.layer.id, newId)}
/>
<FieldType
disabled={true}
wdKey="layer-editor.layer-type"
error={errorData.type}
value={this.props.layer.type}
onChange={newType => this.props.onLayerChanged(
+28
View File
@@ -30,6 +30,34 @@ describe("changeType", () => {
const changed = changeType(layer, "line") as any;
expect(changed.paint["line-color"]).toBe("#000");
});
it("drops source, source-layer and filter when changing to background", () => {
const layer = {
id: "l",
type: "fill",
source: "s",
"source-layer": "water",
filter: ["==", "class", "lake"],
} as unknown as LayerSpecification;
const changed = changeType(layer, "background") as any;
expect("source" in changed).toBe(false);
expect("source-layer" in changed).toBe(false);
expect("filter" in changed).toBe(false);
});
it("keeps source, source-layer and filter when changing between source layers", () => {
const layer = {
id: "l",
type: "fill",
source: "s",
"source-layer": "water",
filter: ["==", "class", "lake"],
} as unknown as LayerSpecification;
const changed = changeType(layer, "line") as any;
expect(changed.source).toBe("s");
expect(changed["source-layer"]).toBe("water");
expect(changed.filter).toEqual(["==", "class", "lake"]);
});
});
describe("changeProperty", () => {
+11 -2
View File
@@ -17,12 +17,21 @@ export function changeType(layer: LayerSpecification, newType: string): LayerSpe
}
});
return {
const changedLayer = {
...layer,
paint: changedPaintProps,
layout: changedLayoutProps,
type: newType
} as LayerSpecification;
} as Record<string, unknown>;
// A background layer can't have a source, so drop the keys that depend on one
if (newType === "background") {
delete changedLayer.source;
delete changedLayer["source-layer"];
delete changedLayer.filter;
}
return changedLayer as unknown as LayerSpecification;
}
/** A {@property} in either the paint our layout {@group} has changed