red-ui/apps/red-ui/src/app/modules/dossier/utils/pdf-viewer.utils.ts
2021-12-15 23:30:45 +02:00

149 lines
4.2 KiB
TypeScript

import { translateQuads } from '@utils/pdf-coordinates';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { Core, WebViewerInstance } from '@pdftron/webviewer';
import { ViewModeService } from '../screens/file-preview-screen/services/view-mode.service';
import { File } from '@red/domain';
import Annotation = Core.Annotations.Annotation;
const DISABLED_HOTKEYS = [
'CTRL+SHIFT+EQUAL',
'COMMAND+SHIFT+EQUAL',
'CTRL+SHIFT+MINUS',
'COMMAND+SHIFT+MINUS',
'CTRL+V',
'COMMAND+V',
'CTRL+Y',
'COMMAND+Y',
'CTRL+O',
'COMMAND+O',
'CTRL+P',
'COMMAND+P',
'SPACE',
'UP',
'DOWN',
'R',
'P',
'A',
'C',
'E',
'I',
'L',
'N',
'O',
'T',
'S',
'G',
'H',
'K',
'U',
] as const;
export class PdfViewerUtils {
ready = false;
constructor(readonly instance: WebViewerInstance, readonly viewModeService: ViewModeService) {}
get paginationOffset() {
return this.viewModeService.isCompare ? 2 : 1;
}
get currentPage() {
try {
return this.viewModeService.isCompare ? Math.ceil(this._currentInternalPage / 2) : this._currentInternalPage;
} catch (e) {
return null;
}
}
get totalPages() {
if (!this.ready) {
return null;
}
try {
return this.viewModeService.isCompare ? Math.ceil(this._totalInternalPages / 2) : this._totalInternalPages;
} catch (e) {
return null;
}
}
private get _documentViewer() {
return this.instance?.Core.documentViewer;
}
private get _annotationManager() {
return this.instance?.Core.annotationManager;
}
private get _currentInternalPage() {
return this.instance?.Core.documentViewer?.getCurrentPage();
}
private get _totalInternalPages() {
return this.instance?.Core.documentViewer?.getPageCount();
}
isCurrentPageExcluded(file: File) {
const currentPage = this.currentPage;
return !!file?.excludedPages?.includes(currentPage);
}
navigateToPage(pageNumber: string | number) {
const parsedNumber = typeof pageNumber === 'string' ? parseInt(pageNumber, 10) : pageNumber;
this._navigateToPage(this.paginationOffset === 2 ? parsedNumber * this.paginationOffset - 1 : parsedNumber);
}
previousPage() {
if (this._currentInternalPage > 1) {
this._navigateToPage(Math.max(this._currentInternalPage - this.paginationOffset, 1));
}
}
nextPage() {
if (this._currentInternalPage < this._totalInternalPages) {
this._navigateToPage(Math.min(this._currentInternalPage + this.paginationOffset, this._totalInternalPages));
}
}
disableHotkeys(): void {
for (const hotkey of DISABLED_HOTKEYS) {
this.instance.UI.hotkeys.off(hotkey);
}
}
translateQuads(page: number, quads: any) {
const rotation = this._documentViewer.getCompleteRotation(page);
return translateQuads(page, rotation, quads);
}
deselectAllAnnotations() {
this._annotationManager.deselectAllAnnotations();
}
selectAnnotations(annotations: AnnotationWrapper[], multiSelectActive: boolean) {
if (!multiSelectActive) {
this.deselectAllAnnotations();
}
const annotationsFromViewer = annotations.map(ann => this._getAnnotationById(ann.id));
this._annotationManager.selectAnnotations(annotationsFromViewer);
// this.navigateToPage(annotations[0].pageNumber*this.paginationOffset);
this._annotationManager.jumpToAnnotation(annotationsFromViewer[0]);
}
deselectAnnotations(annotations: AnnotationWrapper[]) {
const ann = annotations.map(a => this._getAnnotationById(a.id));
this._annotationManager.deselectAnnotations(ann);
}
private _navigateToPage(pageNumber) {
if (this._currentInternalPage !== pageNumber) {
this._documentViewer.displayPageLocation(pageNumber, 0, 0);
}
}
private _getAnnotationById(id: string): Annotation {
return this._annotationManager.getAnnotationById(id);
}
}