From 11c7daa2bbec82ef299413b97793017959b0561c Mon Sep 17 00:00:00 2001 From: SHAIK KHADEER BASHA <120262074+KhadeerBasha1232@users.noreply.github.com> Date: Fri, 4 Dec 2026 23:51:34 +0530 Subject: [PATCH] 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 | |---|---| | | | **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 --- CHANGELOG.md | 1 + e2e/layer-editor.spec.ts | 10 ++++++++++ src/components/LayerEditor.tsx | 2 +- src/libs/layer.test.ts | 28 ++++++++++++++++++++++++++++ src/libs/layer.ts | 13 +++++++++++-- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69320ebc..aeb67627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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..._ diff --git a/e2e/layer-editor.spec.ts b/e2e/layer-editor.spec.ts index 5d1d8b00..de0b6bea 100644 --- a/e2e/layer-editor.spec.ts +++ b/e2e/layer-editor.spec.ts @@ -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({ diff --git a/src/components/LayerEditor.tsx b/src/components/LayerEditor.tsx index d6c9b6dd..8767d1ed 100644 --- a/src/components/LayerEditor.tsx +++ b/src/components/LayerEditor.tsx @@ -222,7 +222,7 @@ class LayerEditorInternal extends React.Component this.props.onLayerIdChange(this.props.layerIndex, this.props.layer.id, newId)} /> this.props.onLayerChanged( diff --git a/src/libs/layer.test.ts b/src/libs/layer.test.ts index ae98bd14..1531d07f 100644 --- a/src/libs/layer.test.ts +++ b/src/libs/layer.test.ts @@ -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", () => { diff --git a/src/libs/layer.ts b/src/libs/layer.ts index 68063e23..e171e2a1 100644 --- a/src/libs/layer.ts +++ b/src/libs/layer.ts @@ -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; + + // 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