Skip to content

Commit

Permalink
Fix append file & remove redundant parts
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Oct 27, 2024
1 parent 1681230 commit 1238d4a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ type NavigationDirection = 'next' | 'prev';
export class PdfPreviewEnlargedCanvasComponent {
enlargedCanvas = viewChild.required<ElementRef<HTMLCanvasElement>>('enlargedCanvas');

readonly DEFAULT_SLIDE_HEIGHT = 800;
readonly DEFAULT_ENLARGED_SLIDE_HEIGHT = 800;

// Inputs
pdfContainer = input.required<HTMLDivElement>();
originalCanvas = input<HTMLCanvasElement>();
totalPages = input<number>(0);

isEnlargedViewOutput = output<boolean>();

// Signals
currentPage = signal<number>(1);

//Outputs
isEnlargedViewOutput = output<boolean>();

constructor() {
effect(
() => {
this.displayEnlargedCanvas(this.originalCanvas()!, false);
this.displayEnlargedCanvas(this.originalCanvas()!);
},
{ allowSignalWrites: true },
);
Expand Down Expand Up @@ -65,8 +68,7 @@ export class PdfPreviewEnlargedCanvasComponent {
}
};

displayEnlargedCanvas(originalCanvas: HTMLCanvasElement, isVertical: boolean) {
this.adjustPdfContainerSize(isVertical);
displayEnlargedCanvas(originalCanvas: HTMLCanvasElement) {
this.currentPage.set(Number(originalCanvas.id));
this.toggleBodyScroll(true);
setTimeout(() => {
Expand Down Expand Up @@ -109,7 +111,7 @@ export class PdfPreviewEnlargedCanvasComponent {

if (originalCanvas.height > originalCanvas.width) {
// Vertical slide
const fixedHeight = this.DEFAULT_SLIDE_HEIGHT;
const fixedHeight = this.DEFAULT_ENLARGED_SLIDE_HEIGHT;
scaleY = fixedHeight / originalCanvas.height;
scaleX = containerWidth / originalCanvas.width;
} else {
Expand Down Expand Up @@ -174,7 +176,7 @@ export class PdfPreviewEnlargedCanvasComponent {
adjustPdfContainerSize(isVertical: boolean): void {
const pdfContainer = this.pdfContainer();
if (isVertical) {
pdfContainer.style.height = `${this.DEFAULT_SLIDE_HEIGHT}px`;
pdfContainer.style.height = `${this.DEFAULT_ENLARGED_SLIDE_HEIGHT}px`;
} else {
pdfContainer.style.height = '60vh';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="pdf-container" #pdfContainer>
@if (isEnlargedView()) {
<jhi-pdf-preview-enlarged-canvas-component
[style]="{ display: 'contents' }"
[pdfContainer]="pdfContainer"
[totalPages]="totalPages()"
[originalCanvas]="originalCanvas()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ export class PdfPreviewThumbnailGridComponent {
pdfContainer = viewChild.required<ElementRef<HTMLDivElement>>('pdfContainer');

readonly DEFAULT_SLIDE_WIDTH = 250;
readonly DEFAULT_SLIDE_HEIGHT = 800;

// Inputs
currentPdfUrl = input<string>();
appendFile = input<boolean>();

isPdfLoading = output<boolean>();

// Signals
isEnlargedView = signal<boolean>(false);
totalPages = signal<number>(0);
selectedPages = signal<Set<number>>(new Set());
originalCanvas = signal<HTMLCanvasElement | undefined>(undefined);

// Outputs
isPdfLoading = output<boolean>();
totalPagesOutput = output<number>();
selectedPagesOutput = output<Set<number>>();

// Injected services
private readonly alertService = inject(AlertService);

constructor() {
effect(
() => {
this.loadOrAppendPdf(this.currentPdfUrl()!, false);
this.loadOrAppendPdf(this.currentPdfUrl()!, this.appendFile());
},
{ allowSignalWrites: true },
);
Expand Down Expand Up @@ -186,7 +188,6 @@ export class PdfPreviewThumbnailGridComponent {
* @param originalCanvas - The original canvas element of the PDF page to be enlarged.
* */
displayEnlargedCanvas(originalCanvas: HTMLCanvasElement) {
//const isVertical = originalCanvas.height > originalCanvas.width;
this.originalCanvas.set(originalCanvas);
this.isEnlargedView.set(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
attachmentSub: Subscription;
attachmentUnitSub: Subscription;

// Signals
course = signal<Course | undefined>(undefined);
attachment = signal<Attachment | undefined>(undefined);
attachmentUnit = signal<AttachmentUnit | undefined>(undefined);
Expand Down Expand Up @@ -202,6 +203,8 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {

const objectUrl = URL.createObjectURL(this.currentPdfBlob()!);
this.currentPdfUrl.set(objectUrl);
this.appendFile.set(false);
this.dialogErrorSource.next('');
} catch (error) {
this.alertService.error('artemisApp.attachment.pdfPreview.pageDeleteError', { error: error.message });
} finally {
Expand Down Expand Up @@ -233,13 +236,12 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
this.selectedPages()!.clear();

const objectUrl = URL.createObjectURL(this.currentPdfBlob()!);
this.appendFile.set(true);
this.currentPdfUrl.set(objectUrl);
this.appendFile.set(true);
} catch (error) {
this.alertService.error('artemisApp.attachment.pdfPreview.mergeFailedError', { error: error.message });
} finally {
this.isPdfLoading.set(false);
this.appendFile.set(false);
this.fileInput()!.nativeElement.value = '';
}
}
Expand Down

0 comments on commit 1238d4a

Please sign in to comment.