Improved EPUB navigation on mobile devices with swipe gestures (#1265)

This commit is contained in:
Aditya Chandel
2025-10-05 00:39:10 -06:00
committed by GitHub
parent f778f1932d
commit ba61885790
2 changed files with 70 additions and 3 deletions
@@ -61,6 +61,16 @@
height: 100%;
width: 100%;
overflow: hidden;
@media (max-width: 768px) {
touch-action: manipulation;
user-select: none;
}
iframe {
pointer-events: auto;
touch-action: manipulation;
}
}
.menu-toggle-button,
@@ -91,6 +101,10 @@
border: none;
padding: 10px;
transition: color 0.3s ease, transform 0.2s ease;
@media (max-width: 768px) {
display: none;
}
}
.epub-controls-left {
@@ -1,4 +1,4 @@
import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, AfterViewInit} from '@angular/core';
import ePub from 'epubjs';
import {Drawer} from 'primeng/drawer';
import {Button} from 'primeng/button';
@@ -75,6 +75,11 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
epub!: Book;
private touchStartX: number = 0;
private touchStartY: number = 0;
private minSwipeDistance: number = 50;
private maxVerticalDistance: number = 100;
ngOnInit(): void {
this.route.paramMap.subscribe((params) => {
this.isLoading = true;
@@ -152,6 +157,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
displayPromise.then(() => {
this.setupKeyListener();
this.setupTouchListeners();
this.trackProgress();
this.isLoading = false;
});
@@ -193,6 +199,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.applyCombinedTheme();
this.setupKeyListener();
this.setupTouchListeners();
this.rendition.display(cfi || undefined);
this.updateViewerSetting();
}
@@ -277,6 +284,54 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
document.addEventListener('keyup', this.keyListener);
}
private setupTouchListeners(): void {
if (!this.isMobileDevice() || this.selectedFlow === 'scrolled') return;
this.rendition.on('rendered', () => {
const iframe = this.epubContainer.nativeElement.querySelector('iframe');
if (iframe && iframe.contentDocument) {
const iframeDoc = iframe.contentDocument;
iframeDoc.addEventListener('touchstart', this.onTouchStart.bind(this), {passive: true});
iframeDoc.addEventListener('touchend', this.onTouchEnd.bind(this), {passive: true});
}
});
const container = this.epubContainer.nativeElement;
container.addEventListener('touchstart', this.onTouchStart.bind(this), {passive: true});
container.addEventListener('touchend', this.onTouchEnd.bind(this), {passive: true});
}
onTouchStart(event: TouchEvent): void {
if (this.selectedFlow === 'scrolled') return;
this.touchStartX = event.touches[0].clientX;
this.touchStartY = event.touches[0].clientY;
}
onTouchEnd(event: TouchEvent): void {
if (this.selectedFlow === 'scrolled') return;
const touchEndX = event.changedTouches[0].clientX;
const touchEndY = event.changedTouches[0].clientY;
const deltaX = touchEndX - this.touchStartX;
const deltaY = Math.abs(touchEndY - this.touchStartY);
if (Math.abs(deltaX) > this.minSwipeDistance && deltaY < this.maxVerticalDistance) {
event.preventDefault();
if (deltaX > 0) {
this.prevPage();
} else {
this.nextPage();
}
}
}
private isMobileDevice(): boolean {
return window.innerWidth <= 768;
}
prevPage(): void {
if (this.rendition) {
this.rendition.prev();
@@ -333,7 +388,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
return this.book.locations.generate(1600);
}).then(() => {
this.locationsReady = true;
// Recalculate progress with new locations
if (this.rendition.currentLocation()) {
const location = this.rendition.currentLocation();
const cfi = location.end.cfi;
@@ -341,7 +395,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.progressPercentage = Math.round(percentage * 1000) / 10;
}
}).catch(() => {
// If location generation fails, keep using spine-based calculation
this.locationsReady = false;
});
}