From 55e6ef0ca2b9bf09db462826b4c9d985d7b6b964 Mon Sep 17 00:00:00 2001 From: Send_Nukez Date: Sat, 4 Dec 2021 15:24:01 +0100 Subject: [PATCH] add ability to get icons as raw svg string without type annd stuff --- src/js/Icons.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/js/Icons.js b/src/js/Icons.js index e57dfb5..c9c923c 100644 --- a/src/js/Icons.js +++ b/src/js/Icons.js @@ -19,6 +19,22 @@ export default class Icons { this.#icons = process.env.DRIBBBLISH_ICONS; } + getRawSVG(name, style = "round") { + if (!this.#icons.hasOwnProperty(name)) throw new Error(`Icon "${name}" does not exist`); + + if (typeof this.#icons[name] == "string") { + return this.#icons[name]; + } else { + if (!this.#icons[name].hasOwnProperty(style)) { + const styles = Object.keys(this.#icons[name]) + .map((s) => `"${s}"`) + .join(", "); + throw new Error(`Icon "${name}" does not have style "${style}". It is available in styles [${styles}].`); + } + return this.#icons[name][style]; + } + } + /** * @param {String} name icon name lowercase with dashes like `ac-unit` * @param {IconOptions} options @@ -36,14 +52,7 @@ export default class Icons { }; options = { ...defaultOptions, ...options }; - if (!this.#icons.hasOwnProperty(name)) throw new Error(`Icon "${name}" does not exist`); - let svg; - if (typeof this.#icons[name] == "string") { - svg = parseSVG(this.#icons[name]); - } else { - if (!this.#icons[name].hasOwnProperty(options.style)) throw new Error(`Icon "${name}" does not have style "${options.style}"`); - svg = parseSVG(this.#icons[name][options.style]); - } + const svg = parseSVG(this.getRawSVG(name, options.style)); svg.attributes.type = "dribbblish-icon"; svg.attributes.fill = options.fill;