diff options
| -rw-r--r-- | src/index.d.ts | 31 | ||||
| -rw-r--r-- | src/reader.ts | 51 |
2 files changed, 49 insertions, 33 deletions
diff --git a/src/index.d.ts b/src/index.d.ts index 1e6a886..002b179 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -53,6 +53,31 @@ declare const BUILD_MANIFEST_ASSETS: []; declare const BUILD_VERSION: string; -declare var hyphenationPatternsEnUs; -declare var hyphenationPatternsRu; -declare var createHyphenator; +/** Pattern definition as produced by the hyphen library's build process. */ +interface HyphenPatternDefinition { + patterns: string[]; + exceptions?: string[]; +} + +/** Optional configuration for createHyphenator. */ +interface CreateHyphenatorOptions { + async?: boolean; + debug?: boolean; + html?: boolean; + hyphenChar?: string; + minWordLength?: number; +} + +/** A hyphenation function: takes text, returns hyphenated HTML, technically this could also return a promise in async mode but we don't use it */ +interface HyphenatorFunction { + (text: string, options?: { debug?: boolean; hyphenChar?: string; minWordLength?: number }): string | Promise<string> +} + +/** Language-code -> hyphenator function map. */ +type HyphenateMap = Record<string, HyphenatorFunction>; + +declare var hyphenationPatternsEnUs: HyphenPatternDefinition; +declare var hyphenationPatternsRu: HyphenPatternDefinition; +declare var createHyphenator: { + (patternsDefinition: HyphenPatternDefinition, options?: CreateHyphenatorOptions): HyphenatorFunction; +}; diff --git a/src/reader.ts b/src/reader.ts index 55c9844..567c920 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -2,10 +2,8 @@ 'use strict'; -declare var createHyphenator: any; +/** Pattern definition as produced by the hyphen library's build process. */ -declare var hyphenationPatternsEnUs: any; -declare var hyphenationPatternsRu: any; declare interface HTMLElement { checked: boolean } declare interface Event { deltaY: number, deltaX: number } @@ -20,7 +18,7 @@ declare type SwipeState = { startX: number, startY: number, startTime: number, - direction: 'left'|'right'|''; + direction: 'left'|'right'; } declare type SwipeConfig = { @@ -49,7 +47,7 @@ class ReaderClass extends AppCommon { open_last_read_when_displayed = true; - hyphenate = {}; + hyphenate = {} as HyphenateMap; _prevent_lastread_timeout: any; @@ -59,10 +57,7 @@ class ReaderClass extends AppCommon { cosmeticVersion = BUILD_VERSION; - /** - * @type {ePub.Book} - */ - book; + book: ePub.Book; /** @type {SwipeState} */ _swipeState = { @@ -73,8 +68,7 @@ class ReaderClass extends AppCommon { direction: '', }; - /** @type {SwipeConfig} */ - _swipeConfig = { + _swipeConfig: SwipeConfig = { minDistance: 50, // px — minimum horizontal travel to count as swipe velocityThreshold: 0.3, // px/ms — minimum swipe speed verticalDistanceThreshold: 80, // px — vertical movement before cancelling @@ -358,9 +352,8 @@ class ReaderClass extends AppCommon { const enable_hyphens = await localforage.getItem("epube.enable-hyphens"); if (enable_hyphens) { - /* global hyphenationPatternsEnUs, hyphenationPatternsRu, createHyphenator */ - (this.hyphenate as any).en = createHyphenator(hyphenationPatternsEnUs, { html: true }); - (this.hyphenate as any).ru = createHyphenator(hyphenationPatternsRu, { html: true }); + this.hyphenate.en = createHyphenator(hyphenationPatternsEnUs, { html: true }); + this.hyphenate.ru = createHyphenator(hyphenationPatternsRu, { html: true }); } this.initRenditionHooks(this.book, rendition); @@ -759,7 +752,7 @@ class ReaderClass extends AppCommon { .on("keyup", (evt: any) => { if (evt.which === 13) { evt.preventDefault(); - this.lookupWord(null, null, '.dict_query'); + this.lookupWord('', null, '.dict_query'); } }); @@ -889,7 +882,7 @@ class ReaderClass extends AppCommon { */ cleanupEmbeddedStyles(doc: Document) { $(doc).find("p") - .filter((i: any, e: any) => (($(e).text().length >= MIN_LENGTH_TO_JUSTIFY) ? e : null)) + .filter((i, e: any) => (($(e).text().length >= MIN_LENGTH_TO_JUSTIFY) ? e : null)) .css("text-align", "justify"); $(doc).find("html") @@ -919,12 +912,13 @@ class ReaderClass extends AppCommon { .css("background", "") .css("background-color", ""); - if (typeof (this.hyphenate as any).en != "undefined") { - $(doc).find('p, div').each((i: any, elem: any) => { - const p = $(elem); - p.html((this.hyphenate as any).en(p.html())); - p.html((this.hyphenate as any).ru(p.html())); -}); + if (this.hyphenate?.en) { + $(doc).find('p, div').each((i, elem) => { + const p = $(elem); + + p.html(this.hyphenate.en(p.html()) as string); + p.html(this.hyphenate.ru(p.html()) as string); + }); } } @@ -1399,9 +1393,10 @@ class ReaderClass extends AppCommon { $(".header,.footer").fadeIn(); } - async lookupWord(word: any, callback: any, elem: any = null) { + /** @param [elem=null] jquery selector for the input element to use */ + async lookupWord(word: string, callback: any, elem: string|null = null) { if (elem) - word = $(elem).val(); + word = $(elem).val() as string; word = word.replace(//g, ""); @@ -1423,8 +1418,7 @@ class ReaderClass extends AppCommon { if (callback) callback(); } } catch (e) { - // @type {NetworkError} - const err = /** @type {HttpError} */(e); + const err = e as NetworkError; console.error(err); @@ -1432,10 +1426,7 @@ class ReaderClass extends AppCommon { } } - /** - * @returns {Promise<void>} - */ - async search() { + async search(): Promise<void> { const query = $(".search_input").val(); const list = $(".search_results"); |
