fix(readers): fix mobile text selection (#2667)

This commit is contained in:
Alex
2026-09-18 22:37:24 +01:00
committed by GitHub
parent c8a1a59bbf
commit 6a778f82e5
6 changed files with 41 additions and 6 deletions
@@ -63,6 +63,7 @@ export type ViewEvent =
| { type: 'draw-annotation'; detail: DrawAnnotationEventDetail }
| { type: 'show-annotation'; detail?: unknown }
| { type: 'text-selected'; detail: TextSelection; popupPosition: PopupPosition }
| { type: 'text-deselected' }
| { type: 'toggle-fullscreen' }
| { type: 'toggle-shortcuts-help' }
| { type: 'escape-pressed' }
@@ -338,6 +339,7 @@ export class ReaderEventService {
this.selectionChangeTimeout = setTimeout(() => {
const selection = doc.defaultView?.getSelection();
if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
this.eventSubject.next({type: 'text-deselected'});
return;
}
@@ -411,7 +413,6 @@ export class ReaderEventService {
if (hasSelection) {
this.isTextSelectionInProgress = false;
event.preventDefault();
setTimeout(() => {
this.handleSelectionEnd(doc);
@@ -1,4 +1,4 @@
import {ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, effect, HostListener, inject, OnInit, signal} from '@angular/core';
import {ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, effect, HostListener, inject, OnInit, signal, ViewChild} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {forkJoin, from, Observable, of, throwError} from 'rxjs';
import {catchError, map, switchMap, tap} from 'rxjs/operators';
@@ -115,6 +115,9 @@ export class EbookReaderComponent implements OnInit {
private pendingInitialChapterRestore: PendingInitialChapterRestore | null = null;
private pendingInitialChapterRestoreTimeout?: ReturnType<typeof setTimeout>;
@ViewChild(TextSelectionPopupComponent, {static: true})
private selectionPopup!: TextSelectionPopupComponent;
isLoading = signal(true);
showQuickSettings = signal(false);
showControls = signal(false);
@@ -338,6 +341,9 @@ export class EbookReaderComponent implements OnInit {
case 'text-selected':
this.selectionService.handleTextSelected(event.detail, event.popupPosition);
break;
case 'text-deselected':
this.selectionPopup.onDismiss();
break;
case 'toggle-fullscreen':
this.toggleFullscreen();
break;
@@ -70,6 +70,9 @@ export class ReaderSelectionService {
}
handleTextSelected(detail: SelectionDetail, popupPosition?: { x: number; y: number; showBelow?: boolean }): void {
if (this.previewCfi && this.previewCfi !== detail.cfi && this.previewColor && this.previewStyle) {
this.updatePreview(detail.cfi, this.previewColor, this.previewStyle);
}
this.currentSelection = detail;
this._overlappingAnnotationId = this.findOverlappingAnnotation(detail.cfi);
this._visible = true;
@@ -2,6 +2,7 @@
@if (visible) {
<div class="popup-backdrop" role="button" tabindex="0" (click)="onDismiss($event)" (keydown.enter)="onDismiss($event)"></div>
<div class="text-selection-popup"
(mousedown)="$event.preventDefault()"
[class.show-below]="showBelow"
[style.left.px]="position.x"
[style.top.px]="position.y">
@@ -7,6 +7,12 @@
background: transparent;
}
@media (pointer: coarse) {
.popup-backdrop {
pointer-events: none;
}
}
.text-selection-popup {
@include surface;
@@ -1,4 +1,4 @@
import {Component, EventEmitter, Input, Output} from '@angular/core';
import {Component, EventEmitter, HostListener, Input, Output} from '@angular/core';
import {TranslocoDirective} from '@jsverse/transloco';
import {ReaderIconComponent} from './icon.component';
@@ -134,9 +134,27 @@ export class TextSelectionPopupComponent {
this.hasPreview = false;
}
onDismiss(event: Event): void {
event.stopPropagation();
event.preventDefault();
@HostListener('document:touchend', ['$event'])
onDocumentTouchEnd(event: TouchEvent): void {
if (!window.matchMedia('(pointer: coarse)').matches) {
return;
}
const target = event.target;
if (target instanceof Element && target.closest('.text-selection-popup, foliate-view')) {
return;
}
this.onDismiss();
}
onDismiss(event?: Event): void {
if (!this.visible) {
return;
}
event?.stopPropagation();
event?.preventDefault();
if (this.hasPreview) {
this.action.emit({