feat: implement loaders for EPUB and PDF readers (#38)

This commit is contained in:
aditya.chandel
2025-03-28 02:31:53 -06:00
committed by Aditya Chandel
parent fac3f8c819
commit 6a4b6b2bcb
4 changed files with 127 additions and 95 deletions
@@ -1,5 +1,6 @@
<div class="epub-viewer-container"> <div #epubContainer class="epub-viewer-container">
<p-progressSpinner *ngIf="isLoading"></p-progressSpinner>
<div *ngIf="!isLoading">
<p-button class="menu-toggle-button" (click)="toggleDrawer()" icon="pi pi-bars" severity="secondary"></p-button> <p-button class="menu-toggle-button" (click)="toggleDrawer()" icon="pi pi-bars" severity="secondary"></p-button>
<p-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" header="Chapters"> <p-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" header="Chapters">
<ul class="chapter-list"> <ul class="chapter-list">
@@ -32,4 +33,5 @@
<button class="epub-controls-left" (click)="prevPage()" >←</button> <button class="epub-controls-left" (click)="prevPage()" >←</button>
<button class="epub-controls-right" (click)="nextPage()">→</button> <button class="epub-controls-right" (click)="nextPage()">→</button>
</div>
</div> </div>
@@ -2,16 +2,17 @@ import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild} from '@angu
import ePub from 'epubjs'; import ePub from 'epubjs';
import {Drawer} from 'primeng/drawer'; import {Drawer} from 'primeng/drawer';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {NgForOf} from '@angular/common'; import {NgForOf, NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {Divider} from 'primeng/divider'; import {Divider} from 'primeng/divider';
import {ActivatedRoute} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
import {Book, BookSetting} from '../../../model/book.model'; import {Book, BookSetting} from '../../../model/book.model';
import {BookService} from '../../../service/book.service'; import {BookService} from '../../../service/book.service';
import {filter, forkJoin, take} from 'rxjs'; import {forkJoin} from 'rxjs';
import {AppSettingsService} from '../../../../core/service/app-settings.service';
import {Select} from 'primeng/select'; import {Select} from 'primeng/select';
import {UserService} from '../../../../user.service'; import {UserService} from '../../../../user.service';
import {ProgressSpinner} from 'primeng/progressspinner';
import {MessageService} from 'primeng/api';
const FALLBACK_EPUB_SETTINGS = { const FALLBACK_EPUB_SETTINGS = {
fontSize: 150, fontSize: 150,
@@ -25,12 +26,13 @@ const FALLBACK_EPUB_SETTINGS = {
selector: 'app-epub-viewer', selector: 'app-epub-viewer',
templateUrl: './epub-viewer.component.html', templateUrl: './epub-viewer.component.html',
styleUrls: ['./epub-viewer.component.scss'], styleUrls: ['./epub-viewer.component.scss'],
imports: [Drawer, Button, NgForOf, FormsModule, Divider, Select], imports: [Drawer, Button, NgForOf, FormsModule, Divider, Select, ProgressSpinner, NgIf],
standalone: true standalone: true
}) })
export class EpubViewerComponent implements OnInit, OnDestroy { export class EpubViewerComponent implements OnInit, OnDestroy {
@ViewChild('epubContainer', {static: true}) epubContainer!: ElementRef; @ViewChild('epubContainer', {static: false}) epubContainer!: ElementRef;
isLoading = true;
chapters: { label: string; href: string }[] = []; chapters: { label: string; href: string }[] = [];
isDrawerVisible = false; isDrawerVisible = false;
isSettingsDrawerVisible = false; isSettingsDrawerVisible = false;
@@ -61,11 +63,13 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
private route = inject(ActivatedRoute); private route = inject(ActivatedRoute);
private userService = inject(UserService); private userService = inject(UserService);
private bookService = inject(BookService); private bookService = inject(BookService);
private messageService = inject(MessageService);
epub!: Book; epub!: Book;
ngOnInit(): void { ngOnInit(): void {
this.route.paramMap.subscribe((params) => { this.route.paramMap.subscribe((params) => {
this.isLoading = true;
const bookId = +params.get('bookId')!; const bookId = +params.get('bookId')!;
const myself$ = this.userService.getMyself(); const myself$ = this.userService.getMyself();
@@ -73,7 +77,8 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
const epubData$ = this.bookService.getFileContent(bookId); const epubData$ = this.bookService.getFileContent(bookId);
const bookSetting$ = this.bookService.getBookSetting(bookId); const bookSetting$ = this.bookService.getBookSetting(bookId);
forkJoin([myself$, epub$, epubData$, bookSetting$]).subscribe((results) => { forkJoin([myself$, epub$, epubData$, bookSetting$]).subscribe({
next: (results) => {
const myself = results[0]; const myself = results[0];
const epub = results[1]; const epub = results[1];
const epubData = results[2]; const epubData = results[2];
@@ -126,9 +131,15 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.setupKeyListener(); this.setupKeyListener();
this.trackProgress(); this.trackProgress();
this.isLoading = false;
}; };
fileReader.readAsArrayBuffer(epubData); fileReader.readAsArrayBuffer(epubData);
},
error: () => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to load the book'});
this.isLoading = false;
}
}); });
}); });
} }
@@ -1,4 +1,9 @@
<div *ngIf="isLoading" class="flex justify-center items-center h-screen relative">
<p-progressSpinner></p-progressSpinner>
</div>
<ngx-extended-pdf-viewer <ngx-extended-pdf-viewer
*ngIf="!isLoading"
[src]="bookData" [src]="bookData"
[handTool]="handTool" [handTool]="handTool"
[height]="'auto'" [height]="'auto'"
@@ -7,14 +7,19 @@ import {filter, forkJoin, Subscription, take} from 'rxjs';
import {BookSetting, PdfViewerSetting} from '../../model/book.model'; import {BookSetting, PdfViewerSetting} from '../../model/book.model';
import {PdfSettings} from '../../../core/model/app-settings.model'; import {PdfSettings} from '../../../core/model/app-settings.model';
import {UserService} from '../../../user.service'; import {UserService} from '../../../user.service';
import {NgIf} from '@angular/common';
import {ProgressSpinner} from 'primeng/progressspinner';
import {MessageService} from 'primeng/api';
@Component({ @Component({
selector: 'app-pdf-viewer', selector: 'app-pdf-viewer',
standalone: true, standalone: true,
imports: [NgxExtendedPdfViewerModule], imports: [NgxExtendedPdfViewerModule, NgIf, ProgressSpinner],
templateUrl: './pdf-viewer.component.html', templateUrl: './pdf-viewer.component.html',
}) })
export class PdfViewerComponent implements OnInit, OnDestroy { export class PdfViewerComponent implements OnInit, OnDestroy {
isLoading = true;
handTool = true; handTool = true;
rotation: 0 | 90 | 180 | 270 = 0; rotation: 0 | 90 | 180 | 270 = 0;
@@ -28,10 +33,12 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
private bookService = inject(BookService); private bookService = inject(BookService);
private userService = inject(UserService); private userService = inject(UserService);
private messageService = inject(MessageService);
private route = inject(ActivatedRoute); private route = inject(ActivatedRoute);
ngOnInit(): void { ngOnInit(): void {
this.route.paramMap.subscribe((params) => { this.route.paramMap.subscribe((params) => {
this.isLoading = true;
this.bookId = +params.get('bookId')!; this.bookId = +params.get('bookId')!;
const myself$ = this.userService.getMyself(); const myself$ = this.userService.getMyself();
@@ -39,7 +46,8 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
const bookSetting$ = this.bookService.getBookSetting(this.bookId); const bookSetting$ = this.bookService.getBookSetting(this.bookId);
const pdfData$ = this.bookService.getFileContent(this.bookId); const pdfData$ = this.bookService.getFileContent(this.bookId);
forkJoin([book$, bookSetting$, pdfData$, myself$]).subscribe((results) => { forkJoin([book$, bookSetting$, pdfData$, myself$]).subscribe({
next: (results) => {
const pdfMeta = results[0]; const pdfMeta = results[0];
const pdfPrefs = results[1]; const pdfPrefs = results[1];
const pdfData = results[2]; const pdfData = results[2];
@@ -55,6 +63,12 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
} }
this.page = pdfMeta.pdfProgress || 1; this.page = pdfMeta.pdfProgress || 1;
this.bookData = pdfData; this.bookData = pdfData;
this.isLoading = false;
},
error: () => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to load the book'});
this.isLoading = false;
}
}); });
}); });
} }