diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html index abf1c2d04..2ba218bf2 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html @@ -1,6 +1,7 @@
+
    @@ -10,6 +11,10 @@
+
+

Chapter 1: Introduction | Page 3 of 120

+
+
diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss index 535026e41..1bfd7be6b 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss @@ -1,5 +1,6 @@ .epub-viewer-container { display: flex; + flex-direction: column; justify-content: center; align-items: center; height: 100vh; @@ -29,6 +30,16 @@ right: 10px; } +.epub-header { + position: absolute; + top: 2%; + left: 50%; + transform: translate(-50%, -50%); + z-index: 500; + text-align: center; + color: #333; +} + .epub-controls-left, .epub-controls-right { position: absolute; diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts index edbfa728e..566b04512 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts @@ -1,5 +1,5 @@ import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild} from '@angular/core'; -import ePub from 'epubjs'; +import ePub, {EpubCFI} from 'epubjs'; import {Drawer} from 'primeng/drawer'; import {Button} from 'primeng/button'; import {NgForOf, NgIf} from '@angular/common'; @@ -22,6 +22,32 @@ const FALLBACK_EPUB_SETTINGS = { minFontSize: 50, }; +function flatten(chapters: any) { + return [].concat.apply([], chapters.map((chapter: any) => [].concat.apply([chapter], flatten(chapter.subitems)))); +} + +export function getCfiFromHref(book: any, href: string): string { + const [_, id] = href.split('#'); + const section = book.spine.get(href); + const el = (id ? section.document.getElementById(id) : section.document.body) as Element; + return section.cfiFromElement(el); +} + +export function getChapter(book: any, location: any) { + const locationHref = location.start.href; + + let match = flatten(book.navigation.toc) + .filter((chapter: any) => { + return book.canonical(chapter.href).includes(book.canonical(locationHref)); + }) + .reduce((result: any | null, chapter: any) => { + const locationAfterChapter = EpubCFI.prototype.compare(location.start.cfi, getCfiFromHref(book, chapter.href)) > 0; + return locationAfterChapter ? chapter : result; + }, null); + + return match; +} + @Component({ selector: 'app-epub-viewer', templateUrl: './epub-viewer.component.html', @@ -236,11 +262,14 @@ export class EpubViewerComponent implements OnInit, OnDestroy { private trackProgress(): void { if (this.rendition) { this.rendition.on('relocated', (location: any) => { + const currentChapter = getChapter(this.book, location); + console.log(currentChapter?.label || 'Unknown Chapter'); this.bookService.saveEpubProgress(this.epub.id, location.start.cfi).subscribe(); }); } } + ngOnDestroy(): void { if (this.rendition) { this.rendition.off('keyup', this.keyListener);