WIP: ePub support
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<div class="epub-viewer-container">
|
||||
|
||||
<p-button class="menu-toggle-button" (click)="toggleDrawer()" icon="pi pi-bars"></p-button>
|
||||
|
||||
<p-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" header="Chapters">
|
||||
<ul class="chapter-list">
|
||||
<li *ngFor="let chapter of chapters" (click)="navigateToChapter(chapter); $event.stopPropagation()" class="chapter-item">
|
||||
{{ chapter.label }}
|
||||
</li>
|
||||
</ul>
|
||||
</p-drawer>
|
||||
|
||||
<p-button class="settings-toggle-button" (click)="toggleSettingsDrawer()" icon="pi pi-cog"></p-button>
|
||||
<p-drawer [(visible)]="isSettingsDrawerVisible" [modal]="false" [position]="'right'" header="Settings">
|
||||
<div class="flex flex-col">
|
||||
<div class="settings-content">
|
||||
<p>Font Size: {{ fontSize }}%</p>
|
||||
<div class="flex flex-row gap-4">
|
||||
<p-button icon="pi pi-minus" (click)="decreaseFontSize()"></p-button>
|
||||
<p-button icon="pi pi-plus" (click)="increaseFontSize()"></p-button>
|
||||
</div>
|
||||
<p-divider></p-divider>
|
||||
<label for="font-type">Font Type:</label>
|
||||
<p-dropdown id="font-type" [options]="fontTypes" [(ngModel)]="selectedFontType" (onChange)="changeFontType($event)">
|
||||
</p-dropdown>
|
||||
</div>
|
||||
</div>
|
||||
</p-drawer>
|
||||
|
||||
<div id="epubContainer" #epubContainer></div>
|
||||
|
||||
<button class="epub-controls-left" (click)="prevPage()">←</button>
|
||||
<button class="epub-controls-right" (click)="nextPage()">→</button>
|
||||
</div>
|
||||
@@ -0,0 +1,106 @@
|
||||
.epub-viewer-container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
#epubContainer {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.menu-toggle-button,
|
||||
.settings-toggle-button {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
z-index: 1000;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.menu-toggle-button {
|
||||
left: 10px;
|
||||
}
|
||||
|
||||
.settings-toggle-button {
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.epub-controls-left,
|
||||
.epub-controls-right {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
z-index: 10;
|
||||
font-size: 30px;
|
||||
color: rgba(0, 0, 0, 0.5);
|
||||
cursor: pointer;
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 10px;
|
||||
transition: color 0.3s ease, transform 0.2s ease;
|
||||
}
|
||||
|
||||
.epub-controls-left {
|
||||
left: 10px;
|
||||
}
|
||||
|
||||
.epub-controls-right {
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
.epub-controls-left:hover,
|
||||
.epub-controls-right:hover {
|
||||
color: rgba(0, 0, 0, 0.8);
|
||||
transform: scale(1.1) translateY(-50%);
|
||||
}
|
||||
|
||||
.epub-controls-left:focus,
|
||||
.epub-controls-right:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.chapter-list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.chapter-item {
|
||||
padding: 0.5px 0.5px;
|
||||
font-size: 14px;
|
||||
color: var(--text-color);
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s ease, color 0.3s ease;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.chapter-item:hover {
|
||||
color: greenyellow;
|
||||
}
|
||||
|
||||
.chapter-item:active {
|
||||
background-color: var(--card-background);
|
||||
}
|
||||
|
||||
.settings-content {
|
||||
padding: 10px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.settings-content label {
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.settings-content input[type="range"] {
|
||||
width: 100%;
|
||||
margin-top: 5px;
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import {Component, ElementRef, 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 {DropdownModule} from 'primeng/dropdown';
|
||||
|
||||
@Component({
|
||||
selector: 'app-epub-viewer',
|
||||
templateUrl: './epub-viewer.component.html',
|
||||
styleUrls: ['./epub-viewer.component.scss'],
|
||||
imports: [
|
||||
Drawer,
|
||||
Button,
|
||||
NgForOf,
|
||||
FormsModule,
|
||||
Divider,
|
||||
DropdownModule
|
||||
]
|
||||
})
|
||||
export class EpubViewerComponent implements OnInit, OnDestroy {
|
||||
|
||||
@ViewChild('epubContainer', {static: true}) epubContainer!: ElementRef;
|
||||
bookId = 1;
|
||||
chapters: { label: string; href: string }[] = [];
|
||||
isDrawerVisible = false;
|
||||
isSettingsDrawerVisible: boolean = false;
|
||||
private book: any;
|
||||
private rendition: any;
|
||||
private keyListener: (e: KeyboardEvent) => void = () => {
|
||||
};
|
||||
fontSize: number = 100;
|
||||
|
||||
fontTypes: any[] = [
|
||||
{label: 'Serif', value: 'serif'},
|
||||
{label: 'Sans Serif', value: 'sans-serif'},
|
||||
{label: 'Roboto', value: 'roboto'},
|
||||
{label: 'Cursive', value: 'cursive'},
|
||||
{label: 'Monospace', value: 'monospace'}
|
||||
];
|
||||
selectedFontType: string = 'serif';
|
||||
|
||||
constructor(private epubService: EpubService) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadEpub();
|
||||
}
|
||||
|
||||
loadEpub(): void {
|
||||
this.epubService.downloadEpub(this.bookId).subscribe(
|
||||
(data: Blob) => {
|
||||
const fileReader = new FileReader();
|
||||
fileReader.onload = () => {
|
||||
const epubData = fileReader.result as ArrayBuffer;
|
||||
this.book = ePub(epubData);
|
||||
|
||||
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,
|
||||
});
|
||||
this.rendition.display();
|
||||
this.setupKeyListener();
|
||||
this.updateFontSize();
|
||||
};
|
||||
fileReader.readAsArrayBuffer(data);
|
||||
},
|
||||
(error) => {
|
||||
console.error('Failed to load the EPUB:', error);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.off('keyup', this.keyListener);
|
||||
}
|
||||
document.removeEventListener('keyup', this.keyListener);
|
||||
}
|
||||
|
||||
updateFontSize(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.themes.fontSize(`${this.fontSize}%`);
|
||||
}
|
||||
}
|
||||
|
||||
increaseFontSize(): void {
|
||||
this.fontSize += 10;
|
||||
if (this.fontSize > 200) {
|
||||
this.fontSize = 200;
|
||||
}
|
||||
this.updateFontSize();
|
||||
}
|
||||
|
||||
decreaseFontSize(): void {
|
||||
this.fontSize -= 10;
|
||||
if (this.fontSize < 50) {
|
||||
this.fontSize = 50;
|
||||
}
|
||||
this.updateFontSize();
|
||||
}
|
||||
|
||||
changeFontType(event: any): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.themes.font(this.selectedFontType);
|
||||
}
|
||||
}
|
||||
|
||||
navigateToChapter(chapter: { label: string; href: string }): void {
|
||||
if (this.book && chapter.href) {
|
||||
this.book.rendition.display(chapter.href);
|
||||
}
|
||||
}
|
||||
|
||||
nextPage(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.next();
|
||||
}
|
||||
}
|
||||
|
||||
prevPage(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.prev();
|
||||
}
|
||||
}
|
||||
|
||||
toggleDrawer(): void {
|
||||
this.isDrawerVisible = !this.isDrawerVisible;
|
||||
}
|
||||
|
||||
toggleSettingsDrawer(): void {
|
||||
this.isSettingsDrawerVisible = !this.isSettingsDrawerVisible;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user