From 440063c3b36b4eba8b39686e3e6b3897e6342c4c Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Mon, 31 Jan 2022 20:42:32 +0200 Subject: [PATCH] remove file from fileData --- .../src/app/models/file/file-data.model.ts | 10 -- .../pdf-viewer/pdf-viewer.component.ts | 60 +++++----- .../file-preview-screen.component.html | 2 +- .../file-preview-screen.component.ts | 110 ++++++------------ .../services/annotation-actions.service.ts | 108 ++++++++--------- .../services/file-preview-state.service.ts | 40 +++++-- .../screens/file-preview-screen/utils.ts | 27 +++++ 7 files changed, 174 insertions(+), 183 deletions(-) create mode 100644 apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/utils.ts diff --git a/apps/red-ui/src/app/models/file/file-data.model.ts b/apps/red-ui/src/app/models/file/file-data.model.ts index 6f508752a..7b73d7228 100644 --- a/apps/red-ui/src/app/models/file/file-data.model.ts +++ b/apps/red-ui/src/app/models/file/file-data.model.ts @@ -19,7 +19,6 @@ export class FileDataModel { allAnnotations: AnnotationWrapper[]; readonly hasChangeLog$ = new BehaviorSubject(false); readonly blob$ = new BehaviorSubject(undefined); - readonly file$ = new BehaviorSubject(undefined); constructor( private readonly _file: File, @@ -29,19 +28,10 @@ export class FileDataModel { private _dictionaryData?: { [p: string]: Dictionary }, private _areDevFeaturesEnabled?: boolean, ) { - this.file$.next(_file); this.blob$.next(_blob); this._buildAllAnnotations(); } - get file(): File { - return this.file$.value; - } - - set file(file: File) { - this.file$.next(file); - } - get redactionLog(): IRedactionLog { return this._redactionLog; } diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts index 58d71be6c..70a352ba4 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts @@ -11,7 +11,7 @@ import { SimpleChanges, ViewChild, } from '@angular/core'; -import { Dossier, IManualRedactionEntry } from '@red/domain'; +import { Dossier, File, IManualRedactionEntry } from '@red/domain'; import WebViewer, { Core, WebViewerInstance } from '@pdftron/webviewer'; import { TranslateService } from '@ngx-translate/core'; import { @@ -37,7 +37,7 @@ import { toPosition } from '../../../../utils/pdf-calculation.utils'; import { ViewModeService } from '../../services/view-mode.service'; import { MultiSelectService } from '../../services/multi-select.service'; import { FilePreviewStateService } from '../../services/file-preview-state.service'; -import { filter, switchMap, tap } from 'rxjs/operators'; +import { filter, switchMap, tap, withLatestFrom } from 'rxjs/operators'; import Tools = Core.Tools; import TextTool = Tools.TextTool; import Annotation = Core.Annotations.Annotation; @@ -126,18 +126,19 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha switchMap(fileData => fileData.blob$), // Skip document reload if file content hasn't changed shareDistinctLast(), - tap(() => this._loadDocument()), + withLatestFrom(this.stateService.file$), + tap(([blob, file]) => this._loadDocument(blob, file)), ) .subscribe(); } - ngOnChanges(changes: SimpleChanges): void { + async ngOnChanges(changes: SimpleChanges): Promise { if (!this.instance) { return; } if (changes.canPerformActions) { - this._handleCustomActions(); + await this._handleCustomActions(); } } @@ -163,12 +164,13 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha this._loadingService.start(); this.utils.ready = false; const mergedDocument = await pdfNet.PDFDoc.create(); + const file = await this.stateService.file; await loadCompareDocumentWrapper( currentDocument, compareDocument, mergedDocument, this.instance, - this.stateService.fileData.file, + file, () => { this.viewModeService.compareMode = true; }, @@ -211,9 +213,10 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha const pdfNet = this.instance.Core.PDFNet; await pdfNet.initialize(environment.licenseKey ? atob(environment.licenseKey) : null); const currentDocument = await pdfNet.PDFDoc.createFromBuffer(await this.stateService.fileData.blob$.value.arrayBuffer()); - this.instance.UI.loadDocument(currentDocument, { - filename: this.stateService.fileData?.file?.filename ?? 'document.pdf', - }); + + const filename = (await this.stateService.file).filename ?? 'document.pdf'; + this.instance.UI.loadDocument(currentDocument, { filename }); + this.instance.UI.disableElements([dataElements.CLOSE_COMPARE_BUTTON]); this.instance.UI.enableElements([dataElements.COMPARE_BUTTON]); this.utils.navigateToPage(1); @@ -238,7 +241,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha this._setSelectionMode(); this._configureElements(); this.utils.disableHotkeys(); - this._configureTextPopup(); + await this._configureTextPopup(); this.annotationManager.addEventListener('annotationSelected', (annotations: Annotation[], action) => { this.annotationSelected.emit(this.annotationManager.getSelectedAnnotations().map(ann => ann.Id)); @@ -265,7 +268,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha this.utils.deselectAllAnnotations(); } this._ngZone.run(() => this.pageChanged.emit(pageNumber)); - this._handleCustomActions(); + return this._handleCustomActions(); }); this.documentViewer.addEventListener('documentLoaded', this._setReadyAndInitialState); @@ -288,11 +291,12 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha } }); - this.documentViewer.addEventListener('textSelected', (quads, selectedText) => { + this.documentViewer.addEventListener('textSelected', async (quads, selectedText) => { this._selectedText = selectedText; const textActions = [dataElements.ADD_DICTIONARY, dataElements.ADD_FALSE_POSITIVE]; - if (selectedText.length > 2 && this.canPerformActions && !this.utils.isCurrentPageExcluded(this.stateService.fileData.file)) { + const file = await this.stateService.file; + if (selectedText.length > 2 && this.canPerformActions && !this.utils.isCurrentPageExcluded(file)) { this.instance.UI.enableElements(textActions); } else { this.instance.UI.disableElements(textActions); @@ -479,14 +483,13 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha ]); } - this.instance.UI.annotationPopup.add( - this._annotationActionsService.getViewerAvailableActions( - this.instance, - this.stateService.fileData.file, - annotationWrappers, - this.annotationsChanged, - ), + const actions = this._annotationActionsService.getViewerAvailableActions( + this.instance, + this.dossier, + annotationWrappers, + this.annotationsChanged, ); + this.instance.UI.annotationPopup.add(actions); } private _configureRectangleAnnotationPopup() { @@ -576,7 +579,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha }, ]); - this._handleCustomActions(); + return this._handleCustomActions(); } private _addManualRedactionOfType(type: ManualRedactionEntryType) { @@ -586,7 +589,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha this.manualAnnotationRequested.emit(new ManualRedactionEntryWrapper(selectedQuads, manualRedaction, type)); } - private _handleCustomActions() { + private async _handleCustomActions() { this.instance.UI.setToolMode('AnnotationEdit'); const { ANNOTATION_POPUP, ADD_RECTANGLE, ADD_REDACTION, SHAPE_TOOL_GROUP_BUTTON } = dataElements; const elements = [ @@ -598,7 +601,9 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha ANNOTATION_POPUP, ]; - if (this.canPerformActions && !this.utils.isCurrentPageExcluded(this.stateService.fileData.file)) { + const isCurrentPageExcluded = this.utils.isCurrentPageExcluded(await this.stateService.file); + + if (this.canPerformActions && !isCurrentPageExcluded) { try { this.instance.UI.enableTools(['AnnotationCreateRectangle']); } catch (e) { @@ -615,7 +620,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha let elementsToDisable = [...elements, ADD_RECTANGLE]; - if (this.utils.isCurrentPageExcluded(this.stateService.fileData.file)) { + if (isCurrentPageExcluded) { const allowedActionsWhenPageExcluded: string[] = [ANNOTATION_POPUP, ADD_RECTANGLE, ADD_REDACTION, SHAPE_TOOL_GROUP_BUTTON]; elementsToDisable = elementsToDisable.filter(element => !allowedActionsWhenPageExcluded.includes(element)); } else { @@ -645,9 +650,10 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha return entry; } - private _loadDocument() { - this.instance.UI.loadDocument(this.stateService.fileData.blob$.value, { - filename: this.stateService.fileData?.file?.filename ?? 'document.pdf', + private _loadDocument(blob: Blob, file: File) { + console.log(blob, file); + this.instance.UI.loadDocument(blob, { + filename: file?.filename ?? 'document.pdf', }); } diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.html b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.html index 1fdaebc1a..6e5a51511 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.html +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.html @@ -1,4 +1,4 @@ - +