|
1 | 1 | import { merge } from 'lodash-es' |
2 | 2 |
|
| 3 | +/** |
| 4 | + * @param {String} prefix - prefix for the icon pack |
| 5 | + * @param {Array} icon - icon definition |
| 6 | + * @returns {{path: string, viewBox: string, attrs: *}} |
| 7 | + */ |
| 8 | +const createIcon = (prefix, icon) => { |
| 9 | + const [w, h, content, svg, data, , attrs] = icon |
| 10 | + let path |
| 11 | + |
| 12 | + const createPath = (d, attrs = {}) => `<path d="${d}" ${attrs.className ? `class="${attrs.className}"` : ''} fill="currentColor" />` |
| 13 | + |
| 14 | + const createGroupedPath = (groups, prefix) => { |
| 15 | + const paths = groups.map((d, idx) => |
| 16 | + createPath(d, { |
| 17 | + className: idx ? `${prefix}-primary` : `${prefix}-secondary`, |
| 18 | + }) |
| 19 | + ) |
| 20 | + |
| 21 | + return `<g fill="currentColor" class="${prefix}-group">${paths.join('')}</g>` |
| 22 | + } |
| 23 | + |
| 24 | + if (prefix.startsWith('fa')) { |
| 25 | + path = Array.isArray(data) |
| 26 | + ? createGroupedPath(data, prefix.substr(0, 2)) |
| 27 | + : createPath(data) |
| 28 | + } else { |
| 29 | + path = prefix.startsWith('fe') ? content : svg |
| 30 | + } |
| 31 | + |
| 32 | + return { |
| 33 | + path, |
| 34 | + viewBox: `0 0 ${w} ${h}`, |
| 35 | + attrs, |
| 36 | + } |
| 37 | +} |
| 38 | + |
3 | 39 | /** |
4 | 40 | * @description Custom parse all Icons provided by user |
5 | 41 | * @param {Object} iconSet - Registered Icons object |
6 | 42 | * @returns {Object} |
7 | 43 | */ |
8 | 44 | const parseIcons = (iconSet = {}) => { |
9 | 45 | const parseIcon = (iconObject) => { |
10 | | - const { icon } = iconObject |
| 46 | + const { icon, prefix, iconName } = iconObject |
11 | 47 | // Is library icon |
12 | 48 | if (icon) { |
13 | | - const [w, h, content, svg, path, , attrs] = icon |
14 | 49 | return { |
15 | | - [`${iconObject.iconName}`]: { |
16 | | - path: iconObject.prefix.startsWith('fa') |
17 | | - ? `<path d="${path}" fill="currentColor" />` |
18 | | - : iconObject.prefix.startsWith('fe') |
19 | | - ? content |
20 | | - : svg, |
21 | | - viewBox: `0 0 ${w} ${h}`, |
22 | | - attrs |
23 | | - } |
| 50 | + [`${iconName}`]: createIcon(prefix, icon) |
24 | 51 | } |
25 | 52 | } else { |
26 | 53 | return {} |
27 | 54 | } |
28 | 55 | } |
29 | 56 |
|
30 | | - const result = Object.values(iconSet) |
| 57 | + return Object.values(iconSet) |
31 | 58 | .map(value => parseIcon(value)) |
32 | 59 | .reduce((target, source) => merge(target, source), {}) |
33 | | - |
34 | | - return result |
35 | 60 | } |
36 | 61 |
|
37 | 62 | /** |
38 | 63 | * @description Parse Icon packs |
39 | | - * @param {String} pack Icon pack name |
40 | 64 | * @param {Object} iconSet Registered Icon set |
41 | 65 | * @returns {Object} Parsed pack icons object |
42 | 66 | */ |
43 | 67 | export const parsePackIcons = (iconSet) => { |
44 | 68 | // TODO: Add support for other icon libraries |
45 | | - // - Material Icons |
| 69 | + // - Material Icons: these are string constants, and need lots of work |
46 | 70 | // - Tailwind Icons (Hero icons) |
47 | | - const packIcons = parseIcons(iconSet) |
48 | | - return packIcons |
| 71 | + return parseIcons(iconSet) |
49 | 72 | } |
0 commit comments