shoelace/src/components/icon/library.ts

50 wiersze
1.2 KiB
TypeScript
Czysty Zwykły widok Historia

import defaultLibrary from './library.default';
import systemLibrary from './library.system';
import type SlIcon from '~/components/icon/icon';
2020-10-07 13:34:05 +00:00
export type IconLibraryResolver = (name: string) => string;
export type IconLibraryMutator = (svg: SVGElement) => void;
export interface IconLibrary {
2020-10-07 13:34:05 +00:00
name: string;
resolver: IconLibraryResolver;
mutator?: IconLibraryMutator;
}
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) {
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
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);
}