From fa76007bcac76240363b9a855c7706b34683c56e Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Fri, 18 Mar 2022 16:36:43 +0200 Subject: [PATCH] fix drawing annotations after reanalisys --- .../file-preview-screen.component.ts | 39 ++++++++++++------- .../services/annotation-draw.service.ts | 4 +- .../services/pdf-viewer.service.ts | 4 ++ 3 files changed, 30 insertions(+), 17 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 a13995a7a..c3aa09a7e 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 @@ -29,7 +29,7 @@ import { FileWorkloadComponent } from './components/file-workload/file-workload. import { TranslateService } from '@ngx-translate/core'; import { FilesService } from '@services/entity-services/files.service'; import { FileManagementService } from '@services/entity-services/file-management.service'; -import { catchError, filter, map, startWith, switchMap, tap, withLatestFrom } from 'rxjs/operators'; +import { catchError, debounceTime, map, startWith, switchMap, tap } from 'rxjs/operators'; import { FilesMapService } from '@services/entity-services/files-map.service'; import { WatermarkService } from '@shared/services/watermark.service'; import { ExcludedPagesService } from './services/excluded-pages.service'; @@ -210,6 +210,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni this._viewModeService.switchToStandard(); await this.ngOnInit(); + await this._fileDataService.loadRedactionLog(); this._lastPage = previousRoute.queryParams.page; this._changeDetectorRef.markForCheck(); } @@ -421,10 +422,9 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni const documentLoaded$ = this.pdf.documentLoaded$.pipe(tap(() => this.viewerReady())); let start; return combineLatest([documentLoaded$, this._fileDataService.annotations$]).pipe( - withLatestFrom(this.stateService.file$), - filter(([, file]) => !file.isProcessing), + debounceTime(300), tap(() => (start = new Date().getTime())), - map(([[, annotations]]) => annotations), + map(([, annotations]) => annotations), startWith({} as Record), pairwise(), tap(annotations => this.deleteAnnotations(...annotations)), @@ -446,7 +446,25 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni } drawChangedAnnotations(oldAnnotations: Record, newAnnotations: Record) { - const annotationsToDraw = Object.values(newAnnotations).filter(newAnnotation => { + let annotationsToDraw: readonly AnnotationWrapper[]; + if (this.pdf.hasAnnotations) { + annotationsToDraw = this.#getAnnotationsToDraw(newAnnotations, oldAnnotations); + } else { + annotationsToDraw = Object.values(newAnnotations); + } + + if (annotationsToDraw.length === 0) { + return firstValueFrom(of({})); + } + + console.log('%c [ANNOTATIONS] To draw: ', 'color: aqua', annotationsToDraw); + const annotationsToDrawIds = annotationsToDraw.map(a => a.annotationId); + this.pdf.deleteAnnotations(annotationsToDrawIds); + return this._cleanupAndRedrawAnnotations(annotationsToDraw); + } + + #getAnnotationsToDraw(newAnnotations: Record, oldAnnotations: Record) { + return Object.values(newAnnotations).filter(newAnnotation => { const oldAnnotation = oldAnnotations[newAnnotation.id]; if (!oldAnnotation) { return true; @@ -465,15 +483,6 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni return changed; }); - - if (annotationsToDraw.length === 0) { - return firstValueFrom(of({})); - } - - console.log('%c [ANNOTATIONS] To draw: ', 'color: aqua', annotationsToDraw); - const annotationsToDrawIds = annotationsToDraw.map(a => a.annotationId); - this.pdf.deleteAnnotations(annotationsToDrawIds); - return this._cleanupAndRedrawAnnotations(annotationsToDraw); } async #deactivateMultiSelect() { @@ -601,7 +610,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni this._workloadComponent?.scrollAnnotations(); } - private async _cleanupAndRedrawAnnotations(newAnnotations: AnnotationWrapper[]) { + private async _cleanupAndRedrawAnnotations(newAnnotations: readonly AnnotationWrapper[]) { if (!this.pdf.ready) { return; } diff --git a/apps/red-ui/src/app/modules/file-preview/services/annotation-draw.service.ts b/apps/red-ui/src/app/modules/file-preview/services/annotation-draw.service.ts index 0b4de01c7..38f2cfe8a 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/annotation-draw.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/annotation-draw.service.ts @@ -35,7 +35,7 @@ export class AnnotationDrawService { private readonly _fileDataService: FileDataService, ) {} - drawAnnotations(annotationWrappers: AnnotationWrapper[]) { + drawAnnotations(annotationWrappers: readonly AnnotationWrapper[]) { if (!this._pdf.instance || !this._pdf.ready) { return; } @@ -87,7 +87,7 @@ export class AnnotationDrawService { return new this._pdf.instance.Core.Math.Quad(x1, y1, x2, y2, x3, y3, x4, y4); } - private async _drawAnnotations(annotationWrappers: AnnotationWrapper[]) { + private async _drawAnnotations(annotationWrappers: readonly AnnotationWrapper[]) { const document = await this._pdf.documentViewer.getDocument()?.getPDFDoc(); if (!this._pdf.ready || !document) { return; diff --git a/apps/red-ui/src/app/modules/file-preview/services/pdf-viewer.service.ts b/apps/red-ui/src/app/modules/file-preview/services/pdf-viewer.service.ts index bc27286b3..d18711d77 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/pdf-viewer.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/pdf-viewer.service.ts @@ -34,6 +34,10 @@ export class PdfViewer { return this.instance.Core.PDFNet; } + get hasAnnotations() { + return this.annotationManager?.getAnnotationsList().length > 0 ?? false; + } + get paginationOffset() { return this.viewModeService.isCompare ? 2 : 1; }