2022-02-26 14:12:13 +00:00
|
|
|
import { getCustomIcon } from './custom';
|
2022-03-03 21:30:16 +00:00
|
|
|
import type { UniversalIconLoader } from './types';
|
2022-02-27 18:55:06 +00:00
|
|
|
import { searchForIcon } from './modern';
|
2022-02-26 14:12:13 +00:00
|
|
|
|
2022-03-14 14:13:13 +00:00
|
|
|
export const loadIcon: UniversalIconLoader = async (
|
2022-03-03 21:30:16 +00:00
|
|
|
collection,
|
|
|
|
icon,
|
|
|
|
options
|
|
|
|
) => {
|
2022-02-26 14:12:13 +00:00
|
|
|
const custom = options?.customCollections?.[collection];
|
|
|
|
|
|
|
|
if (custom) {
|
2022-02-27 18:55:06 +00:00
|
|
|
if (typeof custom === 'function') {
|
|
|
|
const result = await custom(icon);
|
|
|
|
if (result) {
|
|
|
|
if (typeof result === 'string') {
|
2022-03-14 14:13:13 +00:00
|
|
|
return await getCustomIcon(
|
|
|
|
() => result,
|
|
|
|
collection,
|
|
|
|
icon,
|
|
|
|
options
|
|
|
|
);
|
2022-02-27 18:55:06 +00:00
|
|
|
}
|
|
|
|
// if using dynamic import and requesting the json file
|
|
|
|
// for example: carbon: () => import('@iconify-json/carbon/icons.json').then(i => i.default as any)
|
|
|
|
if ('icons' in result) {
|
|
|
|
// possible icon names
|
|
|
|
const ids = [
|
|
|
|
icon,
|
|
|
|
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(),
|
|
|
|
icon.replace(/([a-z])(\d+)/g, '$1-$2'),
|
|
|
|
];
|
2022-03-14 14:13:13 +00:00
|
|
|
return await searchForIcon(
|
|
|
|
result,
|
|
|
|
collection,
|
|
|
|
ids,
|
|
|
|
options
|
|
|
|
);
|
2022-02-27 18:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return await getCustomIcon(custom, collection, icon, options);
|
2022-02-26 14:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 21:30:16 +00:00
|
|
|
return undefined;
|
2022-03-14 14:13:13 +00:00
|
|
|
};
|