about summary refs log tree commit diff stats
path: root/src/reader.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/reader.js')
-rw-r--r--src/reader.js65
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);
 		}
 	}