feat(icons): Made icons for libraries and shelves optional with no default icons. (#2599)

* feat(icons): Made icons for libraries and shelves optional with no default icon displayed.

* Added tests for making sure the API handles nullable icons for Shelves and Libraries.

* Fixed some issues identified during PR review.

* Rebased on develop.
This commit is contained in:
Giroux Arthur
2026-02-07 08:25:34 -07:00
committed by GitHub
parent 07cbf89e4c
commit 14236299f2
28 changed files with 817 additions and 90 deletions
@@ -45,17 +45,18 @@
/>
</div>
<label [for]="'shelf-' + shelf.id" class="shelf-label">
<div class="shelf-icon-wrapper">
@if (shelf.icon) {
@if (shelf.icon) {
<div class="shelf-icon-wrapper">
<app-icon-display
[icon]="getShelfIcon(shelf)"
size="20px"
iconClass="shelf-icon"
/>
} @else {
<i class="pi pi-bookmark shelf-icon"></i>
}
</div>
</div>
} @else {
<div class="shelf-icon-wrapper-empty">
</div>
}
<span class="shelf-name">{{ shelf.name }}</span>
</label>
</div>
@@ -153,12 +153,15 @@
cursor: pointer;
min-width: 0;
.shelf-icon-wrapper, .shelf-icon-wrapper-empty {
width: 34px;
height: 34px;
}
.shelf-icon-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 34px;
height: 34px;
background: var(--overlay-background);
border-radius: 8px;
flex-shrink: 0;
@@ -187,7 +190,7 @@
.shelf-label {
gap: 0.5rem;
.shelf-icon-wrapper {
.shelf-icon-wrapper, .shelf-icon-wrapper-empty {
width: 32px;
height: 32px;
@@ -123,10 +123,10 @@ export class ShelfAssignerComponent implements OnInit {
}
getShelfIcon(shelf: Shelf): IconSelection {
if (shelf.iconType === 'PRIME_NG') {
return {type: 'PRIME_NG', value: `pi pi-${shelf.icon}`};
if (shelf.iconType === 'CUSTOM_SVG') {
return {type: 'CUSTOM_SVG', value: shelf.icon ?? ""};
} else {
return {type: 'CUSTOM_SVG', value: shelf.icon};
return {type: 'PRIME_NG', value: `pi pi-${shelf.icon}`};
}
}
}
@@ -55,8 +55,8 @@ export class ShelfCreatorComponent {
}
createShelf(): void {
const iconValue = this.selectedIcon?.value || 'bookmark';
const iconType = this.selectedIcon?.type || 'PRIME_NG';
const iconValue = this.selectedIcon?.value ?? null;
const iconType = this.selectedIcon?.type ?? null;
const newShelf: Partial<Shelf> = {
name: this.shelfName,
@@ -47,10 +47,12 @@ export class ShelfEditDialogComponent implements OnInit {
if (this.shelf) {
this.shelfName = this.shelf.name;
this.isPublic = this.shelf.publicShelf ?? false;
if (this.shelf.iconType === 'PRIME_NG') {
this.selectedIcon = {type: 'PRIME_NG', value: `pi pi-${this.shelf.icon}`};
} else {
this.selectedIcon = {type: 'CUSTOM_SVG', value: this.shelf.icon};
if (this.shelf.iconType && this.shelf.icon) {
if (this.shelf.iconType === 'PRIME_NG') {
this.selectedIcon = {type: 'PRIME_NG', value: `pi pi-${this.shelf.icon}`};
} else {
this.selectedIcon = {type: 'CUSTOM_SVG', value: this.shelf.icon};
}
}
}
}
@@ -68,8 +70,8 @@ export class ShelfEditDialogComponent implements OnInit {
}
save() {
const iconValue = this.selectedIcon?.value || 'bookmark';
const iconType = this.selectedIcon?.type || 'PRIME_NG';
const iconValue = this.selectedIcon?.value ?? null;
const iconType = this.selectedIcon?.type ?? null;
const shelf: Shelf = {
name: this.shelfName,
@@ -4,8 +4,8 @@ import {BookType} from './book.model';
export interface Library {
id?: number;
name: string;
icon: string;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
icon?: string | null;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG' | null;
watch: boolean;
fileNamingPattern?: string;
sort?: SortOption;
@@ -3,8 +3,8 @@ import {SortOption} from './sort.model';
export interface Shelf {
id?: number;
name: string;
icon: string;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
icon?: string | null;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG' | null;
sort?: SortOption;
publicShelf?: boolean;
userId?: number;
@@ -45,7 +45,7 @@
<div class="form-field">
<label>
Icon <span class="required">*</span>
Icon (Optional)
</label>
@if (!selectedIcon) {
<button class="icon-picker-trigger" (click)="openIconPicker()" type="button">
@@ -70,11 +70,13 @@ export class LibraryCreatorComponent implements OnInit {
this.chosenLibraryName = name;
this.editModeLibraryName = name;
if (iconType === 'CUSTOM_SVG') {
this.selectedIcon = {type: 'CUSTOM_SVG', value: icon};
} else {
const value = icon.slice(0, 6) === 'pi pi-' ? icon : `pi pi-${icon}`;
this.selectedIcon = {type: 'PRIME_NG', value: value};
if (icon != null && iconType) {
if (iconType === 'CUSTOM_SVG') {
this.selectedIcon = {type: 'CUSTOM_SVG', value: icon};
} else {
const value = icon.slice(0, 6) === 'pi pi-' ? icon : `pi pi-${icon}`;
this.selectedIcon = {type: 'PRIME_NG', value: value};
}
}
this.watch = watch;
@@ -186,7 +188,7 @@ export class LibraryCreatorComponent implements OnInit {
}
isLibraryDetailsValid(): boolean {
return !!this.chosenLibraryName.trim() && !!this.selectedIcon;
return !!this.chosenLibraryName.trim();
}
isDirectorySelectionValid(): boolean {
@@ -207,8 +209,8 @@ export class LibraryCreatorComponent implements OnInit {
}
}
const iconValue = this.selectedIcon?.value || 'heart';
const iconType = this.selectedIcon?.type || 'PRIME_NG';
const iconValue = this.selectedIcon?.value ?? null;
const iconType = this.selectedIcon?.type ?? null;
const library: Library = {
name: this.chosenLibraryName,
@@ -29,22 +29,36 @@
<div class="icon-picker-section">
<div class="icon-picker-wrapper">
<label>Shelf Icon</label>
<div class="icon-display-container" (click)="openIconPicker()">
@if (selectedIcon) {
<app-icon-display
[icon]="selectedIcon"
[size]="'16px'"
iconClass="selected-icon"
alt="Selected shelf icon">
</app-icon-display>
<span class="icon-label">Click to change</span>
} @else {
@if (selectedIcon) {
<div class="selected-icon-display">
<div class="icon-display-container" (click)="openIconPicker()" (keydown.enter)="openIconPicker()" tabindex="0" role="button">
<app-icon-display
[icon]="selectedIcon"
[size]="'16px'"
iconClass="selected-icon"
alt="Selected shelf icon">
</app-icon-display>
<span class="icon-label">Click to change</span>
</div>
<p-button
icon="pi pi-times"
severity="danger"
[outlined]="true"
[rounded]="true"
size="small"
(onClick)="clearSelectedIcon()"
pTooltip="Remove icon"
tooltipPosition="left"
/>
</div>
} @else {
<div class="icon-display-container" (click)="openIconPicker()" (keydown.enter)="openIconPicker()" tabindex="0" role="button">
<div class="icon-placeholder">
<i class="pi pi-plus"></i>
<span>Select Icon</span>
</div>
}
</div>
</div>
}
</div>
</div>
@if (isAdmin) {
@@ -74,6 +74,12 @@
font-size: 0.9rem;
}
.selected-icon-display {
display: flex;
align-items: center;
gap: 0.5rem;
}
.icon-display-container {
display: flex;
align-items: center;
@@ -89,6 +95,11 @@
border-color: var(--primary-color);
}
&:focus {
outline: 2px solid var(--primary-color);
outline-offset: 2px;
}
.selected-icon {
flex-shrink: 0;
}
@@ -19,6 +19,7 @@ import {IconPickerService, IconSelection} from '../../../shared/service/icon-pic
import {CheckboxChangeEvent, CheckboxModule} from "primeng/checkbox";
import {UserService} from "../../settings/user-management/user.service";
import {IconDisplayComponent} from '../../../shared/components/icon-display/icon-display.component';
import {Tooltip} from 'primeng/tooltip';
import {BookService} from '../../book/service/book.service';
import {ShelfService} from '../../book/service/shelf.service';
import {Shelf} from '../../book/model/shelf.model';
@@ -167,7 +168,8 @@ const FIELD_CONFIGS: Record<RuleField, FullFieldConfig> = {
MultiSelect,
AutoComplete,
CheckboxModule,
IconDisplayComponent
IconDisplayComponent,
Tooltip
]
})
export class MagicShelfComponent implements OnInit {
@@ -271,7 +273,7 @@ export class MagicShelfComponent implements OnInit {
this.form = new FormGroup({
name: new FormControl<string | null>(data?.name ?? null, {nonNullable: true, validators: [Validators.required]}),
icon: new FormControl<string | null>(iconValue, {nonNullable: true, validators: [Validators.required]}),
icon: new FormControl<string | null>(iconValue),
isPublic: new FormControl<boolean>(data?.isPublic ?? false),
group: data?.filterJson ? this.buildGroupFromData(JSON.parse(data.filterJson)) : this.createGroup()
});
@@ -285,7 +287,7 @@ export class MagicShelfComponent implements OnInit {
} else {
this.form = new FormGroup({
name: new FormControl<string | null>(null, {nonNullable: true, validators: [Validators.required]}),
icon: new FormControl<string | null>(null, {nonNullable: true, validators: [Validators.required]}),
icon: new FormControl<string | null>(null),
isPublic: new FormControl<boolean>(false),
group: this.createGroup()
});
@@ -477,6 +479,11 @@ export class MagicShelfComponent implements OnInit {
});
}
clearSelectedIcon(): void {
this.selectedIcon = null;
this.form.get('icon')?.setValue(null);
}
private hasAtLeastOneValidRule(group: GroupFormGroup): boolean {
const rulesArray = group.get('rules') as FormArray;
@@ -11,8 +11,8 @@ import {GroupRule} from '../component/magic-shelf-component';
export interface MagicShelf {
id?: number | null;
name: string;
icon?: string;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
icon?: string | null;
iconType?: 'PRIME_NG' | 'CUSTOM_SVG' | null;
filterJson: string;
isPublic?: boolean;
}
@@ -98,7 +98,7 @@ export class MagicShelfService {
const payload: MagicShelf = {
id: data.id,
name: data.name ?? '',
icon: data.icon ?? 'pi pi-book',
icon: data.icon,
iconType: data.iconType,
filterJson: JSON.stringify(data.group),
isPublic: data.isPublic ?? false
@@ -21,6 +21,10 @@ import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
[ngStyle]="getSvgStyle()"
></div>
}
} @else if (displayEmpty) {
<div
[ngStyle]="getEmptyIconStyle()"
></div>
}
`,
styles: [`
@@ -50,6 +54,7 @@ export class IconDisplayComponent implements OnInit, OnChanges {
@Input() iconStyle: Record<string, string> = {};
@Input() size: string = '16px';
@Input() alt: string = 'Icon';
@Input() displayEmpty: boolean = false;
private iconCache = inject(IconCacheService);
private iconService = inject(IconService);
@@ -115,6 +120,13 @@ export class IconDisplayComponent implements OnInit, OnChanges {
};
}
getEmptyIconStyle(): Record<string, string> {
return {
width: this.size,
height: this.size,
};
}
getPrimeNgStyle(): Record<string, string> {
const fontSize = this.size.endsWith('px')
? `${parseInt(this.size) * 0.85}px`
@@ -109,8 +109,8 @@ export class AppMenuComponent implements OnInit {
menu: this.libraryShelfMenuService.initializeLibraryMenuItems(library),
label: library.name,
type: 'Library',
icon: library.icon,
iconType: (library.iconType || 'PRIME_NG') as 'PRIME_NG' | 'CUSTOM_SVG',
icon: library.icon || undefined,
iconType: (library.iconType || undefined) as 'PRIME_NG' | 'CUSTOM_SVG' | undefined,
routerLink: [`/library/${library.id}/books`],
bookCount$: this.libraryService.getBookCount(library.id ?? 0),
})),
@@ -132,8 +132,8 @@ export class AppMenuComponent implements OnInit {
items: sortedShelves.map((shelf) => ({
label: shelf.name,
type: 'magicShelfItem',
icon: shelf.icon || 'pi pi-book',
iconType: (shelf.iconType || 'PRIME_NG') as 'PRIME_NG' | 'CUSTOM_SVG',
icon: shelf.icon || undefined,
iconType: (shelf.iconType || undefined) as 'PRIME_NG' | 'CUSTOM_SVG' | undefined,
menu: this.libraryShelfMenuService.initializeMagicShelfMenuItems(shelf),
routerLink: [`/magic-shelf/${shelf.id}/books`],
bookCount$: this.magicShelfService.getBookCount(shelf.id ?? 0),
@@ -158,8 +158,8 @@ export class AppMenuComponent implements OnInit {
menu: this.libraryShelfMenuService.initializeShelfMenuItems(shelf),
label: shelf.name,
type: 'Shelf',
icon: shelf.icon,
iconType: (shelf.iconType || 'PRIME_NG') as 'PRIME_NG' | 'CUSTOM_SVG',
icon: shelf.icon || undefined,
iconType: (shelf.iconType || undefined) as 'PRIME_NG' | 'CUSTOM_SVG' | undefined,
routerLink: [`/shelf/${shelf.id}/books`],
bookCount$: this.shelfService.getBookCount(shelf.id ?? 0),
}));
@@ -173,13 +173,13 @@ export class AppMenuComponent implements OnInit {
bookCount$: this.shelfService.getUnshelvedBookCount?.() ?? of(0),
};
const items = [unshelvedItem];
const items: MenuItem[] = [unshelvedItem];
if (koboShelf) {
items.push({
label: koboShelf.name,
type: 'Shelf',
icon: koboShelf.icon,
iconType: (koboShelf.iconType || 'PRIME_NG') as 'PRIME_NG' | 'CUSTOM_SVG',
icon: koboShelf.icon || undefined,
iconType: (koboShelf.iconType || undefined) as 'PRIME_NG' | 'CUSTOM_SVG' | undefined,
routerLink: [`/shelf/${koboShelf.id}/books`],
bookCount$: this.shelfService.getBookCount(koboShelf.id ?? 0),
});
@@ -57,6 +57,7 @@
[icon]="getIconSelection()"
iconClass="layout-menuitem-icon"
size="18px"
[displayEmpty]="true"
></app-icon-display>
<span class="layout-menuitem-text menu-item-text">
{{ item.label }}