-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,31 @@ const ALIASES = Object.assign( | |
mi: "material", | ||
em: "emojicc", | ||
fl: "flags", | ||
in: "iconoir", | ||
}, | ||
(window.LastIcon && window.LastIcon.aliases) || {} | ||
); | ||
const ENABLE_ICONS = (window.LastIcon && window.LastIcon.fonts) || []; | ||
const FONT_ICONS = { | ||
material: { | ||
class: "material-icons-{type}", | ||
types: { | ||
baseline: "", | ||
twotone: "two-tone", | ||
}, | ||
}, | ||
boxicons: { | ||
class: "bx {prefix}-{icon}", | ||
}, | ||
bootstrap: { | ||
class: "bi-{icon}", | ||
}, | ||
fontawesome: { | ||
class: "{prefix} fa-{icon}", | ||
}, | ||
iconoir: { | ||
class: "iconoir-{icon}", | ||
}, | ||
}; | ||
const TYPES = Object.assign( | ||
{ | ||
boxicons: "solid", | ||
|
@@ -34,6 +55,13 @@ const PREFIXES = Object.assign( | |
regular: "bx", | ||
logos: "bxl", | ||
}, | ||
fontawesome: { | ||
solid: "fas", | ||
regular: "far", | ||
light: "fal", | ||
duotone: "fad", | ||
brands: "fab", | ||
}, | ||
}, | ||
(window.LastIcon && window.LastIcon.prefixes) || {} | ||
); | ||
|
@@ -46,7 +74,7 @@ const PATHS = Object.assign( | |
boxicons: "https://cdn.jsdelivr.net/npm/[email protected]/svg/{type}/{prefix}-{icon}.svg", | ||
cssgg: "https://cdn.jsdelivr.net/npm/[email protected]/icons/svg/{icon}.svg", | ||
tabler: "https://cdn.jsdelivr.net/npm/@tabler/[email protected]/icons/{icon}.svg", | ||
// type: solid, regular, brands | ||
// type: solid, regular, brands, light, duotone | ||
fontawesome: "https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/svgs/{type}/{icon}.svg", | ||
bytesize: "https://cdn.jsdelivr.net/npm/[email protected]/dist/icons/{icon}.svg", | ||
supertiny: "https://cdn.jsdelivr.net/npm/[email protected]/images/svg/{icon}.svg", | ||
|
@@ -55,7 +83,6 @@ const PATHS = Object.assign( | |
// type : 4x3 or 1x1 | ||
flags: "https://cdn.jsdelivr.net/npm/[email protected]/flags/{type}/{icon}.svg", | ||
emojicc: "https://cdn.jsdelivr.net/npm/[email protected]/svg/{icon}.svg", | ||
iconoir: "https://cdn.jsdelivr.net/gh/lucaburgio/iconoir/icons/{icon}.svg" | ||
}, | ||
(window.LastIcon && window.LastIcon.paths) || {} | ||
); | ||
|
@@ -80,7 +107,6 @@ class LastIcon extends HTMLElement { | |
* @return {Promise<String, Error>} | ||
*/ | ||
static getIconSvg(iconName, iconSet, iconType) { | ||
let iconPrefix = (PREFIXES[iconSet] && PREFIXES[iconSet][iconType]) || null; | ||
let iconUrl = PATHS[iconSet]; | ||
let cacheKey = iconSet + "-" + iconName; | ||
if (iconType) { | ||
|
@@ -96,14 +122,7 @@ class LastIcon extends HTMLElement { | |
}); | ||
} | ||
|
||
// Replace placeholders | ||
iconUrl = iconUrl.replace("{icon}", iconName); | ||
if (iconType) { | ||
iconUrl = iconUrl.replace("{type}", iconType); | ||
} | ||
if (iconPrefix) { | ||
iconUrl = iconUrl.replace("{prefix}", iconPrefix); | ||
} | ||
iconUrl = LastIcon.replacePlaceholders(iconUrl, iconName, iconSet, iconType); | ||
|
||
// If we have it in cache | ||
if (iconUrl && CACHE[cacheKey]) { | ||
|
@@ -123,19 +142,57 @@ class LastIcon extends HTMLElement { | |
return CACHE[cacheKey]; | ||
} | ||
|
||
/** | ||
* @param {string} value | ||
* @param {string} iconName | ||
* @param {string} iconSet | ||
* @param {string} iconType | ||
* @return {string} | ||
*/ | ||
static replacePlaceholders(value, iconName, iconSet, iconType) { | ||
let iconPrefix = (PREFIXES[iconSet] && PREFIXES[iconSet][iconType]) || null; | ||
value = value.replace("{icon}", iconName); | ||
if (iconType) { | ||
value = value.replace("{type}", iconType); | ||
} else { | ||
// Maybe we want to remove the type like in material icons | ||
value = value.replace("-{type}", ""); | ||
} | ||
if (iconPrefix) { | ||
value = value.replace("{prefix}", iconPrefix); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* @param {object} inst | ||
* @param {string} iconName | ||
* @param {string} iconSet | ||
* @param {string} iconType | ||
*/ | ||
static refreshIcon(inst, iconName, iconSet, iconType) { | ||
if (ENABLE_ICONS.includes(iconSet)) { | ||
LastIcon.log("Using font for " + iconName); | ||
let iconClass = FONT_ICONS[iconSet]["class"]; | ||
let nameAsClass = iconClass.includes("{icon}"); | ||
let fontType = iconType; | ||
if (FONT_ICONS[iconSet]["types"] && iconType in FONT_ICONS[iconSet]["types"]) { | ||
fontType = FONT_ICONS[iconSet]["types"][iconType]; | ||
} | ||
iconClass = LastIcon.replacePlaceholders(iconClass, iconName, iconSet, fontType); | ||
if (nameAsClass) { | ||
inst.innerHTML = '<i class="' + iconClass + '"></i>'; | ||
} else { | ||
inst.innerHTML = '<i class="' + iconClass + '">' + iconName + "</i>"; | ||
} | ||
return; | ||
} | ||
LastIcon.getIconSvg(iconName, iconSet, iconType) | ||
.then((iconData) => { | ||
if (inst.stroke) { | ||
iconData = iconData.replace(/stroke-width="([0-9]*)"/, 'stroke-width="' + inst.stroke + '"'); | ||
} | ||
if (FIX_FILL.indexOf(inst.set) !== -1) { | ||
if (FIX_FILL.includes(inst.set)) { | ||
iconData = iconData.replace(/(<svg.*?)>/, '$1 fill="currentColor">'); | ||
} | ||
inst.innerHTML = iconData; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters