RED-7605: changed the way selectedText is retrieved and updated pdftron

This commit is contained in:
Nicoleta Panaghiu 2023-11-01 15:46:10 +02:00
parent 8cd751fdf4
commit cbd0d5ce26
2 changed files with 15 additions and 15 deletions

View File

@ -44,14 +44,14 @@ export class PdfViewer {
totalPages$: Observable<number>; totalPages$: Observable<number>;
searchOptions = { searchOptions = {
caseSensitive: true, // match case caseSensitive: false, // match case
wholeWord: true, // match whole words only wholeWord: false, // match whole words only
wildcard: false, // allow using '*' as a wildcard value wildcard: false, // allow using '*' as a wildcard value
regex: false, // string is treated as a regular expression regex: false, // string is treated as a regular expression
searchUp: false, // search from the end of the document upwards searchUp: false, // search from the end of the document upwards
ambientString: true, // return ambient string as part of the result ambientString: true, // return ambient string as part of the result
}; };
searchedWord?: string; selectedText = '';
constructor( constructor(
private readonly _logger: NGXLogger, private readonly _logger: NGXLogger,
@ -170,6 +170,7 @@ export class PdfViewer {
this.#setSelectionMode(); this.#setSelectionMode();
this.#configureElements(); this.#configureElements();
this.#disableHotkeys(); this.#disableHotkeys();
this.#getSelectedText();
this.#listenForCommandF(); this.#listenForCommandF();
this.#listenForEsc(); this.#listenForEsc();
this.#clearSearchResultsWhenVisibilityChanged(); this.#clearSearchResultsWhenVisibilityChanged();
@ -257,7 +258,7 @@ export class PdfViewer {
} else { } else {
this.activateSearch(); this.activateSearch();
} }
if (this.documentViewer.getSelectedText()) { if (this.selectedText.length) {
this.#searchForSelectedText(); this.#searchForSelectedText();
} }
setTimeout(() => this.#focusSearch(), 40); setTimeout(() => this.#focusSearch(), 40);
@ -299,15 +300,14 @@ export class PdfViewer {
} }
#searchForSelectedText() { #searchForSelectedText() {
const uniqueText = [...new Set(this.documentViewer.getSelectedText().split('\n'))]; this.#instance.UI.searchTextFull(this.selectedText, this.searchOptions);
const sameWord = new Set(uniqueText.map(text => text.toLowerCase())).size === 1; this.documentViewer.clearSelection();
if (uniqueText.length === 1 && sameWord) { }
this.searchedWord = uniqueText.join('\n');
} else if (!sameWord) { #getSelectedText() {
this.searchedWord = this.documentViewer.getSelectedText(); this.documentViewer.addEventListener('textSelected', (_, selectedText) => {
} this.selectedText = selectedText;
const selected = this.searchedWord; });
this.#instance.UI.searchTextFull(selected, this.searchOptions);
} }
#clearSearchResultsWhenVisibilityChanged() { #clearSearchResultsWhenVisibilityChanged() {
@ -374,7 +374,7 @@ export class PdfViewer {
if (input) { if (input) {
input.focus(); input.focus();
} }
if (input.value.length > 0) { if (input?.value?.length > 0) {
input.select(); input.select();
} }
} }

View File

@ -39,7 +39,7 @@
"@ngx-translate/core": "^14.0.0", "@ngx-translate/core": "^14.0.0",
"@ngx-translate/http-loader": "^7.0.0", "@ngx-translate/http-loader": "^7.0.0",
"@nrwl/angular": "15.6.3", "@nrwl/angular": "15.6.3",
"@pdftron/webviewer": "10.1.1", "@pdftron/webviewer": "10.3.0",
"chart.js": "4.4.0", "chart.js": "4.4.0",
"dayjs": "^1.11.5", "dayjs": "^1.11.5",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",