Allow free-text and suggestion selection in authors and categories auto-complete

This commit is contained in:
aditya.chandel
2025-09-01 12:29:55 -06:00
committed by Aditya Chandel
parent 48c9f1d422
commit 0159fcf2a8
4 changed files with 148 additions and 56 deletions
@@ -102,9 +102,18 @@
<div class="flex flex-col gap-1 md:basis-[71%]">
<label class="text-sm" for="authors">Authors</label>
<div class="flex justify-between items-center gap-2">
<div class="w-full">
<p-autoComplete formControlName="authors" [multiple]="true" [typeahead]="false" [dropdown]="false" [forceSelection]="false" [showClear]="true" (onBlur)="onAutoCompleteBlur('authors', $event)" styleClass="w-full"></p-autoComplete>
</div>
<p-autoComplete
class="w-full"
formControlName="authors"
[multiple]="true"
[dropdown]="false"
[suggestions]="filteredAuthors"
[forceSelection]="false"
[showClear]="true"
(completeMethod)="filterAuthors($event)"
(onKeyUp)="onAutoCompleteKeyUp('authors', $event)"
(onSelect)="onAutoCompleteSelect('authors', $event)">
</p-autoComplete>
@if (!book.metadata!['authorsLocked']) {
<p-button size="small" icon="pi pi-lock-open" [outlined]="true" (onClick)="toggleLock('authors')" severity="success"></p-button>
}
@@ -144,7 +153,18 @@
<label class="text-sm" for="categories">Categories</label>
<div class="flex justify-between items-center gap-2">
<div class="w-full">
<p-autoComplete formControlName="categories" [multiple]="true" [typeahead]="false" [dropdown]="false" [forceSelection]="false" [showClear]="true" (onBlur)="onAutoCompleteBlur('categories', $event)" styleClass="w-full"></p-autoComplete>
<p-autoComplete
class="w-full"
formControlName="categories"
[multiple]="true"
[dropdown]="false"
[suggestions]="filteredCategories"
[forceSelection]="false"
[showClear]="true"
(completeMethod)="filterCategories($event)"
(onKeyUp)="onAutoCompleteKeyUp('categories', $event)"
(onSelect)="onAutoCompleteSelect('categories', $event)">
</p-autoComplete>
</div>
@if (!book.metadata!['categoriesLocked']) {
<p-button size="small" icon="pi pi-lock-open" [outlined]="true" (onClick)="toggleLock('categories')" severity="success"></p-button>
@@ -1,4 +1,4 @@
import {Component, DestroyRef, EventEmitter, inject, Input, OnInit, Output, ViewChild} from '@angular/core';
import {Component, DestroyRef, EventEmitter, inject, Input, OnInit, Output} from '@angular/core';
import {InputText} from 'primeng/inputtext';
import {Button} from 'primeng/button';
import {Divider} from 'primeng/divider';
@@ -13,8 +13,7 @@ import {HttpResponse} from '@angular/common/http';
import {BookService} from '../../../book/service/book.service';
import {ProgressSpinner} from 'primeng/progressspinner';
import {Tooltip} from 'primeng/tooltip';
import {Editor} from 'primeng/editor';
import {debounceTime} from 'rxjs/operators';
import {filter, take} from 'rxjs/operators';
import {MetadataRestoreDialogComponent} from '../../../book/components/book-browser/metadata-restore-dialog-component/metadata-restore-dialog-component';
import {DialogService} from 'primeng/dynamicdialog';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
@@ -51,8 +50,6 @@ import {LazyLoadImageModule} from 'ng-lazyload-image';
})
export class MetadataEditorComponent implements OnInit {
@ViewChild(Editor) quillEditor!: Editor;
@Input() book$!: Observable<Book | null>;
@Output() nextBookClicked = new EventEmitter<void>();
@Output() previousBookClicked = new EventEmitter<void>();
@@ -72,7 +69,6 @@ export class MetadataEditorComponent implements OnInit {
currentBookId!: number;
isUploading = false;
isLoading = false;
htmlTextarea = '';
isSaving = false;
refreshingBookIds = new Set<number>();
@@ -80,22 +76,23 @@ export class MetadataEditorComponent implements OnInit {
originalMetadata!: BookMetadata;
// Handle blur event for AutoComplete to add custom values
onAutoCompleteBlur(fieldName: string, event: any) {
const inputValue = event.target.value?.trim();
if (inputValue) {
const currentValue = this.metadataForm.get(fieldName)?.value || [];
const values = Array.isArray(currentValue) ? currentValue :
typeof currentValue === 'string' && currentValue ? currentValue.split(',').map((v: string) => v.trim()) : [];
allAuthors!: string[];
allCategories!: string[];
filteredCategories: string[] = [];
filteredAuthors: string[] = [];
// Add the new value if it's not already in the array
if (!values.includes(inputValue)) {
values.push(inputValue);
this.metadataForm.get(fieldName)?.setValue(values);
}
// Clear the input
event.target.value = '';
}
filterCategories(event: { query: string }) {
const query = event.query.toLowerCase();
this.filteredCategories = this.allCategories.filter(cat =>
cat.toLowerCase().includes(query)
);
}
filterAuthors(event: { query: string }) {
const query = event.query.toLowerCase();
this.filteredAuthors = this.allAuthors.filter(cat =>
cat.toLowerCase().includes(query)
);
}
constructor() {
@@ -164,30 +161,31 @@ export class MetadataEditorComponent implements OnInit {
.subscribe(book => {
const metadata = book?.metadata;
if (!metadata) return;
this.currentBookId = metadata.bookId;
if (this.refreshingBookIds.has(book.id)) {
this.refreshingBookIds.delete(book.id);
this.isAutoFetching = false;
}
this.originalMetadata = structuredClone(metadata);
if (this.quillEditor?.quill) {
this.quillEditor.quill.root.innerHTML = metadata.description;
this.quillDisabled();
}
this.populateFormFromMetadata(metadata);
});
this.metadataForm.get('description')?.valueChanges
.pipe(debounceTime(100), takeUntilDestroyed(this.destroyRef))
.subscribe(value => {
if (this.htmlTextarea !== value) {
this.htmlTextarea = value;
}
this.bookService.bookState$
.pipe(
filter(bookState => bookState.loaded),
take(1)
)
.subscribe(bookState => {
const authors = new Set<string>();
const categories = new Set<string>();
(bookState.books ?? []).forEach(book => {
book.metadata?.authors?.forEach(author => authors.add(author));
book.metadata?.categories?.forEach(category => categories.add(category));
});
this.allAuthors = Array.from(authors);
this.allCategories = Array.from(categories);
});
}
@@ -289,6 +287,28 @@ export class MetadataEditorComponent implements OnInit {
}
}
onAutoCompleteSelect(fieldName: string, event: any) {
const values = this.metadataForm.get(fieldName)?.value || [];
if (!values.includes(event.value)) {
this.metadataForm.get(fieldName)?.setValue([...values, event.value]);
}
(event.originalEvent.target as HTMLInputElement).value = '';
}
onAutoCompleteKeyUp(fieldName: string, event: KeyboardEvent) {
if (event.key === 'Enter') {
const input = event.target as HTMLInputElement;
const value = input.value?.trim();
if (value) {
const values = this.metadataForm.get(fieldName)?.value || [];
if (!values.includes(value)) {
this.metadataForm.get(fieldName)?.setValue([...values, value]);
}
input.value = '';
}
}
}
onSave(): void {
this.isSaving = true;
this.bookService.updateBookMetadata(this.currentBookId, this.buildMetadataWrapper(undefined), false).subscribe({
@@ -100,7 +100,18 @@
<p-button icon="pi pi-lock" [outlined]="true" (onClick)="toggleLock(field.controlName)" severity="warn"></p-button>
}
<div class="w-full px-4">
<p-autoComplete formControlName="{{field.controlName}}" [multiple]="true" [typeahead]="false" [dropdown]="false" [forceSelection]="false" (onBlur)="onAutoCompleteBlur(field.controlName, $event)" styleClass="w-full"></p-autoComplete>
<p-autoComplete
class="w-full"
formControlName="{{field.controlName}}"
[multiple]="true"
[dropdown]="false"
[forceSelection]="false"
[showClear]="true"
[suggestions]="getFiltered(field.controlName)"
(completeMethod)="filterItems($event, field.controlName)"
(onKeyUp)="onAutoCompleteKeyUp(field.controlName, $event)"
(onSelect)="onAutoCompleteSelect(field.controlName, $event)">
</p-autoComplete>
</div>
<p-button
[icon]="isValueSaved(field.controlName) ? 'pi pi-check' : (hoveredFields[field.controlName] && isValueCopied(field.controlName) ? 'pi pi-times' : 'pi pi-arrow-left')"
@@ -11,7 +11,7 @@ import {Tooltip} from 'primeng/tooltip';
import {UrlHelperService} from '../../../utilities/service/url-helper.service';
import {BookService} from '../../../book/service/book.service';
import {Textarea} from 'primeng/textarea';
import {filter, map} from 'rxjs/operators';
import {filter, map, take} from 'rxjs/operators';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {AutoComplete} from 'primeng/autocomplete';
import {Image} from 'primeng/image';
@@ -81,21 +81,21 @@ export class MetadataPickerComponent implements OnInit {
@Input() book$!: Observable<Book | null>;
@Output() goBack = new EventEmitter<boolean>();
// Handle blur event for AutoComplete to add custom values
onAutoCompleteBlur(fieldName: string, event: any) {
const inputValue = event.target.value?.trim();
if (inputValue) {
const currentValue = this.metadataForm.get(fieldName)?.value || [];
const values = Array.isArray(currentValue) ? currentValue :
typeof currentValue === 'string' && currentValue ? currentValue.split(',').map((v: string) => v.trim()) : [];
allAuthors!: string[];
allCategories!: string[];
filteredCategories: string[] = [];
filteredAuthors: string[] = [];
// Add the new value if it's not already in the array
if (!values.includes(inputValue)) {
values.push(inputValue);
this.metadataForm.get(fieldName)?.setValue(values);
}
// Clear the input
event.target.value = '';
getFiltered(controlName: string): string[] {
return controlName === 'authors' ? this.filteredAuthors : this.filteredCategories;
}
filterItems(event: { query: string }, controlName: string) {
const query = event.query.toLowerCase();
if (controlName === 'authors') {
this.filteredAuthors = this.allAuthors.filter(a => a.toLowerCase().includes(query));
} else if (controlName === 'categories') {
this.filteredCategories = this.allCategories.filter(c => c.toLowerCase().includes(query));
}
}
@@ -171,6 +171,25 @@ export class MetadataPickerComponent implements OnInit {
}
ngOnInit(): void {
this.bookService.bookState$
.pipe(
filter(bookState => bookState.loaded),
take(1)
)
.subscribe(bookState => {
const authors = new Set<string>();
const categories = new Set<string>();
(bookState.books ?? []).forEach(book => {
book.metadata?.authors?.forEach(author => authors.add(author));
book.metadata?.categories?.forEach(category => categories.add(category));
});
this.allAuthors = Array.from(authors);
this.allCategories = Array.from(categories);
});
this.book$
.pipe(
filter((book): book is Book => !!book && !!book.metadata),
@@ -275,6 +294,28 @@ export class MetadataPickerComponent implements OnInit {
});
}
onAutoCompleteSelect(fieldName: string, event: any) {
const values = this.metadataForm.get(fieldName)?.value || [];
if (!values.includes(event.value)) {
this.metadataForm.get(fieldName)?.setValue([...values, event.value]);
}
(event.originalEvent.target as HTMLInputElement).value = '';
}
onAutoCompleteKeyUp(fieldName: string, event: KeyboardEvent) {
if (event.key === 'Enter') {
const input = event.target as HTMLInputElement;
const value = input.value?.trim();
if (value) {
const values = this.metadataForm.get(fieldName)?.value || [];
if (!values.includes(value)) {
this.metadataForm.get(fieldName)?.setValue([...values, value]);
}
input.value = '';
}
}
}
onSave(): void {
this.isSaving = true;
const updatedBookMetadata = this.buildMetadataWrapper(undefined);