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