From 1b99ce9dfae0459d71bc0c591369e2e4e97802f5 Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Mon, 25 Sep 2023 15:29:21 +0300 Subject: [PATCH] RED-7605: fixed search for marked word. --- .../file-preview-screen.component.ts | 3 +- .../pdf-viewer/services/pdf-viewer.service.ts | 44 ++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/apps/red-ui/src/app/modules/file-preview/file-preview-screen.component.ts b/apps/red-ui/src/app/modules/file-preview/file-preview-screen.component.ts index aed22cb1b..4740a39d9 100644 --- a/apps/red-ui/src/app/modules/file-preview/file-preview-screen.component.ts +++ b/apps/red-ui/src/app/modules/file-preview/file-preview-screen.component.ts @@ -358,11 +358,12 @@ export class FilePreviewScreenComponent } if (['Escape'].includes($event.key)) { + $event.preventDefault(); this.fullScreen = false; this.closeFullScreen(); this.pdf.deactivateSearch(); + this.pdf.focusViewer(); this._changeRef.markForCheck(); - window.focus(); } if (['f', 'F'].includes($event.key) && !$event.ctrlKey) { 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 e7904083b..e8cfbe893 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 @@ -102,23 +102,19 @@ export class PdfViewer { return page$.pipe(map(page => this.#adjustPage(page))); } - focusSearch() { - const iframeWindow = this.#instance.UI.iframeWindow; - const input = iframeWindow.document.getElementById('SearchPanel__input') as HTMLInputElement; - if (input) { - input.focus(); - input.select(); - } - } - activateSearch() { this.#instance.UI.openElements(['searchPanel']); } deactivateSearch() { + this.#clearSearchResultsWhenVisibilityChanged(); this.#instance.UI.closeElements(['searchPanel']); } + focusViewer() { + this.instance.UI.iframeWindow.focus(); + } + resetAnnotationActions() { if (this.#instance.UI.annotationPopup.getItems().length) { this.#instance.UI.annotationPopup.update([]); @@ -258,7 +254,8 @@ export class PdfViewer { } #searchForSelectedText() { - this.#instance.UI.searchTextFull(this.documentViewer.getSelectedText(), SEARCH_OPTIONS); + const selected = [...new Set(this.documentViewer.getSelectedText().split('\n'))].join('\n'); + this.#instance.UI.searchTextFull(selected, SEARCH_OPTIONS); } #clearSearchResultsWhenVisibilityChanged() { @@ -281,8 +278,13 @@ export class PdfViewer { #listenForCommandF() { this.#instance.UI.hotkeys.on('command+f, ctrl+f', e => { e.preventDefault(); - this.activateSearch(); - this.focusSearch(); + if (!this.#isElementActive('searchPanel')) { + this.activateSearch(); + } + if (this.documentViewer.getSelectedText()) { + this.#searchForSelectedText(); + } + setTimeout(() => this.#focusSearch(), 250); }); } @@ -317,4 +319,22 @@ export class PdfViewer { return WebViewer(options, htmlElement); } + + #isElementActive(element: string): boolean { + return this.#instance.UI.isElementOpen(element); + } + + #focusSearch() { + if (this.#isElementActive('textPopup')) { + this.#instance.UI.closeElements(['textPopup']); + } + const iframeWindow = this.#instance.UI.iframeWindow; + const input = iframeWindow.document.getElementById('SearchPanel__input') as HTMLInputElement; + if (input) { + input.focus(); + } + if (input.value.length > 0) { + input.select(); + } + } }