Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing custom themes to create layers #234

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion styles/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
dist
node_modules
34 changes: 33 additions & 1 deletion styles/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LayerSpecification } from "@maplibre/maplibre-gl-style-spec";
import { labels_layers, nolabels_layers } from "./base_layers";
import themes from "./themes";
import themes, { Theme } from "./themes";

export default function (source: string, key: string): LayerSpecification[] {
const theme = themes[key];
Expand All @@ -16,3 +16,35 @@ export function labels(source: string, key: string): LayerSpecification[] {
const theme = themes[key];
return labels_layers(source, theme);
}

export function layersWithCustomTheme(
source: string,
theme: Theme,
): LayerSpecification[] {
return nolabels_layers(source, theme).concat(labels_layers(source, theme));
}

export function layersWithPartialCustomTheme(
source: string,
key: string,
partialTheme: Partial<Theme>,
): LayerSpecification[] {
const mergedTheme = { ...themes[key], ...partialTheme };
return nolabels_layers(source, mergedTheme).concat(
labels_layers(source, mergedTheme),
);
}

export function noLabelsWithCustomTheme(
bdon marked this conversation as resolved.
Show resolved Hide resolved
source: string,
theme: Theme,
): LayerSpecification[] {
return nolabels_layers(source, theme);
}

export function labelsWithCustomTheme(
source: string,
theme: Theme,
): LayerSpecification[] {
return labels_layers(source, theme);
}
40 changes: 40 additions & 0 deletions styles/test/custom_themes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from "node:assert";
import { test } from "node:test";
import { validateStyleMin } from "@maplibre/maplibre-gl-style-spec";
import {
labelsWithCustomTheme,
layersWithPartialCustomTheme,
} from "../src/index";
import themes from "../src/themes";

const STUB = {
version: 8,
glyphs: "https://example.com/{fontstack}/{range}.pbf",
sources: {
sourcename: {
type: "vector",
},
},
};

test("validate custom themes", () => {
const customTheme = themes.dark;
STUB.layers = labelsWithCustomTheme("sourcename", customTheme);
const errors = validateStyleMin(STUB);
assert.deepStrictEqual([], errors);
});

test("validate layers with partial custom theme overrides", () => {
const customBackgroundColor = "#fff";
const partialTheme = { background: customBackgroundColor };
STUB.layers = layersWithPartialCustomTheme(
"sourcename",
"dark",
partialTheme,
);
const errors = validateStyleMin(STUB);
assert.deepStrictEqual([], errors);
assert.deepStrictEqual(STUB.layers.find((l) => l.id === "background").paint, {
"background-color": customBackgroundColor,
});
});
1 change: 1 addition & 0 deletions styles/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import layers from "../src/index";
import themes from "../src/themes";

import "./base_layers.test";
import "./custom_themes.test";
import "./themes.test";

const STUB = {
Expand Down
Loading