106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
import { Component, OnDestroy, effect, inject, input, output, signal } from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { RegionSpec } from '../../api/api.types';
|
|
import { PdfSourceApi } from '../../api/pdf-source.api';
|
|
import { DrawnRegionBox, RegionDrawOverlay } from './region-draw-overlay.directive';
|
|
|
|
/**
|
|
* Shows one PDF page at a time as an image, with a prev/next pager and a
|
|
* RegionDrawOverlay on top. The parent listens to `regionDrawn` and decides
|
|
* what a drawn box means.
|
|
*
|
|
* Page images are cached as Blobs rather than object URLs. The displayed
|
|
* URL is revoked on every navigation and a fresh one is created from the
|
|
* cached Blob, so no URL is leaked and no page is fetched twice.
|
|
*/
|
|
@Component({
|
|
imports: [MatButtonModule, MatIconModule, MatProgressBarModule, RegionDrawOverlay],
|
|
selector: 'app-pdf-page-viewer',
|
|
styleUrl: './pdf-page-viewer.scss',
|
|
templateUrl: './pdf-page-viewer.html',
|
|
})
|
|
export class PdfPageViewer implements OnDestroy {
|
|
readonly file = input.required<File>();
|
|
readonly pageNumber = input.required<number>();
|
|
readonly pageCount = input.required<number>();
|
|
/** Drawn regions on all pages; only the current page's are rendered. */
|
|
readonly regions = input<RegionSpec[]>([]);
|
|
|
|
readonly pageNumberChange = output<number>();
|
|
readonly regionDrawn = output<DrawnRegionBox>();
|
|
readonly regionRemove = output<RegionSpec>();
|
|
|
|
private readonly pdfApi = inject(PdfSourceApi);
|
|
|
|
readonly imageUrl = signal<string | null>(null);
|
|
readonly loading = signal(false);
|
|
readonly error = signal<string | null>(null);
|
|
|
|
private readonly blobCache = new Map<number, Blob>();
|
|
private currentObjectUrl: string | null = null;
|
|
private currentFile: File | null = null;
|
|
|
|
constructor() {
|
|
effect(() => {
|
|
const file = this.file();
|
|
const pageNumber = this.pageNumber();
|
|
if (file !== this.currentFile) {
|
|
this.blobCache.clear();
|
|
this.currentFile = file;
|
|
}
|
|
this.loadPage(file, pageNumber);
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
if (this.currentObjectUrl) URL.revokeObjectURL(this.currentObjectUrl);
|
|
}
|
|
|
|
previous(): void {
|
|
if (this.pageNumber() > 1) this.pageNumberChange.emit(this.pageNumber() - 1);
|
|
}
|
|
|
|
next(): void {
|
|
if (this.pageNumber() < this.pageCount()) this.pageNumberChange.emit(this.pageNumber() + 1);
|
|
}
|
|
|
|
regionsOnCurrentPage(): RegionSpec[] {
|
|
return this.regions().filter((region) => region.page_numbers.includes(this.pageNumber()));
|
|
}
|
|
|
|
onRemoveClick(event: Event, region: RegionSpec): void {
|
|
event.stopPropagation();
|
|
this.regionRemove.emit(region);
|
|
}
|
|
|
|
private loadPage(file: File, pageNumber: number): void {
|
|
const cached = this.blobCache.get(pageNumber);
|
|
if (cached) {
|
|
this.showBlob(cached);
|
|
return;
|
|
}
|
|
this.loading.set(true);
|
|
this.error.set(null);
|
|
this.pdfApi.pageImage(file, pageNumber).subscribe({
|
|
next: (blob) => {
|
|
this.blobCache.set(pageNumber, blob);
|
|
this.loading.set(false);
|
|
this.showBlob(blob);
|
|
},
|
|
error: () => {
|
|
this.loading.set(false);
|
|
this.error.set('No se pudo cargar la página.');
|
|
},
|
|
});
|
|
}
|
|
|
|
private showBlob(blob: Blob): void {
|
|
const url = URL.createObjectURL(blob);
|
|
if (this.currentObjectUrl) URL.revokeObjectURL(this.currentObjectUrl);
|
|
this.currentObjectUrl = url;
|
|
this.imageUrl.set(url);
|
|
}
|
|
}
|