Skip to content

Commit

Permalink
[MNT-24098] fixes for pdf with HTTP (#9155)
Browse files Browse the repository at this point in the history
* fixes for pdf with https

* [ci:force] cleanup code

* [ci:force] add missing types

* reduce duplicated code
  • Loading branch information
DenysVuika authored Dec 12, 2023
1 parent 468c326 commit bed9e4b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ export class NodesApiService {
}
}

return this.createNodeInsideRoot(name || window.crypto.randomUUID(), nodeType, properties, path);
return this.createNodeInsideRoot(name || this.randomNodeName(), nodeType, properties, path);
}

private randomNodeName(): string {
return `node_${Date.now()}`;
}

/**
Expand Down
51 changes: 27 additions & 24 deletions lib/core/src/lib/viewer/components/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ import {
SimpleChanges
} from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { LogService } from '../../common/services/log.service';
import { RenderingQueueServices } from '../services/rendering-queue.services';
import { PdfPasswordDialogComponent } from './pdf-viewer-password-dialog';
import { AppConfigService } from '../../app-config/app-config.service';
import { PDFDocumentProxy } from 'pdfjs-dist';
import { PDFDocumentProxy, OnProgressParameters, PDFDocumentLoadingTask } from 'pdfjs-dist';
import { Subject } from 'rxjs';
import { catchError, delay } from 'rxjs/operators';

declare const pdfjsLib: any;
declare const pdfjsViewer: any;

export type PdfScaleMode = 'init' | 'page-actual' | 'page-width' | 'page-height' | 'page-fit' | 'auto';

@Component({
selector: 'adf-pdf-viewer',
templateUrl: './pdf-viewer.component.html',
Expand Down Expand Up @@ -87,14 +88,14 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
totalPages: number;
loadingPercent: number;
pdfViewer: any;
currentScaleMode: 'init' | 'page-actual' | 'page-width' | 'page-height' | 'page-fit' | 'auto' = 'init';
currentScaleMode: PdfScaleMode = 'init';

MAX_AUTO_SCALE: number = 1.25;
DEFAULT_SCALE_DELTA: number = 1.1;
MIN_SCALE: number = 0.25;
MAX_SCALE: number = 10.0;

loadingTask: any;
loadingTask: PDFDocumentLoadingTask;
isPanelDisabled = true;
showThumbnails: boolean = false;
pdfThumbnailsContext: { viewer: any } = { viewer: null };
Expand All @@ -118,14 +119,14 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
constructor(
private dialog: MatDialog,
private renderingQueueServices: RenderingQueueServices,
private logService: LogService,
private appConfigService: AppConfigService
) {
// needed to preserve "this" context
this.onPageChange = this.onPageChange.bind(this);
this.onPagesLoaded = this.onPagesLoaded.bind(this);
this.onPageRendered = this.onPageRendered.bind(this);
this.randomPdfId = window.crypto.randomUUID();

this.randomPdfId = Date.now().toString();
this.pdfjsWorkerDestroy$
.pipe(
catchError(() => null),
Expand Down Expand Up @@ -200,14 +201,14 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
this.onPdfPassword(callback, reason);
};

this.loadingTask.onProgress = (progressData) => {
this.loadingTask.onProgress = (progressData: OnProgressParameters) => {
const level = progressData.loaded / progressData.total;
this.loadingPercent = Math.round(level * 100);
};

this.isPanelDisabled = true;
this.loadingTask.promise
.then((pdfDocument: PDFDocumentProxy) => {
.then((pdfDocument) => {
this.totalPages = pdfDocument.numPages;
this.page = 1;
this.displayPage = 1;
Expand Down Expand Up @@ -276,10 +277,10 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
*
* @param scaleMode - new scale mode
*/
scalePage(scaleMode) {
scalePage(scaleMode: PdfScaleMode) {
this.currentScaleMode = scaleMode;

const viewerContainer = document.getElementById(`${this.randomPdfId}-viewer-main-container`);
const viewerContainer = this.getMainContainer();
const documentContainer = this.getDocumentContainer();

if (this.pdfViewer && documentContainer) {
Expand All @@ -303,32 +304,30 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
let scale: number;
switch (this.currentScaleMode) {
case 'init':
case 'page-fit': {
scale = this.getUserScaling();
if (!scale) {
scale = this.autoScaling(pageHeightScale, pageWidthScale);
}
break;
case 'page-actual':
}
case 'page-actual': {
scale = 1;
break;
case 'page-width':
}
case 'page-width': {
scale = pageWidthScale;
break;
case 'page-height':
}
case 'page-height': {
scale = pageHeightScale;
break;
case 'page-fit':
scale = this.getUserScaling();
if (!scale) {
scale = this.autoScaling(pageHeightScale, pageWidthScale);
}
break;
case 'auto':
}
case 'auto': {
scale = this.autoScaling(pageHeightScale, pageWidthScale);

break;
}
default:
this.logService.error(`pdfViewSetScale: '${scaleMode}' is an unknown zoom value.`);
return;
}

Expand All @@ -348,11 +347,15 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
return this.checkPageFitInContainer(scale);
}

private getDocumentContainer() {
private getMainContainer(): HTMLElement {
return document.getElementById(`${this.randomPdfId}-viewer-main-container`);
}

private getDocumentContainer(): HTMLElement {
return document.getElementById(`${this.randomPdfId}-viewer-pdf-viewer`);
}

private getViewer() {
private getViewer(): HTMLElement {
return document.getElementById(`${this.randomPdfId}-viewer-viewerPdf`);
}

Expand Down

0 comments on commit bed9e4b

Please sign in to comment.