From 03bea5cfb4e89e730c5d5afd50fa7b33837ad16c Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Wed, 12 Dec 2018 22:28:01 +0100 Subject: [PATCH] fix(map): global map --- src/components/components.d.ts | 2 -- src/components/icon/icon.tsx | 10 ++++------ src/components/icon/utils.ts | 18 ++++++++++++++++++ src/components/index.ts | 2 ++ 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/components/index.ts diff --git a/src/components/components.d.ts b/src/components/components.d.ts index 055ac7c0..ef974013 100644 --- a/src/components/components.d.ts +++ b/src/components/components.d.ts @@ -25,7 +25,6 @@ export namespace Components { * A combination of both `name` and `src`. If a `src` url is detected it will set the `src` property. Otherwise it assumes it's a built-in named SVG and set the `name` property. */ 'icon'?: string; - 'iconMap'?: {[name: string]: string}; /** * Specifies which icon to use on `ios` mode. */ @@ -68,7 +67,6 @@ export namespace Components { * A combination of both `name` and `src`. If a `src` url is detected it will set the `src` property. Otherwise it assumes it's a built-in named SVG and set the `name` property. */ 'icon'?: string; - 'iconMap'?: {[name: string]: string}; /** * Specifies which icon to use on `ios` mode. */ diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index 97947b87..96e20c23 100755 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -1,5 +1,5 @@ import { Component, Element, Prop, State, Watch } from '@stencil/core'; -import { getName, getSrc, isValid } from './utils'; +import { getIconMap, getName, getSrc, isValid } from './utils'; @Component({ tag: 'ion-icon', @@ -20,9 +20,6 @@ export class Icon { @Prop({ context: 'document' }) doc!: Document; @Prop({ context: 'window' }) win: any; - /** @internal */ - @Prop() iconMap?: {[name: string]: string}; - /** * The color to use for the background of the item. */ @@ -167,8 +164,9 @@ export class Icon { } private getNamedUrl(name: string) { - if (this.iconMap) { - return this.iconMap[name]; + const url = getIconMap().get(name); + if (url) { + return url; } return `${this.resourcesUrl}svg/${name}.svg`; } diff --git a/src/components/icon/utils.ts b/src/components/icon/utils.ts index 894801c6..930599e5 100644 --- a/src/components/icon/utils.ts +++ b/src/components/icon/utils.ts @@ -1,4 +1,22 @@ +let CACHED_MAP: Map; + +export function getIconMap(): Map { + if (!CACHED_MAP) { + const win = window as any; + win.Ionicons = win.Ionicons || {}; + CACHED_MAP = win.Ionicons.map = win.Ionicons.map || new Map(); + } + return CACHED_MAP; +} + +export function addIcons(icons: {[name: string]: string }) { + const map = getIconMap(); + Object.keys(icons).forEach(name => { + map.set(name, icons[name]); + }); +} + export function getName( name: string | undefined, mode: string | undefined, diff --git a/src/components/index.ts b/src/components/index.ts new file mode 100644 index 00000000..f5ed6045 --- /dev/null +++ b/src/components/index.ts @@ -0,0 +1,2 @@ + +export { addIcons } from './icon/utils';