Enhance EPUB reader navigation by adding tap zones for easier page control (#2092)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2026-01-01 13:26:46 -07:00
committed by GitHub
co-authored by acx10
parent e0dec6f2f6
commit 78344d1d0e
2 changed files with 331 additions and 186 deletions
@@ -65,6 +65,9 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
private hideHeaderTimeout?: number; private hideHeaderTimeout?: number;
private isMouseInTopRegion = false; private isMouseInTopRegion = false;
private headerShownByMobileTouch = false; private headerShownByMobileTouch = false;
private currentIframe: HTMLIFrameElement | null = null;
private iframeTouchHandler: ((event: TouchEvent) => void) | null = null;
private iframeClickHandler: ((event: MouseEvent) => void) | null = null;
editingBookmark: BookMark | null = null; editingBookmark: BookMark | null = null;
showEditBookmarkDialog = false; showEditBookmarkDialog = false;
@@ -388,65 +391,183 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
} }
private setupTouchListener(): void { private setupTouchListener(): void {
if (!this.isMobileDevice() || !this.rendition) return; if (!this.isMobileDevice()) return;
const container = this.epubContainer.nativeElement;
container.addEventListener('touchend', (event: TouchEvent) => {
event.stopPropagation();
this.ngZone.run(() => {
this.handleTouchEnd(event);
});
}, { passive: false });
container.addEventListener('click', (event: MouseEvent) => {
event.stopPropagation();
this.ngZone.run(() => {
this.handleClickAsTap(event);
});
}, { capture: true });
this.rendition.on('rendered', () => { this.rendition.on('rendered', () => {
const iframe = this.epubContainer.nativeElement.querySelector('iframe'); const iframe = this.epubContainer.nativeElement.querySelector('iframe');
if (iframe && iframe.contentDocument) {
iframe.contentDocument.addEventListener('touchstart', () => { if (iframe && iframe !== this.currentIframe) {
if (this.currentIframe && this.currentIframe.contentDocument) {
if (this.iframeTouchHandler) {
this.currentIframe.contentDocument.removeEventListener('touchend', this.iframeTouchHandler, true);
}
if (this.iframeClickHandler) {
this.currentIframe.contentDocument.removeEventListener('click', this.iframeClickHandler, true);
}
}
this.currentIframe = iframe;
if (iframe.contentDocument) {
this.iframeTouchHandler = (event: TouchEvent) => {
event.stopPropagation();
event.preventDefault();
this.ngZone.run(() => { this.ngZone.run(() => {
this.onBookTouch(); this.handleTouchEnd(event);
}); });
};
this.iframeClickHandler = (event: MouseEvent) => {
event.stopPropagation();
event.preventDefault();
this.ngZone.run(() => {
this.handleClickAsTap(event);
}); });
};
iframe.contentDocument.addEventListener('touchend', this.iframeTouchHandler, { passive: false, capture: true });
iframe.contentDocument.addEventListener('click', this.iframeClickHandler, { capture: true });
}
} }
}); });
} }
public isMobileDevice(): boolean { public handleTouchEnd(event: TouchEvent): void {
return window.innerWidth <= 768; if (!this.isMobileDevice()) return;
const touch = event.changedTouches[0];
let touchX = touch.clientX;
let touchY = touch.clientY;
if (event.target && (event.target as HTMLElement).ownerDocument !== document) {
const iframe = this.currentIframe;
if (iframe) {
const iframeRect = iframe.getBoundingClientRect();
touchX = touch.clientX + iframeRect.left;
touchY = touch.clientY + iframeRect.top;
}
} }
prevPage(): void { const screenWidth = window.innerWidth;
if (this.rendition) { const screenHeight = window.innerHeight;
this.rendition.prev().then(() => {
const location = this.rendition.currentLocation(); const isTopTouch = touchY < screenHeight * 0.2;
this.readingSessionService.updateProgress( const isInTapZoneVertically = touchY >= screenHeight * 0.2;
location?.start?.cfi,
if (isTopTouch) {
if (!this.showHeader) {
this.showHeader = true;
this.headerShownByMobileTouch = true;
this.clearHeaderTimeout();
this.startHeaderAutoHide();
} else if (this.headerShownByMobileTouch) {
this.showHeader = false;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
}
return;
}
if (isInTapZoneVertically) {
const leftZoneEnd = screenWidth / 3;
const rightZoneStart = screenWidth * (2 / 3);
if (touchX < leftZoneEnd) {
this.prevPage();
return;
}
if (touchX > rightZoneStart) {
this.nextPage();
return;
}
this.showControlsTemporarily();
}
}
private handleClickAsTap(event: MouseEvent): void {
if (!this.isMobileDevice()) return;
let clickX = event.clientX;
let clickY = event.clientY;
if (event.target && (event.target as HTMLElement).ownerDocument !== document) {
const iframe = this.currentIframe;
if (iframe) {
const iframeRect = iframe.getBoundingClientRect();
clickX = event.clientX + iframeRect.left;
clickY = event.clientY + iframeRect.top;
}
}
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const isTopClick = clickY < screenHeight * 0.2;
const isInTapZoneVertically = clickY >= screenHeight * 0.2;
if (isTopClick) {
if (!this.showHeader) {
this.showHeader = true;
this.headerShownByMobileTouch = true;
this.clearHeaderTimeout();
this.startHeaderAutoHide();
} else if (this.headerShownByMobileTouch) {
this.showHeader = false;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
}
return;
}
if (isInTapZoneVertically) {
const leftZoneEnd = screenWidth / 3;
const rightZoneStart = screenWidth * (2 / 3);
if (clickX < leftZoneEnd) {
this.prevPage();
return;
}
if (clickX > rightZoneStart) {
this.nextPage();
return;
}
this.showControlsTemporarily();
}
}
closeReader(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage this.progressPercentage
); );
});
}
}
nextPage(): void {
if (this.rendition) {
this.rendition.next().then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
navigateToChapter(chapter: { label: string; href: string; level: number }): void {
if (this.book && chapter.href) {
this.book.rendition.display(chapter.href).then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
} }
this.location.back();
} }
toggleDrawer(): void { toggleDrawer(): void {
this.isDrawerVisible = !this.isDrawerVisible; this.isDrawerVisible = !this.isDrawerVisible;
if (this.isDrawerVisible) { if (this.isDrawerVisible) {
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout(); this.clearHeaderTimeout();
} else { } else {
this.startHeaderAutoHide(); this.startHeaderAutoHide();
@@ -456,14 +577,40 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
toggleSettingsDrawer(): void { toggleSettingsDrawer(): void {
this.isSettingsDrawerVisible = !this.isSettingsDrawerVisible; this.isSettingsDrawerVisible = !this.isSettingsDrawerVisible;
if (this.isSettingsDrawerVisible) { if (this.isSettingsDrawerVisible) {
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout(); this.clearHeaderTimeout();
} else { } else {
this.startHeaderAutoHide(); this.startHeaderAutoHide();
} }
} }
navigateToChapter(chapter: { label: string; href: string; level: number }): void {
if (this.rendition) {
this.rendition.display(chapter.href).then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
prevPage(): void {
if (this.rendition) {
this.rendition.prev();
}
}
nextPage(): void {
if (this.rendition) {
this.rendition.next();
}
}
isMobileDevice(): boolean {
return window.innerWidth <= 768;
}
private trackProgress(): void { private trackProgress(): void {
if (!this.book || !this.rendition) return; if (!this.book || !this.rendition) return;
this.rendition.on('relocated', (location: any) => { this.rendition.on('relocated', (location: any) => {
@@ -528,6 +675,15 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.routeSubscription?.unsubscribe(); this.routeSubscription?.unsubscribe();
if (this.currentIframe && this.currentIframe.contentDocument) {
if (this.iframeTouchHandler) {
this.currentIframe.contentDocument.removeEventListener('touchend', this.iframeTouchHandler, true);
}
if (this.iframeClickHandler) {
this.currentIframe.contentDocument.removeEventListener('click', this.iframeClickHandler, true);
}
}
if (this.rendition) { if (this.rendition) {
this.rendition.off('keyup', this.keyListener); this.rendition.off('keyup', this.keyListener);
} }
@@ -550,23 +706,14 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
} }
onBookClick(event: MouseEvent): void { onBookClick(event: MouseEvent): void {
const clickX = event.clientX;
const clickY = event.clientY; const clickY = event.clientY;
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight; const screenHeight = window.innerHeight;
if (this.isMobileDevice()) { if (this.isMobileDevice()) {
const isTopClick = clickY < screenHeight * 0.2; this.handleClickAsTap(event);
const isBottomClick = clickY > screenHeight * 0.2;
if (isTopClick && !this.showHeader) {
this.showHeader = true;
this.headerShownByMobileTouch = true;
this.clearHeaderTimeout();
this.startHeaderAutoHide();
} else if (isBottomClick && this.showHeader && this.headerShownByMobileTouch) {
this.showHeader = false;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
}
} else { } else {
const isTopClick = clickY < screenHeight * 0.1; const isTopClick = clickY < screenHeight * 0.1;
if (isTopClick) { if (isTopClick) {
@@ -615,8 +762,17 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
} }
} }
onBookTouch(): void { onBookTouch(event?: TouchEvent): void {
if (this.isMobileDevice()) { if (this.isMobileDevice()) {
if (event) {
this.handleTouchEnd(event);
} else {
this.showControlsTemporarily();
}
}
}
private showControlsTemporarily(): void {
this.showControls = true; this.showControls = true;
this.showHeader = true; this.showHeader = true;
this.headerShownByMobileTouch = false; this.headerShownByMobileTouch = false;
@@ -635,7 +791,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}); });
}, 2000); }, 2000);
} }
}
startHeaderAutoHide(): void { startHeaderAutoHide(): void {
this.clearHeaderTimeout(); this.clearHeaderTimeout();
@@ -834,14 +989,4 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.showEditBookmarkDialog = false; this.showEditBookmarkDialog = false;
this.editingBookmark = null; this.editingBookmark = null;
} }
closeReader(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage
);
}
this.location.back();
}
} }