diff --git a/frontend/src/app/features/readers/ebook-reader/core/event.service.ts b/frontend/src/app/features/readers/ebook-reader/core/event.service.ts index 49822ed61..a125f53e4 100644 --- a/frontend/src/app/features/readers/ebook-reader/core/event.service.ts +++ b/frontend/src/app/features/readers/ebook-reader/core/event.service.ts @@ -2,12 +2,6 @@ import {inject, Injectable} from '@angular/core'; import {Subject} from 'rxjs'; import {ReaderAnnotationService} from '../features/annotations/annotation-renderer.service'; -export interface ViewEvent { - type: 'load' | 'relocate' | 'error' | 'middle-single-tap' | 'draw-annotation' | 'show-annotation' | 'text-selected' | 'toggle-fullscreen' | 'toggle-shortcuts-help' | 'escape-pressed' | 'go-first-section' | 'go-last-section' | 'toggle-toc' | 'toggle-search' | 'toggle-notes'; - detail?: any; - popupPosition?: { x: number; y: number; showBelow?: boolean }; -} - export interface TextSelection { text: string; cfi: string; @@ -15,6 +9,68 @@ export interface TextSelection { index: number; } +interface PopupPosition { + x: number; + y: number; + showBelow?: boolean; +} + +interface LoadEventDetail { + doc?: Document; +} + +interface RelocateEventItem { + href?: string; + label?: string; +} + +interface RelocateEventDetail { + cfi?: string | null; + fraction?: number; + tocItem?: RelocateEventItem; + pageItem?: RelocateEventItem; + section?: { current: number; total: number }; + time?: { section?: number; total?: number }; +} + +interface DrawAnnotationEventDetail { + draw: (overlayer: (rects: DOMRectList, options: { color?: string }) => SVGElement, options: { color: string }) => void; + annotation: { value: string }; + doc: Document; + range: Range; +} + +interface IframeClickMessage { + type: 'iframe-click'; + clientX: number; + clientY: number; + iframeLeft: number; + iframeWidth: number; + eventClientX: number; + target?: string; +} + +interface EventServiceView extends HTMLElement { + addAnnotation(annotation: { value: string }): void; +} + +export type ViewEvent = + | { type: 'load'; detail?: LoadEventDetail } + | { type: 'relocate'; detail: RelocateEventDetail } + | { type: 'error'; detail?: unknown } + | { type: 'middle-single-tap' } + | { type: 'draw-annotation'; detail: DrawAnnotationEventDetail } + | { type: 'show-annotation'; detail?: unknown } + | { type: 'text-selected'; detail: TextSelection; popupPosition: PopupPosition } + | { type: 'toggle-fullscreen' } + | { type: 'toggle-shortcuts-help' } + | { type: 'escape-pressed' } + | { type: 'go-first-section' } + | { type: 'go-last-section' } + | { type: 'toggle-toc' } + | { type: 'toggle-search' } + | { type: 'toggle-notes' }; + interface ViewCallbacks { prev: () => void; next: () => void; @@ -34,7 +90,7 @@ export class ReaderEventService { private annotationService = inject(ReaderAnnotationService); - private view: any; + private view: EventServiceView | null = null; private viewCallbacks: ViewCallbacks | null = null; private isNavigating = false; private lastClickTime = 0; @@ -53,7 +109,7 @@ export class ReaderEventService { private eventSubject = new Subject(); public events$ = this.eventSubject.asObservable(); - initialize(view: any, callbacks: ViewCallbacks): void { + initialize(view: EventServiceView, callbacks: ViewCallbacks): void { this.view = view; this.viewCallbacks = callbacks; this.attachViewEventListeners(); @@ -78,7 +134,8 @@ export class ReaderEventService { private attachViewEventListeners(): void { if (!this.view) return; - this.view.addEventListener('load', (e: any) => { + this.view.addEventListener('load', (event: Event) => { + const e = event as CustomEvent; this.eventSubject.next({type: 'load', detail: e.detail}); if (e.detail?.doc) { if (this.keydownHandler) { @@ -97,15 +154,18 @@ export class ReaderEventService { } }); - this.view.addEventListener('relocate', (e: any) => { + this.view.addEventListener('relocate', (event: Event) => { + const e = event as CustomEvent; this.eventSubject.next({type: 'relocate', detail: e.detail}); }); - this.view.addEventListener('error', (e: any) => { + this.view.addEventListener('error', (event: Event) => { + const e = event as CustomEvent; this.eventSubject.next({type: 'error', detail: e.detail}); }); - this.view.addEventListener('draw-annotation', (e: any) => { + this.view.addEventListener('draw-annotation', (event: Event) => { + const e = event as CustomEvent; const {draw, annotation, doc, range} = e.detail; const storedStyle = this.annotationService.getAnnotationStyle(annotation.value); if (storedStyle) { @@ -115,7 +175,8 @@ export class ReaderEventService { this.eventSubject.next({type: 'draw-annotation', detail: {annotation, doc, range}}); }); - this.view.addEventListener('show-annotation', (e: any) => { + this.view.addEventListener('show-annotation', (event: Event) => { + const e = event as CustomEvent; this.eventSubject.next({type: 'show-annotation', detail: e.detail}); }); } @@ -170,12 +231,16 @@ export class ReaderEventService { private attachWindowMessageHandler(): void { window.addEventListener('message', (event) => { - if (event.data?.type === 'iframe-click') { + if (this.isIframeClickMessage(event.data)) { this.handleIframeClickMessage(event.data); } }); } + private isIframeClickMessage(value: unknown): value is IframeClickMessage { + return !!value && typeof value === 'object' && 'type' in value && value.type === 'iframe-click'; + } + private attachIframeEventHandlers(doc: Document): void { if (this.clickedDocs.has(doc)) { return; @@ -416,7 +481,7 @@ export class ReaderEventService { }, 10); } - private handleIframeClickMessage(data: any): void { + private handleIframeClickMessage(data: IframeClickMessage): void { if (!this.view) return; const now = Date.now(); @@ -454,7 +519,7 @@ export class ReaderEventService { }, this.DOUBLE_CLICK_INTERVAL_MS); } - private processIframeClick(data: any): void { + private processIframeClick(data: IframeClickMessage): void { if (!this.longHoldTimeout) { return; } diff --git a/frontend/src/app/features/readers/ebook-reader/core/view-manager.service.ts b/frontend/src/app/features/readers/ebook-reader/core/view-manager.service.ts index e720c6942..f8f5395d9 100644 --- a/frontend/src/app/features/readers/ebook-reader/core/view-manager.service.ts +++ b/frontend/src/app/features/readers/ebook-reader/core/view-manager.service.ts @@ -9,6 +9,12 @@ import {EpubStreamingService, EpubBookInfo} from './epub-streaming.service'; export type {ViewEvent, TextSelection} from './event.service'; export type {PageInfo, ThemeInfo} from '../shared/header-footer.util'; +interface FoliateTocItem { + label: string; + href: string; + subitems?: FoliateTocItem[]; +} + interface TocItem { label: string; href: string; @@ -24,7 +30,73 @@ export interface BookMetadata { identifier?: string; coverUrl?: string; - [key: string]: any; + [key: string]: unknown; +} + +interface RendererContent { + index: number; + doc: Document; +} + +export interface FoliateRenderer { + heads?: HTMLElement[]; + feet?: HTMLElement[]; + getContents(): RendererContent[]; + setAttribute(name: string, value: string | number): void; + removeAttribute(name: string): void; + setStyles?(css: string): void; +} + +interface FoliateBook { + toc?: FoliateTocItem[]; + metadata?: BookMetadata; + getCover?(): Promise | null; +} + +interface FoliateSearchSubitem { + cfi: string; + excerpt: { + pre: string; + match: string; + post: string; + }; +} + +interface FoliateSearchProgress { + progress: number; +} + +interface FoliateSearchSectionResult { + label?: string; + subitems?: FoliateSearchSubitem[]; +} + +type FoliateSearchResult = FoliateSearchProgress | FoliateSearchSectionResult | 'done'; + +interface FoliateViewElement extends HTMLElement { + renderer?: FoliateRenderer | null; + book?: FoliateBook; + open(target: File | object): Promise; + goTo(target: string | number): Promise; + goToFraction(fraction: number): Promise; + prev(): void; + next(): void; + getCFI(index: number, range: Range): string | null; + deselect(): void; + addAnnotation(annotation: { value: string }): void; + getSectionFractions?(): number[]; + search?(opts: { query: string; matchCase?: boolean; matchWholeWords?: boolean }): AsyncGenerator; + clearSearch?(): void; +} + +interface StreamingBookFactoryWindow extends Window { + makeStreamingBook?: ( + bookId: number, + baseUrl: string, + bookInfo: EpubBookInfo, + authToken: string | null, + bookType?: string + ) => Promise; } @Injectable({ @@ -34,14 +106,14 @@ export class ReaderViewManagerService { private annotationService = inject(ReaderAnnotationService); private eventService = inject(ReaderEventService); private epubStreamingService = inject(EpubStreamingService); - private view: any; + private view: FoliateViewElement | null = null; public get events$(): Observable { return this.eventService.events$; } createView(container: HTMLElement): void { - this.view = document.createElement('foliate-view'); + this.view = document.createElement('foliate-view') as FoliateViewElement; this.view.style.width = '100%'; this.view.style.height = '100%'; this.view.style.display = 'block'; @@ -92,7 +164,7 @@ export class ReaderViewManagerService { } private async openStreamingBook(bookId: number, bookInfo: EpubBookInfo, bookType?: string): Promise { - const makeStreamingBook = (window as any).makeStreamingBook; + const makeStreamingBook = (window as StreamingBookFactoryWindow).makeStreamingBook; if (!makeStreamingBook) { throw new Error('makeStreamingBook not available - Foliate script may not be loaded'); } @@ -141,8 +213,8 @@ export class ReaderViewManagerService { this.view?.next(); } - getRenderer(): any { - return this.view?.renderer; + getRenderer(): FoliateRenderer | null { + return this.view?.renderer ?? null; } getSelection(): TextSelection | null { @@ -192,7 +264,7 @@ export class ReaderViewManagerService { getChapters(): TocItem[] { if (!this.view?.book?.toc) return []; - const mapToc = (items: any[]): TocItem[] => + const mapToc = (items: FoliateTocItem[]): TocItem[] => items.map(item => ({ label: item.label, href: item.href, @@ -244,7 +316,7 @@ export class ReaderViewManagerService { ); } - async* search(opts: { query: string; matchCase?: boolean; matchWholeWords?: boolean }): AsyncGenerator { + async* search(opts: { query: string; matchCase?: boolean; matchWholeWords?: boolean }): AsyncGenerator { if (!this.view?.search) return; yield* this.view.search(opts); }