From 576f6ec6e35c40043a29165517dbd7986889d160 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Mon, 23 May 2022 21:37:47 +0300 Subject: [PATCH] RED-3988: update document viewer service --- .../services/annotation-actions.service.ts | 13 +++++------ .../services/pdf-proxy.service.ts | 2 +- .../file-preview/services/stamp.service.ts | 4 +--- .../services/annotation-draw.service.ts | 9 ++------ .../services/annotation-manager.service.ts | 19 +++++++++++++-- .../services/document-viewer.service.ts | 23 +++++++++++++++++++ .../services/page-rotation.service.ts | 22 ++++-------------- .../pdf-viewer/services/pdf-viewer.service.ts | 7 +----- .../app/modules/pdf-viewer/utils/functions.ts | 10 +++++++- 9 files changed, 65 insertions(+), 44 deletions(-) diff --git a/apps/red-ui/src/app/modules/file-preview/services/annotation-actions.service.ts b/apps/red-ui/src/app/modules/file-preview/services/annotation-actions.service.ts index 3116e084d..f1166db04 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/annotation-actions.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/annotation-actions.service.ts @@ -405,17 +405,16 @@ export class AnnotationActionsService { if (annotationWrapper.rectangle || annotationWrapper.imported || annotationWrapper.isImage) { this._annotationManager.delete(annotationWrapper); const rectangleAnnotation = this.#generateRectangle(annotationWrapper); - this._pdf.annotationManager.addAnnotation(rectangleAnnotation, { imported: true }); - await this._pdf.annotationManager.drawAnnotationsFromList([rectangleAnnotation]); - this._pdf.annotationManager.selectAnnotation(rectangleAnnotation); + await this._annotationManager.add(rectangleAnnotation); + this._annotationManager.select(rectangleAnnotation); return; } viewerAnnotation.ReadOnly = false; viewerAnnotation.Hidden = false; viewerAnnotation.disableRotationControl(); - this._pdf.annotationManager.redrawAnnotation(viewerAnnotation); - this._pdf.annotationManager.selectAnnotation(viewerAnnotation); + this._annotationManager.redraw(viewerAnnotation); + this._annotationManager.select(viewerAnnotation); } async acceptResize($event: MouseEvent, annotation: AnnotationWrapper): Promise { @@ -468,7 +467,7 @@ export class AnnotationActionsService { #generateRectangle(annotationWrapper: AnnotationWrapper) { const annotation = this._pdf.rectangle(); - const pageHeight = this._pdf.documentViewer.getPageHeight(annotationWrapper.pageNumber); + const pageHeight = this._documentViewer.getHeight(annotationWrapper.pageNumber); const rectangle: IRectangle = annotationWrapper.positions[0]; annotation.PageNumber = annotationWrapper.pageNumber; annotation.X = rectangle.topLeft.x; @@ -576,7 +575,7 @@ export class AnnotationActionsService { } else { const rect = toPosition( viewerAnnotation.getPageNumber(), - this._pdf.documentViewer.getPageHeight(viewerAnnotation.getPageNumber()), + this._documentViewer.getHeight(viewerAnnotation.getPageNumber()), this._annotationDrawService.annotationToQuads(viewerAnnotation), ); return { diff --git a/apps/red-ui/src/app/modules/file-preview/services/pdf-proxy.service.ts b/apps/red-ui/src/app/modules/file-preview/services/pdf-proxy.service.ts index 6f117f6a9..41bbcc196 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/pdf-proxy.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/pdf-proxy.service.ts @@ -257,7 +257,7 @@ export class PdfProxyService { private _addManualRedactionOfType(type: ManualRedactionEntryType) { const selectedQuads: Record = this._pdf.documentViewer.getSelectedTextQuads(); - const text = this._pdf.documentViewer.getSelectedText(); + const text = this._documentViewer.selectedText; const manualRedactionEntry = this._getManualRedaction(selectedQuads, text, true); this.manualAnnotationRequested$.next({ manualRedactionEntry, type }); } diff --git a/apps/red-ui/src/app/modules/file-preview/services/stamp.service.ts b/apps/red-ui/src/app/modules/file-preview/services/stamp.service.ts index 2b943cb39..82f7805c1 100644 --- a/apps/red-ui/src/app/modules/file-preview/services/stamp.service.ts +++ b/apps/red-ui/src/app/modules/file-preview/services/stamp.service.ts @@ -48,9 +48,7 @@ export class StampService { await this._stampExcludedPages(pdfDoc, file.excludedPages); } - this._pdf.documentViewer.refreshAll(); - const currentPage = this._pdf.currentPage; - this._pdf.documentViewer.updateView([currentPage], currentPage); + this._documentViewer.refreshAndUpdateView(); } private async _stampExcludedPages(document: PDFNet.PDFDoc, excludedPages: number[]): Promise { diff --git a/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-draw.service.ts b/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-draw.service.ts index 4e1f519d4..608d99dee 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-draw.service.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-draw.service.ts @@ -90,8 +90,7 @@ export class AnnotationDrawService { const annotations = annotationWrappers .map(annotation => this._computeAnnotation(annotation, dossierTemplateId, hideSkipped)) .filter(a => !!a); - this._pdf.annotationManager.addAnnotations(annotations, { imported: true }); - await this._pdf.annotationManager.drawAnnotationsFromList(annotations); + await this._annotationManager.add(annotations); if (this._userPreferenceService.areDevFeaturesEnabled) { const { dossierId, fileId } = this._pdf; @@ -107,13 +106,9 @@ export class AnnotationDrawService { const sectionRectangles = sectionGrid.rectanglesPerPage[page]; sectionRectangles.forEach(sectionRectangle => { sections.push(this._computeSection(dossierTemplateId, parseInt(page, 10), sectionRectangle)); - // sectionRectangle.tableCells?.forEach(cell =>{ - // sections.push(this.computeSection(activeViewer, parseInt(page, 10), cell)); - // }) }); } - this._pdf.annotationManager.addAnnotations(sections, { imported: true }); - await this._pdf.annotationManager.drawAnnotationsFromList(sections); + await this._annotationManager.add(sections); } private _computeSection(dossierTemplateId: string, pageNumber: number, sectionRectangle: ISectionRectangle) { diff --git a/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-manager.service.ts b/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-manager.service.ts index ed326bd39..bce4a3e08 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-manager.service.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/services/annotation-manager.service.ts @@ -2,10 +2,10 @@ import { Injectable } from '@angular/core'; import { Core } from '@pdftron/webviewer'; import type { List } from '@iqser/common-ui'; import { AnnotationPredicate, DeleteAnnotationsOptions } from '../utils/types'; -import { AnnotationWrapper } from '../../../models/file/annotation.wrapper'; +import { AnnotationWrapper } from '@models/file/annotation.wrapper'; import { fromEvent, Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; -import { getId, isStringOrWrapper } from '../utils/functions'; +import { asList, getId, isStringOrWrapper } from '../utils/functions'; import AnnotationManager = Core.AnnotationManager; import Annotation = Core.Annotations.Annotation; @@ -83,6 +83,21 @@ export class REDAnnotationManager { this.#manager.selectAnnotations(annotationsFromViewer); } + select(annotations: Annotation | Annotation[]) { + this.#manager.selectAnnotations(asList(annotations)); + } + + redraw(annotations: Annotation | Annotation[]) { + annotations = asList(annotations); + annotations.forEach(annotation => this.#manager.redrawAnnotation(annotation)); + } + + add(annotations: Annotation | Annotation[]) { + annotations = asList(annotations); + this.#manager.addAnnotations(annotations, { imported: true }); + return this.#manager.drawAnnotationsFromList(annotations); + } + #getById(annotation: AnnotationWrapper | string) { return this.#manager.getAnnotationById(getId(annotation)); } 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 24f741b92..2e524a5cf 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 @@ -8,6 +8,7 @@ import { PdfViewer } from './pdf-viewer.service'; import { UserPreferenceService } from '@services/user-preference.service'; import { log, shareLast } from '@iqser/common-ui'; import { stopAndPrevent, stopAndPreventIfNotAllowed } from '../utils/functions'; +import { RotationType, RotationTypes } from '@red/domain'; import DocumentViewer = Core.DocumentViewer; import Color = Core.Annotations.Color; import Quad = Core.Math.Quad; @@ -148,6 +149,28 @@ export class REDDocumentViewer { } } + refreshAndUpdateView() { + this.#document.refreshAll(); + const currentPage = this._pdf.currentPage; + this.#document.updateView([currentPage], currentPage); + } + + resetRotation(pages: number | number[] | string | string[]) { + if (!(pages instanceof Array)) { + pages = [Number(pages)]; + } + + pages.forEach(page => this.#document.setRotation(0, Number(page))); + } + + rotate(rotation: RotationType, page = this._pdf.currentPage) { + if (rotation === RotationTypes.LEFT) { + this.#document.rotateCounterClockwise(page); + } else { + this.#document.rotateClockwise(page); + } + } + #setCurrentPage() { const currentDocPage = this._activatedRoute.snapshot.queryParamMap.get('page'); this.#document.setCurrentPage(Number(currentDocPage ?? '1')); diff --git a/apps/red-ui/src/app/modules/pdf-viewer/services/page-rotation.service.ts b/apps/red-ui/src/app/modules/pdf-viewer/services/page-rotation.service.ts index 74f541614..182bbddbd 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/services/page-rotation.service.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/services/page-rotation.service.ts @@ -1,6 +1,6 @@ import { Injectable, Injector } from '@angular/core'; import { BehaviorSubject, firstValueFrom, of } from 'rxjs'; -import { RotationType, RotationTypes } from '@red/domain'; +import { RotationType } from '@red/domain'; import { FileManagementService } from '../../../services/files/file-management.service'; import { distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'; import { @@ -16,8 +16,7 @@ import { FilesService } from '../../../services/files/files.service'; import { PdfViewer } from './pdf-viewer.service'; import { FilesMapService } from '../../../services/files/files-map.service'; import { NGXLogger } from 'ngx-logger'; - -const ONE_ROTATION_DEGREE = 90; +import { REDDocumentViewer } from './document-viewer.service'; @Injectable() export class PageRotationService { @@ -31,6 +30,7 @@ export class PageRotationService { private readonly _fileManagementService: FileManagementService, private readonly _filesService: FilesService, private readonly _filesMapService: FilesMapService, + private readonly _documentViewer: REDDocumentViewer, ) {} get hasRotations() { @@ -68,15 +68,7 @@ export class PageRotationService { } discardRotation() { - const rotations = this.#rotations$.value; - - for (const page of Object.keys(rotations)) { - const times = rotations[page] / ONE_ROTATION_DEGREE; - for (let i = 1; i <= times; i++) { - this._pdf.documentViewer.rotateCounterClockwise(Number(page)); - } - } - + this._documentViewer.resetRotation(Object.keys(this.#rotations$.value)); this.clearRotations(); } @@ -87,11 +79,7 @@ export class PageRotationService { this.#rotations$.next({ ...this.#rotations$.value, [pageNumber]: rotationValue }); - if (rotation === RotationTypes.LEFT) { - this._pdf.documentViewer.rotateCounterClockwise(pageNumber); - } else { - this._pdf.documentViewer.rotateClockwise(pageNumber); - } + this._documentViewer.rotate(rotation); } clearRotations() { 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 2b628a687..75f134aa5 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 @@ -14,7 +14,6 @@ import { Rgb } from '../utils/types'; import { asList } from '../utils/functions'; import { REDAnnotationManager } from './annotation-manager.service'; import { TranslateService } from '@ngx-translate/core'; -import AnnotationManager = Core.AnnotationManager; import TextTool = Core.Tools.TextTool; import Annotation = Core.Annotations.Annotation; import TextHighlightAnnotation = Core.Annotations.TextHighlightAnnotation; @@ -32,10 +31,7 @@ export class PdfViewer { * @deprecated Use REDDocumentViewer service instead */ documentViewer: DocumentViewer; - /** - * @deprecated Use REDAnnotationManager service instead - */ - annotationManager: AnnotationManager; + fileId: string; dossierId: string; @@ -144,7 +140,6 @@ export class PdfViewer { this._logger.info('[PDF] Initialized'); this.documentViewer = this.#instance.Core.documentViewer; - this.annotationManager = this.#instance.Core.annotationManager; this.compareMode$ = this.#compareMode$.asObservable(); this.pageChanged$ = this.#pageChanged$.pipe(shareLast()); diff --git a/apps/red-ui/src/app/modules/pdf-viewer/utils/functions.ts b/apps/red-ui/src/app/modules/pdf-viewer/utils/functions.ts index 63f19dfa7..c8a8586d6 100644 --- a/apps/red-ui/src/app/modules/pdf-viewer/utils/functions.ts +++ b/apps/red-ui/src/app/modules/pdf-viewer/utils/functions.ts @@ -25,9 +25,12 @@ export function isStringOrWrapper(value: unknown): value is string | AnnotationW return typeof value === 'string' || value instanceof AnnotationWrapper; } +export function asList(items: T[] | T): T[]; export function asList(items: string[] | string): string[]; export function asList(items: AnnotationWrapper[] | AnnotationWrapper): AnnotationWrapper[]; -export function asList(items: string[] | string | AnnotationWrapper[] | AnnotationWrapper): string[] | AnnotationWrapper[] { +export function asList( + items: string[] | string | AnnotationWrapper[] | AnnotationWrapper | T | T[], +): string[] | AnnotationWrapper[] | T[] { if (typeof items === 'string') { return [items]; } @@ -36,5 +39,10 @@ export function asList(items: string[] | string | AnnotationWrapper[] | Annotati return [items]; } + const isArray = items instanceof Array; + if (!isArray) { + return [items]; + } + return items; }