diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 20:34:28 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 20:34:28 +0300 |
| commit | ec163996772bb0239ebc1beeffe79deb83a17140 (patch) | |
| tree | e859eafdfb0a19622a2908997df2be42b1bb9813 /src/reader.js | |
| parent | a578fa50b2fc2d11ff5a0d45c2cd4d390409739a (diff) | |
| download | the-epube-ec163996772bb0239ebc1beeffe79deb83a17140.tar.gz the-epube-ec163996772bb0239ebc1beeffe79deb83a17140.tar.xz | |
add missing interface for objects returned by find() and fix related problems
Diffstat (limited to 'src/reader.js')
| -rw-r--r-- | src/reader.js | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/reader.js b/src/reader.js index aa9f103..b3a470a 100644 --- a/src/reader.js +++ b/src/reader.js @@ -1368,40 +1368,49 @@ class ReaderClass extends AppCommon { } } - search() { + /** + * @returns {Promise<void>} + */ + async search() { const query = $(".search_input").val(); const list = $(".search_results"); list.html(""); - if (query) { - list.html("<li>Loading, please wait...</li>"); - - /* eslint-disable prefer-spread */ - Promise.all( - this.book.spine.spineItems.map( - (item) => item.load(this.book.load.bind(this.book)) - // @ts-ignore TODO something wrong with the types (?) - .then(item.find.bind(item, query)) - .finally(item.unload.bind(item))) - ) - .then((results) => Promise.resolve([].concat.apply([], results))) - .then((/** @type {Array<{cfi: string, excerpt: string, id: string}>} */ results) => { - list.html(""); - - $.each(results, (i, /** @type {{cfi: string, excerpt: string, id: string}} */ row) => { - const a = $("<a>") - .attr('href', '#') - .html("<b class='pull-right'>" + this.book.locations.locationFromCfi(row.cfi) + "</b>" + row.excerpt) - .attr('data-cfi', row.cfi) - .attr('data-id', row.id) - .on('click', () => { - this.book.rendition.display(a.attr('data-cfi')); - }); - - list.append($("<li>").append(a)); + if (!query) return; + + list.html("<li>Loading, please wait...</li>"); + + try { + const searchResults = await Promise.all( + this.book.spine.spineItems.map(async (item) => { + try { + await item.load(this.book.load.bind(this.book)); + const results = await item.find(`${query}`); + return results; + } finally { + await item.unload(); + } + }) + ); + + list.html(""); + + searchResults.flat().forEach((row) => { + console.log('sr', row); + + const a = $("<a>") + .attr('href', '#') + .html(`<b class='pull-right'>${this.book.locations.locationFromCfi(row.cfi)}</b>${row.excerpt}`) + .attr('data-cfi', row.cfi) + .on('click', () => { + this.book.rendition.display($(a).attr('data-cfi')); }); - }); + + list.append($("<li>").append(a)); + }); + } catch (e) { + console.error('Search failed:', e); } } |
