shoelace/src/components/icon/library.ts

55 wiersze
1.3 KiB
TypeScript
Czysty Zwykły widok Historia

2021-03-09 00:14:32 +00:00
import { SlIcon } from '../../shoelace';
2021-02-26 14:09:13 +00:00
import { getBasePath } from '../../utilities/base-path';
2020-10-07 13:34:05 +00:00
export type IconLibraryResolver = (name: string) => string;
export type IconLibraryMutator = (svg: SVGElement) => void;
2021-02-26 14:09:13 +00:00
2020-10-07 13:34:05 +00:00
interface IconLibraryRegistry {
name: string;
resolver: IconLibraryResolver;
mutator?: IconLibraryMutator;
}
let registry: IconLibraryRegistry[] = [
{
name: 'default',
2021-02-26 14:09:13 +00:00
resolver: name => `${getBasePath()}/assets/icons/${name}.svg`
}
];
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) {
2020-10-07 13:34:05 +00:00
return registry.filter(lib => lib.name === name)[0];
}
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
watchedIcons.map(icon => {
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);
}