mirror of
https://github.com/maputnik/editor.git
synced 2026-11-30 13:54:20 +00:00
# fix: improve previews for small sprites ## Description Small sprites appeared at their original display size in the sprite picker, making them difficult to recognize when choosing an icon or pattern. - Render previews in a fixed 36 × 36 CSS pixel canvas. Scale and center each sprite while preserving its aspect ratio. - Prefer matching `@2x.json` metadata and `@2x.png` atlas for previews. Use the base atlas when `@2x` metadata is unavailable. - Size the canvas backing store for the display's device pixel ratio. Preserve query strings and fragments when building sprite URLs. Follow-up to [#2364](https://github.com/maplibre/maputnik/pull/2364), which extended sprite previews to all sprite-backed fields as part of [#1294](https://github.com/maplibre/maputnik/issues/1294). ## Visuals ### Before Small sprites appear at their original display size in the picker.  ### After The same sprites are scaled to fit the fixed-size preview using the `@2x` atlas.  ## Verification All checks for this change passed. - Playwright covers the `@2x` atlas, the centered 36 × 36 preview for a small rectangular sprite, the 72 × 72 canvas at device pixel ratio 2, and the base atlas fallback. Existing coverage exercises SDF, sprite selection, image failures, and catalog updates. - `npm run lint` - `npm run lint-css` - `npm run build` - `npm run test-unit -- --run` (55 passed) - `npm run test -- e2e/sprite-picker.spec.ts --workers=2` (21 passed) - Confirmed that deliberately changing the expected CSS width, backing canvas width, and fallback pixel color fails the corresponding tests. Restored the correct assertions. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues. - [x] Include before/after visuals or GIFs if this PR includes visual changes. - [x] Write tests for all new functionality. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Co-authored-by: FantasticSPM <asd507172@gmail.com>
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.element("[data-wd-key='spec-field-container:icon-image'] .maputnik-expression-editor")).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");
|
|
});
|
|
});
|