feat(pdf): move ebook reader to embedpdf, add bookmarks, remove old pdf.js stuff (#393)

* feat(pdf): move ebook reader to embedpdf, add bookmarks, remove old pdf.js stuff

* feat(pdf): add page number support for bookmarks and enhance mobile navigation

* feat(pdf): implement document ID tracking for search functionality

* feat(pdf): add page number column to book marks and update unique index

* feat(pdf): enhance annotation handling and improve search result scrolling

* feat(pdf): improve layout handling and enhance resize observer functionality

* feat(pdf): improve document viewer initialization and improve close button styling

* fix(pdf): remove 'form' from disabled categories

* feat(pdf): add pan mode functionality and enhance sidebar layout

* feat(pdf): improve book viewer initialization with enhanced element detection

* feat(pdf): add link handling to text selection and improve popup actions

* feat(pdf): enhance link detection and improve selection handling in event service

* feat(pdf): improve accessibility and improve resource management in PDF viewer

* feat(pdf): add resize observer for improved layout handling

* feat(pdf): error handling and improve navigation in PDF reader
This commit is contained in:
brios
2026-04-22 17:27:07 -04:00
committed by Zach Yale
parent e3b22ed9c6
commit 6c8a907325
29 changed files with 3681 additions and 724 deletions
@@ -12,12 +12,18 @@ interface TestView extends HTMLDivElement {
interface RangeLike {
toString: () => string;
getBoundingClientRect: () => DOMRect;
commonAncestorContainer: Node;
startContainer: Node;
endContainer: Node;
intersectsNode: (node: Node) => boolean;
}
interface TestSelection {
isCollapsed: boolean;
rangeCount: number;
getRangeAt: (index: number) => RangeLike;
anchorNode: Node | null;
focusNode: Node | null;
}
interface PrivateReaderEventService {
@@ -138,7 +144,13 @@ describe('ReaderEventService', () => {
getRangeAt: () => ({
toString: () => '',
getBoundingClientRect: () => new DOMRect(0, 0, 0, 0),
commonAncestorContainer: doc.body,
startContainer: doc.body,
endContainer: doc.body,
intersectsNode: () => false,
}),
anchorNode: null,
focusNode: null,
};
defaultView = {
frameElement: iframe,
@@ -296,11 +308,17 @@ describe('ReaderEventService', () => {
const selectedRange: RangeLike = {
toString: () => 'Highlighted text',
getBoundingClientRect: () => new DOMRect(20, 15, 80, 20),
commonAncestorContainer: doc.body,
startContainer: doc.body,
endContainer: doc.body,
intersectsNode: () => false,
};
installSelection(doc, {
isCollapsed: false,
rangeCount: 1,
getRangeAt: () => selectedRange,
anchorNode: doc.body,
focusNode: doc.body,
});
getContents.mockReturnValue([{index: 7, doc}]);
getCFI.mockReturnValue('epubcfi(/6/2!/4/2:0)');
@@ -324,11 +342,17 @@ describe('ReaderEventService', () => {
const emptyRange: RangeLike = {
toString: () => ' ',
getBoundingClientRect: () => new DOMRect(0, 0, 0, 0),
commonAncestorContainer: doc.body,
startContainer: doc.body,
endContainer: doc.body,
intersectsNode: () => false,
};
installSelection(doc, {
isCollapsed: false,
rangeCount: 1,
getRangeAt: () => emptyRange,
anchorNode: doc.body,
focusNode: doc.body,
});
privateService.handleSelectionChange(doc);
@@ -338,11 +362,17 @@ describe('ReaderEventService', () => {
const visibleRange: RangeLike = {
toString: () => 'Visible note',
getBoundingClientRect: () => new DOMRect(10, 10, 60, 25),
commonAncestorContainer: doc.body,
startContainer: doc.body,
endContainer: doc.body,
intersectsNode: () => false,
};
installSelection(doc, {
isCollapsed: false,
rangeCount: 1,
getRangeAt: () => visibleRange,
anchorNode: doc.body,
focusNode: doc.body,
});
getContents.mockReturnValue([{index: 3, doc}]);
getCFI.mockReturnValueOnce(null).mockReturnValueOnce('epubcfi(/6/8!/4/2:0)');
@@ -7,6 +7,7 @@ export interface TextSelection {
cfi: string;
range: Range;
index: number;
linkUrl?: string;
}
interface PopupPosition {
@@ -476,15 +477,58 @@ export class ReaderEventService {
popupX = Math.max(100, Math.min(popupX, window.innerWidth - 150));
const linkUrl = this.getLinkUrl(range, selection);
this.eventSubject.next({
type: 'text-selected',
detail: {text, cfi, range, index},
detail: {text, cfi, range, index, linkUrl},
popupPosition: {x: popupX, y: popupY, showBelow}
});
}
}, 10);
}
private getLinkUrl(range: Range, selection: Selection): string | undefined {
const getLinkFromNode = (node: Node | null | undefined): string | undefined => {
let current: Node | null | undefined = node;
while (current && current.nodeType !== Node.DOCUMENT_NODE) {
if (current.nodeType === Node.ELEMENT_NODE) {
const element = current as HTMLElement;
if (element.tagName?.toLowerCase() === 'a') {
return element.getAttribute('href') || undefined;
}
const closestLink = typeof element.closest === 'function' ? element.closest('a') : null;
if (closestLink?.getAttribute('href')) {
return closestLink.getAttribute('href')!;
}
}
current = current.parentNode;
}
return undefined;
};
// Check ancestors and self for common ancestor, start, end, anchor and focus nodes
let linkUrl = getLinkFromNode(range.commonAncestorContainer) ||
getLinkFromNode(range.startContainer) ||
getLinkFromNode(range.endContainer) ||
getLinkFromNode(selection.anchorNode) ||
getLinkFromNode(selection.focusNode);
// If still not found, check if the range contains any links
if (!linkUrl && range.commonAncestorContainer?.nodeType === Node.ELEMENT_NODE) {
const containerElement = range.commonAncestorContainer as HTMLElement;
// Find all links in the container
if (typeof containerElement.querySelectorAll === 'function') {
const links = Array.from(containerElement.querySelectorAll('a'));
// Find the first link that intersects with the selection range
const internalLink = links.find(link => typeof range.intersectsNode === 'function' && range.intersectsNode(link));
linkUrl = internalLink?.getAttribute('href') || undefined;
}
}
return linkUrl;
}
private handleIframeClickMessage(data: IframeClickMessage): void {
if (!this.view) return;
@@ -73,6 +73,7 @@
[showBelow]="selectionState().showBelow"
[overlappingAnnotationId]="selectionState().overlappingAnnotationId"
[selectedText]="selectionState().selectedText"
[linkUrl]="selectionState().linkUrl"
(action)="handleSelectionAction($event)"
></app-text-selection-popup>
@@ -345,12 +345,12 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
this.toggleImmersiveMode();
break;
case 'go-first-section':
this.viewManager.goToSection(0).subscribe();
this.viewManager.goToSection(0).pipe(catchError(() => of(undefined))).subscribe();
break;
case 'go-last-section': {
const s = this.progressService.currentProgressData?.section;
if (s && s.total > 0) {
this.viewManager.goToSection(s.total - 1).subscribe();
this.viewManager.goToSection(s.total - 1).pipe(catchError(() => of(undefined))).subscribe();
}
break;
}
@@ -429,7 +429,10 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
onProgressChange(fraction: number): void {
this.viewManager.goToFraction(fraction)
.pipe(takeUntil(this.destroy$))
.pipe(
takeUntil(this.destroy$),
catchError(() => of(undefined))
)
.subscribe();
}
@@ -481,6 +484,17 @@ export class EbookReaderComponent implements OnInit, OnDestroy {
handleSelectionAction(action: TextSelectionAction): void {
if (action.type === 'note') {
this.noteService.openNewNoteDialog();
} else if (action.type === 'go-to-link') {
const linkUrl = this.selectionState().linkUrl;
if (linkUrl) {
this.viewManager.goTo(linkUrl)
.pipe(
takeUntil(this.destroy$),
catchError(() => of(undefined))
)
.subscribe();
}
this.selectionService.handleAction(action);
} else {
this.selectionService.handleAction(action);
}
@@ -14,6 +14,7 @@ export interface SelectionState {
showBelow: boolean;
overlappingAnnotationId: number | null;
selectedText: string;
linkUrl?: string;
}
export interface SelectionDetail {
@@ -21,6 +22,7 @@ export interface SelectionDetail {
cfi: string;
range: Range;
index: number;
linkUrl?: string;
}
@Injectable()
@@ -48,7 +50,8 @@ export class ReaderSelectionService {
position: {x: 0, y: 0},
showBelow: false,
overlappingAnnotationId: null,
selectedText: ''
selectedText: '',
linkUrl: undefined
};
private readonly _state = signal<SelectionState>(this.defaultState);
@@ -121,6 +124,10 @@ export class ReaderSelectionService {
action.color || '#FFFF00',
action.style || 'highlight'
);
} else if (action.type === 'go-to-link' && this.currentSelection?.linkUrl) {
// Handled by component, but we clear state here
this.clearPreview();
this.viewManager.clearSelection();
}
this.currentSelection = null;
@@ -283,7 +290,8 @@ export class ReaderSelectionService {
position: this._position,
showBelow: this._showBelow,
overlappingAnnotationId: this._overlappingAnnotationId,
selectedText: this.currentSelection?.text || ''
selectedText: this.currentSelection?.text || '',
linkUrl: this.currentSelection?.linkUrl
});
}
@@ -35,7 +35,8 @@ export type ReaderIconName =
| 'long-strip'
| 'direction-ltr'
| 'direction-rtl'
| 'magnifier';
| 'magnifier'
| 'link';
interface IconPath {
d: string;
@@ -199,6 +200,10 @@ const ICONS: Record<ReaderIconName, IconPath[]> = {
{d: 'M21 21l-5-5'},
{d: 'M7,10 L13,10', type: 'line'},
{d: 'M10,7 L10,13', type: 'line'}
],
'link': [
{d: 'M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71'},
{d: 'M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71'}
]
};
@@ -10,6 +10,13 @@
<app-reader-icon name="copy" [size]="16"></app-reader-icon>
</button>
@if (linkUrl) {
<div class="divider"></div>
<button class="action-btn" (click)="onGoToLink()" [title]="t('goToLink')">
<app-reader-icon name="link" [size]="16"></app-reader-icon>
</button>
}
<div class="divider"></div>
<button class="action-btn" (click)="onSearch()" [title]="t('searchInBook')">
@@ -6,7 +6,7 @@ import {ReaderIconComponent} from './icon.component';
export type AnnotationStyle = 'highlight' | 'underline' | 'strikethrough' | 'squiggly';
export interface TextSelectionAction {
type: 'select' | 'annotate' | 'delete' | 'dismiss' | 'preview' | 'search' | 'note';
type: 'select' | 'annotate' | 'delete' | 'dismiss' | 'preview' | 'search' | 'note' | 'go-to-link';
color?: string;
style?: AnnotationStyle;
annotationId?: number;
@@ -37,6 +37,7 @@ export class TextSelectionPopupComponent {
@Input() showBelow = false;
@Input() overlappingAnnotationId: number | null = null;
@Input() selectedText = '';
@Input() linkUrl?: string;
@Output() action = new EventEmitter<TextSelectionAction>();
showAnnotationOptions = false;
@@ -122,6 +123,12 @@ export class TextSelectionPopupComponent {
this.hasPreview = false;
}
onGoToLink(): void {
if (this.linkUrl) {
this.action.emit({type: 'go-to-link'});
}
}
onNote(): void {
this.action.emit({type: 'note'});
this.showAnnotationOptions = false;