In the EPUB reader, remove swipe gestures and add auto-hiding navigation buttons. (#1292)
This commit is contained in:
+2
-2
@@ -175,10 +175,10 @@
|
|||||||
</div>
|
</div>
|
||||||
<div id="epubContainer" #epubContainer></div>
|
<div id="epubContainer" #epubContainer></div>
|
||||||
@if (selectedFlow !== 'scrolled') {
|
@if (selectedFlow !== 'scrolled') {
|
||||||
<button class="epub-controls-left" (click)="prevPage()">←</button>
|
<button class="epub-controls-left" [class.visible]="showControls" (click)="prevPage(); onBookTouch()">←</button>
|
||||||
}
|
}
|
||||||
@if (selectedFlow !== 'scrolled') {
|
@if (selectedFlow !== 'scrolled') {
|
||||||
<button class="epub-controls-right" (click)="nextPage()">→</button>
|
<button class="epub-controls-right" [class.visible]="showControls" (click)="nextPage(); onBookTouch()">→</button>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-13
@@ -61,16 +61,6 @@
|
|||||||
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,
|
||||||
@@ -100,10 +90,16 @@
|
|||||||
background: none;
|
background: none;
|
||||||
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, opacity 0.3s ease;
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
@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 {
|
::ng-deep .p-divider.p-divider-horizontal {
|
||||||
margin: 0.25rem 0 0.5rem 0 !important;
|
margin: 0.25rem 0 0.5rem 0 !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+33
-49
@@ -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 ePub from 'epubjs';
|
||||||
import {Drawer} from 'primeng/drawer';
|
import {Drawer} from 'primeng/drawer';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
@@ -40,6 +40,9 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
public exactProgress = 0;
|
public exactProgress = 0;
|
||||||
public progressPercentage = 0;
|
public progressPercentage = 0;
|
||||||
|
|
||||||
|
showControls = !this.isMobileDevice();
|
||||||
|
private hideControlsTimeout?: number;
|
||||||
|
|
||||||
private book: any;
|
private book: any;
|
||||||
private rendition: any;
|
private rendition: any;
|
||||||
private keyListener: (e: KeyboardEvent) => void = () => {
|
private keyListener: (e: KeyboardEvent) => void = () => {
|
||||||
@@ -73,14 +76,10 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
private userService = inject(UserService);
|
private userService = inject(UserService);
|
||||||
private bookService = inject(BookService);
|
private bookService = inject(BookService);
|
||||||
private messageService = inject(MessageService);
|
private messageService = inject(MessageService);
|
||||||
|
private ngZone = inject(NgZone);
|
||||||
|
|
||||||
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;
|
||||||
@@ -161,8 +160,8 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
displayPromise.then(() => {
|
displayPromise.then(() => {
|
||||||
this.setupKeyListener();
|
this.setupKeyListener();
|
||||||
this.setupTouchListeners();
|
|
||||||
this.trackProgress();
|
this.trackProgress();
|
||||||
|
this.setupTouchListener();
|
||||||
this.isLoading = false;
|
this.isLoading = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -204,7 +203,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||||
this.applyCombinedTheme();
|
this.applyCombinedTheme();
|
||||||
this.setupKeyListener();
|
this.setupKeyListener();
|
||||||
this.setupTouchListeners();
|
|
||||||
this.rendition.display(cfi || undefined);
|
this.rendition.display(cfi || undefined);
|
||||||
this.updateViewerSetting();
|
this.updateViewerSetting();
|
||||||
}
|
}
|
||||||
@@ -227,7 +225,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||||
this.applyCombinedTheme();
|
this.applyCombinedTheme();
|
||||||
this.setupKeyListener();
|
this.setupKeyListener();
|
||||||
this.setupTouchListeners();
|
|
||||||
this.rendition.display(cfi || undefined);
|
this.rendition.display(cfi || undefined);
|
||||||
this.updateViewerSetting();
|
this.updateViewerSetting();
|
||||||
}
|
}
|
||||||
@@ -313,50 +310,19 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
document.addEventListener('keyup', this.keyListener);
|
document.addEventListener('keyup', this.keyListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private setupTouchListeners(): void {
|
private setupTouchListener(): void {
|
||||||
if (!this.isMobileDevice() || this.selectedFlow === 'scrolled') return;
|
if (!this.isMobileDevice() || !this.rendition) return;
|
||||||
|
|
||||||
const container = this.epubContainer.nativeElement;
|
this.rendition.on('rendered', () => {
|
||||||
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(() => {
|
|
||||||
const iframe = this.epubContainer.nativeElement.querySelector('iframe');
|
const iframe = this.epubContainer.nativeElement.querySelector('iframe');
|
||||||
if (iframe && iframe.contentDocument) {
|
if (iframe && iframe.contentDocument) {
|
||||||
const iframeDoc = iframe.contentDocument;
|
iframe.contentDocument.addEventListener('touchstart', () => {
|
||||||
iframeDoc.addEventListener('touchstart', this.onTouchStart.bind(this), {passive: true});
|
this.ngZone.run(() => {
|
||||||
iframeDoc.addEventListener('touchend', this.onTouchEnd.bind(this), {passive: true});
|
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 {
|
public isMobileDevice(): boolean {
|
||||||
@@ -435,6 +401,10 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.rendition.off('keyup', this.keyListener);
|
this.rendition.off('keyup', this.keyListener);
|
||||||
}
|
}
|
||||||
document.removeEventListener('keyup', this.keyListener);
|
document.removeEventListener('keyup', this.keyListener);
|
||||||
|
|
||||||
|
if (this.hideControlsTimeout) {
|
||||||
|
window.clearTimeout(this.hideControlsTimeout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getThemeColor(themeKey: string | undefined): string {
|
getThemeColor(themeKey: string | undefined): string {
|
||||||
@@ -456,4 +426,18 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
this.selectedTheme = themeKey;
|
this.selectedTheme = themeKey;
|
||||||
this.changeThemes();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user