From 8c307501fab0d3ccc06d4478d13d58ccbde4a97b Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Fri, 13 Oct 2023 10:38:03 +0300 Subject: [PATCH] RED-7605: removed word duplicates. --- .../pdf-viewer/services/pdf-viewer.service.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) 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 5b346e840..bcb5231a3 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 @@ -35,8 +35,10 @@ export class PdfViewer { img: this.#convertPath('/assets/icons/general/pdftron-action-search.svg'), title: inject(TranslateService).instant(_('pdf-viewer.text-popup.actions.search')), onClick: () => { - setTimeout(() => this.#searchForSelectedText(), 250); - this.#focusSearch(); + setTimeout(() => { + this.#searchForSelectedText(); + this.#focusSearch(); + }, 250); }, }; readonly #destroyRef = inject(DestroyRef); @@ -62,6 +64,7 @@ export class PdfViewer { searchUp: false, // search from the end of the document upwards ambientString: true, // return ambient string as part of the result }; + searchedWord?: string; constructor( private readonly _logger: NGXLogger, @@ -263,14 +266,15 @@ export class PdfViewer { #listenForCommandF() { this.#instance.UI.hotkeys.on('command+f, ctrl+f', e => { e.preventDefault(); - if (!this.#isElementActive('searchPanel')) { + if (this.#isElementActive('searchPanel')) { + this.#updateSearchOptions(); + } else { this.activateSearch(); } if (this.documentViewer.getSelectedText()) { - this.#updateSearchOptions(); this.#searchForSelectedText(); } - setTimeout(() => this.#focusSearch(), 30); + setTimeout(() => this.#focusSearch(), 40); }); } @@ -309,7 +313,14 @@ export class PdfViewer { } #searchForSelectedText() { - const selected = [...new Set(this.documentViewer.getSelectedText().split('\n'))].join('\n'); + 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 { + this.searchedWord = this.documentViewer.getSelectedText(); + } + const selected = this.searchedWord ?? uniqueText.join('\n'); this.#instance.UI.searchTextFull(selected, this.searchOptions); } @@ -376,7 +387,7 @@ export class PdfViewer { if (input) { input.focus(); } - if (input.value.length > 0) { + if (input?.value.length > 0) { input.select(); } }