Add support for paginated and scrolled modes in EPUB reader

This commit is contained in:
adityachandelgit
2025-05-17 12:36:25 -06:00
committed by Aditya Chandel
parent 98a4272abf
commit 9675820052
12 changed files with 87 additions and 12 deletions
@@ -33,6 +33,9 @@
<p-divider></p-divider>
<label for="theme">Theme:</label>
<p-select id="theme" [options]="themes" [(ngModel)]="selectedTheme" (onChange)="changeThemes()"></p-select>
<p-divider></p-divider>
<label for="flow">Flow:</label>
<p-select id="flow" [options]="flows" [(ngModel)]="selectedFlow" (onChange)="changeScrollMode()"></p-select>
</div>
</div>
</p-drawer>
@@ -42,7 +45,7 @@
<div id="epubContainer" #epubContainer></div>
<button *ngIf="!isMobile" class="epub-controls-left" (click)="prevPage()">←</button>
<button *ngIf="!isMobile" class="epub-controls-right" (click)="nextPage()">→</button>
<button *ngIf="selectedFlow !== 'scrolled'" class="epub-controls-left" (click)="prevPage()">←</button>
<button *ngIf="selectedFlow !== 'scrolled'" class="epub-controls-right" (click)="nextPage()">→</button>
</div>
</div>
@@ -18,6 +18,7 @@ const FALLBACK_EPUB_SETTINGS = {
fontSize: 150,
fontType: 'serif',
theme: 'white',
flow: 'paginated',
maxFontSize: 300,
minFontSize: 50,
};
@@ -60,16 +61,15 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
currentChapter = '';
isDrawerVisible = false;
isSettingsDrawerVisible = false;
isMobile = 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;
selectedFontType = FALLBACK_EPUB_SETTINGS.fontType;
fontTypes: any[] = [
{label: 'Serif', value: 'serif'},
{label: 'Sans Serif', value: 'sans-serif'},
@@ -78,6 +78,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
{label: 'Monospace', value: 'monospace'},
];
selectedTheme = FALLBACK_EPUB_SETTINGS.theme;
themes: any[] = [
{label: 'White', value: 'white'},
{label: 'Black', value: 'black'},
@@ -85,6 +86,12 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
{label: 'Sepia', value: 'sepia'},
];
selectedFlow = FALLBACK_EPUB_SETTINGS.flow;
flows: any[] = [
{label: 'Scrolled', value: 'scrolled'},
{label: 'Paginated', value: 'paginated'}
];
private route = inject(ActivatedRoute);
private userService = inject(UserService);
private bookService = inject(BookService);
@@ -115,8 +122,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
fileReader.onload = () => {
this.book = ePub(fileReader.result as ArrayBuffer);
this.isMobile = window.innerWidth <= 768;
this.book.loaded.navigation.then((nav: any) => {
this.chapters = nav.toc.map((chapter: any) => ({
label: chapter.label,
@@ -124,9 +129,15 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
}));
});
let globalOrIndividual = myself.userSettings.perBookSetting.epub;
const flow = globalOrIndividual === 'Global'
? myself.userSettings.epubReaderSetting.flow || 'paginated'
: individualSetting?.flow || myself.userSettings.epubReaderSetting.flow || 'paginated';
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
flow: this.isMobile ? 'scrolled' : 'paginated',
manager: this.isMobile ? 'continuous' : 'default',
flow,
manager: flow === 'scrolled' ? 'continuous' : 'default',
width: '100%',
height: '100%',
allowScriptedContent: true,
@@ -142,15 +153,16 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.rendition.themes.register(name, theme);
});
let globalOrIndividual = myself.userSettings.perBookSetting.epub;
if (globalOrIndividual === 'Global') {
this.selectedTheme = myself.userSettings.epubReaderSetting.theme || FALLBACK_EPUB_SETTINGS.theme;
this.selectedFontType = myself.userSettings.epubReaderSetting.font || FALLBACK_EPUB_SETTINGS.fontType;
this.fontSize = myself.userSettings.epubReaderSetting.fontSize || FALLBACK_EPUB_SETTINGS.fontSize;
this.selectedFlow = myself.userSettings.epubReaderSetting.flow || FALLBACK_EPUB_SETTINGS.flow;
} else {
this.selectedTheme = individualSetting?.theme || myself.userSettings.epubReaderSetting.theme || FALLBACK_EPUB_SETTINGS.theme;
this.selectedFontType = individualSetting?.font || myself.userSettings.epubReaderSetting.font || FALLBACK_EPUB_SETTINGS.fontType;
this.fontSize = individualSetting?.fontSize || myself.userSettings.epubReaderSetting.fontSize || FALLBACK_EPUB_SETTINGS.fontSize;
this.selectedFlow = individualSetting?.flow || myself.userSettings.epubReaderSetting.flow || FALLBACK_EPUB_SETTINGS.flow;
}
this.rendition.themes.select(this.selectedTheme);
@@ -172,6 +184,31 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
});
}
changeScrollMode(): void {
if (!this.rendition || !this.book) return;
const cfi = this.rendition.currentLocation()?.start?.cfi;
this.rendition.destroy();
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
flow: this.selectedFlow,
manager: this.selectedFlow === 'scrolled' ? 'continuous' : 'default',
width: '100%',
height: '100%',
allowScriptedContent: true,
});
this.themesMap.forEach((theme, name) => this.rendition.themes.register(name, theme));
this.rendition.themes.select(this.selectedTheme);
this.rendition.themes.font(this.selectedFontType);
this.rendition.themes.fontSize(`${this.fontSize}%`);
this.setupKeyListener();
this.trackProgress();
this.rendition.display(cfi || undefined);
this.updateViewerSetting();
}
changeThemes(): void {
if (this.rendition) {
this.rendition.themes.select(this.selectedTheme);
@@ -211,6 +248,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
theme: this.selectedTheme,
font: this.selectedFontType,
fontSize: this.fontSize,
flow: this.selectedFlow
}
}
this.bookService.updateViewerSetting(bookSetting, this.epub.id).subscribe();
@@ -333,5 +371,4 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
}
]
]);
}
@@ -89,6 +89,7 @@ export interface EpubViewerSetting {
theme: string;
font: string;
fontSize: number;
flow: string;
}
export interface BookSetting {
@@ -33,6 +33,7 @@ export interface EpubReaderSetting {
theme: string;
font: string;
fontSize: number;
flow: string;
}
export interface UserSettings {
@@ -120,6 +120,19 @@
</p-select>
</div>
<p>Flow:</p>
<div class="flex gap-4 justify-start">
<p-select
[options]="flowOptions"
[(ngModel)]="selectedFlow"
optionLabel="name"
optionValue="key"
placeholder="Select a Flow"
class="w-full md:w-56"
(ngModelChange)="onFlowChange()">
</p-select>
</div>
<p class="py-1">Font Size:</p>
<div class="flex gap-4 justify-start items-center">
<p-button icon="pi pi-minus" rounded="true" size="small" (click)="decreaseFontSize()"></p-button>
@@ -47,6 +47,11 @@ export class UserPreferences implements OnInit, OnDestroy {
{name: 'Monospace', key: 'monospace'}
];
flowOptions = [
{name: 'Paginated', key: 'paginated'},
{name: 'Scrolled', key: 'scrolled'}
];
readonly sortingOptions = [
{label: 'Name | Ascending', value: {field: 'name', order: 'asc'}},
{label: 'Name | Descending', value: {field: 'name', order: 'desc'}},
@@ -59,6 +64,7 @@ export class UserPreferences implements OnInit, OnDestroy {
selectedZoom!: string;
selectedTheme!: string;
selectedFont!: string;
selectedFlow!: string;
fontSize = 100;
showSidebar = false;
selectedPdfScope!: string;
@@ -93,6 +99,7 @@ export class UserPreferences implements OnInit, OnDestroy {
this.selectedZoom = settings.pdfReaderSetting.pageZoom;
this.showSidebar = settings.pdfReaderSetting.showSidebar;
this.selectedTheme = settings.epubReaderSetting.theme;
this.selectedFlow = settings.epubReaderSetting.flow;
this.fontSize = settings.epubReaderSetting.fontSize;
this.selectedFont = settings.epubReaderSetting.font;
this.selectedLibrarySorting = settings.sidebarLibrarySorting;
@@ -126,6 +133,10 @@ export class UserPreferences implements OnInit, OnDestroy {
this.updatePreference(['epubReaderSetting', 'font'], this.selectedFont);
}
onFlowChange() {
this.updatePreference(['epubReaderSetting', 'flow'], this.selectedFlow);
}
onFontSizeChange() {
this.updatePreference(['epubReaderSetting', 'fontSize'], this.fontSize);
}
@@ -167,5 +178,4 @@ export class UserPreferences implements OnInit, OnDestroy {
this.onFontSizeChange();
}
}
}