Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
MINOR: Fix theme extends to respect multiple image textures (#2230)
Browse files Browse the repository at this point in the history
Signed-off-by: German Zargaryan <2526045+germanz@users.noreply.github.com>
  • Loading branch information
germanz committed Jul 1, 2021
1 parent 1310268 commit 9bff7be
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
38 changes: 37 additions & 1 deletion @here/harp-mapview/lib/ThemeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isJsonExpr } from "@here/harp-datasource-protocol";
import {
Definitions,
FlatTheme,
ImageTexture,
isJsonExprReference,
Style,
Styles,
Expand Down Expand Up @@ -524,7 +525,42 @@ export class ThemeLoader {
} else if (theme.styles) {
styles = { ...theme.styles };
}
return { ...baseTheme, ...theme, definitions, styles };

return {
...baseTheme,
...theme,
// Due to nested structure of the images/textures it needs a
// deep merge with a duplicate exclusion.
...ThemeLoader.mergeImageTextures(theme, baseTheme),
definitions,
styles
};
}

private static mergeImageTextures(
theme: Theme,
baseTheme: Theme
): Pick<FlatTheme, "images" | "imageTextures"> {
const images = { ...baseTheme.images, ...theme.images };
let imageTextures: ImageTexture[] = [];

if (!baseTheme.imageTextures && theme.imageTextures) {
imageTextures = theme.imageTextures;
} else if (baseTheme.imageTextures && !theme.imageTextures) {
imageTextures = baseTheme.imageTextures;
} else if (baseTheme.imageTextures && theme.imageTextures) {
imageTextures = theme.imageTextures.slice();
baseTheme.imageTextures.forEach(val => {
if (!imageTextures.find(({ name }) => name === val.name)) {
imageTextures.push(val);
}
});
}

return {
images,
imageTextures
};
}

private static convertFlatTheme(theme: Theme | FlatTheme): Theme {
Expand Down
65 changes: 65 additions & 0 deletions @here/harp-mapview/test/ThemeLoaderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,71 @@ describe("ThemeLoader", function () {
assert.deepEqual(result.definitions!.roadColor, { type: "color", value: "#fff" });
assert.deepEqual(result.definitions!.waterColor, { type: "color", value: "#0f0" });
});
it("supports multiple inheritance with textures", async function () {
const inheritedTheme: Theme = {
extends: [
{
images: {
foo: {
url: "icons://maki_icons.png",
preload: true
},
baz: {
url: "icons://maki_icons.png",
preload: true
}
},
imageTextures: [
{
name: "foo",
image: "foo"
},
{
name: "baz",
image: "baz"
}
]
},
{
images: {
bar: {
url: "icons://maki_icons.png",
preload: true
},
baz: {
url: "icons://override.png",
atlas: "icons://icons/maki_icons.json",
preload: true
}
},
imageTextures: [
{
name: "bar",
image: "bar"
},
{
name: "baz",
image: "baz"
}
]
}
]
};

const result = await ThemeLoader.load(inheritedTheme);
assert.exists(result.images?.foo);
assert.exists(result.images?.bar);
assert.exists(result.images?.baz);

assert.equal(result.images?.baz.url, "icons://override.png");
assert.exists(result.images?.baz.atlas);

assert.deepEqual(result.imageTextures?.map(imageTexture => imageTexture.name).sort(), [
"bar",
"baz",
"foo"
]);
});
});

describe("#load support for inheritance and optional reference resolving", function () {
Expand Down

0 comments on commit 9bff7be

Please sign in to comment.