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