diff options
Diffstat (limited to 'src/reader.ts')
| -rw-r--r-- | src/reader.ts | 177 |
1 files changed, 112 insertions, 65 deletions
diff --git a/src/reader.ts b/src/reader.ts index dbd505b..55c9844 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -2,6 +2,33 @@ 'use strict'; +declare var createHyphenator: any; + +declare var hyphenationPatternsEnUs: any; +declare var hyphenationPatternsRu: any; + +declare interface HTMLElement { checked: boolean } +declare interface Event { deltaY: number, deltaX: number } +declare interface Node { data: string } + +interface NetworkError extends Error { + responseText?: string; // optional +} + +declare type SwipeState = { + active: boolean, + startX: number, + startY: number, + startTime: number, + direction: 'left'|'right'|''; +} + +declare type SwipeConfig = { + minDistance: number, + velocityThreshold: number, + verticalDistanceThreshold: number +} + const DEFAULT_FONT_SIZE = 16; const DEFAULT_FONT_FAMILY = "Arial"; const DEFAULT_LINE_HEIGHT = 140; @@ -43,7 +70,7 @@ class ReaderClass extends AppCommon { startX: 0, startY: 0, startTime: 0, - direction: "", + direction: '', }; /** @type {SwipeConfig} */ @@ -174,9 +201,9 @@ class ReaderClass extends AppCommon { } /** - * @param {boolean} offline + * @param offline Whether the app is in offline mode. */ - async onOfflineModeChanged(offline: any) { + async onOfflineModeChanged(offline: boolean) { console.log('onOfflineModeChanged', offline); if (!offline && this.book) { @@ -349,9 +376,9 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Book} book + * @param book The ePub book whose info should be displayed. */ - async showBookInfo(book: any) { + async showBookInfo(book: ePub.Book) { const bookinfo = await localforage.getItem(this.cacheId()) as BookInfo; let title; @@ -380,10 +407,10 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Book} book + * @param book The ePub book whose locations should be loaded. */ - async loadLocations(book: any) { - let locations = await localforage.getItem(this.cacheId("locations")); + async loadLocations(book: ePub.Book) { + let locations = await localforage.getItem(this.cacheId("locations")) as string[]; // legacy format is array of objects {cfi: ..., page: ...} if (locations && typeof locations[0] == "string") { @@ -458,10 +485,10 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Book} book - * @param {ePub.Rendition} rendition + * @param book The ePub book instance. + * @param rendition The epub.js rendition to hook into. */ - initRenditionHooks(book: any, rendition: any) { + initRenditionHooks(book: ePub.Book, rendition: ePub.Rendition) { console.log('initRenditionHooks'); book.spine.hooks.content.register((doc: any/*, section */) => { @@ -748,9 +775,9 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Book} book + * @param book The ePub book whose TOC should be initialized. */ - initToc(book: any) { + initToc(book: ePub.Book) { const _reader = this; function toc_loc_msg(href: any) { @@ -772,11 +799,11 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.NavItem} row - * @param {JQuery<HTMLElement>} list - * @param {number} nest + * @param row The TOC navigation item to process. + * @param list The jQuery element containing the TOC sublist. + * @param nest Current nesting depth (max 3). */ - function process_toc_sublist(row: any, list: any, nest: any) { + function process_toc_sublist(row: ePub.NavItem, list: JQuery<HTMLElement>, nest: number) { if (nest == 3) return false; @@ -858,9 +885,9 @@ class ReaderClass extends AppCommon { /* embedded styles may conflict with our font sizes, etc */ /** - * @param {Document} doc + * @param doc The Document to clean up embedded styles from. */ - cleanupEmbeddedStyles(doc: any) { + cleanupEmbeddedStyles(doc: Document) { $(doc).find("p") .filter((i: any, e: any) => (($(e).text().length >= MIN_LENGTH_TO_JUSTIFY) ? e : null)) .css("text-align", "justify"); @@ -902,9 +929,9 @@ class ReaderClass extends AppCommon { } /** - * @param {string} base_url + * @param base_url The base URL for resolving font paths. */ - generateFontsCss(base_url: any) { + generateFontsCss(base_url: string) { return ` /** Calibri */ @@ -1079,10 +1106,10 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.NavItem} entry - * @param {number} nest + * @param entry The TOC entry whose sub-items should be flattened. + * @param nest Current nesting depth (max 3). */ - flattenTocSubItems(entry: any, nest: any) { + flattenTocSubItems(entry: ePub.NavItem, nest: number) { let rv: any = []; if (nest == 3) return false; @@ -1100,11 +1127,11 @@ class ReaderClass extends AppCommon { } /** - * @param { ePub.Book } book - * - * @returns { ePub.NavItem[] } + * Flattens the book's TOC into a single array of navigation items. + * @param book The ePub book whose TOC should be flattened. + * @returns An array of all TOC entries including nested sub-items. */ - flattenToc(book: any) { + flattenToc(book: ePub.Book): ePub.NavItem[] { if (this._flattened_toc) { return this._flattened_toc; } else { @@ -1124,10 +1151,10 @@ class ReaderClass extends AppCommon { } /** - * @param { ePub.Book } book - * @param { any[] } toc + * @param book The ePub book whose TOC bar should be generated. + * @param toc Array of TOC entries to render in the bar. */ - generateTocBar(book: any, toc: any) { + generateTocBar(book: ePub.Book, toc: any[]) { $(".spacer") .html(""); @@ -1162,10 +1189,10 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Book} book - * @param {ePub.Location} location + * @param book The ePub book instance. + * @param location The current reading location to position the TOC bar at. */ - updateTocBarPosition(book: any, location: any) { + updateTocBarPosition(book: ePub.Book, location: ePub.Location) { const cur_pct = Math.round(location.start.location / book.locations.length() * 100); $(".toc-bar-entry.current-position") @@ -1255,9 +1282,9 @@ class ReaderClass extends AppCommon { } /** - * @param {JQuery.KeyUpEvent<Document, undefined, Document, Document>} e + * @param e The keyboard up event to handle. */ - hotkeyHandler(e: any) { + hotkeyHandler(e: JQuery.KeyUpEvent<Document, undefined, Document, Document>) { try { //console.log('K3:' + e.which, e); @@ -1347,15 +1374,18 @@ class ReaderClass extends AppCommon { } } - /** @param {string} [suffix] */ - cacheId(suffix: string = "") { + /** + * Get the cache key for this book, optionally with a suffix. + * @param suffix Optional suffix to append to the cache key. + */ + cacheId(suffix?: string): string { return "epube-book." + this.urlParam("b") + (suffix ? "." + suffix : ""); } /** - * @param {boolean} show + * @param show Whether to show (true) or hide (false) the UI. */ - showUI(show: any) { + showUI(show: boolean) { if (show) $(".header,.footer").fadeIn(); else @@ -1471,9 +1501,10 @@ class ReaderClass extends AppCommon { } /** - * @param {HTMLElement} elem + * Navigate to the previously stored CFI location. + * @param elem The HTMLElement containing the data-location-cfi attribute. */ - async openPrevious(elem: any) { + async openPrevious(elem: HTMLElement) { const cfi = $(elem).attr("data-location-cfi"); if (cfi) @@ -1498,10 +1529,11 @@ class ReaderClass extends AppCommon { } /** - * @param { ePub.Book } book - * @param { ePub.Location } location + * Called when the reader's location is relocated (page turn, etc.). + * @param book The ePub book instance. + * @param location The new reading location. */ - async onRelocated(book: any, location: any) { + async onRelocated(book: ePub.Book, location: ePub.Location) { // locations not generated yet if (book.locations.length() == 0) return; @@ -1639,10 +1671,12 @@ class ReaderClass extends AppCommon { } /** - * @param {?string} a - * @param {?string} b + * Calculate the delta (distance) between two CFI strings. + * Returns -1 if they cannot be compared or have different step counts/ids. + * @param a First CFI string (may be null). + * @param b Second CFI string (may be null). */ - calculateCfiDelta(a: any, b: any) { + calculateCfiDelta(a: string | null, b: string | null) { if (!a || !b) return -1; @@ -1675,10 +1709,11 @@ class ReaderClass extends AppCommon { } /** - * @param {ePub.Rendition} rendition - * @param {string} cfi + * Attempt to navigate to a CFI and verify the move was successful. + * @param rendition The ePub.js rendition to display on. + * @param cfi The target CFI string to navigate to. */ - async moveToCfiVerify(rendition: any, cfi: any, attempts = 5, maxDelta = 5) { + async moveToCfiVerify(rendition: ePub.Rendition, cfi: string, attempts = 5, maxDelta = 5) { for (let att = 1; att < attempts; att++) { this.blockLastReadUpdate(); @@ -1713,40 +1748,44 @@ class ReaderClass extends AppCommon { } /** - * @param {HTMLElement} elem + * Apply the selected theme after a change. + * @param elem The HTMLElement containing the selected theme value. */ - async onThemeChanged(elem: any) { - const theme = $(elem).val(); + async onThemeChanged(elem: HTMLElement) { + const theme = $(elem).val() as string; await localforage.setItem("epube.theme", theme); this.applyTheme(); } /** - * @param {HTMLElement} elem + * Apply the selected line height after a change. + * @param elem The HTMLElement containing the selected line height value. */ - async onLineHeightChanged(elem: any) { - const height = $(elem).val(); + async onLineHeightChanged(elem: HTMLElement) { + const height = $(elem).val() as string; await localforage.setItem("epube.lineHeight", height); this.applyTheme(); } /** - * @param {HTMLElement} elem + * Apply the selected font size after a change. + * @param elem The HTMLElement containing the selected font size value. */ - async onTextSizeChanged(elem: any) { - const size = $(elem).val(); + async onTextSizeChanged(elem: HTMLElement) { + const size = $(elem).val() as string; await localforage.setItem("epube.fontSize", size); this.applyTheme(); } /** - * @param {HTMLElement} elem + * Apply the selected font family after a change. + * @param elem The HTMLElement containing the selected font value. */ - async onFontChanged(elem: any) { - const font = $(elem).val(); + async onFontChanged(elem: HTMLElement) { + const font = $(elem).val() as string; await localforage.setItem("epube.fontFamily", font); this.applyTheme(); @@ -1757,11 +1796,19 @@ class ReaderLoader { _res_data = {}; /** - * @param {ReaderClass} app + * Initialize the resource loader by reading cached assets. + * @param app The ReaderClass instance to use for cache operations. */ - async init(app: any) { + async init(app: ReaderClass) { const cacheName = await app.getActiveCacheName(); + if (!cacheName) { + console.error('failed to determine active cache name'); + + $(".loading-message").html("Failed to determine active cache name"); + return; + } + console.log('loader init for cache:', cacheName); if (typeof EpubeApp != "undefined") { |
