From 18344f0d084d75af1d015435d37d5f08aec30b01 Mon Sep 17 00:00:00 2001 From: Anurag Kamble Date: Mon, 16 Mar 2026 10:38:57 +0530 Subject: [PATCH] Fix text selection in Search inside panel results --- src/plugins/search/search-results.js | 193 +++++++++++++++++---------- 1 file changed, 120 insertions(+), 73 deletions(-) diff --git a/src/plugins/search/search-results.js b/src/plugins/search/search-results.js index 9d25c4254..1eef31f9c 100644 --- a/src/plugins/search/search-results.js +++ b/src/plugins/search/search-results.js @@ -1,13 +1,13 @@ /* eslint-disable class-methods-use-this */ -import { unsafeHTML } from 'lit/directives/unsafe-html.js'; -import { css, html, LitElement, nothing } from 'lit'; -import '@internetarchive/ia-activity-indicator/ia-activity-indicator.js'; -import checkmarkIconTemplate from '../../css/icon_checkmark.js'; -import closeIconTemplate from '@internetarchive/icon-close/index.js'; -import buttonCSS from '../../css/button-base.js'; -import { ifDefined } from 'lit/directives/if-defined.js'; -import { sharedStyles } from '../../css/sharedStyles.js'; -import { svgToDataUrl } from '../../util/lit.js'; +import { unsafeHTML } from "lit/directives/unsafe-html.js"; +import { css, html, LitElement, nothing } from "lit"; +import "@internetarchive/ia-activity-indicator/ia-activity-indicator.js"; +import checkmarkIconTemplate from "../../css/icon_checkmark.js"; +import closeIconTemplate from "@internetarchive/icon-close/index.js"; +import buttonCSS from "../../css/button-base.js"; +import { ifDefined } from "lit/directives/if-defined.js"; +import { sharedStyles } from "../../css/sharedStyles.js"; +import { svgToDataUrl } from "../../util/lit.js"; /** @typedef {import('@/src/plugins/search/plugin.search.js').SearchInsideMatch} SearchInsideMatch */ const checkmarkIconData = svgToDataUrl(checkmarkIconTemplate.strings[0]); @@ -30,11 +30,11 @@ export class IABookSearchResults extends LitElement { /** @type {SearchInsideMatch[]} */ this.results = []; - this.query = ''; + this.query = ""; this.queryInProgress = false; this.renderHeader = false; this.renderSearchAllFiles = false; - this.errorMessage = ''; + this.errorMessage = ""; this.bindBookReaderListeners(); } @@ -45,7 +45,10 @@ export class IABookSearchResults extends LitElement { } bindBookReaderListeners() { - document.addEventListener('BookReader:SearchCallback', this.setResults.bind(this)); + document.addEventListener( + "BookReader:SearchCallback", + this.setResults.bind(this), + ); } /** @@ -55,7 +58,7 @@ export class IABookSearchResults extends LitElement { if (this.results.length) { return; } - const searchInput = this.shadowRoot.querySelector('input[type=\'search\']'); + const searchInput = this.shadowRoot.querySelector("input[type='search']"); searchInput.focus(); } @@ -76,28 +79,34 @@ export class IABookSearchResults extends LitElement { if (!input || !input.value) { return; } - this.dispatchEvent(new CustomEvent('bookSearchInitiated', { - bubbles: true, - composed: true, - detail: { - query: this.query, - }, - })); + this.dispatchEvent( + new CustomEvent("bookSearchInitiated", { + bubbles: true, + composed: true, + detail: { + query: this.query, + }, + }), + ); } /** * @param {SearchInsideMatch} match */ selectResult(match) { - this.dispatchEvent(new CustomEvent('resultSelected', { - bubbles: true, - composed: true, - detail: { match }, - })); - this.dispatchEvent(new CustomEvent('closeMenu', { - bubbles: true, - composed: true, - })); + this.dispatchEvent( + new CustomEvent("resultSelected", { + bubbles: true, + composed: true, + detail: { match }, + }), + ); + this.dispatchEvent( + new CustomEvent("closeMenu", { + bubbles: true, + composed: true, + }), + ); } cancelSearch() { @@ -106,12 +115,12 @@ export class IABookSearchResults extends LitElement { } dispatchSearchCanceled() { - this.dispatchEvent(new Event('bookSearchCanceled')); + this.dispatchEvent(new Event("bookSearchCanceled")); } get resultsCount() { const count = this.results.length; - return count ? `${count} result${count > 1 ? 's' : ''}` : nothing; + return count ? `${count} result${count > 1 ? "s" : ""}` : nothing; } get headerSection() { @@ -133,57 +142,82 @@ export class IABookSearchResults extends LitElement { get loadingIndicator() { return html`
- +

Searching

- +
`; } get resultsSet() { return html` - + e.preventDefault(); + this.shadowRoot + .querySelector(".results li:first-child .result-item") + .focus(); + }} + >Skip to first result + `; } get setErrorMessage() { - return html` -

${this.errorMessage}

- `; + return html`

${this.errorMessage}

`; } render() { - const showSearchCTA = (!this.queryInProgress && !this.errorMessage) - && (!this.queryInProgress && !this.results.length); + const showSearchCTA = + !this.queryInProgress && + !this.errorMessage && + !this.queryInProgress && + !this.results.length; return html` ${this.headerSection}
@@ -196,7 +230,10 @@ export class IABookSearchResults extends LitElement { @search=${this.setQuery} .value=${this.query} /> -