Skip to content

Commit

Permalink
Track hidden slides indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Nov 18, 2024
1 parent 1c24c93 commit caf2932
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ElementRef, effect, inject, input, output, signal, viewChild } from '@angular/core';
import { Component, ElementRef, OnChanges, SimpleChanges, effect, inject, input, output, signal, viewChild } from '@angular/core';
import * as PDFJS from 'pdfjs-dist';
import 'pdfjs-dist/build/pdf.worker';
import { ArtemisSharedModule } from 'app/shared/shared.module';
Expand All @@ -14,7 +14,7 @@ import { clone } from 'lodash-es';
standalone: true,
imports: [ArtemisSharedModule, PdfPreviewEnlargedCanvasComponent],
})
export class PdfPreviewThumbnailGridComponent {
export class PdfPreviewThumbnailGridComponent implements OnChanges {
pdfContainer = viewChild.required<ElementRef<HTMLDivElement>>('pdfContainer');

readonly DEFAULT_SLIDE_WIDTH = 250;
Expand Down Expand Up @@ -42,10 +42,15 @@ export class PdfPreviewThumbnailGridComponent {
// Injected services
private readonly alertService = inject(AlertService);

ngOnChanges(changes: SimpleChanges): void {
if (changes['hiddenPages']) {
this.newHiddenPages.set(clone(this.hiddenPages()!));
}
}

constructor() {
effect(
() => {
this.newHiddenPages.set(clone(this.hiddenPages()!));
this.loadOrAppendPdf(this.currentPdfUrl()!, this.appendFile());
},
{ allowSignalWrites: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h4>
(isPdfLoading)="isPdfLoading.set($event)"
(selectedPagesOutput)="selectedPages.set($event)"
(totalPagesOutput)="totalPages.set($event)"
(newHiddenPagesOutput)="newHiddenPages.set($event)"
(newHiddenPagesOutput)="hiddenPages.set($event)"
/>
} @else {
<div class="empty-pdf-container"></div>
Expand Down
30 changes: 26 additions & 4 deletions src/main/webapp/app/lecture/pdf-preview/pdf-preview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
* Triggers the file input to select files.
*/
triggerFileInput(): void {
this.fileInput().nativeElement.click();
//this.fileInput().nativeElement.click();
console.log(this.hiddenPages());
}

/**
Expand Down Expand Up @@ -228,9 +229,9 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
const pagesToDelete = Array.from(this.selectedPages()!)
.map((page) => page - 1)
.sort((a, b) => b - a);
pagesToDelete.forEach((pageIndex) => {
pdfDoc.removePage(pageIndex);
});

this.updateHiddenPages(pagesToDelete);
pagesToDelete.forEach((pageIndex) => pdfDoc.removePage(pageIndex));

this.isFileChanged.set(true);
const pdfBytes = await pdfDoc.save();
Expand All @@ -248,6 +249,27 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
}
}

/**
* Updates hidden pages after selected pages are deleted.
* @param pagesToDelete - Array of pages to be deleted (0-indexed).
*/
private updateHiddenPages(pagesToDelete: number[]) {
const updatedHiddenPages = new Set<number>();
this.hiddenPages().forEach((hiddenPage) => {
// Adjust hiddenPage based on the deleted pages
const adjustedPage = pagesToDelete.reduce((acc, pageIndex) => {
if (acc === pageIndex + 1) {
return;
}
return pageIndex < acc - 1 ? acc - 1 : acc;
}, hiddenPage);
if (adjustedPage !== -1) {
updatedHiddenPages.add(adjustedPage!);
}
});
this.hiddenPages.set(updatedHiddenPages);
}

/**
* Adds a selected PDF file at the end of the current PDF document.
* @param event - The event containing the file input.
Expand Down

0 comments on commit caf2932

Please sign in to comment.