refactor(covers): consolidate cover images into app-cover (#1928)

This commit is contained in:
Alex
2026-07-09 10:13:37 +01:00
committed by GitHub
parent 0d287a0291
commit b4570e3366
54 changed files with 354 additions and 593 deletions
@@ -1,67 +0,0 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {CoverPlaceholderComponent, coverColorFor} from './cover-generator.component';
describe('CoverPlaceholderComponent', () => {
let fixture: ComponentFixture<CoverPlaceholderComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CoverPlaceholderComponent],
}).compileComponents();
fixture = TestBed.createComponent(CoverPlaceholderComponent);
});
afterEach(() => {
TestBed.resetTestingModule();
});
it('renders the title and author text', () => {
fixture.componentRef.setInput('title', 'Dune');
fixture.componentRef.setInput('author', 'Frank Herbert');
fixture.detectChanges();
const el: HTMLElement = fixture.nativeElement;
expect(el.textContent).toContain('Dune');
expect(el.textContent).toContain('Frank Herbert');
});
it('applies the computed background color', () => {
fixture.componentRef.setInput('title', 'Dune');
fixture.componentRef.setInput('author', 'Frank Herbert');
fixture.detectChanges();
const container = fixture.nativeElement.querySelector('.placeholder');
expect(container.style.background).toBeTruthy();
});
it('applies the correct size class', () => {
fixture.componentRef.setInput('title', 'Test');
fixture.componentRef.setInput('size', 'lg');
fixture.detectChanges();
const container = fixture.nativeElement.querySelector('.placeholder');
expect(container.classList.contains('size-lg')).toBe(true);
});
});
describe('coverColorFor', () => {
it('returns a deterministic color for the same inputs', () => {
const first = coverColorFor('Dune', 'Frank Herbert');
const second = coverColorFor('Dune', 'Frank Herbert');
expect(first).toBe(second);
});
it('returns different colors for different inputs', () => {
const a = coverColorFor('Dune', 'Frank Herbert');
const b = coverColorFor('Project Hail Mary', 'Andy Weir');
expect(a).not.toBe(b);
});
it('returns a valid hex color', () => {
const color = coverColorFor('Test', 'Author');
expect(color).toMatch(/^#[0-9A-Fa-f]{6}$/);
});
});
@@ -1,112 +0,0 @@
import {ChangeDetectionStrategy, Component, computed, input} from '@angular/core';
const COVER_COLORS = [
'#1a1a2e', '#2d3436', '#0c3547', '#1e3d59', '#2c2c54', '#1b262c',
'#2B2D42', '#3D405B', '#463F3A', '#1B2838', '#2E4057', '#4A3728',
];
function hashString(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash = hash & hash;
}
return Math.abs(hash);
}
export function coverColorFor(title: string, author: string): string {
return COVER_COLORS[hashString(title + author) % COVER_COLORS.length];
}
export type CoverPlaceholderSize = 'sm' | 'md' | 'lg';
@Component({
selector: 'app-cover-placeholder',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div
class="placeholder"
[class.size-sm]="size() === 'sm'"
[class.size-md]="size() === 'md'"
[class.size-lg]="size() === 'lg'"
[style.background]="color()"
>
<div class="placeholder-title">{{ title() }}</div>
<div class="placeholder-author">{{ author() }}</div>
</div>
`,
styles: [`
:host {
display: block;
width: 100%;
height: 100%;
container-type: inline-size;
}
.placeholder {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
border-radius: inherit;
overflow: hidden;
}
.placeholder.size-sm { padding: min(0.4375rem, 4cqi); }
.placeholder.size-md { padding: min(0.875rem, 6cqi); }
.placeholder.size-lg { padding: min(1.3125rem, 8cqi); }
.placeholder-title {
color: rgba(255, 255, 255, 0.9);
font-weight: 600;
line-height: 1.375;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
word-break: break-word;
max-width: 100%;
}
.size-sm .placeholder-title {
font-size: min(11px, 10cqi);
margin-bottom: 2px;
}
.size-md .placeholder-title {
font-size: min(13px, 10cqi);
margin-bottom: 4px;
}
.size-lg .placeholder-title {
font-size: min(15px, 10cqi);
margin-bottom: 8px;
}
.placeholder-author {
color: rgba(255, 255, 255, 0.5);
letter-spacing: 0.025em;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
word-break: break-word;
max-width: 100%;
}
.size-sm .placeholder-author { font-size: min(9px, 8cqi); }
.size-md .placeholder-author { font-size: min(11px, 8cqi); }
.size-lg .placeholder-author { font-size: min(12px, 8cqi); }
`]
})
export class CoverPlaceholderComponent {
title = input('');
author = input('');
size = input<CoverPlaceholderSize>('md');
protected color = computed(() => coverColorFor(this.title(), this.author()));
}
@@ -0,0 +1,34 @@
@if (showImage() && preview()) {
<p-image
styleClass="block h-full w-full"
[imageClass]="imageClass()"
[src]="src() ?? ''"
[alt]="alt()"
[loading]="loading()"
appendTo="body"
[preview]="true"
(onImageError)="onError()"
/>
} @else if (showImage()) {
<img
[class]="imageClass()"
[src]="src() ?? ''"
[alt]="alt()"
[attr.loading]="loading()"
decoding="async"
(error)="onError()"
/>
} @else {
<div
class="placeholder group flex aspect-[5/7] h-full w-full flex-col items-center justify-center gap-1 overflow-hidden rounded-[inherit] p-1 text-center data-[size=md]:p-2 data-[size=lg]:gap-2 data-[size=lg]:p-4"
[attr.data-size]="size()"
[style.background]="color()"
>
<div class="placeholder-title line-clamp-3 max-w-full overflow-hidden break-words text-xs font-semibold leading-snug text-white/90 group-data-[size=md]:text-sm group-data-[size=lg]:text-base">
{{ title() }}
</div>
<div class="placeholder-author line-clamp-1 max-w-full overflow-hidden break-words text-xs text-white/55 group-data-[size=lg]:text-sm">
{{ authorsLabel() }}
</div>
</div>
}
@@ -0,0 +1,81 @@
import {ChangeDetectionStrategy, Component, computed, DestroyRef, inject, input, signal, viewChild} from '@angular/core';
import {Image} from 'primeng/image';
const COVER_COLORS = [
'#1a1a2e', '#2d3436', '#0c3547', '#1e3d59', '#2c2c54', '#1b262c',
'#2B2D42', '#3D405B', '#463F3A', '#1B2838', '#2E4057', '#4A3728',
];
function hashString(str: string): number {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = 31 * hash + str.charCodeAt(i);
hash |= 0;
}
return hash;
}
function coverColorFor(title: string, author: string): string {
return COVER_COLORS[Math.abs(hashString(title + author) % COVER_COLORS.length)];
}
type CoverSize = 'sm' | 'md' | 'lg';
type CoverFit = 'cover' | 'contain';
type CoverAuthors = string | string[];
@Component({
selector: 'app-cover',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [Image],
templateUrl: './cover.component.html',
host: {
class: 'block w-full',
'[class.h-full]': '!natural()',
'[class.h-auto]': 'natural()',
'(window:popstate)': 'closePreview()',
},
})
export class CoverComponent {
readonly src = input<string | null | undefined>(null);
readonly title = input<string | null | undefined>('');
readonly authors = input<CoverAuthors | null | undefined>([]);
readonly size = input<CoverSize>('md');
readonly fit = input<CoverFit>('cover');
readonly loading = input<'eager' | 'lazy'>('eager');
readonly alt = input('');
readonly natural = input(false);
readonly preview = input(false);
private readonly previewImage = viewChild(Image);
private readonly failedSrc = signal<string | null | undefined>(null);
protected readonly authorsLabel = computed(() => {
const authors = this.authors();
return Array.isArray(authors) ? authors.join(', ') : authors ?? '';
});
protected readonly color = computed(() => coverColorFor(this.title() ?? '', this.authorsLabel()));
protected readonly imageClass = computed(() => [
'cover-img block w-full rounded-[inherit]',
this.natural() ? 'h-auto' : 'h-full',
this.fit() === 'cover' ? 'object-cover' : 'object-contain',
].join(' '));
protected readonly showImage = computed(() => !!this.src() && this.failedSrc() !== this.src());
constructor() {
inject(DestroyRef).onDestroy(() => this.closePreview());
}
protected closePreview(): void {
if (!this.preview()) {
return;
}
this.previewImage()?.closePreview();
}
protected onError(): void {
this.failedSrc.set(this.src());
}
}