mirror of
https://github.com/penpot/penpot-plugins.git
synced 2025-01-22 14:49:27 -05:00
fix(icons-plugin): null insert error
This commit is contained in:
parent
a2d6418550
commit
5961469bf4
4 changed files with 33 additions and 31 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { Component } from '@angular/core';
|
import { Component, signal } from '@angular/core';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
import { FeatherIconNames, icons } from 'feather-icons';
|
import { FeatherIconNames, icons } from 'feather-icons';
|
||||||
import { SafeHtmlPipe } from './pipes/safe-html.pipe';
|
import { SafeHtmlPipe } from './pipes/safe-html.pipe';
|
||||||
|
@ -21,37 +21,42 @@ import { IconSearchComponent } from './components/icon-search/icon-search.compon
|
||||||
(searchIcons)="this.searchIcons($event)"
|
(searchIcons)="this.searchIcons($event)"
|
||||||
></app-icon-search>
|
></app-icon-search>
|
||||||
</div>
|
</div>
|
||||||
@for (key of iconKeys; track key) {
|
@for (key of iconKeys(); track key) {
|
||||||
<app-icon-button
|
<app-icon-button
|
||||||
[icon]="icons[key]"
|
[icon]="icons()[key]"
|
||||||
(insertIcon)="this.insertIcon(key)"
|
(insertIcon)="this.insertIcon(key)"
|
||||||
></app-icon-button>
|
></app-icon-button>
|
||||||
}
|
}
|
||||||
</div>`,
|
</div>`,
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
public icons = icons;
|
public icons = signal(icons);
|
||||||
public iconKeys: FeatherIconNames[] = Object.keys(
|
public iconKeys = signal(Object.keys(icons) as FeatherIconNames[]);
|
||||||
icons
|
|
||||||
) as FeatherIconNames[];
|
|
||||||
|
|
||||||
public insertIcon(key: FeatherIconNames): void {
|
public insertIcon(key: FeatherIconNames): void {
|
||||||
this.sendMessage({ content: icons[key].toSvg(), name: icons[key].name });
|
if (key && this.icons()[key] && this.icons()[key].toSvg()) {
|
||||||
|
this.sendMessage({
|
||||||
|
content: this.icons()[key].toSvg(),
|
||||||
|
name: this.icons()[key].name || key,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public searchIcons(search: string): void {
|
public searchIcons(search: string): void {
|
||||||
const allKeys = Object.keys(icons) as FeatherIconNames[];
|
const allKeys = Object.keys(icons) as FeatherIconNames[];
|
||||||
|
|
||||||
if (search === '') {
|
if (search === '') {
|
||||||
this.iconKeys = allKeys;
|
this.iconKeys.set(allKeys);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.iconKeys = allKeys.filter(
|
const filtered = allKeys.filter(
|
||||||
(key) =>
|
(key) =>
|
||||||
this.icons[key].tags.some((t) => t.match(search)) ||
|
this.icons()[key].tags.some((t) => t.match(search)) ||
|
||||||
this.icons[key].name.match(search)
|
this.icons()[key].name.match(search)
|
||||||
) as FeatherIconNames[];
|
) as FeatherIconNames[];
|
||||||
|
|
||||||
|
this.iconKeys.set(filtered);
|
||||||
}
|
}
|
||||||
|
|
||||||
private sendMessage(message: unknown): void {
|
private sendMessage(message: unknown): void {
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
import { Component, EventEmitter, Input, Output } from '@angular/core';
|
import { Component, output, input } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
|
||||||
import { SafeHtmlPipe } from '../../pipes/safe-html.pipe';
|
import { SafeHtmlPipe } from '../../pipes/safe-html.pipe';
|
||||||
import { FeatherIcon } from 'feather-icons';
|
import { FeatherIcon } from 'feather-icons';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-icon-button',
|
selector: 'app-icon-button',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [CommonModule, SafeHtmlPipe],
|
imports: [SafeHtmlPipe],
|
||||||
styleUrl: './icon-button.component.css',
|
styleUrl: './icon-button.component.css',
|
||||||
template: `<button
|
template: `<button
|
||||||
class="icon-button"
|
class="icon-button"
|
||||||
[attr.aria-label]="'Insert icon: ' + icon.name"
|
[attr.aria-label]="'Insert icon: ' + icon().name"
|
||||||
[title]="icon.name"
|
[title]="icon().name"
|
||||||
(click)="onInsertIcon()"
|
(click)="onInsertIcon()"
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
|
@ -25,16 +24,13 @@ import { FeatherIcon } from 'feather-icons';
|
||||||
stroke-width="2"
|
stroke-width="2"
|
||||||
stroke-linecap="round"
|
stroke-linecap="round"
|
||||||
stroke-linejoin="round"
|
stroke-linejoin="round"
|
||||||
[innerHtml]="icon.contents | safeHtml"
|
[innerHtml]="icon().contents | safeHtml"
|
||||||
></svg>
|
></svg>
|
||||||
</button>`,
|
</button>`,
|
||||||
})
|
})
|
||||||
export class IconButtonComponent {
|
export class IconButtonComponent {
|
||||||
@Input({ required: true })
|
public icon = input.required<FeatherIcon>();
|
||||||
public icon!: FeatherIcon;
|
public insertIcon = output<void>();
|
||||||
|
|
||||||
@Output()
|
|
||||||
private insertIcon = new EventEmitter<void>();
|
|
||||||
|
|
||||||
public onInsertIcon(): void {
|
public onInsertIcon(): void {
|
||||||
this.insertIcon.emit();
|
this.insertIcon.emit();
|
||||||
|
|
|
@ -1,23 +1,20 @@
|
||||||
import { Component, EventEmitter, Output } from '@angular/core';
|
import { Component, output, signal } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
|
||||||
import { icons } from 'feather-icons';
|
import { icons } from 'feather-icons';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-icon-search',
|
selector: 'app-icon-search',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [CommonModule],
|
imports: [],
|
||||||
styleUrl: './icon-search.component.css',
|
styleUrl: './icon-search.component.css',
|
||||||
template: `<input
|
template: `<input
|
||||||
type="search"
|
type="search"
|
||||||
[placeholder]="'Search ' + iconsCount + ' icons'"
|
[placeholder]="'Search ' + iconsCount() + ' icons'"
|
||||||
(input)="onSearchIcons($event)"
|
(input)="onSearchIcons($event)"
|
||||||
/>`,
|
/>`,
|
||||||
})
|
})
|
||||||
export class IconSearchComponent {
|
export class IconSearchComponent {
|
||||||
@Output()
|
public searchIcons = output<string>();
|
||||||
private searchIcons = new EventEmitter<string>();
|
public iconsCount = signal(Object.keys(icons).length);
|
||||||
|
|
||||||
public iconsCount = Object.keys(icons).length;
|
|
||||||
|
|
||||||
public onSearchIcons(event: Event): void {
|
public onSearchIcons(event: Event): void {
|
||||||
const target = event.target as HTMLInputElement;
|
const target = event.target as HTMLInputElement;
|
||||||
|
|
|
@ -4,6 +4,10 @@ penpot.ui.open('Icons plugin', 'http://localhost:4202', {
|
||||||
});
|
});
|
||||||
|
|
||||||
penpot.ui.onMessage<{ content: string; name: string }>((message) => {
|
penpot.ui.onMessage<{ content: string; name: string }>((message) => {
|
||||||
|
if (!message.content || !message.name) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const svgIcon = message.content;
|
const svgIcon = message.content;
|
||||||
const iconName = message.name;
|
const iconName = message.name;
|
||||||
const icon = penpot.createShapeFromSvg(svgIcon);
|
const icon = penpot.createShapeFromSvg(svgIcon);
|
||||||
|
|
Loading…
Add table
Reference in a new issue