Improved EPUB navigation on mobile devices with swipe gestures (#1265)
This commit is contained in:
@@ -61,6 +61,16 @@
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
touch-action: manipulation;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
iframe {
|
||||||
|
pointer-events: auto;
|
||||||
|
touch-action: manipulation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-toggle-button,
|
.menu-toggle-button,
|
||||||
@@ -91,6 +101,10 @@
|
|||||||
border: none;
|
border: none;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
transition: color 0.3s ease, transform 0.2s ease;
|
transition: color 0.3s ease, transform 0.2s ease;
|
||||||
|
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.epub-controls-left {
|
.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 ePub from 'epubjs';
|
||||||
import {Drawer} from 'primeng/drawer';
|
import {Drawer} from 'primeng/drawer';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
@@ -75,6 +75,11 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
epub!: Book;
|
epub!: Book;
|
||||||
|
|
||||||
|
private touchStartX: number = 0;
|
||||||
|
private touchStartY: number = 0;
|
||||||
|
private minSwipeDistance: number = 50;
|
||||||
|
private maxVerticalDistance: number = 100;
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.route.paramMap.subscribe((params) => {
|
this.route.paramMap.subscribe((params) => {
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
@@ -152,6 +157,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
displayPromise.then(() => {
|
displayPromise.then(() => {
|
||||||
this.setupKeyListener();
|
this.setupKeyListener();
|
||||||
|
this.setupTouchListeners();
|
||||||
this.trackProgress();
|
this.trackProgress();
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
});
|
});
|
||||||
@@ -193,6 +199,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.applyCombinedTheme();
|
this.applyCombinedTheme();
|
||||||
|
|
||||||
this.setupKeyListener();
|
this.setupKeyListener();
|
||||||
|
this.setupTouchListeners();
|
||||||
this.rendition.display(cfi || undefined);
|
this.rendition.display(cfi || undefined);
|
||||||
this.updateViewerSetting();
|
this.updateViewerSetting();
|
||||||
}
|
}
|
||||||
@@ -277,6 +284,54 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
document.addEventListener('keyup', this.keyListener);
|
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 {
|
prevPage(): void {
|
||||||
if (this.rendition) {
|
if (this.rendition) {
|
||||||
this.rendition.prev();
|
this.rendition.prev();
|
||||||
@@ -333,7 +388,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
return this.book.locations.generate(1600);
|
return this.book.locations.generate(1600);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
this.locationsReady = true;
|
this.locationsReady = true;
|
||||||
// Recalculate progress with new locations
|
|
||||||
if (this.rendition.currentLocation()) {
|
if (this.rendition.currentLocation()) {
|
||||||
const location = this.rendition.currentLocation();
|
const location = this.rendition.currentLocation();
|
||||||
const cfi = location.end.cfi;
|
const cfi = location.end.cfi;
|
||||||
@@ -341,7 +395,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
// If location generation fails, keep using spine-based calculation
|
|
||||||
this.locationsReady = false;
|
this.locationsReady = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user