Simplify custom icon set, return only 1 icon per call (#5822)

This commit is contained in:
Bram Kragten 2020-05-09 20:59:02 +02:00 committed by GitHub
parent d10be4ef2d
commit 29ed1144d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View File

@ -10,7 +10,7 @@ import {
CSSResult, CSSResult,
} from "lit-element"; } from "lit-element";
import "./ha-svg-icon"; import "./ha-svg-icon";
import { customIconsets, CustomIcons } from "../data/custom_iconsets"; import { customIconsets, CustomIcon } from "../data/custom_iconsets";
import { import {
Chunks, Chunks,
MDI_PREFIXES, MDI_PREFIXES,
@ -64,11 +64,16 @@ export class HaIcon extends LitElement {
return; return;
} }
const [iconPrefix, iconName] = this.icon.split(":", 2); const [iconPrefix, iconName] = this.icon.split(":", 2);
if (!iconPrefix || !iconName) {
return;
}
if (!MDI_PREFIXES.includes(iconPrefix)) { if (!MDI_PREFIXES.includes(iconPrefix)) {
if (iconPrefix in customIconsets) { if (iconPrefix in customIconsets) {
const customIconset = customIconsets[iconPrefix]; const customIconset = customIconsets[iconPrefix];
if (customIconset) { if (customIconset) {
this._setCustomPath(customIconset(iconName), iconName); this._setCustomPath(customIconset(iconName));
} }
return; return;
} }
@ -97,13 +102,10 @@ export class HaIcon extends LitElement {
debouncedWriteCache(); debouncedWriteCache();
} }
private async _setCustomPath( private async _setCustomPath(promise: Promise<CustomIcon>) {
promise: Promise<CustomIcons>, const icon = await promise;
iconName: string this._path = icon.path;
) { this._viewBox = icon.viewBox;
const iconPack = await promise;
this._path = iconPack[iconName].path;
this._viewBox = iconPack[iconName].viewBox;
} }
private async _setPath(promise: Promise<Icons>, iconName: string) { private async _setPath(promise: Promise<Icons>, iconName: string) {

View File

@ -1,9 +1,10 @@
export interface CustomIcons { export interface CustomIcon {
[key: string]: { path: string; viewBox?: string }; path: string;
viewBox?: string;
} }
export interface CustomIconsetsWindow { export interface CustomIconsetsWindow {
customIconsets?: { [key: string]: (name: string) => Promise<CustomIcons> }; customIconsets?: { [key: string]: (name: string) => Promise<CustomIcon> };
} }
const customIconsetsWindow = window as CustomIconsetsWindow; const customIconsetsWindow = window as CustomIconsetsWindow;