From 8149d6cae528b6e12e55c38c04a3a87668c689a0 Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Mon, 25 Sep 2023 15:12:54 +0300 Subject: [PATCH] RED-7605: fixed search for marked word. --- .../file-preview-screen.component.ts | 3 +- .../pdf-viewer/services/pdf-viewer.service.ts | 56 +++++++++++-------- 2 files changed, 36 insertions(+), 23 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 3be5dda62..53a69f27b 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 @@ -394,10 +394,11 @@ export class FilePreviewScreenComponent } if (['Escape'].includes($event.key)) { + $event.preventDefault(); this.fullScreen = false; this.closeFullScreen(); this.pdf.deactivateSearch(); - window.focus(); + this.pdf.focusViewer(); this._changeRef.markForCheck(); } 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 a6113cfc9..731980fb4 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 @@ -24,7 +24,7 @@ import TextTool = Core.Tools.TextTool; @Injectable() export class PdfViewer { - private readonly _convertPath = inject(BASE_HREF_FN); + readonly #convertPath = inject(BASE_HREF_FN); #instance: WebViewerInstance; readonly #licenseKey = inject(LicenseService).activeLicenseKey; readonly #config = getConfig(); @@ -32,11 +32,12 @@ export class PdfViewer { readonly #isCompareMode$ = toObservable(this.#isCompareMode); readonly #searchButton: IHeaderElement = { type: 'actionButton', - img: this._convertPath('/assets/icons/general/pdftron-action-search.svg'), + img: this.#convertPath('/assets/icons/general/pdftron-action-search.svg'), title: inject(TranslateService).instant(_('pdf-viewer.text-popup.actions.search')), onClick: () => { - this.#instance.UI.openElements(['searchPanel']); + this.activateSearch(); setTimeout(() => this.#searchForSelectedText(), 250); + this.#focusSearch(); }, }; readonly #destroyRef = inject(DestroyRef); @@ -102,27 +103,19 @@ export class PdfViewer { return page$.pipe(map(page => this.#adjustPage(page))); } - get searchActive() { - return this.instance.UI.isElementOpen('searchPanel'); - } - - 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.#logger.info('[PDF] Reset annotation actions'); @@ -153,7 +146,7 @@ export class PdfViewer { this.#instance = await this.#getInstance(htmlElement); if (environment.production) { - this.#instance.Core.setCustomFontURL('https://' + window.location.host + this._convertPath('/assets/pdftron')); + this.#instance.Core.setCustomFontURL('https://' + window.location.host + this.#convertPath('/assets/pdftron')); } await this.runWithCleanup(async () => { @@ -264,13 +257,13 @@ export class PdfViewer { #listenForCommandF() { this.#instance.UI.hotkeys.on('command+f, ctrl+f', e => { e.preventDefault(); - if (!this.searchActive) { + if (!this.#isElementActive('searchPanel')) { this.activateSearch(); - this.focusSearch(); } if (this.documentViewer.getSelectedText()) { this.#searchForSelectedText(); } + setTimeout(() => this.#focusSearch(), 250); }); } @@ -287,7 +280,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() { @@ -332,11 +326,29 @@ export class PdfViewer { const options: WebViewerOptions = { licenseKey: this.#licenseKey, fullAPI: true, - path: this._convertPath('/assets/wv-resources'), - css: this._convertPath('/assets/pdftron/stylesheet.css'), + path: this.#convertPath('/assets/wv-resources'), + css: this.#convertPath('/assets/pdftron/stylesheet.css'), backendType: 'ems', }; 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(); + } + } }