From 74edb3f80988c0b4ea723d01e9a6f0f87dd0b893 Mon Sep 17 00:00:00 2001 From: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com> Date: Tue, 7 Oct 2025 14:54:58 -0600 Subject: [PATCH] In the EPUB reader, remove swipe gestures and add auto-hiding navigation buttons. (#1292) --- .../component/epub-viewer.component.html | 4 +- .../component/epub-viewer.component.scss | 21 ++--- .../component/epub-viewer.component.ts | 82 ++++++++----------- 3 files changed, 43 insertions(+), 64 deletions(-) diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html index beb4086e8..d59087fa9 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html @@ -175,10 +175,10 @@
@if (selectedFlow !== 'scrolled') { - + } @if (selectedFlow !== 'scrolled') { - + } } diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss index c51dd2920..50d0d5921 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss @@ -61,16 +61,6 @@ 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, @@ -100,10 +90,16 @@ background: none; border: none; padding: 10px; - transition: color 0.3s ease, transform 0.2s ease; + transition: color 0.3s ease, transform 0.2s ease, opacity 0.3s ease; @media (max-width: 768px) { - display: none; + opacity: 0; + pointer-events: none; + + &.visible { + opacity: 1; + pointer-events: auto; + } } } @@ -178,4 +174,3 @@ ::ng-deep .p-divider.p-divider-horizontal { margin: 0.25rem 0 0.5rem 0 !important; } - diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts index 755e8906b..e0f87f18e 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts @@ -1,4 +1,4 @@ -import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, AfterViewInit} from '@angular/core'; +import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, AfterViewInit, NgZone} from '@angular/core'; import ePub from 'epubjs'; import {Drawer} from 'primeng/drawer'; import {Button} from 'primeng/button'; @@ -40,6 +40,9 @@ export class EpubViewerComponent implements OnInit, OnDestroy { public exactProgress = 0; public progressPercentage = 0; + showControls = !this.isMobileDevice(); + private hideControlsTimeout?: number; + private book: any; private rendition: any; private keyListener: (e: KeyboardEvent) => void = () => { @@ -73,14 +76,10 @@ export class EpubViewerComponent implements OnInit, OnDestroy { private userService = inject(UserService); private bookService = inject(BookService); private messageService = inject(MessageService); + private ngZone = inject(NgZone); 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; @@ -161,8 +160,8 @@ export class EpubViewerComponent implements OnInit, OnDestroy { displayPromise.then(() => { this.setupKeyListener(); - this.setupTouchListeners(); this.trackProgress(); + this.setupTouchListener(); this.isLoading = false; }); }; @@ -204,7 +203,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy { this.rendition.themes.override('font-size', `${this.fontSize}%`); this.applyCombinedTheme(); this.setupKeyListener(); - this.setupTouchListeners(); this.rendition.display(cfi || undefined); this.updateViewerSetting(); } @@ -227,7 +225,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy { this.rendition.themes.override('font-size', `${this.fontSize}%`); this.applyCombinedTheme(); this.setupKeyListener(); - this.setupTouchListeners(); this.rendition.display(cfi || undefined); this.updateViewerSetting(); } @@ -313,50 +310,19 @@ export class EpubViewerComponent implements OnInit, OnDestroy { document.addEventListener('keyup', this.keyListener); } - private setupTouchListeners(): void { - if (!this.isMobileDevice() || this.selectedFlow === 'scrolled') return; + private setupTouchListener(): void { + if (!this.isMobileDevice() || !this.rendition) return; - const container = this.epubContainer.nativeElement; - container.removeEventListener('touchstart', this.onTouchStart.bind(this)); - container.removeEventListener('touchend', this.onTouchEnd.bind(this)); - - container.addEventListener('touchstart', this.onTouchStart.bind(this), {passive: true}); - container.addEventListener('touchend', this.onTouchEnd.bind(this), {passive: true}); - - setTimeout(() => { + 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}); + iframe.contentDocument.addEventListener('touchstart', () => { + this.ngZone.run(() => { + this.onBookTouch(); + }); + }); } - }, 500); - } - - 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(); - } - } + }); } public isMobileDevice(): boolean { @@ -435,6 +401,10 @@ export class EpubViewerComponent implements OnInit, OnDestroy { this.rendition.off('keyup', this.keyListener); } document.removeEventListener('keyup', this.keyListener); + + if (this.hideControlsTimeout) { + window.clearTimeout(this.hideControlsTimeout); + } } getThemeColor(themeKey: string | undefined): string { @@ -456,4 +426,18 @@ export class EpubViewerComponent implements OnInit, OnDestroy { this.selectedTheme = themeKey; this.changeThemes(); } + + onBookTouch(): void { + if (this.isMobileDevice()) { + this.showControls = true; + if (this.hideControlsTimeout) { + window.clearTimeout(this.hideControlsTimeout); + } + this.hideControlsTimeout = window.setTimeout(() => { + this.ngZone.run(() => { + this.showControls = false; + }); + }, 3000); + } + } }