From 5097ce3dba4e83dd6c45582dff8662caa54b58fc Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Mon, 3 Jun 2024 15:19:46 +0300 Subject: [PATCH] RED-9175: prevent search re-trigger upon navigating the file pages. --- .../page-exclusion.component.ts | 15 ++++++++-- .../file-preview-screen.component.ts | 7 ++++- .../services/file-preview-state.service.ts | 28 +++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/apps/red-ui/src/app/modules/file-preview/components/page-exclusion/page-exclusion.component.ts b/apps/red-ui/src/app/modules/file-preview/components/page-exclusion/page-exclusion.component.ts index 1bccd02a6..7c74f2971 100644 --- a/apps/red-ui/src/app/modules/file-preview/components/page-exclusion/page-exclusion.component.ts +++ b/apps/red-ui/src/app/modules/file-preview/components/page-exclusion/page-exclusion.component.ts @@ -4,7 +4,6 @@ import { InputWithActionComponent, LoadingService, shareLast, Toaster } from '@i import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { IPageRange } from '@red/domain'; import { ReanalysisService } from '@services/reanalysis.service'; -import { ExcludedPagesService } from '../../services/excluded-pages.service'; import { combineLatest, firstValueFrom, Observable } from 'rxjs'; import { FilePreviewStateService } from '../../services/file-preview-state.service'; import { map } from 'rxjs/operators'; @@ -23,7 +22,6 @@ export class PageExclusionComponent { constructor( readonly permissionsService: PermissionsService, - readonly excludedPagesService: ExcludedPagesService, private readonly _reanalysisService: ReanalysisService, private readonly _toaster: Toaster, private readonly _loadingService: LoadingService, @@ -58,6 +56,7 @@ export class PageExclusionComponent { const pageRanges = extractPageRanges(inputValue, file); const excludePages$ = this._reanalysisService.excludePages({ pageRanges }, file.dossierId, file); await firstValueFrom(excludePages$); + this._state.excludePages(this._flattenPageRanges(pageRanges)); this._inputComponent.reset(); } catch (e) { this._toaster.error(_('file-preview.tabs.exclude-pages.error')); @@ -76,10 +75,22 @@ export class PageExclusionComponent { file, ); await firstValueFrom(includePages$); + this._state.includePages(this._flattenPageRanges([range])); this._inputComponent.reset(); this._loadingService.stop(); } + private _flattenPageRanges(ranges: IPageRange[]) { + const flattenedPages = []; + ranges.forEach(range => { + for (let page = range.startPage; page <= range.endPage; page++) { + flattenedPages.push(page); + } + }); + + return flattenedPages; + } + private _toPageRange(pages: number[]) { return pages.reduce((ranges: IPageRange[], page) => { if (!ranges.length) { 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 8ea6478b0..b88a86db5 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 @@ -604,11 +604,16 @@ export class FilePreviewScreenComponent ) .subscribe(); - this.addActiveScreenSubscription = combineLatest([this._documentViewer.loaded$, this._viewModeService.viewMode$, this.state.file$]) + this.addActiveScreenSubscription = combineLatest([ + this._documentViewer.loaded$, + this._viewModeService.viewMode$, + this.state.excludedPages$, + ]) .pipe( filter(([loaded, viewMode, shouldUpdate]) => loaded && !!viewMode), tap(async ([loaded, viewMode, shouldUpdate]) => { if (loaded) { + this._logger.info('[PDF] Stamp pdf'); await this._stampService.stampPDF(); } }), diff --git a/apps/red-ui/src/app/modules/file-preview/services/file-preview-state.service.ts b/apps/red-ui/src/app/modules/file-preview/services/file-preview-state.service.ts index 44e334477..2b7d90115 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/file-preview-state.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/file-preview-state.service.ts @@ -1,5 +1,5 @@ import { Injectable, Injector } from '@angular/core'; -import { combineLatest, firstValueFrom, from, merge, Observable, of, pairwise, Subject, switchMap } from 'rxjs'; +import { BehaviorSubject, combineLatest, firstValueFrom, from, merge, Observable, of, pairwise, Subject, switchMap } from 'rxjs'; import { Dictionary, Dossier, DOSSIER_ID, DOSSIER_TEMPLATE_ID, File, FILE_ID } from '@red/domain'; import { Router } from '@angular/router'; import { FilesMapService } from '@services/files/files-map.service'; @@ -37,6 +37,7 @@ export class FilePreviewStateService { readonly dossierFileChange$: Observable; readonly isReadonly$: Observable; readonly isWritable$: Observable; + readonly excludedPages$: Observable; readonly dossierId: string = getParam(DOSSIER_ID); readonly dossierTemplateId: string = getParam(DOSSIER_TEMPLATE_ID); @@ -46,6 +47,7 @@ export class FilePreviewStateService { file: File; #dossierDictionary: Dictionary; readonly #reloadBlob$ = new Subject(); + readonly #excludedPages$ = new BehaviorSubject([]); constructor( router: Router, @@ -62,7 +64,19 @@ export class FilePreviewStateService { ) { const dossiersService = dossiersServiceResolver(_injector, router); this.dossier$ = dossiersService.getEntityChanged$(this.dossierId).pipe(tap(dossier => (this.dossier = dossier))); - this.file$ = filesMapService.watch$(this.dossierId, this.fileId).pipe(tap(file => (this.file = file))); + this.file$ = filesMapService.watch$(this.dossierId, this.fileId).pipe( + tap(file => { + this.file = file; + + const areExcludedPagesEqual = + this.file.excludedPages.length === this.#excludedPages$.value.length && + this.file.excludedPages.every(page => this.#excludedPages$.value.includes(page)); + if (!areExcludedPagesEqual) { + this.#excludedPages$.next(this.file.excludedPages); + } + }), + ); + this.excludedPages$ = this.#excludedPages$.asObservable(); [this.isReadonly$, this.isWritable$] = boolFactory( combineLatest([this.file$, this.dossier$]), ([file, dossier]) => !permissionsService.canPerformAnnotationActions(file, dossier), @@ -107,6 +121,16 @@ export class FilePreviewStateService { this.#reloadBlob$.next(true); } + excludePages(excludedPages: number[]) { + const newExcludedPages = [...new Set([...this.#excludedPages$.value, ...excludedPages])]; + this.#excludedPages$.next(newExcludedPages); + } + + includePages(includedPages: number[]) { + const newExcludedPages = this.#excludedPages$.value.filter(excludedPage => includedPages.includes(excludedPage)); + this.#excludedPages$.next(newExcludedPages); + } + #getRemainingTimeVerbose(event: HttpProgressEvent, startTime: number) { const remainingTime = getRemainingTime(event, startTime); if (remainingTime > 60) {