import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild} from '@angular/core'; import ePub from 'epubjs'; import {EpubService} from '../service/epub.service'; import {Drawer} from 'primeng/drawer'; import {Button} from 'primeng/button'; import {NgForOf} from '@angular/common'; import {FormsModule} from '@angular/forms'; import {Divider} from 'primeng/divider'; import {ActivatedRoute} from '@angular/router'; import {Book, BookSetting, EpubViewerSetting} from '../../book/model/book.model'; import {BookService} from '../../book/service/book.service'; import {filter, forkJoin, take} from 'rxjs'; import {AppSettingsService} from '../../core/service/app-settings.service'; import {Select} from 'primeng/select'; const FALLBACK_EPUB_SETTINGS = { fontSize: 150, fontType: 'serif', theme: 'white', maxFontSize: 300, minFontSize: 50, }; @Component({ selector: 'app-epub-viewer', templateUrl: './epub-viewer.component.html', styleUrls: ['./epub-viewer.component.scss'], imports: [Drawer, Button, NgForOf, FormsModule, Divider, Select], }) export class EpubViewerComponent implements OnInit, OnDestroy { @ViewChild('epubContainer', {static: true}) epubContainer!: ElementRef; chapters: { label: string; href: string }[] = []; isDrawerVisible = false; isSettingsDrawerVisible = false; private book: any; private rendition: any; private keyListener: (e: KeyboardEvent) => void = () => { }; fontSize = FALLBACK_EPUB_SETTINGS.fontSize; selectedFontType = FALLBACK_EPUB_SETTINGS.fontType; selectedTheme = FALLBACK_EPUB_SETTINGS.theme; fontTypes: any[] = [ {label: 'Serif', value: 'serif'}, {label: 'Sans Serif', value: 'sans-serif'}, {label: 'Roboto', value: 'roboto'}, {label: 'Cursive', value: 'cursive'}, {label: 'Monospace', value: 'monospace'}, ]; themes: any[] = [ {label: 'White', value: 'white'}, {label: 'Black', value: 'black'}, {label: 'Grey', value: 'grey'}, {label: 'Sepia', value: 'sepia'}, ]; private epubService = inject(EpubService); private route = inject(ActivatedRoute); private bookService = inject(BookService); private appSettingsService = inject(AppSettingsService); epub!: Book; ngOnInit(): void { this.route.paramMap.subscribe((params) => { const bookId = +params.get('bookId')!; this.appSettingsService.appSettings$ .pipe(filter((appSettings) => appSettings !== null), take(1)) .subscribe((appSettings) => { const epub$ = this.bookService.getBookByIdFromAPI(bookId, false); const epubData$ = this.epubService.downloadEpub(bookId); const bookSetting$ = this.bookService.getBookSetting(bookId); forkJoin([epub$, epubData$, bookSetting$]).subscribe((results) => { const epub = results[0]; const epubData = results[1]; const individualSetting = results[2]?.epubSettings; const globalSetting = appSettings.epub; const epubScope = appSettings?.readerSettings?.epubScope; this.epub = epub; const fileReader = new FileReader(); fileReader.onload = () => { this.book = ePub(fileReader.result as ArrayBuffer); this.book.loaded.navigation.then((nav: any) => { this.chapters = nav.toc.map((chapter: any) => ({ label: chapter.label, href: chapter.href, })); }); this.rendition = this.book.renderTo(this.epubContainer.nativeElement, { flow: 'paginated', width: '100%', height: '100%', allowScriptedContent: true, }); if (this.epub?.epubProgress) { this.rendition.display(this.epub.epubProgress); } else { this.rendition.display(); } this.themesMap.forEach((theme, name) => { this.rendition.themes.register(name, theme); }); if (epubScope === 'Global') { this.selectedTheme = globalSetting?.theme || FALLBACK_EPUB_SETTINGS.theme; this.selectedFontType = globalSetting?.font || FALLBACK_EPUB_SETTINGS.fontType; this.fontSize = globalSetting?.fontSize || FALLBACK_EPUB_SETTINGS.fontSize; } else { this.selectedTheme = individualSetting?.theme || globalSetting?.theme || FALLBACK_EPUB_SETTINGS.theme; this.selectedFontType = individualSetting?.font || globalSetting?.font || FALLBACK_EPUB_SETTINGS.fontType; this.fontSize = individualSetting?.fontSize || globalSetting?.fontSize || FALLBACK_EPUB_SETTINGS.fontSize; } this.rendition.themes.select(this.selectedTheme); this.rendition.themes.fontSize(`${this.fontSize}%`); this.rendition.themes.font(this.selectedFontType); this.setupKeyListener(); this.trackProgress(); this.updateLastReadTime(); }; fileReader.readAsArrayBuffer(epubData); }); }); }); } private updateLastReadTime(): void { this.bookService.updateLastReadTime(this.epub.id).subscribe(); } changeThemes(): void { if (this.rendition) { console.log(this.selectedTheme) this.rendition.themes.select(this.selectedTheme); this.rendition.clear(); this.rendition.start(); this.updateViewerSetting(); } } updateFontSize(): void { if (this.rendition) { this.rendition.themes.fontSize(`${this.fontSize}%`); this.updateViewerSetting(); } } changeFontType(): void { if (this.rendition) { this.rendition.themes.font(this.selectedFontType); this.updateViewerSetting(); } } increaseFontSize(): void { this.fontSize = Math.min(this.fontSize + 10, FALLBACK_EPUB_SETTINGS.maxFontSize); this.updateFontSize(); } decreaseFontSize(): void { this.fontSize = Math.max(this.fontSize - 10, FALLBACK_EPUB_SETTINGS.minFontSize); this.updateFontSize(); } private updateViewerSetting(): void { const bookSetting: BookSetting = { epubSettings: { theme: this.selectedTheme, font: this.selectedFontType, fontSize: this.fontSize, } } this.bookService.updateViewerSetting(bookSetting, this.epub.id).subscribe(); } private setupKeyListener(): void { this.keyListener = (e: KeyboardEvent) => { switch (e.key) { case 'ArrowLeft': this.prevPage(); break; case 'ArrowRight': this.nextPage(); break; default: break; } }; if (this.rendition) { this.rendition.on('keyup', this.keyListener); } document.addEventListener('keyup', this.keyListener); } private trackProgress(): void { if (this.rendition) { this.rendition.on('relocated', (location: any) => { this.bookService.saveEpubProgress(this.epub.id, location.start.cfi).subscribe(); }); } } prevPage(): void { if (this.rendition) { this.rendition.prev(); } } nextPage(): void { if (this.rendition) { this.rendition.next(); } } navigateToChapter(chapter: { label: string; href: string }): void { if (this.book && chapter.href) { this.book.rendition.display(chapter.href); } } toggleDrawer(): void { this.isDrawerVisible = !this.isDrawerVisible; } toggleSettingsDrawer(): void { this.isSettingsDrawerVisible = !this.isSettingsDrawerVisible; } ngOnDestroy(): void { if (this.rendition) { this.rendition.off('keyup', this.keyListener); } this.updateLastReadTime(); document.removeEventListener('keyup', this.keyListener); } themesMap = new Map([ [ 'black', { "body": {"background-color": "#000000", "color": "#f9f9f9"}, "p": {"color": "#f9f9f9"}, "h1, h2, h3, h4, h5, h6": {"color": "#f9f9f9"}, "a": {"color": "#f9f9f9"}, "img": { "-webkit-filter": "invert(1) hue-rotate(180deg)", "filter": "invert(1) hue-rotate(180deg)" }, "code": {"color": "#00ff00", "background-color": "black"} } ], [ 'sepia', { "body": {"background-color": "#f4ecd8", "color": "#6e4b3a"}, "p": {"color": "#6e4b3a"}, "h1, h2, h3, h4, h5, h6": {"color": "#6e4b3a"}, "a": {"color": "#8b4513"}, "img": { "-webkit-filter": "sepia(1) contrast(1.5)", "filter": "sepia(1) contrast(1.5)" }, "code": {"color": "#8b0000", "background-color": "#f4ecd8"} } ], [ 'white', { "body": {"background-color": "#ffffff", "color": "#000000"}, "p": {"color": "#000000"}, "h1, h2, h3, h4, h5, h6": {"color": "#000000"}, "a": {"color": "#000000"}, "img": { "-webkit-filter": "none", "filter": "none" }, "code": {"color": "#d14", "background-color": "#f5f5f5"} } ], [ 'grey', { "body": {"background-color": "#404040", "color": "#d3d3d3"}, "p": {"color": "#d3d3d3"}, "h1, h2, h3, h4, h5, h6": {"color": "#d3d3d3"}, "a": {"color": "#1e90ff"}, "img": { "filter": "none" }, "code": {"color": "#d14", "background-color": "#585858"} } ] ]); }