diff --git a/apps/red-ui/src/app/modules/pdf-viewer/services/pdf-viewer.service.ts b/apps/red-ui/src/app/modules/pdf-viewer/services/pdf-viewer.service.ts index 5e31cd85a..593c5d132 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/services/pdf-viewer.service.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/services/pdf-viewer.service.ts @@ -42,7 +42,6 @@ export class PdfViewer { }, }; readonly #destroyRef = inject(DestroyRef); - readonly #logger = inject(NGXLogger); readonly #totalPages = signal(0); readonly currentPage$ = inject(ActivatedRoute).queryParamMap.pipe( map(params => Number(params.get('page') ?? '1')), @@ -57,14 +56,14 @@ export class PdfViewer { readonly totalPages: Signal; searchOptions = { - caseSensitive: true, // match case - wholeWord: true, // match whole words only + caseSensitive: false, // match case + wholeWord: false, // match whole words only wildcard: false, // allow using '*' as a wildcard value regex: false, // string is treated as a regular expression searchUp: false, // search from the end of the document upwards ambientString: true, // return ambient string as part of the result }; - searchedWord?: string; + selectedText = ''; constructor( private readonly _logger: NGXLogger, @@ -168,6 +167,7 @@ export class PdfViewer { this.#setSelectionMode(); this.#configureElements(); this.#disableHotkeys(); + this.#getSelectedText(); this.#listenForCommandF(); this.#listenForEsc(); this.#clearSearchResultsWhenVisibilityChanged(); @@ -264,7 +264,7 @@ export class PdfViewer { } else { this.activateSearch(); } - if (this.documentViewer.getSelectedText()) { + if (this.selectedText.length) { this.#searchForSelectedText(); } setTimeout(() => this.#focusSearch(), 40); @@ -306,15 +306,14 @@ export class PdfViewer { } #searchForSelectedText() { - const uniqueText = [...new Set(this.documentViewer.getSelectedText().split('\n'))]; - const sameWord = [...new Set(uniqueText.map(text => text.toLowerCase()))].length === 1; - if (uniqueText.length === 1 && sameWord) { - this.searchedWord = uniqueText.join('\n'); - } else if (!sameWord) { - this.searchedWord = this.documentViewer.getSelectedText(); - } - const selected = this.searchedWord; - this.#instance.UI.searchTextFull(selected, this.searchOptions); + this.#instance.UI.searchTextFull(this.selectedText, this.searchOptions); + this.documentViewer.clearSelection(); + } + + #getSelectedText() { + this.documentViewer.addEventListener('textSelected', (q, selectedText) => { + this.selectedText = selectedText; + }); } #clearSearchResultsWhenVisibilityChanged() { @@ -380,7 +379,7 @@ export class PdfViewer { if (input) { input.focus(); } - if (input?.value.length > 0) { + if (input?.value?.length > 0) { input.select(); } }