fix: fix text overflow with attach-to-book flow when dealing with long titles (#265)

* Measure the actual rendered pixel width of the autocomplete wrapper div
* Use the calculated measure to style the component, overriding the PrimeNG default styles
* Add CSS attributes to ensure long titles are truncated with ellipses if they overflow
This commit is contained in:
Sergio Visinoni
2026-04-22 17:26:56 -04:00
committed by Zach Yale
parent a92459ddbf
commit 1799493ed7
3 changed files with 103 additions and 104 deletions
@@ -1,118 +1,95 @@
<ng-container *transloco="let t; prefix: 'book.fileAttacher'"> <ng-container *transloco="let t; prefix: 'book.fileAttacher'">
<div class="book-file-attacher-container"> <div class="book-file-attacher-container">
<div class="panel-header"> <div class="panel-header">
<div class="header-icon-wrapper"> <div class="header-icon-wrapper">
<i class="pi pi-link header-icon"></i> <i class="pi pi-link header-icon"></i>
</div>
<div class="header-text">
<h2 class="panel-title">{{ isBulkMode ? t('titleBulk') : t('title') }}</h2>
<p class="panel-description">{{ isBulkMode ? t('descriptionBulk') : t('description') }}</p>
</div>
<p-button icon="pi pi-times" [rounded]="true" [text]="true" severity="secondary" (click)="closeDialog()"
class="close-button">
</p-button>
</div> </div>
<div class="header-text">
<h2 class="panel-title">{{ isBulkMode ? t('titleBulk') : t('title') }}</h2>
<p class="panel-description">{{ isBulkMode ? t('descriptionBulk') : t('description') }}</p>
</div>
<p-button
icon="pi pi-times"
[rounded]="true"
[text]="true"
severity="secondary"
(click)="closeDialog()"
class="close-button">
</p-button>
</div>
<div class="dialog-content"> <div class="dialog-content">
<!-- Source Book(s) Info --> <!-- Source Book(s) Info -->
<div class="source-info"> <div class="source-info">
<h3 class="section-title">{{ isBulkMode ? t('sourceBooksLabel', { count: sourceBooks.length }) : t('sourceBookLabel', { count: sourceBooks.length }) }}</h3> <h3 class="section-title">{{ isBulkMode ? t('sourceBooksLabel', { count: sourceBooks.length }) :
<div class="source-books-list"> t('sourceBookLabel', { count: sourceBooks.length }) }}</h3>
@for (book of sourceBooks; track book.id) { <div class="source-books-list">
@for (book of sourceBooks; track book.id) {
<div class="book-info-card"> <div class="book-info-card">
<div class="book-title">{{ book.metadata?.title || t('unknownTitle') }}</div> <div class="book-title">{{ book.metadata?.title || t('unknownTitle') }}</div>
@if (book.metadata?.authors?.length) { @if (book.metadata?.authors?.length) {
<div class="book-authors">{{ book.metadata?.authors?.join(', ') }}</div> <div class="book-authors">{{ book.metadata?.authors?.join(', ') }}</div>
} }
<div class="file-info"> <div class="file-info">
<i class="pi pi-file"></i> <i class="pi pi-file"></i>
<span>{{ getSourceFileInfo(book) }}</span> <span>{{ getSourceFileInfo(book) }}</span>
</div> </div>
</div> </div>
} }
</div>
</div> </div>
</div>
<!-- Target Book Selection --> <!-- Target Book Selection -->
<div class="target-selection"> <div class="target-selection" #autocompleteWrapper>
<h3 class="section-title">{{ t('selectTargetBook') }}</h3> <h3 class="section-title">{{ t('selectTargetBook') }}</h3>
<p-autocomplete <p-autocomplete [(ngModel)]="targetBook" [suggestions]="filteredBooks" (completeMethod)="filterBooks($event)"
[(ngModel)]="targetBook" (onSelect)="onBookSelect($event)" (onClear)="onBookClear()" optionLabel="metadata.title" [dropdown]="true"
[suggestions]="filteredBooks" [showClear]="true" [placeholder]="t('searchPlaceholder')" [panelStyle]="autocomplePanelStyle" fluid
(completeMethod)="filterBooks($event)" [disabled]="isAttaching">
(onSelect)="onBookSelect($event)" <ng-template let-book pTemplate="item">
(onClear)="onBookClear()" <div class="book-suggestion">
optionLabel="metadata.title" <div class="suggestion-title">{{ book.metadata?.title || t('unknownTitle') }}</div>
[dropdown]="true" @if (book.metadata?.authors?.length) {
[showClear]="true"
[placeholder]="t('searchPlaceholder')"
fluid
[disabled]="isAttaching">
<ng-template let-book pTemplate="item">
<div class="book-suggestion">
<div class="suggestion-title">{{ book.metadata?.title || t('unknownTitle') }}</div>
@if (book.metadata?.authors?.length) {
<div class="suggestion-authors">{{ book.metadata.authors.join(', ') }}</div> <div class="suggestion-authors">{{ book.metadata.authors.join(', ') }}</div>
} }
@if (book.primaryFile) { @if (book.primaryFile) {
<div class="suggestion-format">{{ book.primaryFile.extension?.toUpperCase() || book.primaryFile.bookType }}</div> <div class="suggestion-format">{{ book.primaryFile.extension?.toUpperCase() || book.primaryFile.bookType
} }}</div>
</div> }
</ng-template> </div>
<ng-template let-book pTemplate="selectedItem"> </ng-template>
<span>{{ getBookDisplayName(book) }}</span> <ng-template let-book pTemplate="selectedItem">
</ng-template> <span>{{ getBookDisplayName(book) }}</span>
</p-autocomplete> </ng-template>
</div> </p-autocomplete>
<!-- Options -->
<div class="delete-option">
<p-checkbox
[(ngModel)]="moveFiles"
[binary]="true"
inputId="moveFiles"
[disabled]="isAttaching">
</p-checkbox>
<label for="moveFiles" class="delete-label">
{{ t('moveFilesLabel') }}
</label>
</div>
<!-- Warning Message -->
<div class="warning-message">
<i class="pi pi-info-circle"></i>
<div class="warning-text">
@if (moveFiles) {
<p>{{ isBulkMode ? t('warningMoveBulk') : t('warningMove') }}</p>
} @else {
<p>{{ isBulkMode ? t('warningNoMoveBulk') : t('warningNoMove') }}</p>
}
<p>{{ isBulkMode ? t('warningDeleteBulk') : t('warningDelete') }}</p>
</div> </div>
</div>
<!-- Action Buttons --> <!-- Options -->
<div class="action-buttons"> <div class="delete-option">
<p-button <p-checkbox [(ngModel)]="moveFiles" [binary]="true" inputId="moveFiles" [disabled]="isAttaching">
[label]="'common.cancel' | transloco" </p-checkbox>
[outlined]="true" <label for="moveFiles" class="delete-label">
severity="secondary" {{ t('moveFilesLabel') }}
(click)="closeDialog()" </label>
[disabled]="isAttaching"> </div>
</p-button>
<p-button <!-- Warning Message -->
[label]="isBulkMode ? t('attachFilesBulk') : t('attachFile')" <div class="warning-message">
icon="pi pi-link" <i class="pi pi-info-circle"></i>
(click)="attach()" <div class="warning-text">
[disabled]="!canAttach()" @if (moveFiles) {
[loading]="isAttaching"> <p>{{ isBulkMode ? t('warningMoveBulk') : t('warningMove') }}</p>
</p-button> } @else {
<p>{{ isBulkMode ? t('warningNoMoveBulk') : t('warningNoMove') }}</p>
}
<p>{{ isBulkMode ? t('warningDeleteBulk') : t('warningDelete') }}</p>
</div>
</div>
<!-- Action Buttons -->
<div class="action-buttons">
<p-button [label]="'common.cancel' | transloco" [outlined]="true" severity="secondary" (click)="closeDialog()"
[disabled]="isAttaching">
</p-button>
<p-button [label]="isBulkMode ? t('attachFilesBulk') : t('attachFile')" icon="pi pi-link" (click)="attach()"
[disabled]="!canAttach()" [loading]="isAttaching">
</p-button>
</div>
</div> </div>
</div> </div>
</div> </ng-container>
</ng-container>
@@ -100,15 +100,22 @@
.book-suggestion { .book-suggestion {
padding: 0.25rem 0; padding: 0.25rem 0;
overflow: hidden;
.suggestion-title { .suggestion-title {
font-weight: 500; font-weight: 500;
margin-bottom: 0.125rem; margin-bottom: 0.125rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} }
.suggestion-authors { .suggestion-authors {
font-size: 0.875rem; font-size: 0.875rem;
color: var(--p-text-muted-color); color: var(--p-text-muted-color);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} }
.suggestion-format { .suggestion-format {
@@ -116,6 +123,9 @@
color: var(--p-primary-color); color: var(--p-primary-color);
font-weight: 600; font-weight: 600;
margin-top: 0.25rem; margin-top: 0.25rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} }
} }
@@ -178,4 +188,4 @@
gap: 0.75rem; gap: 0.75rem;
margin-top: auto; margin-top: auto;
padding-top: 0.5rem; padding-top: 0.5rem;
} }
@@ -1,4 +1,4 @@
import { Component, computed, inject, OnInit, OnDestroy, Signal, signal } from '@angular/core'; import { Component, computed, inject, OnInit, OnDestroy, AfterViewInit, ElementRef, ViewChild, Signal, signal } from '@angular/core';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/dynamicdialog'; import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/dynamicdialog';
import { AutoComplete, AutoCompleteSelectEvent } from 'primeng/autocomplete'; import { AutoComplete, AutoCompleteSelectEvent } from 'primeng/autocomplete';
@@ -8,7 +8,7 @@ import { Subject, takeUntil } from 'rxjs';
import { BookService } from '../../service/book.service'; import { BookService } from '../../service/book.service';
import { BookFileService } from '../../service/book-file.service'; import { BookFileService } from '../../service/book-file.service';
import { Book } from '../../model/book.model'; import { Book } from '../../model/book.model';
import {MessageService, PrimeTemplate} from 'primeng/api'; import { MessageService, PrimeTemplate } from 'primeng/api';
import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco'; import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco';
import { AppSettingsService } from '../../../../shared/service/app-settings.service'; import { AppSettingsService } from '../../../../shared/service/app-settings.service';
@@ -27,13 +27,16 @@ import { AppSettingsService } from '../../../../shared/service/app-settings.serv
templateUrl: './book-file-attacher.component.html', templateUrl: './book-file-attacher.component.html',
styleUrls: ['./book-file-attacher.component.scss'] styleUrls: ['./book-file-attacher.component.scss']
}) })
export class BookFileAttacherComponent implements OnInit, OnDestroy { export class BookFileAttacherComponent implements OnInit, AfterViewInit, OnDestroy {
@ViewChild('autocompleteWrapper') autocompleteWrapper!: ElementRef;
sourceBooks: Book[] = []; sourceBooks: Book[] = [];
targetBook: Book | null = null; targetBook: Book | null = null;
moveFiles = false; moveFiles = false;
isAttaching = false; isAttaching = false;
searchQuery = ''; searchQuery = '';
filteredBooks: Book[] = []; filteredBooks: Book[] = [];
autocomplePanelStyle: Record<string, string> = {};
private destroy$ = new Subject<void>(); private destroy$ = new Subject<void>();
private allBooks: Signal<Book[]> = signal([]); private allBooks: Signal<Book[]> = signal([]);
@@ -46,6 +49,15 @@ export class BookFileAttacherComponent implements OnInit, OnDestroy {
private bookFileService = inject(BookFileService); private bookFileService = inject(BookFileService);
private messageService = inject(MessageService); private messageService = inject(MessageService);
ngAfterViewInit(): void {
setTimeout(() => {
const width = this.autocompleteWrapper?.nativeElement?.offsetWidth;
if (width) {
this.autocomplePanelStyle = { 'width': `${width}px`, 'max-width': `${width}px` };
}
});
}
ngOnInit(): void { ngOnInit(): void {
// Support both single book and multiple books // Support both single book and multiple books
if (this.config.data.sourceBook) { if (this.config.data.sourceBook) {
@@ -86,7 +98,7 @@ export class BookFileAttacherComponent implements OnInit, OnDestroy {
return this.sourceBooks.length > 1; return this.sourceBooks.length > 1;
} }
filterBooks(event: { query: string }): void { filterBooks(event: { query: string; }): void {
const query = event.query.toLowerCase().trim(); const query = event.query.toLowerCase().trim();
const books = this.allBooks(); const books = this.allBooks();
if (!query) { if (!query) {