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
@@ -19,101 +19,101 @@
<div class="progress-info">
<span class="progress-percentage"><span class="progress-label">Progress: </span>{{ progressPercentage }}%</span>
</div>
<p-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" [style]="{ width: '320px' }" (click)="$event.stopPropagation()">
<ng-template pTemplate="header">
<div class="drawer-header">
<span class="drawer-title">Table of Contents</span>
</div>
</ng-template>
<p-tabs value="0">
<p-tablist>
<p-tab value="0">
<div class="tab-header">
<i class="pi pi-book"></i>
<span>Chapters</span>
</div>
</p-tab>
<p-tab value="1">
<div class="tab-header">
<i class="pi pi-bookmark"></i>
<span>Bookmarks</span>
</div>
</p-tab>
</p-tablist>
<p-tabpanels>
<p-tabpanel value="0">
<div class="tab-content">
<ul class="chapter-list">
@for (chapter of chapters; track chapter) {
<li (click)="navigateToChapter(chapter); $event.stopPropagation()"
class="chapter-item"
[class.current-chapter]="chapter.href === currentChapterHref"
[style.padding-left.rem]="chapter.level * 1.5 + 0.75">
<i class="pi pi-chevron-right chapter-icon"></i>
<span class="chapter-label">{{ chapter.label }}</span>
</li>
}
</ul>
</div>
</p-tabpanel>
<p-tabpanel value="1">
<div class="tab-content">
<div class="p-2 mb-2">
<p-iconfield class="w-full">
<p-inputicon class="pi pi-search"/>
<input
pInputText
type="text"
[(ngModel)]="filterText"
placeholder="Search bookmarks..."
class="w-full p-inputtext-sm"
(click)="$event.stopPropagation()">
</p-iconfield>
</div>
@if (filteredBookmarks.length === 0) {
<div class="empty-state">
<i class="pi pi-bookmark"></i>
<p>No bookmarks found</p>
<span>{{ filterText ? 'Try a different search term' : 'Tap the bookmark icon to save your place' }}</span>
</div>
} @else {
<ul class="bookmark-list">
@for (bookmark of filteredBookmarks; track bookmark.id) {
<li class="bookmark-item" (click)="navigateToBookmark(bookmark); $event.stopPropagation()">
<i class="pi pi-bookmark-fill bookmark-icon" [style.color]="bookmark.color || 'var(--primary-color)'"></i>
<span class="bookmark-label">{{ bookmark.title }}</span>
<div class="bookmark-actions">
<button
class="bookmark-action-btn"
(click)="openViewDialog(bookmark); $event.stopPropagation()"
pTooltip="View Details"
tooltipPosition="bottom">
<i class="pi pi-info-circle"></i>
</button>
<button
class="bookmark-action-btn"
(click)="openEditBookmarkDialog(bookmark); $event.stopPropagation()"
pTooltip="Edit"
tooltipPosition="bottom">
<i class="pi pi-pencil"></i>
</button>
<button
class="bookmark-action-btn delete"
(click)="deleteBookmark(bookmark.id); $event.stopPropagation()"
pTooltip="Delete"
tooltipPosition="bottom">
<i class="pi pi-trash"></i>
</button>
<p-drawer [(visible)]="isDrawerVisible" [modal]="false" [position]="'left'" [style]="{ width: '320px' }" (click)="$event.stopPropagation()">
<ng-template pTemplate="header">
<div class="drawer-header">
<span class="drawer-title">Table of Contents</span>
</div>
</ng-template>
<p-tabs value="0">
<p-tablist>
<p-tab value="0">
<div class="tab-header">
<i class="pi pi-book"></i>
<span>Chapters</span>
</div>
</li>
}
</ul>
}
</div>
</p-tabpanel>
</p-tabpanels>
</p-tabs>
</p-drawer>
</p-tab>
<p-tab value="1">
<div class="tab-header">
<i class="pi pi-bookmark"></i>
<span>Bookmarks</span>
</div>
</p-tab>
</p-tablist>
<p-tabpanels>
<p-tabpanel value="0">
<div class="tab-content">
<ul class="chapter-list">
@for (chapter of chapters; track chapter) {
<li (click)="navigateToChapter(chapter); $event.stopPropagation()"
class="chapter-item"
[class.current-chapter]="chapter.href === currentChapterHref"
[style.padding-left.rem]="chapter.level * 1.5 + 0.75">
<i class="pi pi-chevron-right chapter-icon"></i>
<span class="chapter-label">{{ chapter.label }}</span>
</li>
}
</ul>
</div>
</p-tabpanel>
<p-tabpanel value="1">
<div class="tab-content">
<div class="p-2 mb-2">
<p-iconfield class="w-full">
<p-inputicon class="pi pi-search"/>
<input
pInputText
type="text"
[(ngModel)]="filterText"
placeholder="Search bookmarks..."
class="w-full p-inputtext-sm"
(click)="$event.stopPropagation()">
</p-iconfield>
</div>
@if (filteredBookmarks.length === 0) {
<div class="empty-state">
<i class="pi pi-bookmark"></i>
<p>No bookmarks found</p>
<span>{{ filterText ? 'Try a different search term' : 'Tap the bookmark icon to save your place' }}</span>
</div>
} @else {
<ul class="bookmark-list">
@for (bookmark of filteredBookmarks; track bookmark.id) {
<li class="bookmark-item" (click)="navigateToBookmark(bookmark); $event.stopPropagation()">
<i class="pi pi-bookmark-fill bookmark-icon" [style.color]="bookmark.color || 'var(--primary-color)'"></i>
<span class="bookmark-label">{{ bookmark.title }}</span>
<div class="bookmark-actions">
<button
class="bookmark-action-btn"
(click)="openViewDialog(bookmark); $event.stopPropagation()"
pTooltip="View Details"
tooltipPosition="bottom">
<i class="pi pi-info-circle"></i>
</button>
<button
class="bookmark-action-btn"
(click)="openEditBookmarkDialog(bookmark); $event.stopPropagation()"
pTooltip="Edit"
tooltipPosition="bottom">
<i class="pi pi-pencil"></i>
</button>
<button
class="bookmark-action-btn delete"
(click)="deleteBookmark(bookmark.id); $event.stopPropagation()"
pTooltip="Delete"
tooltipPosition="bottom">
<i class="pi pi-trash"></i>
</button>
</div>
</li>
}
</ul>
}
</div>
</p-tabpanel>
</p-tabpanels>
</p-tabs>
</p-drawer>
</div>
<div class="header-center">
<p class="text font-medium">{{ currentChapter }}</p>
@@ -305,14 +305,14 @@
</div>
}
</div>
<app-bookmark-edit-dialog
[(visible)]="showEditBookmarkDialog"
[bookmark]="editingBookmark"
[isSaving]="isEditingBookmark"
(save)="onBookmarkSave($event)"
(cancelEdit)="onBookmarkCancel()">
</app-bookmark-edit-dialog>
<app-bookmark-view-dialog
[(visible)]="viewDialogVisible"
[bookmark]="selectedBookmark">
</app-bookmark-view-dialog>
<app-bookmark-edit-dialog
[(visible)]="showEditBookmarkDialog"
[bookmark]="editingBookmark"
[isSaving]="isEditingBookmark"
(save)="onBookmarkSave($event)"
(cancelEdit)="onBookmarkCancel()">
</app-bookmark-edit-dialog>
<app-bookmark-view-dialog
[(visible)]="viewDialogVisible"
[bookmark]="selectedBookmark">
</app-bookmark-view-dialog>
@@ -65,6 +65,9 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
private hideHeaderTimeout?: number;
private isMouseInTopRegion = 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;
showEditBookmarkDialog = false;
@@ -388,65 +391,183 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
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', () => {
const iframe = this.epubContainer.nativeElement.querySelector('iframe');
if (iframe && iframe.contentDocument) {
iframe.contentDocument.addEventListener('touchstart', () => {
this.ngZone.run(() => {
this.onBookTouch();
});
});
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.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 {
return window.innerWidth <= 768;
}
public handleTouchEnd(event: TouchEvent): void {
if (!this.isMobileDevice()) return;
prevPage(): void {
if (this.rendition) {
this.rendition.prev().then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
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;
}
}
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const isTopTouch = touchY < screenHeight * 0.2;
const isInTapZoneVertically = touchY >= screenHeight * 0.2;
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();
}
}
nextPage(): void {
if (this.rendition) {
this.rendition.next().then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
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();
}
}
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
);
});
closeReader(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage
);
}
this.location.back();
}
toggleDrawer(): void {
this.isDrawerVisible = !this.isDrawerVisible;
if (this.isDrawerVisible) {
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
} else {
this.startHeaderAutoHide();
@@ -456,14 +577,40 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
toggleSettingsDrawer(): void {
this.isSettingsDrawerVisible = !this.isSettingsDrawerVisible;
if (this.isSettingsDrawerVisible) {
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
} else {
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 {
if (!this.book || !this.rendition) return;
this.rendition.on('relocated', (location: any) => {
@@ -528,6 +675,15 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
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) {
this.rendition.off('keyup', this.keyListener);
}
@@ -550,23 +706,14 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
onBookClick(event: MouseEvent): void {
const clickX = event.clientX;
const clickY = event.clientY;
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
if (this.isMobileDevice()) {
const isTopClick = clickY < screenHeight * 0.2;
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();
}
this.handleClickAsTap(event);
} else {
const isTopClick = clickY < screenHeight * 0.1;
if (isTopClick) {
@@ -615,28 +762,36 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
}
onBookTouch(): void {
onBookTouch(event?: TouchEvent): void {
if (this.isMobileDevice()) {
this.showControls = true;
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
if (this.hideControlsTimeout) {
window.clearTimeout(this.hideControlsTimeout);
if (event) {
this.handleTouchEnd(event);
} else {
this.showControlsTemporarily();
}
this.hideControlsTimeout = window.setTimeout(() => {
this.ngZone.run(() => {
this.showControls = false;
if (!this.isDrawerVisible && !this.isSettingsDrawerVisible) {
this.showHeader = false;
}
});
}, 2000);
}
}
private showControlsTemporarily(): void {
this.showControls = true;
this.showHeader = true;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
if (this.hideControlsTimeout) {
window.clearTimeout(this.hideControlsTimeout);
}
this.hideControlsTimeout = window.setTimeout(() => {
this.ngZone.run(() => {
this.showControls = false;
if (!this.isDrawerVisible && !this.isSettingsDrawerVisible) {
this.showHeader = false;
}
});
}, 2000);
}
startHeaderAutoHide(): void {
this.clearHeaderTimeout();
@@ -834,14 +989,4 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.showEditBookmarkDialog = false;
this.editingBookmark = null;
}
closeReader(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage
);
}
this.location.back();
}
}