Move files around
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<div class="epub-viewer-container">
|
||||
|
||||
<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">
|
||||
<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" severity="secondary"></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()" severity="info"></p-button>
|
||||
<p-button icon="pi pi-plus" (click)="increaseFontSize()" severity="info"></p-button>
|
||||
</div>
|
||||
<p-divider></p-divider>
|
||||
<label for="font-type">Font Type:</label>
|
||||
<p-select id="font-type" [options]="fontTypes" [(ngModel)]="selectedFontType" (onChange)="changeFontType()"></p-select>
|
||||
<p-divider></p-divider>
|
||||
<label for="theme">Theme:</label>
|
||||
<p-select id="theme" [options]="themes" [(ngModel)]="selectedTheme" (onChange)="changeThemes()"></p-select>
|
||||
</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,295 @@
|
||||
import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
||||
import ePub from 'epubjs';
|
||||
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} from '../../../model/book.model';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
import {filter, forkJoin, take} from 'rxjs';
|
||||
import {AppSettingsService} from '../../../../core/service/app-settings.service';
|
||||
import {Select} from 'primeng/select';
|
||||
import {UserService} from '../../../../user.service';
|
||||
|
||||
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],
|
||||
standalone: true
|
||||
})
|
||||
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 route = inject(ActivatedRoute);
|
||||
private userService = inject(UserService);
|
||||
private bookService = inject(BookService);
|
||||
|
||||
epub!: Book;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.route.paramMap.subscribe((params) => {
|
||||
const bookId = +params.get('bookId')!;
|
||||
|
||||
const myself$ = this.userService.getMyself();
|
||||
const epub$ = this.bookService.getBookByIdFromAPI(bookId, false);
|
||||
const epubData$ = this.bookService.getFileContent(bookId);
|
||||
const bookSetting$ = this.bookService.getBookSetting(bookId);
|
||||
|
||||
forkJoin([myself$, epub$, epubData$, bookSetting$]).subscribe((results) => {
|
||||
const myself = results[0];
|
||||
const epub = results[1];
|
||||
const epubData = results[2];
|
||||
const individualSetting = results[3]?.epubSettings;
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
let globalOrIndividual = myself.bookPreferences.perBookSetting.epub;
|
||||
if (globalOrIndividual === 'Global') {
|
||||
this.selectedTheme = myself.bookPreferences.epubReaderSetting.theme || FALLBACK_EPUB_SETTINGS.theme;
|
||||
this.selectedFontType = myself.bookPreferences.epubReaderSetting.font || FALLBACK_EPUB_SETTINGS.fontType;
|
||||
this.fontSize = myself.bookPreferences.epubReaderSetting.fontSize || FALLBACK_EPUB_SETTINGS.fontSize;
|
||||
} else {
|
||||
this.selectedTheme = individualSetting?.theme || myself.bookPreferences.epubReaderSetting.theme || FALLBACK_EPUB_SETTINGS.theme;
|
||||
this.selectedFontType = individualSetting?.font || myself.bookPreferences.epubReaderSetting.font || FALLBACK_EPUB_SETTINGS.fontType;
|
||||
this.fontSize = individualSetting?.fontSize || myself.bookPreferences.epubReaderSetting.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();
|
||||
};
|
||||
|
||||
fileReader.readAsArrayBuffer(epubData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
changeThemes(): void {
|
||||
if (this.rendition) {
|
||||
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(Number(this.fontSize) + 10, FALLBACK_EPUB_SETTINGS.maxFontSize);
|
||||
this.updateFontSize();
|
||||
}
|
||||
|
||||
decreaseFontSize(): void {
|
||||
this.fontSize = Math.max(Number(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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private trackProgress(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.on('relocated', (location: any) => {
|
||||
this.bookService.saveEpubProgress(this.epub.id, location.start.cfi).subscribe();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (this.rendition) {
|
||||
this.rendition.off('keyup', this.keyListener);
|
||||
}
|
||||
document.removeEventListener('keyup', this.keyListener);
|
||||
this.trackProgress();
|
||||
}
|
||||
|
||||
themesMap = new Map<string, any>([
|
||||
[
|
||||
'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"}
|
||||
}
|
||||
]
|
||||
]);
|
||||
|
||||
}
|
||||
@@ -23,7 +23,6 @@ export class BookService {
|
||||
});
|
||||
bookState$ = this.bookStateSubject.asObservable();
|
||||
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user