WIP
This commit is contained in:
committed by
Aditya Chandel
parent
3630b27e37
commit
b8947dcf27
@@ -1,6 +1,7 @@
|
||||
<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-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" header="Chapters">
|
||||
<ul class="chapter-list">
|
||||
@@ -10,6 +11,10 @@
|
||||
</ul>
|
||||
</p-drawer>
|
||||
|
||||
<div class="epub-header">
|
||||
<h3>Chapter 1: Introduction | Page 3 of 120</h3>
|
||||
</div>
|
||||
|
||||
<p-button class="settings-toggle-button" (click)="toggleSettingsDrawer()" icon="pi pi-cog" severity="secondary"></p-button>
|
||||
<p-drawer [(visible)]="isSettingsDrawerVisible" [modal]="false" [position]="'right'" header="Settings">
|
||||
<div class="flex flex-col">
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user