2023-10-31 21:03:39 +01:00
|
|
|
import {
|
|
|
|
|
ChangeDetectorRef,
|
|
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
Inject,
|
|
|
|
|
Input,
|
|
|
|
|
OnChanges,
|
|
|
|
|
Renderer2,
|
|
|
|
|
SimpleChange,
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import { LucideIconData } from '../icons/types';
|
2023-04-20 16:08:34 +02:00
|
|
|
import defaultAttributes from '../icons/constants/default-attributes';
|
2023-10-31 21:03:39 +01:00
|
|
|
import { LUCIDE_ICONS, LucideIconProviderInterface } from './lucide-icon.provider';
|
|
|
|
|
import { LucideIconConfig } from './lucide-icon.config';
|
2023-04-20 16:08:34 +02:00
|
|
|
|
|
|
|
|
interface TypedChange<T> extends SimpleChange {
|
2023-10-31 21:03:39 +01:00
|
|
|
previousValue: T;
|
|
|
|
|
currentValue: T;
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 21:03:39 +01:00
|
|
|
type SvgAttributes = { [key: string]: string | number };
|
|
|
|
|
|
2023-04-20 16:08:34 +02:00
|
|
|
type LucideAngularComponentChanges = {
|
2023-10-31 21:03:39 +01:00
|
|
|
name?: TypedChange<string | LucideIconData>;
|
|
|
|
|
img?: TypedChange<LucideIconData | undefined>;
|
|
|
|
|
color?: TypedChange<string>;
|
|
|
|
|
size?: TypedChange<number>;
|
|
|
|
|
strokeWidth?: TypedChange<number>;
|
|
|
|
|
absoluteStrokeWidth?: TypedChange<boolean>;
|
2024-07-22 09:29:16 +02:00
|
|
|
class: TypedChange<string>;
|
2023-04-20 16:08:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function formatFixed(number: number, decimals = 3): string {
|
2023-10-31 21:03:39 +01:00
|
|
|
return parseFloat(number.toFixed(decimals)).toString(10);
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2021-04-11 15:48:50 +04:30
|
|
|
|
|
|
|
|
@Component({
|
2023-10-31 21:03:39 +01:00
|
|
|
selector: 'lucide-angular, lucide-icon, i-lucide, span-lucide',
|
|
|
|
|
template: '<ng-content></ng-content>',
|
2021-04-11 15:48:50 +04:30
|
|
|
})
|
|
|
|
|
export class LucideAngularComponent implements OnChanges {
|
2023-10-31 21:03:39 +01:00
|
|
|
@Input() class?: string;
|
|
|
|
|
@Input() name?: string | LucideIconData;
|
2024-07-22 09:29:16 +02:00
|
|
|
@Input() img?: LucideIconData;
|
2023-10-31 21:03:39 +01:00
|
|
|
@Input() color?: string;
|
|
|
|
|
@Input() absoluteStrokeWidth = false;
|
|
|
|
|
defaultSize: number;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(ElementRef) private elem: ElementRef,
|
|
|
|
|
@Inject(Renderer2) private renderer: Renderer2,
|
|
|
|
|
@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef,
|
|
|
|
|
@Inject(LUCIDE_ICONS) private iconProviders: LucideIconProviderInterface[],
|
2024-02-01 22:38:21 +09:00
|
|
|
@Inject(LucideIconConfig) private iconConfig: LucideIconConfig,
|
2023-10-31 21:03:39 +01:00
|
|
|
) {
|
|
|
|
|
this.defaultSize = defaultAttributes.height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_size?: number;
|
|
|
|
|
|
|
|
|
|
get size(): number {
|
|
|
|
|
return this._size ?? this.iconConfig.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input() set size(value: string | number | undefined) {
|
|
|
|
|
if (value) {
|
|
|
|
|
this._size = this.parseNumber(value);
|
|
|
|
|
} else {
|
|
|
|
|
delete this._size;
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
}
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2023-10-31 21:03:39 +01:00
|
|
|
_strokeWidth?: number;
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2023-10-31 21:03:39 +01:00
|
|
|
get strokeWidth(): number {
|
|
|
|
|
return this._strokeWidth ?? this.iconConfig.strokeWidth;
|
|
|
|
|
}
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2023-10-31 21:03:39 +01:00
|
|
|
@Input() set strokeWidth(value: string | number | undefined) {
|
|
|
|
|
if (value) {
|
|
|
|
|
this._strokeWidth = this.parseNumber(value);
|
|
|
|
|
} else {
|
|
|
|
|
delete this._strokeWidth;
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: LucideAngularComponentChanges): void {
|
2024-07-22 09:29:16 +02:00
|
|
|
if (
|
|
|
|
|
changes.name ||
|
|
|
|
|
changes.img ||
|
|
|
|
|
changes.color ||
|
|
|
|
|
changes.size ||
|
|
|
|
|
changes.absoluteStrokeWidth ||
|
|
|
|
|
changes.strokeWidth ||
|
|
|
|
|
changes.class
|
|
|
|
|
) {
|
|
|
|
|
this.color = this.color ?? this.iconConfig.color;
|
|
|
|
|
this.size = this.parseNumber(this.size ?? this.iconConfig.size);
|
|
|
|
|
this.strokeWidth = this.parseNumber(this.strokeWidth ?? this.iconConfig.strokeWidth);
|
|
|
|
|
this.absoluteStrokeWidth = this.absoluteStrokeWidth ?? this.iconConfig.absoluteStrokeWidth;
|
|
|
|
|
const nameOrIcon = this.img ?? this.name;
|
|
|
|
|
if (typeof nameOrIcon === 'string') {
|
|
|
|
|
const icoOfName = this.getIcon(this.toPascalCase(nameOrIcon));
|
2023-10-31 21:03:39 +01:00
|
|
|
if (icoOfName) {
|
|
|
|
|
this.replaceElement(icoOfName);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
2024-07-22 09:29:16 +02:00
|
|
|
`The "${nameOrIcon}" icon has not been provided by any available icon providers.`,
|
2023-10-31 21:03:39 +01:00
|
|
|
);
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2024-07-22 09:29:16 +02:00
|
|
|
} else if (Array.isArray(nameOrIcon)) {
|
|
|
|
|
this.replaceElement(nameOrIcon);
|
2023-10-31 21:03:39 +01:00
|
|
|
} else {
|
|
|
|
|
throw new Error(`No icon name or image has been provided.`);
|
|
|
|
|
}
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-31 21:03:39 +01:00
|
|
|
this.changeDetector.markForCheck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replaceElement(img: LucideIconData): void {
|
|
|
|
|
const attributes = {
|
|
|
|
|
...defaultAttributes,
|
|
|
|
|
width: this.size,
|
|
|
|
|
height: this.size,
|
|
|
|
|
stroke: this.color ?? this.iconConfig.color,
|
|
|
|
|
'stroke-width': this.absoluteStrokeWidth
|
|
|
|
|
? formatFixed(this.strokeWidth / (this.size / this.defaultSize))
|
|
|
|
|
: this.strokeWidth.toString(10),
|
|
|
|
|
};
|
|
|
|
|
const icoElement = this.createElement(['svg', attributes, img]);
|
|
|
|
|
icoElement.classList.add('lucide');
|
|
|
|
|
if (typeof this.name === 'string') {
|
|
|
|
|
icoElement.classList.add(`lucide-${this.name.replace('_', '-')}`);
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
if (this.class) {
|
|
|
|
|
icoElement.classList.add(
|
|
|
|
|
...this.class
|
|
|
|
|
.split(/ /)
|
|
|
|
|
.map((a) => a.trim())
|
2024-02-01 22:38:21 +09:00
|
|
|
.filter((a) => a.length > 0),
|
2023-10-31 21:03:39 +01:00
|
|
|
);
|
2021-04-11 15:48:50 +04:30
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
const childElements = this.elem.nativeElement.childNodes;
|
|
|
|
|
for (const child of childElements) {
|
|
|
|
|
this.renderer.removeChild(this.elem.nativeElement, child);
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
this.renderer.appendChild(this.elem.nativeElement, icoElement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toPascalCase(str: string): string {
|
|
|
|
|
return str.replace(
|
|
|
|
|
/(\w)([a-z0-9]*)(_|-|\s*)/g,
|
2024-02-01 22:38:21 +09:00
|
|
|
(g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase(),
|
2023-10-31 21:03:39 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private parseNumber(value: string | number): number {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
const parsedValue = parseInt(value, 10);
|
|
|
|
|
if (isNaN(parsedValue)) {
|
|
|
|
|
throw new Error(`${value} is not numeric.`);
|
|
|
|
|
}
|
|
|
|
|
return parsedValue;
|
2021-04-11 15:48:50 +04:30
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getIcon(name: string): LucideIconData | null {
|
|
|
|
|
for (const iconProvider of Array.isArray(this.iconProviders)
|
|
|
|
|
? this.iconProviders
|
|
|
|
|
: [this.iconProviders]) {
|
|
|
|
|
if (iconProvider.hasIcon(name)) {
|
|
|
|
|
return iconProvider.getIcon(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createElement([tag, attrs, children = []]: readonly [
|
|
|
|
|
string,
|
|
|
|
|
SvgAttributes,
|
2024-02-01 22:38:21 +09:00
|
|
|
LucideIconData?,
|
2023-10-31 21:03:39 +01:00
|
|
|
]) {
|
|
|
|
|
const element = this.renderer.createElement(tag, 'http://www.w3.org/2000/svg');
|
|
|
|
|
|
|
|
|
|
Object.keys(attrs).forEach((name) => {
|
|
|
|
|
const attrValue: string =
|
|
|
|
|
typeof attrs[name] === 'string' ? (attrs[name] as string) : attrs[name].toString(10);
|
|
|
|
|
this.renderer.setAttribute(element, name, attrValue);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (children.length) {
|
|
|
|
|
children.forEach((child) => {
|
|
|
|
|
const childElement = this.createElement(child);
|
|
|
|
|
this.renderer.appendChild(element, childElement);
|
|
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
}
|
2023-10-31 21:03:39 +01:00
|
|
|
|
|
|
|
|
return element;
|
|
|
|
|
}
|
2021-04-11 15:48:50 +04:30
|
|
|
}
|