Implement support for comic book files (CBZ, CBR and CB7)

This commit is contained in:
aditya.chandel
2025-05-23 22:58:23 -06:00
committed by Aditya Chandel
parent 25ee510379
commit ff64362edb
56 changed files with 1617 additions and 141 deletions
@@ -0,0 +1,35 @@
<div class="comic-reader-container">
<div class="navigation">
<div class="page-controls">
<button (click)="previousPage()" [disabled]="currentPage === 0">&#8592;</button>
<span>
Page {{ currentPage + 1 }}{{ isTwoPageView && imageUrls.length > 1 ? '-' + (currentPage + 2) : '' }} of {{ pages.length }}
</span>
<button (click)="nextPage()" [disabled]="currentPage >= pages.length - 1">&#8594;</button>
</div>
<div class="spread-controls">
<button *ngIf="isTwoPageView" (click)="toggleSpreadDirection()" title="Toggle Spread Direction">
{{ pageSpread === 'ODD' ? '⬅️' : '➡️' }}
</button>
<button (click)="toggleView()" title="Toggle View">
{{ isTwoPageView ? '1️⃣' : '2️⃣' }}
</button>
</div>
</div>
<div class="image-container" [class.two-page-view]="isTwoPageView">
<ng-container *ngIf="!isLoading; else loading">
<ng-container *ngIf="pages.length > 0; else noPages">
<img *ngFor="let url of imageUrls" [src]="url" alt="Comic Page"/>
</ng-container>
</ng-container>
<ng-template #noPages>
<p>No pages available.</p>
</ng-template>
<ng-template #loading>
<div style="display: flex; justify-content: center; align-items: center; height: 100%;">
<p-progressSpinner></p-progressSpinner>
</div>
</ng-template>
</div>
</div>
@@ -0,0 +1,96 @@
.comic-reader-container {
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
width: 100vw;
overflow-x: hidden;
box-sizing: border-box;
.navigation {
position: relative;
width: 100%;
margin: 1rem;
.page-controls {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
gap: 1rem;
align-items: center;
white-space: nowrap;
span {
min-width: 8rem;
display: inline-block;
text-align: center;
font-variant-numeric: tabular-nums;
}
button {
min-width: 3rem;
padding: 0 0.5rem;
text-align: center;
white-space: nowrap;
cursor: pointer;
&:disabled {
cursor: default;
color: #999;
}
}
}
.spread-controls {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
display: flex;
gap: 0.5rem;
align-items: center;
button {
padding-left: 0.5rem;
padding-right: 0.5rem;
}
}
}
.image-container {
flex-grow: 1;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
overflow: hidden;
padding: 0 1rem;
box-sizing: border-box;
&.two-page-view {
img {
max-width: calc(50% - 0.5rem);
}
}
&:not(.two-page-view) {
img {
max-width: 100%;
}
}
img {
max-height: 100%;
height: auto;
width: auto;
object-fit: contain;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
flex-shrink: 1;
margin: 0;
display: block;
}
}
}
@@ -0,0 +1,195 @@
import {Component, HostListener, inject, OnInit} from '@angular/core';
import {NgFor, NgIf} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {CbxReaderService} from '../../service/cbx-reader.service';
import {BookService} from '../../service/book.service';
import {UserService} from '../../../settings/user-management/user.service';
import {MessageService} from 'primeng/api';
import {forkJoin} from 'rxjs';
import {BookSetting, CbxPageSpread, CbxPageViewMode} from '../../model/book.model';
import {ProgressSpinner} from 'primeng/progressspinner';
@Component({
selector: 'app-cbx-reader',
standalone: true,
imports: [NgIf, NgFor, ProgressSpinner],
templateUrl: './cbx-reader.component.html',
styleUrl: './cbx-reader.component.scss'
})
export class CbxReaderComponent implements OnInit {
bookId!: number;
pages: number[] = [];
currentPage = 0;
isLoading = true;
pageSpread: CbxPageSpread = CbxPageSpread.ODD;
pageViewMode: CbxPageViewMode = CbxPageViewMode.SINGLE_PAGE;
private touchStartX = 0;
private touchEndX = 0;
private route = inject(ActivatedRoute);
private cbxReaderService = inject(CbxReaderService);
private bookService = inject(BookService);
private userService = inject(UserService);
private messageService = inject(MessageService);
ngOnInit() {
this.route.paramMap.subscribe((params) => {
this.isLoading = true;
this.bookId = +params.get('bookId')!;
forkJoin([
this.bookService.getBookByIdFromAPI(this.bookId, false),
this.bookService.getBookSetting(this.bookId),
this.userService.getMyself(),
this.cbxReaderService.getAvailablePages(this.bookId)
]).subscribe({
next: ([cbxMeta, bookSettings, myself, pages]) => {
const userSettings = myself.userSettings;
const global = userSettings.perBookSetting.cbx === 'Global';
this.pageViewMode = global
? userSettings.cbxReaderSetting.pageViewMode || CbxPageViewMode.SINGLE_PAGE
: bookSettings.cbxSettings?.pageViewMode || userSettings.cbxReaderSetting.pageViewMode || CbxPageViewMode.SINGLE_PAGE;
this.pageSpread = global
? userSettings.cbxReaderSetting.pageSpread || CbxPageSpread.ODD
: bookSettings.cbxSettings?.pageSpread || userSettings.cbxReaderSetting.pageSpread || CbxPageSpread.ODD;
this.pages = pages;
this.currentPage = (cbxMeta.cbxProgress?.page || 1) - 1;
this.alignCurrentPageToParity();
this.isLoading = false;
},
error: (err) => {
const errorMessage = err?.error?.message || 'Failed to load the book';
this.messageService.add({severity: 'error', summary: 'Error', detail: errorMessage});
this.isLoading = false;
}
});
});
}
get isTwoPageView(): boolean {
return this.pageViewMode === CbxPageViewMode.TWO_PAGE;
}
toggleView() {
if (!this.isTwoPageView && this.isPhonePortrait()) return;
this.pageViewMode = this.isTwoPageView ? CbxPageViewMode.SINGLE_PAGE : CbxPageViewMode.TWO_PAGE;
this.alignCurrentPageToParity();
this.updateViewerSetting();
}
toggleSpreadDirection() {
this.pageSpread = this.pageSpread === CbxPageSpread.ODD ? CbxPageSpread.EVEN : CbxPageSpread.ODD;
this.alignCurrentPageToParity();
this.updateViewerSetting();
}
nextPage() {
if (this.isTwoPageView) {
this.currentPage += (this.currentPage < this.pages.length - 2) ? 2 : 1;
} else if (this.currentPage < this.pages.length - 1) {
this.currentPage++;
}
this.updateProgress();
}
previousPage() {
if (this.isTwoPageView) {
this.currentPage = Math.max(0, this.currentPage - 2);
} else {
this.currentPage = Math.max(0, this.currentPage - 1);
}
this.updateProgress();
}
private alignCurrentPageToParity() {
if (!this.pages.length || !this.isTwoPageView) return;
const desiredOdd = this.pageSpread === CbxPageSpread.ODD;
for (let i = this.currentPage; i >= 0; i--) {
if ((this.pages[i] % 2 === 1) === desiredOdd) {
this.currentPage = i;
this.updateProgress();
return;
}
}
for (let i = 0; i < this.pages.length; i++) {
if ((this.pages[i] % 2 === 1) === desiredOdd) {
this.currentPage = i;
this.updateProgress();
return;
}
}
}
@HostListener('window:keydown', ['$event'])
handleKeyDown(event: KeyboardEvent) {
if (event.key === 'ArrowRight') this.nextPage();
else if (event.key === 'ArrowLeft') this.previousPage();
}
@HostListener('touchstart', ['$event'])
onTouchStart(event: TouchEvent) {
this.touchStartX = event.changedTouches[0].screenX;
}
@HostListener('touchend', ['$event'])
onTouchEnd(event: TouchEvent) {
this.touchEndX = event.changedTouches[0].screenX;
this.handleSwipeGesture();
}
@HostListener('window:resize')
onResize() {
this.enforcePortraitSinglePageView();
}
private handleSwipeGesture() {
const delta = this.touchEndX - this.touchStartX;
if (Math.abs(delta) >= 50) delta < 0 ? this.nextPage() : this.previousPage();
}
private enforcePortraitSinglePageView() {
if (this.isPhonePortrait() && this.isTwoPageView) {
this.pageViewMode = CbxPageViewMode.SINGLE_PAGE;
this.updateViewerSetting();
}
}
private isPhonePortrait(): boolean {
return window.innerWidth < 768 && window.innerHeight > window.innerWidth;
}
get imageUrls(): string[] {
if (!this.pages.length) return [];
const urls = [this.cbxReaderService.getPageImageUrl(this.bookId, this.pages[this.currentPage])];
if (this.isTwoPageView && this.currentPage + 1 < this.pages.length) {
urls.push(this.cbxReaderService.getPageImageUrl(this.bookId, this.pages[this.currentPage + 1]));
}
return urls;
}
private updateViewerSetting(): void {
const bookSetting: BookSetting = {
cbxSettings: {
pageSpread: this.pageSpread,
pageViewMode: this.pageViewMode,
}
}
this.bookService.updateViewerSetting(bookSetting, this.bookId).subscribe();
}
updateProgress(): void {
const percentage = this.pages.length > 0
? Math.round(((this.currentPage + 1) / this.pages.length) * 1000) / 10
: 0;
this.bookService.saveCbxProgress(this.bookId, this.currentPage + 1, percentage).subscribe();
}
}