refactor(ui): unify author/series/book browsers using tanstack virtual (#941)
This commit is contained in:
+28
-39
@@ -54,17 +54,7 @@
|
||||
<p-popover #displaySettingsPopover>
|
||||
<div class="display-settings-content">
|
||||
<span class="display-settings-label">{{ t('cardSize') }}</span>
|
||||
<p-slider
|
||||
[ngModel]="authorScaleService.scaleFactor()"
|
||||
(ngModelChange)="authorScaleService.setScale($event)"
|
||||
[min]="0.7"
|
||||
[max]="1.3"
|
||||
[step]="0.01"
|
||||
[style]="{ width: '100%' }">
|
||||
</p-slider>
|
||||
<div class="scale-value-display">
|
||||
{{ authorScaleService.scaleFactor().toFixed(2) }}x
|
||||
</div>
|
||||
<app-grid-density-buttons (densityChange)="adjustDesktopGridDensity($event)"/>
|
||||
</div>
|
||||
</p-popover>
|
||||
}
|
||||
@@ -193,36 +183,35 @@
|
||||
<p>{{ t('noAuthorsFound') }}</p>
|
||||
</div>
|
||||
} @else {
|
||||
<cdk-virtual-scroll-viewport autosize
|
||||
[minBufferPx]="600" [maxBufferPx]="1200"
|
||||
#scroll
|
||||
class="virtual-scroller">
|
||||
<div class="author-grid"
|
||||
*cdkVirtualFor="let row of authorRows(); let rowIndex = index"
|
||||
[ngStyle]="{'grid-template-columns': 'repeat(' + gridColumns() + ', 1fr)'}">
|
||||
@for (author of row; track author.id; let colIdx = $index) {
|
||||
<div class="virtual-scroller-item"
|
||||
[ngStyle]="{width: cardWidth() + 'px', height: cardHeight() + 'px'}"
|
||||
[style.--card-scale]="authorScaleService.scaleFactor()">
|
||||
<app-author-card
|
||||
[author]="author"
|
||||
[index]="rowIndex * gridColumns() + colIdx"
|
||||
[cacheBuster]="thumbnailCacheBusters.get(author.id) || 0"
|
||||
[canQuickMatch]="canEditMetadata"
|
||||
[canDelete]="canDeleteBook"
|
||||
[isCheckboxEnabled]="canEditMetadata || canDeleteBook"
|
||||
[isSelected]="isAuthorSelected(author.id)"
|
||||
(cardClick)="navigateToAuthor($event)"
|
||||
(viewAuthor)="navigateToAuthor($event)"
|
||||
(editAuthor)="navigateToAuthorEdit($event)"
|
||||
(deleteAuthor)="deleteAuthor($event)"
|
||||
(quickMatched)="onAuthorQuickMatched($event)"
|
||||
(checkboxClick)="onCheckboxClicked($event)">
|
||||
</app-author-card>
|
||||
</div>
|
||||
<div #scrollElement class="virtual-scroller">
|
||||
<div class="virtual-content" [style.height.px]="virtualGrid.virtualizer.getTotalSize()">
|
||||
@for (virtualItem of virtualGrid.virtualizer.getVirtualItems(); track virtualItem.key) {
|
||||
@if (authorList[virtualItem.index]; as author) {
|
||||
<div
|
||||
class="virtual-cell"
|
||||
[style.width.px]="virtualGrid.itemWidth()"
|
||||
[style.height.px]="virtualItem.size"
|
||||
[style.transform]="virtualGrid.itemTransform(virtualItem)">
|
||||
<app-author-card
|
||||
[author]="author"
|
||||
[index]="virtualItem.index"
|
||||
[cacheBuster]="thumbnailCacheBusters.get(author.id) || 0"
|
||||
[canQuickMatch]="canEditMetadata"
|
||||
[canDelete]="canDeleteBook"
|
||||
[isCheckboxEnabled]="canEditMetadata || canDeleteBook"
|
||||
[isSelected]="isAuthorSelected(author.id)"
|
||||
(cardClick)="navigateToAuthor($event)"
|
||||
(viewAuthor)="navigateToAuthor($event)"
|
||||
(editAuthor)="navigateToAuthorEdit($event)"
|
||||
(deleteAuthor)="deleteAuthor($event)"
|
||||
(quickMatched)="onAuthorQuickMatched($event)"
|
||||
(checkboxClick)="onCheckboxClicked($event)">
|
||||
</app-author-card>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</cdk-virtual-scroll-viewport>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-22
@@ -107,12 +107,6 @@
|
||||
color: #e2e8f0;
|
||||
}
|
||||
|
||||
.scale-value-display {
|
||||
text-align: center;
|
||||
font-size: 0.8rem;
|
||||
color: #94a3b8;
|
||||
}
|
||||
|
||||
.filter-popover-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -164,22 +158,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.author-grid {
|
||||
display: grid;
|
||||
gap: 1.25rem;
|
||||
place-items: start center;
|
||||
width: 100%;
|
||||
padding-top: 1rem;
|
||||
padding-right: 1rem;
|
||||
padding-left: 1rem;
|
||||
}
|
||||
|
||||
.virtual-scroller-item {
|
||||
position: relative;
|
||||
overflow: visible;
|
||||
transition: width 0.2s ease, height 0.2s ease;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
+63
-86
@@ -1,33 +1,28 @@
|
||||
import {Component, computed, DestroyRef, effect, HostListener, inject, OnInit, signal, ViewChild} from '@angular/core';
|
||||
import {computeGridColumns} from '../../../../shared/util/viewport.util';
|
||||
import {NgStyle} from '@angular/common';
|
||||
import {Component, computed, DestroyRef, effect, ElementRef, HostListener, inject, OnInit, signal, viewChild} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {filter} from 'rxjs/operators';
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Select} from 'primeng/select';
|
||||
import {Slider} from 'primeng/slider';
|
||||
import {Popover} from 'primeng/popover';
|
||||
import {Button} from 'primeng/button';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
import {CdkVirtualScrollViewport, CdkVirtualForOf} from '@angular/cdk/scrolling';
|
||||
import {CdkAutoSizeVirtualScroll} from '@angular/cdk-experimental/scrolling';
|
||||
import {BookBrowserScrollService} from '../../../book/components/book-browser/book-browser-scroll.service';
|
||||
import {RouteScrollPositionService} from '../../../../shared/service/route-scroll-position.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {AuthorService} from '../../service/author.service';
|
||||
import {AuthorSummary, EnrichedAuthor, AuthorFilters, DEFAULT_AUTHOR_FILTERS} from '../../model/author.model';
|
||||
import {AuthorCardComponent} from '../author-card/author-card.component';
|
||||
import {chunk} from '../../../../shared/util/array.util';
|
||||
import {AuthorScalePreferenceService} from '../../service/author-scale-preference.service';
|
||||
import {AuthorSelectionService, AuthorCheckboxClickEvent} from '../../service/author-selection.service';
|
||||
import {PageTitleService} from '../../../../shared/service/page-title.service';
|
||||
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {Book, ReadStatus} from '../../../book/model/book.model';
|
||||
import {createVirtualGrid, scaleForGridColumns} from '../../../../shared/util/virtual-grid.util';
|
||||
import {GridDensityButtonsComponent} from '../../../../shared/components/grid-density-buttons/grid-density-buttons.component';
|
||||
import {LocalStorageService} from '../../../../shared/service/local-storage.service';
|
||||
import {ScalePreference} from '../../../../shared/util/scale-preference.util';
|
||||
|
||||
type SortDirection = 'asc' | 'desc';
|
||||
|
||||
@@ -59,21 +54,17 @@ const DEFAULT_SORT_DIRECTIONS: Record<string, SortDirection> = {
|
||||
templateUrl: './author-browser.component.html',
|
||||
styleUrls: ['./author-browser.component.scss'],
|
||||
imports: [
|
||||
NgStyle,
|
||||
FormsModule,
|
||||
ProgressSpinner,
|
||||
InputText,
|
||||
Select,
|
||||
Slider,
|
||||
Popover,
|
||||
Button,
|
||||
Divider,
|
||||
Tooltip,
|
||||
TranslocoDirective,
|
||||
GridDensityButtonsComponent,
|
||||
AuthorCardComponent,
|
||||
CdkVirtualScrollViewport,
|
||||
CdkVirtualForOf,
|
||||
CdkAutoSizeVirtualScroll,
|
||||
]
|
||||
})
|
||||
export class AuthorBrowserComponent implements OnInit {
|
||||
@@ -82,39 +73,32 @@ export class AuthorBrowserComponent implements OnInit {
|
||||
private static readonly BASE_HEIGHT = 290;
|
||||
private static readonly MOBILE_BASE_WIDTH = 140;
|
||||
private static readonly MOBILE_BASE_HEIGHT = 250;
|
||||
private static readonly SCALE_STORAGE_KEY = 'authorScalePreference';
|
||||
private static readonly MIN_SCALE = 0.7;
|
||||
private static readonly MAX_SCALE = 1.3;
|
||||
|
||||
private authorService = inject(AuthorService);
|
||||
private bookService = inject(BookService);
|
||||
private messageService = inject(MessageService);
|
||||
private pageTitle = inject(PageTitleService);
|
||||
private scrollService = inject(BookBrowserScrollService);
|
||||
private scrollService = inject(RouteScrollPositionService);
|
||||
private t = inject(TranslocoService);
|
||||
private router = inject(Router);
|
||||
private activatedRoute = inject(ActivatedRoute);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
private localStorageService = inject(LocalStorageService);
|
||||
protected userService = inject(UserService);
|
||||
protected authorScaleService = inject(AuthorScalePreferenceService);
|
||||
protected selectionService = inject(AuthorSelectionService);
|
||||
|
||||
virtualScroller: CdkVirtualScrollViewport | undefined;
|
||||
|
||||
@ViewChild('scroll')
|
||||
set scrollViewport(vp: CdkVirtualScrollViewport | undefined) {
|
||||
this.virtualScroller = vp;
|
||||
this.viewportResizeObserver?.disconnect();
|
||||
if (vp) {
|
||||
const el = vp.elementRef.nativeElement as HTMLElement;
|
||||
this.viewportWidth.set(el.clientWidth);
|
||||
this.viewportResizeObserver = new ResizeObserver(entries => {
|
||||
this.viewportWidth.set(entries[0]?.contentRect.width ?? el.clientWidth);
|
||||
});
|
||||
this.viewportResizeObserver.observe(el);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly GRID_GAP = 20;
|
||||
private readonly viewportWidth = signal(0);
|
||||
private viewportResizeObserver: ResizeObserver | undefined;
|
||||
private readonly scrollElement = viewChild<ElementRef<HTMLElement>>('scrollElement');
|
||||
private readonly initialScrollOffset = () => this.scrollService.getPosition(this.scrollService.keyFor(this.activatedRoute)) ?? 0;
|
||||
private readonly scalePreference = new ScalePreference(this.localStorageService, {
|
||||
storageKey: AuthorBrowserComponent.SCALE_STORAGE_KEY,
|
||||
minScale: AuthorBrowserComponent.MIN_SCALE,
|
||||
maxScale: AuthorBrowserComponent.MAX_SCALE,
|
||||
});
|
||||
private readonly scaleFactor = this.scalePreference.scaleFactor;
|
||||
|
||||
readonly screenWidth = signal(typeof window !== 'undefined' ? window.innerWidth : 1024);
|
||||
thumbnailCacheBusters = new Map<number, number>();
|
||||
@@ -204,29 +188,25 @@ export class AuthorBrowserComponent implements OnInit {
|
||||
}
|
||||
|
||||
readonly isMobile = computed(() => this.screenWidth() <= 767);
|
||||
|
||||
readonly cardWidth = computed(() => {
|
||||
const base = this.isMobile()
|
||||
? AuthorBrowserComponent.MOBILE_BASE_WIDTH
|
||||
: AuthorBrowserComponent.BASE_WIDTH;
|
||||
return Math.round(base * this.authorScaleService.scaleFactor());
|
||||
});
|
||||
|
||||
readonly cardHeight = computed(() => {
|
||||
const base = this.isMobile()
|
||||
private readonly baseCardWidth = computed(() => this.isMobile()
|
||||
? AuthorBrowserComponent.MOBILE_BASE_WIDTH
|
||||
: AuthorBrowserComponent.BASE_WIDTH
|
||||
);
|
||||
private readonly cardAspectRatio = computed(() => {
|
||||
const baseHeight = this.isMobile()
|
||||
? AuthorBrowserComponent.MOBILE_BASE_HEIGHT
|
||||
: AuthorBrowserComponent.BASE_HEIGHT;
|
||||
return Math.round(base * this.authorScaleService.scaleFactor());
|
||||
return baseHeight / this.baseCardWidth();
|
||||
});
|
||||
|
||||
readonly gridColumnMinWidth = computed(() => `${this.cardWidth()}px`);
|
||||
|
||||
readonly gridColumns = computed(() => {
|
||||
return computeGridColumns(this.viewportWidth(), this.cardWidth() || 165, AuthorBrowserComponent.GRID_GAP);
|
||||
});
|
||||
|
||||
readonly authorRows = computed(() => {
|
||||
return chunk(this.filteredAuthors(), this.gridColumns());
|
||||
private readonly minCardWidth = computed(() => Math.round(this.baseCardWidth() * this.scaleFactor()));
|
||||
readonly virtualGrid = createVirtualGrid({
|
||||
items: this.filteredAuthors,
|
||||
scrollElement: this.scrollElement,
|
||||
minItemWidth: this.minCardWidth,
|
||||
gap: AuthorBrowserComponent.GRID_GAP,
|
||||
initialOffset: this.initialScrollOffset,
|
||||
fillItemWidth: true,
|
||||
estimateItemHeight: itemWidth => Math.round(itemWidth * this.cardAspectRatio()),
|
||||
});
|
||||
|
||||
sortOptions: SortOption[] = [];
|
||||
@@ -238,7 +218,6 @@ export class AuthorBrowserComponent implements OnInit {
|
||||
|
||||
ngOnInit(): void {
|
||||
this.pageTitle.setPageTitle(this.t.translate('authorBrowser.pageTitle'));
|
||||
this.destroyRef.onDestroy(() => this.viewportResizeObserver?.disconnect());
|
||||
|
||||
this.sortOptions = [
|
||||
{label: this.t.translate('authorBrowser.sort.name'), value: 'name'},
|
||||
@@ -259,11 +238,36 @@ export class AuthorBrowserComponent implements OnInit {
|
||||
this.sortDirection.set(dirParam === 'asc' || dirParam === 'desc' ? dirParam : DEFAULT_SORT_DIRECTIONS[sortParam]);
|
||||
}
|
||||
|
||||
this.setupScrollPositionTracking();
|
||||
this.scrollService.trackRoute({
|
||||
scrollElement: this.scrollElement,
|
||||
route: this.activatedRoute,
|
||||
destroyRef: this.destroyRef,
|
||||
dismissOverlaysBeforeSave: true,
|
||||
});
|
||||
this.destroyRef.onDestroy(() => this.selectionService.deselectAll());
|
||||
}
|
||||
|
||||
adjustDesktopGridDensity(direction: 'smaller' | 'larger'): void {
|
||||
const currentColumns = this.virtualGrid.gridColumns();
|
||||
const columns = Math.max(1, direction === 'smaller'
|
||||
? currentColumns + 1
|
||||
: currentColumns - 1);
|
||||
const viewportWidth = this.virtualGrid.viewportWidth() || this.screenWidth();
|
||||
this.virtualGrid.updatePreservingScrollPosition(() => {
|
||||
this.setScale(scaleForGridColumns(
|
||||
viewportWidth,
|
||||
AuthorBrowserComponent.GRID_GAP,
|
||||
columns,
|
||||
this.baseCardWidth(),
|
||||
AuthorBrowserComponent.MIN_SCALE,
|
||||
AuthorBrowserComponent.MAX_SCALE
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
private setScale(scale: number): void {
|
||||
this.scalePreference.setScale(scale);
|
||||
}
|
||||
|
||||
onSearchChange(value: string): void {
|
||||
this.searchTerm.set(value);
|
||||
@@ -524,33 +528,6 @@ export class AuthorBrowserComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
private getScrollPositionKey(): string {
|
||||
const path = this.activatedRoute.snapshot.routeConfig?.path ?? '';
|
||||
return this.scrollService.createKey(path, this.activatedRoute.snapshot.params);
|
||||
}
|
||||
|
||||
private setupScrollPositionTracking(): void {
|
||||
this.router.events.pipe(
|
||||
filter(event => event instanceof NavigationStart),
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
).subscribe(() => {
|
||||
this.dismissBodyMenus();
|
||||
this.saveScrollPosition();
|
||||
});
|
||||
}
|
||||
|
||||
private dismissBodyMenus(): void {
|
||||
document.querySelectorAll('.p-tieredmenu-overlay').forEach(el => el.remove());
|
||||
}
|
||||
|
||||
private saveScrollPosition(): void {
|
||||
if (this.virtualScroller) {
|
||||
const key = this.getScrollPositionKey();
|
||||
const position = this.virtualScroller.measureScrollOffset('top') ?? 0;
|
||||
this.scrollService.savePosition(key, position);
|
||||
}
|
||||
}
|
||||
|
||||
private removeAuthorsFromList(ids: number[]): void {
|
||||
const idSet = new Set(ids);
|
||||
this.allAuthorsState.update(current => (current ?? []).filter(author => !idSet.has(author.id)));
|
||||
|
||||
+22
-14
@@ -104,22 +104,30 @@
|
||||
<p>{{ t('noBooksFound') }}</p>
|
||||
</div>
|
||||
} @else {
|
||||
<cdk-virtual-scroll-viewport autosize [minBufferPx]="600" [maxBufferPx]="1200" class="books-virtual-scroller">
|
||||
<div *cdkVirtualFor="let row of bookRows(); let rowIdx = index" class="books-grid"
|
||||
[ngStyle]="{'grid-template-columns': 'repeat(' + gridColumns() + ', 1fr)'}">
|
||||
@for (book of row; let j = $index; track book.id) {
|
||||
<div class="virtual-scroller-item"
|
||||
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
|
||||
<app-book-card
|
||||
[index]="rowIdx * gridColumns() + j"
|
||||
[book]="book"
|
||||
[isSeriesCollapsed]="false"
|
||||
[overlayPreferenceService]="bookCardOverlayPreferenceService">
|
||||
</app-book-card>
|
||||
</div>
|
||||
<div #scrollElement class="author-books-scroller virtual-scroller">
|
||||
<div class="virtual-content" [style.height.px]="virtualGrid.virtualizer.getTotalSize()">
|
||||
@for (virtualItem of virtualGrid.virtualizer.getVirtualItems(); track virtualItem.key) {
|
||||
@if (books[virtualItem.index]; as book) {
|
||||
<div
|
||||
class="virtual-cell"
|
||||
[style.width.px]="virtualGrid.itemWidth()"
|
||||
[style.height.px]="virtualItem.size"
|
||||
[style.transform]="virtualGrid.itemTransform(virtualItem)">
|
||||
<div class="virtual-scroller-item"
|
||||
[style.width.px]="currentCardSize.width"
|
||||
[style.height.px]="currentCardSize.height">
|
||||
<app-book-card
|
||||
[index]="virtualItem.index"
|
||||
[book]="book"
|
||||
[isSeriesCollapsed]="false"
|
||||
[overlayPreferenceService]="bookCardOverlayPreferenceService">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</cdk-virtual-scroll-viewport>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
+2
-13
@@ -157,10 +157,11 @@
|
||||
color: #e2e8f0;
|
||||
}
|
||||
|
||||
.books-virtual-scroller {
|
||||
.author-books-scroller {
|
||||
width: 100%;
|
||||
height: calc(100dvh - 22rem);
|
||||
box-sizing: border-box;
|
||||
overflow-y: auto;
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: var(--primary-color) rgb(20 20 30 / 50%);
|
||||
|
||||
@@ -174,18 +175,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.books-grid {
|
||||
display: grid;
|
||||
gap: 1.3rem;
|
||||
place-items: start center;
|
||||
width: 100%;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.virtual-scroller-item {
|
||||
position: relative;
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
.empty-books {
|
||||
display: flex;
|
||||
|
||||
+11
-20
@@ -1,4 +1,4 @@
|
||||
import {ElementRef, signal} from '@angular/core';
|
||||
import {signal} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {MessageService} from 'primeng/api';
|
||||
@@ -99,8 +99,7 @@ describe('AuthorDetailComponent', () => {
|
||||
{
|
||||
provide: CoverScalePreferenceService,
|
||||
useValue: {
|
||||
currentCardSize: vi.fn(() => 'medium'),
|
||||
gridColumnMinWidth: vi.fn(() => '12rem'),
|
||||
currentCardSize: vi.fn(() => ({width: 135, height: 220})),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -182,29 +181,21 @@ describe('AuthorDetailComponent', () => {
|
||||
expect(component.canEditMetadata).toBe(true);
|
||||
});
|
||||
|
||||
it('detects description overflow from the assigned element ref only while collapsed', () => {
|
||||
it('detects description overflow from element dimensions only while collapsed', () => {
|
||||
const component = createComponent();
|
||||
|
||||
component.descriptionContentRef = {
|
||||
nativeElement: {
|
||||
scrollHeight: 120,
|
||||
clientHeight: 60,
|
||||
},
|
||||
} as unknown as ElementRef<HTMLElement>;
|
||||
|
||||
component.ngAfterViewChecked();
|
||||
component.updateDescriptionOverflow({
|
||||
scrollHeight: 120,
|
||||
clientHeight: 60,
|
||||
});
|
||||
|
||||
expect(component.isOverflowing).toBe(true);
|
||||
|
||||
component.isExpanded = true;
|
||||
component.descriptionContentRef = {
|
||||
nativeElement: {
|
||||
scrollHeight: 20,
|
||||
clientHeight: 80,
|
||||
},
|
||||
} as unknown as ElementRef<HTMLElement>;
|
||||
|
||||
component.ngAfterViewChecked();
|
||||
component.updateDescriptionOverflow({
|
||||
scrollHeight: 20,
|
||||
clientHeight: 80,
|
||||
});
|
||||
|
||||
expect(component.isOverflowing).toBe(true);
|
||||
});
|
||||
|
||||
+18
-46
@@ -1,14 +1,11 @@
|
||||
import { AfterViewChecked, Component, computed, DestroyRef, ElementRef, inject, OnInit, signal, ViewChild } from '@angular/core';
|
||||
import { computeGridColumns } from '../../../../shared/util/viewport.util';
|
||||
import { AfterViewChecked, Component, computed, ElementRef, inject, OnInit, signal, viewChild } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { NgClass, NgStyle } from '@angular/common';
|
||||
import { NgClass } from '@angular/common';
|
||||
import { Tab, TabList, TabPanel, TabPanels, Tabs } from 'primeng/tabs';
|
||||
import { ProgressSpinner } from 'primeng/progressspinner';
|
||||
import { Button } from 'primeng/button';
|
||||
import { Tag } from 'primeng/tag';
|
||||
import { TranslocoDirective, TranslocoService } from '@jsverse/transloco';
|
||||
import { CdkVirtualScrollViewport, CdkVirtualForOf } from '@angular/cdk/scrolling';
|
||||
import { CdkAutoSizeVirtualScroll } from '@angular/cdk-experimental/scrolling';
|
||||
import { MessageService } from 'primeng/api';
|
||||
import { Tooltip } from 'primeng/tooltip';
|
||||
import { AuthorService } from '../../service/author.service';
|
||||
@@ -21,7 +18,7 @@ import { UserService } from '../../../settings/user-management/user.service';
|
||||
import { AuthorMatchComponent } from '../author-match/author-match.component';
|
||||
import { AuthorEditorComponent } from '../author-editor/author-editor.component';
|
||||
import { PageTitleService } from '../../../../shared/service/page-title.service';
|
||||
import { chunk } from '../../../../shared/util/array.util';
|
||||
import { createVirtualGrid } from '../../../../shared/util/virtual-grid.util';
|
||||
|
||||
@Component({
|
||||
selector: 'app-author-detail',
|
||||
@@ -30,7 +27,6 @@ import { chunk } from '../../../../shared/util/array.util';
|
||||
styleUrls: ['./author-detail.component.scss'],
|
||||
imports: [
|
||||
NgClass,
|
||||
NgStyle,
|
||||
Tabs,
|
||||
TabList,
|
||||
Tab,
|
||||
@@ -41,9 +37,6 @@ import { chunk } from '../../../../shared/util/array.util';
|
||||
Tag,
|
||||
TranslocoDirective,
|
||||
Tooltip,
|
||||
CdkVirtualScrollViewport,
|
||||
CdkVirtualForOf,
|
||||
CdkAutoSizeVirtualScroll,
|
||||
BookCardComponent,
|
||||
AuthorMatchComponent,
|
||||
AuthorEditorComponent
|
||||
@@ -63,27 +56,9 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
|
||||
protected userService = inject(UserService);
|
||||
private pageTitle = inject(PageTitleService);
|
||||
private t = inject(TranslocoService);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
|
||||
@ViewChild('descriptionContent') descriptionContentRef?: ElementRef<HTMLElement>;
|
||||
virtualScroller?: CdkVirtualScrollViewport;
|
||||
|
||||
@ViewChild(CdkVirtualScrollViewport)
|
||||
set scrollViewport(vp: CdkVirtualScrollViewport | undefined) {
|
||||
this.virtualScroller = vp;
|
||||
this.viewportResizeObserver?.disconnect();
|
||||
if (vp) {
|
||||
const el = vp.elementRef.nativeElement as HTMLElement;
|
||||
this.viewportWidth.set(el.clientWidth);
|
||||
this.viewportResizeObserver = new ResizeObserver(entries => {
|
||||
this.viewportWidth.set(entries[0]?.contentRect.width ?? el.clientWidth);
|
||||
});
|
||||
this.viewportResizeObserver.observe(el);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly viewportWidth = signal(0);
|
||||
private viewportResizeObserver: ResizeObserver | undefined;
|
||||
readonly descriptionContentRef = viewChild<ElementRef<HTMLElement>>('descriptionContent');
|
||||
private readonly scrollElement = viewChild<ElementRef<HTMLElement>>('scrollElement');
|
||||
|
||||
loading = signal(true);
|
||||
tab = 'books';
|
||||
@@ -109,16 +84,12 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
|
||||
return this.coverScalePreferenceService.currentCardSize();
|
||||
}
|
||||
|
||||
get gridColumnMinWidth(): string {
|
||||
return this.coverScalePreferenceService.gridColumnMinWidth();
|
||||
}
|
||||
|
||||
readonly gridColumns = computed(() => {
|
||||
return computeGridColumns(this.viewportWidth(), parseInt(this.gridColumnMinWidth, 10) || 180, AuthorDetailComponent.GRID_GAP);
|
||||
});
|
||||
|
||||
readonly bookRows = computed(() => {
|
||||
return chunk(this.authorBooks(), this.gridColumns());
|
||||
readonly virtualGrid = createVirtualGrid({
|
||||
items: this.authorBooks,
|
||||
scrollElement: this.scrollElement,
|
||||
minItemWidth: computed(() => this.currentCardSize.width),
|
||||
estimateItemHeight: () => this.currentCardSize.height,
|
||||
gap: AuthorDetailComponent.GRID_GAP,
|
||||
});
|
||||
|
||||
get photoUrl(): string {
|
||||
@@ -139,15 +110,16 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
|
||||
this.tab = tabParam;
|
||||
}
|
||||
this.loadAuthor(authorId);
|
||||
this.destroyRef.onDestroy(() => this.viewportResizeObserver?.disconnect());
|
||||
}
|
||||
|
||||
|
||||
|
||||
ngAfterViewChecked(): void {
|
||||
if (!this.isExpanded && this.descriptionContentRef) {
|
||||
const el = this.descriptionContentRef.nativeElement;
|
||||
this.isOverflowing = el.scrollHeight > el.clientHeight;
|
||||
const descriptionContent = this.descriptionContentRef();
|
||||
this.updateDescriptionOverflow(descriptionContent?.nativeElement);
|
||||
}
|
||||
|
||||
updateDescriptionOverflow(element?: Pick<HTMLElement, 'scrollHeight' | 'clientHeight'>): void {
|
||||
if (!this.isExpanded && element) {
|
||||
this.isOverflowing = element.scrollHeight > element.clientHeight;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-66
@@ -1,66 +0,0 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
import {LocalStorageService} from '../../../shared/service/local-storage.service';
|
||||
import {AuthorScalePreferenceService} from './author-scale-preference.service';
|
||||
|
||||
describe('AuthorScalePreferenceService', () => {
|
||||
const localStorageService = {
|
||||
get: vi.fn(),
|
||||
set: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.restoreAllMocks();
|
||||
localStorageService.get.mockReset();
|
||||
localStorageService.set.mockReset();
|
||||
localStorageService.get.mockReturnValue(1.5);
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AuthorScalePreferenceService,
|
||||
{provide: LocalStorageService, useValue: localStorageService},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('loads the persisted scale and does not persist unchanged values', () => {
|
||||
const service = TestBed.inject(AuthorScalePreferenceService);
|
||||
|
||||
expect(service.scaleFactor()).toBe(1.5);
|
||||
|
||||
service.setScale(1.5);
|
||||
vi.runAllTimers();
|
||||
|
||||
expect(localStorageService.set).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('debounces and persists the most recent scale value', () => {
|
||||
const service = TestBed.inject(AuthorScalePreferenceService);
|
||||
|
||||
service.setScale(1.2);
|
||||
service.setScale(1.4);
|
||||
|
||||
vi.advanceTimersByTime(999);
|
||||
expect(localStorageService.set).not.toHaveBeenCalled();
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
|
||||
expect(localStorageService.set).toHaveBeenCalledOnce();
|
||||
expect(localStorageService.set).toHaveBeenCalledWith('authorScalePreference', 1.4);
|
||||
});
|
||||
|
||||
it('falls back to the default scale when storage contains an invalid value', () => {
|
||||
localStorageService.get.mockReturnValue(Number.NaN);
|
||||
|
||||
const service = TestBed.inject(AuthorScalePreferenceService);
|
||||
|
||||
expect(service.scaleFactor()).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
import {inject, Injectable, signal} from '@angular/core';
|
||||
import {LocalStorageService} from '../../../shared/service/local-storage.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthorScalePreferenceService {
|
||||
|
||||
private readonly DEBOUNCE_MS = 1000;
|
||||
private readonly STORAGE_KEY = 'authorScalePreference';
|
||||
|
||||
private readonly localStorageService = inject(LocalStorageService);
|
||||
|
||||
private readonly _scaleFactor = signal(1.0);
|
||||
readonly scaleFactor = this._scaleFactor.asReadonly();
|
||||
|
||||
private saveTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
private lastPersistedScale = 1.0;
|
||||
|
||||
constructor() {
|
||||
const initialScale = this.loadScaleFromStorage();
|
||||
this._scaleFactor.set(initialScale);
|
||||
this.lastPersistedScale = initialScale;
|
||||
}
|
||||
|
||||
setScale(scale: number): void {
|
||||
this._scaleFactor.set(scale);
|
||||
if (scale === this.lastPersistedScale) {
|
||||
return;
|
||||
}
|
||||
this.scheduleSave(scale);
|
||||
}
|
||||
|
||||
private scheduleSave(scale: number): void {
|
||||
if (this.saveTimeout) {
|
||||
clearTimeout(this.saveTimeout);
|
||||
}
|
||||
|
||||
this.saveTimeout = setTimeout(() => {
|
||||
this.saveScalePreference(scale);
|
||||
}, this.DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
private saveScalePreference(scale: number): void {
|
||||
this.localStorageService.set(this.STORAGE_KEY, scale);
|
||||
this.lastPersistedScale = scale;
|
||||
}
|
||||
|
||||
private loadScaleFromStorage(): number {
|
||||
const saved = this.localStorageService.get<number>(this.STORAGE_KEY);
|
||||
if (saved !== null && !isNaN(saved)) {
|
||||
return saved;
|
||||
}
|
||||
return 1.0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user