update pdfviewer utils

This commit is contained in:
Dan Percic 2021-09-12 12:23:34 +03:00
parent f40c67337a
commit c8a63b9aa4

View File

@ -1,4 +1,3 @@
// import { Annotations, WebViewerInstance } from '@pdftron/webviewer';
import { ViewMode } from '@models/file/view-mode'; import { ViewMode } from '@models/file/view-mode';
import { translateQuads } from '@utils/pdf-coordinates'; import { translateQuads } from '@utils/pdf-coordinates';
import { Rectangle } from '@redaction/red-ui-http'; import { Rectangle } from '@redaction/red-ui-http';
@ -37,18 +36,19 @@ const DISABLED_HOTKEYS = [
'H', 'H',
'K', 'K',
'U' 'U'
]; ] as const;
export class PdfViewerUtils { export class PdfViewerUtils {
instance: WebViewerInstance;
viewMode: ViewMode;
ready = false; ready = false;
multiSelectActive: boolean;
constructor(instance: WebViewerInstance, viewMode: ViewMode, multiSelectActive: boolean) { constructor(readonly instance: WebViewerInstance, public viewMode: ViewMode, public multiSelectActive: boolean) {}
this.instance = instance;
this.viewMode = viewMode; private get _documentViewer() {
this.multiSelectActive = multiSelectActive; return this.instance?.Core.documentViewer;
}
private get _annotationManager() {
return this.instance?.Core.annotationManager;
} }
get isCompareMode() { get isCompareMode() {
@ -61,9 +61,7 @@ export class PdfViewerUtils {
get currentPage() { get currentPage() {
try { try {
return this.isCompareMode return this.isCompareMode ? Math.ceil(this._currentInternalPage / 2) : this._currentInternalPage;
? Math.ceil(this.instance?.Core.documentViewer?.getCurrentPage() / 2)
: this.instance?.Core.documentViewer?.getCurrentPage();
} catch (e) { } catch (e) {
return null; return null;
} }
@ -75,9 +73,7 @@ export class PdfViewerUtils {
} }
try { try {
return this.isCompareMode return this.isCompareMode ? Math.ceil(this._totalInternalPages / 2) : this._totalInternalPages;
? Math.ceil(this.instance?.Core.documentViewer?.getPageCount() / 2)
: this.instance?.Core.documentViewer?.getPageCount();
} catch (e) { } catch (e) {
return null; return null;
} }
@ -115,12 +111,12 @@ export class PdfViewerUtils {
} }
translateQuads(page: number, quads: any) { translateQuads(page: number, quads: any) {
const rotation = this.instance.Core.documentViewer.getCompleteRotation(page); const rotation = this._documentViewer.getCompleteRotation(page);
return translateQuads(page, rotation, quads); return translateQuads(page, rotation, quads);
} }
toPosition(page: number, selectedQuad: any): Rectangle { toPosition(page: number, selectedQuad: any): Rectangle {
const pageHeight = this.instance.Core.documentViewer.getPageHeight(page); const pageHeight = this._documentViewer.getPageHeight(page);
const height = selectedQuad.y2 - selectedQuad.y4; const height = selectedQuad.y2 - selectedQuad.y4;
return { return {
page: page, page: page,
@ -134,7 +130,7 @@ export class PdfViewerUtils {
} }
deselectAllAnnotations() { deselectAllAnnotations() {
this.instance.Core.annotationManager.deselectAllAnnotations(); this._annotationManager.deselectAllAnnotations();
} }
selectAnnotations($event: AnnotationWrapper[] | { annotations: AnnotationWrapper[]; multiSelect: boolean }) { selectAnnotations($event: AnnotationWrapper[] | { annotations: AnnotationWrapper[]; multiSelect: boolean }) {
@ -153,24 +149,23 @@ export class PdfViewerUtils {
} }
const annotationsFromViewer = annotations.map(ann => this._getAnnotationById(ann.id)); const annotationsFromViewer = annotations.map(ann => this._getAnnotationById(ann.id));
this.instance.Core.annotationManager.selectAnnotations(annotationsFromViewer); this._annotationManager.selectAnnotations(annotationsFromViewer);
// this.navigateToPage(annotations[0].pageNumber*this.paginationOffset); // this.navigateToPage(annotations[0].pageNumber*this.paginationOffset);
this.instance.Core.annotationManager.jumpToAnnotation(annotationsFromViewer[0]); this._annotationManager.jumpToAnnotation(annotationsFromViewer[0]);
} }
deselectAnnotations(annotations: AnnotationWrapper[]) { deselectAnnotations(annotations: AnnotationWrapper[]) {
const ann = annotations.map(a => this._getAnnotationById(a.id)); const ann = annotations.map(a => this._getAnnotationById(a.id));
this.instance.Core.annotationManager.deselectAnnotations(ann); this._annotationManager.deselectAnnotations(ann);
} }
private _navigateToPage(pageNumber) { private _navigateToPage(pageNumber) {
const activePage = this.instance.Core.documentViewer.getCurrentPage(); if (this._currentInternalPage !== pageNumber) {
if (activePage !== pageNumber) { this._documentViewer.displayPageLocation(pageNumber, 0, 0);
this.instance.Core.documentViewer.displayPageLocation(pageNumber, 0, 0);
} }
} }
private _getAnnotationById(id: string): Annotation { private _getAnnotationById(id: string): Annotation {
return this.instance.Core.annotationManager.getAnnotationById(id); return this._annotationManager.getAnnotationById(id);
} }
} }