From 3ab5ccfaecc65e529aec2db72c78a8aadedacf97 Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Thu, 21 Nov 2024 18:12:45 +0200 Subject: [PATCH] RED-10148: fixed text selection; implemented key listener for shift. --- .../services/document-viewer.service.ts | 2 +- .../pdf-viewer/services/pdf-viewer.service.ts | 30 ++++++++++++++----- .../app/modules/pdf-viewer/utils/constants.ts | 5 ++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/red-ui/src/app/modules/pdf-viewer/services/document-viewer.service.ts b/apps/red-ui/src/app/modules/pdf-viewer/services/document-viewer.service.ts index fc85b35ce..88fbff253 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/services/document-viewer.service.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/services/document-viewer.service.ts @@ -78,7 +78,7 @@ export class REDDocumentViewer { } return ($event.target as HTMLElement)?.tagName?.toLowerCase() !== 'input'; }), - filter($event => $event.key.startsWith('Arrow') || ['f', 'h', 'H', 'Escape'].includes($event.key)), + filter($event => $event.key.startsWith('Arrow') || ['f', 'h', 'H', 'Escape', 'Shift'].includes($event.key)), tap(stopAndPrevent), log('[PDF] Keyboard shortcut'), ); 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 6b5ff4a53..6ea094133 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 @@ -13,7 +13,7 @@ import { UserPreferenceService } from '@users/user-preference.service'; import { NGXLogger } from 'ngx-logger'; import { combineLatest, fromEvent, Observable } from 'rxjs'; import { map, startWith } from 'rxjs/operators'; -import { DISABLED_HOTKEYS, DOCUMENT_LOADING_ERROR, USELESS_ELEMENTS } from '../utils/constants'; +import { DISABLED_HOTKEYS, DOCUMENT_LOADING_ERROR, SelectionModes, USELESS_ELEMENTS } from '../utils/constants'; import { asList } from '../utils/functions'; import { Rgb } from '../utils/types'; import { REDAnnotationManager } from './annotation-manager.service'; @@ -160,12 +160,13 @@ export class PdfViewer { this.pageChanged$ = this.#pageChanged$.pipe(shareDistinctLast()); this.#totalPages$.pipe(takeUntilDestroyed(this.#destroyRef)).subscribe(pages => this.#totalPages.set(pages)); - this.#setSelectionMode(); + this.#setSelectionMode(this.#config.SELECTION_MODE); this.#configureElements(); this.#disableHotkeys(); this.#getSelectedText(); this.#listenForCommandF(); this.#listenForEsc(); + this.#listenForShift(); this.#clearSearchResultsWhenVisibilityChanged(); }); @@ -258,7 +259,7 @@ export class PdfViewer { } #listenForCommandF() { - this.#instance.UI.hotkeys.on('command+f, ctrl+f', e => { + this.#instance.UI.hotkeys.on('command+f, ctrl+f', (e: KeyboardEvent) => { e.preventDefault(); if (this.#isElementActive('searchPanel')) { this.#updateSearchOptions(); @@ -274,11 +275,11 @@ export class PdfViewer { #listenForEsc() { this.#instance.UI.hotkeys.on('esc', { - keydown: e => { + keydown: (e: KeyboardEvent) => { e.preventDefault(); this.#clickSelectToolButton(); }, - keyup: e => { + keyup: (e: KeyboardEvent) => { e.preventDefault(); if (this.#isElementActive('searchPanel') && !this._annotationManager.resizingAnnotationId) { this.#focusViewer(); @@ -289,6 +290,21 @@ export class PdfViewer { }); } + #listenForShift() { + this.#instance.UI.iframeWindow.addEventListener('keydown', e => { + e.preventDefault(); + if (e.key === 'Shift') { + this.#setSelectionMode(SelectionModes.RECTANGULAR); + } + }); + this.#instance.UI.iframeWindow.addEventListener('keyup', e => { + e.preventDefault(); + if (e.key === 'Shift') { + this.#setSelectionMode(SelectionModes.STRUCTURAL); + } + }); + } + #getSearchOption(optionId: string): boolean { const iframeWindow = this.#instance.UI.iframeWindow; const checkbox = iframeWindow.document.getElementById(optionId) as HTMLInputElement; @@ -350,9 +366,9 @@ export class PdfViewer { this.#instance.UI.disableElements(USELESS_ELEMENTS); } - #setSelectionMode(): void { + #setSelectionMode(selectionMode: string): void { const textTool = this.#instance.Core.Tools.TextTool as unknown as TextTool; - textTool.SELECTION_MODE = this.#config.SELECTION_MODE; + textTool.SELECTION_MODE = selectionMode; } #getInstance(htmlElement: HTMLElement) { diff --git a/apps/red-ui/src/app/modules/pdf-viewer/utils/constants.ts b/apps/red-ui/src/app/modules/pdf-viewer/utils/constants.ts index 6479fd377..9e679d063 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/utils/constants.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/utils/constants.ts @@ -87,3 +87,8 @@ export const DISABLED_HOTKEYS = [ export const AnnotationToolNames = { AnnotationCreateRectangle: 'AnnotationCreateRectangle', } as const; + +export const SelectionModes = { + RECTANGULAR: 'rectangular', + STRUCTURAL: 'structural', +} as const;