mirror of
https://github.com/maputnik/editor.git
synced 2026-12-05 16:24:20 +00:00
Fixes #730. The Style Settings modal now has a "Use current map view" button above the Center field. It copies the map's current center, zoom, bearing and pitch into the style, so you don't have to copy them from the URL hash by hand. To make bearing and pitch available, the MapLibre map now reports them with center and zoom, and also reports after a rotate or pitch ends (before, only `dragend` and `zoomend` did). The OpenLayers renderer doesn't report bearing or pitch, so with it the button only fills center and zoom. Values are rounded before they go into the style: center to 5 decimals (about 1 m), zoom to 2, bearing and pitch to 1. Without that the style gets values like `2.1635000000000204`. The Center field gets a `key` that changes each time the button is clicked. `InputArray` only reads its value when it mounts, so it keeps half-typed coordinates while you edit, but that also means it wouldn't show a center set from outside. Remounting it on the button click shows the new center and leaves normal typing alone. **Before / after** (Style Settings): | Before | After | |---|---| | <img src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-730/before-style-settings.png" width="400"> | <img src="https://raw.githubusercontent.com/KhadeerBasha1232/maputnik/pr-assets/pr-730/after-style-settings.png" width="400"> | **Performance:** no measurable impact. The map calls the existing `onChange` once more at the end of a rotate or pitch gesture, which is the same work it already does after each drag and zoom. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues: fixes #730. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality: `use current map view copies the map's view into the style` in `modals.spec.ts` opens the map at zoom 5, sets bearing 12 and pitch 30 in the form, clicks the button and checks that the style has the map's center, zoom, bearing 0 and pitch 0. It fails on `main`, and it also fails if the map doesn't report bearing and pitch. A second test, `use current map view shows the map's center in the form`, checks that the Center input shows the new value. - [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 the full `npx playwright test` suite (222 passed). The first test fails on `main`, and also fails if I put back `main`'s `MapMaplibreGl.tsx`. Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the change, the test and this description. I've reviewed and tested the change and can answer questions about it.
191 lines
8.4 KiB
TypeScript
191 lines
8.4 KiB
TypeScript
import { describe, beforeEach, test, expect } from "./utils/fixtures";
|
|
import { MaputnikDriver } from "./maputnik-driver";
|
|
|
|
describe("sprite picker", () => {
|
|
const { given, when, get, then } = new MaputnikDriver();
|
|
beforeEach(async () => {
|
|
await given.setupMockBackedResponses();
|
|
await when.setStyle("sprite");
|
|
await when.openSpritePicker();
|
|
});
|
|
|
|
test("shows the catalog names and @2x atlas crops in the option rows", async () => {
|
|
await then(get.spriteOptions()).shouldHaveLength(4);
|
|
await then(get.spritePixel("red", 8, 18)).shouldDeepNestedInclude([255, 0, 0, 255]);
|
|
await then(get.spritePixel("green", 8, 8)).shouldDeepNestedInclude([0, 128, 0, 255]);
|
|
});
|
|
|
|
test("keeps the menu in the viewport when the field is near the bottom", async () => {
|
|
await when.scrollSpritePickerToBottom();
|
|
await then(get.elementByTestId("sprite-preview:red")).shouldBeInViewport();
|
|
await then(get.elementByTestId("sprite-preview:retina-square")).shouldBeInViewport();
|
|
});
|
|
|
|
test("filters names without case sensitivity", async () => {
|
|
await when.searchSprites("GrEeN");
|
|
await then(get.spriteOptions()).shouldHaveLength(1);
|
|
await then(get.spriteOptions()).shouldHaveText("green");
|
|
});
|
|
|
|
test("stores the selected sprite ID", async () => {
|
|
await when.chooseSprite("green");
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "green" } }] });
|
|
});
|
|
|
|
test("selects a filtered sprite with the keyboard", async () => {
|
|
await when.searchSprites("sdf");
|
|
await when.chooseSpriteWithKeyboard();
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "sdf-circle" } }] });
|
|
await then(get.spriteOptions()).shouldNotExist();
|
|
});
|
|
|
|
test("renders an SDF silhouette with transparent outside pixels", async () => {
|
|
await then(get.spritePixel("sdf-circle", 16, 16)).shouldDeepNestedInclude([0, 0, 0, 255]);
|
|
await then(get.spritePixel("sdf-circle", 0, 0)).shouldDeepNestedInclude([0, 0, 0, 0]);
|
|
});
|
|
|
|
test("enlarges and centers a small sprite without stretching it", async () => {
|
|
await then(get.elementByTestId("sprite-preview:red")).shouldHaveCss("width", "36px");
|
|
await then(get.elementByTestId("sprite-preview:red")).shouldHaveCss("height", "36px");
|
|
await then(get.spritePixel("red", 30, 18)).shouldDeepNestedInclude([255, 0, 0, 255]);
|
|
await then(get.spritePixel("red", 18, 5)).shouldDeepNestedInclude([0, 0, 0, 0]);
|
|
await then(get.spritePixel("red", 18, 30)).shouldDeepNestedInclude([0, 0, 0, 0]);
|
|
});
|
|
|
|
describe("on a high-density display", () => {
|
|
test.use({ deviceScaleFactor: 2 });
|
|
|
|
test("draws a high-resolution thumbnail at the same display size", async () => {
|
|
const preview = get.elementByTestId("sprite-preview:retina-square");
|
|
await then(preview).shouldHaveCss("width", "36px");
|
|
await then(preview).shouldHaveCss("height", "36px");
|
|
await expect(preview).toHaveAttribute("width", "72");
|
|
await expect(preview).toHaveAttribute("height", "72");
|
|
await then(get.spritePixel("retina-square", 60, 60)).shouldDeepNestedInclude([0, 0, 255, 255]);
|
|
});
|
|
});
|
|
|
|
test("keeps free-form sprite names", async () => {
|
|
await when.searchSprites("custom-{type}");
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "custom-{type}" } }] });
|
|
});
|
|
|
|
test("Escape closes the menu without selecting an option", async () => {
|
|
await when.searchSprites("gr");
|
|
await when.closeSpritePicker();
|
|
await then(get.spriteOptions()).shouldNotExist();
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "gr" } }] });
|
|
});
|
|
|
|
test("keeps expressions in the existing expression editor", async () => {
|
|
await when.chooseSprite("green");
|
|
await when.makeExpression("icon-image");
|
|
await then(get.expressionEditor("icon-image")).shouldBeVisible();
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": ["literal", "green"] } }] });
|
|
await then(get.elementByTestId("spec-field-input:icon-image")).shouldNotExist();
|
|
});
|
|
|
|
test("replaces the catalog when a different sprite source is opened", async () => {
|
|
await when.openSpriteStyle("sprite-alternate-style");
|
|
await when.openSpritePicker();
|
|
await then(get.spriteOptions()).shouldHaveLength(1);
|
|
await then(get.spriteOptions()).shouldHaveText("blue");
|
|
});
|
|
|
|
test("renders the base atlas when @2x metadata is unavailable", async () => {
|
|
await when.openSpriteStyle("sprite-alternate-style");
|
|
await when.openSpritePicker();
|
|
await then(get.spritePixel("blue", 18, 18)).shouldDeepNestedInclude([0, 0, 255, 255]);
|
|
});
|
|
|
|
test("clears the catalog when the next style has no sprite", async () => {
|
|
await when.openSpriteStyle("sprite-empty-style");
|
|
await when.openSpritePicker();
|
|
await then(get.spriteOptions()).shouldHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("sprite-backed properties", () => {
|
|
const { given, when, get, then } = new MaputnikDriver();
|
|
const spriteProperties = [
|
|
{ layerType: "background", source: undefined, layerId: "background-pattern", property: "background-pattern" },
|
|
{ layerType: "fill", source: "points", layerId: "fill-pattern", property: "fill-pattern" },
|
|
{ layerType: "line", source: "points", layerId: "line-pattern", property: "line-pattern" },
|
|
{ layerType: "fill-extrusion", source: "points", layerId: "fill-extrusion-pattern", property: "fill-extrusion-pattern" },
|
|
] as const;
|
|
|
|
beforeEach(async () => {
|
|
await given.setupMockBackedResponses();
|
|
await when.setStyle("sprite");
|
|
});
|
|
|
|
for (const { layerType, source, layerId, property } of spriteProperties) {
|
|
test(`${property} uses the sprite preview picker`, async () => {
|
|
await when.modal.open();
|
|
await when.modal.fillLayers({ type: layerType, layer: source, id: layerId });
|
|
await when.openSpritePicker(layerId, property);
|
|
|
|
await then(get.spriteOptions()).shouldHaveLength(4);
|
|
await then(get.spritePixel("red", 8, 18)).shouldDeepNestedInclude([255, 0, 0, 255]);
|
|
|
|
await when.chooseSprite("green");
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
|
|
layers: [
|
|
{ id: "symbols", layout: { "icon-image": "red" } },
|
|
{ id: layerId, paint: { [property]: "green" } },
|
|
],
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
describe("namespaced sprite picker", () => {
|
|
const { given, when, get, then } = new MaputnikDriver();
|
|
beforeEach(async () => {
|
|
await given.setupMockBackedResponses();
|
|
await when.setStyle("sprite_namespaced");
|
|
await when.openSpritePicker();
|
|
});
|
|
|
|
test("retains MapLibre sprite namespaces when selecting an ID", async () => {
|
|
await when.chooseSprite("extra:blue");
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "extra:blue" } }] });
|
|
});
|
|
});
|
|
|
|
describe("sprite image failures", () => {
|
|
const { given, when, get, then } = new MaputnikDriver();
|
|
beforeEach(async () => {
|
|
await given.setupMockBackedResponses();
|
|
await given.interceptAndMockResponse({ url: /sprite-atlas@2x\.png/, response: {} });
|
|
await when.setStyle("sprite");
|
|
await when.openSpritePicker();
|
|
});
|
|
|
|
test("can still select a name when the atlas fails to load", async () => {
|
|
await then(get.spriteOptions()).shouldHaveLength(4);
|
|
await then(get.elementByTestId("sprite-preview:green")).shouldNotExist();
|
|
await when.chooseSprite("green");
|
|
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({ layers: [{ id: "symbols", layout: { "icon-image": "green" } }] });
|
|
});
|
|
});
|
|
|
|
describe("late sprite metadata", () => {
|
|
const { given, when, get, then } = new MaputnikDriver();
|
|
beforeEach(async () => {
|
|
await given.setupMockBackedResponses();
|
|
await given.holdResponse(/sprite-atlas@2x\.json/, "sprite-atlas@2x.json", "old-sprite");
|
|
await when.setStyle("sprite");
|
|
await when.waitForResponse("old-sprite");
|
|
await when.openSpriteStyle("sprite-alternate-style");
|
|
await when.openSpritePicker();
|
|
await then(get.spriteOptions()).shouldHaveText("blue");
|
|
});
|
|
|
|
test("does not restore an old catalog after the source changes", async () => {
|
|
await when.releaseResponse("old-sprite");
|
|
await then(get.spriteOptions()).shouldHaveLength(1);
|
|
await then(get.spriteOptions()).shouldHaveText("blue");
|
|
});
|
|
});
|