2021-04-20 13:37:19 +00:00
|
|
|
import defaultLibrary from './library.default';
|
|
|
|
import systemLibrary from './library.system';
|
2022-01-16 05:47:14 +00:00
|
|
|
import type SlIcon from '~/components/icon/icon';
|
2020-10-07 14:54:21 +00:00
|
|
|
|
2020-10-07 13:34:05 +00:00
|
|
|
export type IconLibraryResolver = (name: string) => string;
|
|
|
|
export type IconLibraryMutator = (svg: SVGElement) => void;
|
2021-04-20 13:37:19 +00:00
|
|
|
export interface IconLibrary {
|
2020-10-07 13:34:05 +00:00
|
|
|
name: string;
|
|
|
|
resolver: IconLibraryResolver;
|
|
|
|
mutator?: IconLibraryMutator;
|
|
|
|
}
|
|
|
|
|
2021-04-20 13:37:19 +00:00
|
|
|
let registry: IconLibrary[] = [defaultLibrary, systemLibrary];
|
2021-03-06 17:01:39 +00:00
|
|
|
let watchedIcons: SlIcon[] = [];
|
2020-10-07 13:34:05 +00:00
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
export function watchIcon(icon: SlIcon) {
|
2020-10-07 13:34:05 +00:00
|
|
|
watchedIcons.push(icon);
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
export function unwatchIcon(icon: SlIcon) {
|
2020-10-07 13:34:05 +00:00
|
|
|
watchedIcons = watchedIcons.filter(el => el !== icon);
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
export function getIconLibrary(name?: string) {
|
2022-01-16 05:47:14 +00:00
|
|
|
return registry.find(lib => lib.name === name);
|
2020-10-07 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
export function registerIconLibrary(
|
|
|
|
name: string,
|
|
|
|
options: { resolver: IconLibraryResolver; mutator?: IconLibraryMutator }
|
|
|
|
) {
|
|
|
|
unregisterIconLibrary(name);
|
|
|
|
registry.push({
|
|
|
|
name,
|
|
|
|
resolver: options.resolver,
|
|
|
|
mutator: options.mutator
|
|
|
|
});
|
2020-10-07 13:34:05 +00:00
|
|
|
|
|
|
|
// Redraw watched icons
|
2022-01-16 05:47:14 +00:00
|
|
|
watchedIcons.forEach(icon => {
|
2020-10-07 13:34:05 +00:00
|
|
|
if (icon.library === name) {
|
|
|
|
icon.redraw();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
export function unregisterIconLibrary(name: string) {
|
2020-10-07 13:34:05 +00:00
|
|
|
registry = registry.filter(lib => lib.name !== name);
|
|
|
|
}
|