feat(reader): add PDF annotations, Range streaming, and optimized chunk loading (#2701)
This commit is contained in:
@@ -6,13 +6,12 @@ import {filter, first, map, switchMap, takeUntil, timeout} from 'rxjs/operators'
|
||||
import {PageTitleService} from "../../../shared/service/page-title.service";
|
||||
import {CbxReaderService} from '../../book/service/cbx-reader.service';
|
||||
import {BookService} from '../../book/service/book.service';
|
||||
import {CbxBackgroundColor, CbxFitMode, CbxPageSpread, CbxPageViewMode, CbxScrollMode, CbxReadingDirection, CbxSlideshowInterval, PdfBackgroundColor, PdfFitMode, PdfPageSpread, PdfPageViewMode, PdfScrollMode, UserService} from '../../settings/user-management/user.service';
|
||||
import {CbxBackgroundColor, CbxFitMode, CbxPageSpread, CbxPageViewMode, CbxScrollMode, CbxReadingDirection, CbxSlideshowInterval, UserService} from '../../settings/user-management/user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Book, BookSetting, BookType} from '../../book/model/book.model';
|
||||
import {BookState} from '../../book/model/state/book-state.model';
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {FormsModule} from "@angular/forms";
|
||||
import {NewPdfReaderService} from '../../book/service/new-pdf-reader.service';
|
||||
import {ReadingSessionService} from '../../../shared/service/reading-session.service';
|
||||
import {ReaderHeaderFooterVisibilityManager} from '../ebook-reader';
|
||||
|
||||
@@ -63,11 +62,11 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
currentPage = 0;
|
||||
isLoading = true;
|
||||
|
||||
pageSpread: CbxPageSpread | PdfPageSpread = CbxPageSpread.ODD;
|
||||
pageViewMode: CbxPageViewMode | PdfPageViewMode = CbxPageViewMode.SINGLE_PAGE;
|
||||
backgroundColor: CbxBackgroundColor | PdfBackgroundColor = CbxBackgroundColor.GRAY;
|
||||
fitMode: CbxFitMode | PdfFitMode = CbxFitMode.FIT_PAGE;
|
||||
scrollMode: CbxScrollMode | PdfScrollMode = CbxScrollMode.PAGINATED;
|
||||
pageSpread: CbxPageSpread = CbxPageSpread.ODD;
|
||||
pageViewMode: CbxPageViewMode = CbxPageViewMode.SINGLE_PAGE;
|
||||
backgroundColor: CbxBackgroundColor = CbxBackgroundColor.GRAY;
|
||||
fitMode: CbxFitMode = CbxFitMode.FIT_PAGE;
|
||||
scrollMode: CbxScrollMode = CbxScrollMode.PAGINATED;
|
||||
|
||||
private touchStartX = 0;
|
||||
private touchEndX = 0;
|
||||
@@ -110,7 +109,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
// Double-tap zoom
|
||||
private lastTapTime = 0;
|
||||
private originalFitMode: CbxFitMode | PdfFitMode | null = null;
|
||||
private originalFitMode: CbxFitMode | null = null;
|
||||
|
||||
// Shortcuts help dialog
|
||||
showShortcutsHelp = false;
|
||||
@@ -124,7 +123,6 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
private route = inject(ActivatedRoute);
|
||||
private router = inject(Router);
|
||||
private cbxReaderService = inject(CbxReaderService);
|
||||
private pdfReaderService = inject(NewPdfReaderService);
|
||||
private bookService = inject(BookService);
|
||||
private userService = inject(UserService);
|
||||
private messageService = inject(MessageService);
|
||||
@@ -141,7 +139,6 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
protected readonly CbxPageViewMode = CbxPageViewMode;
|
||||
protected readonly CbxPageSpread = CbxPageSpread;
|
||||
|
||||
private static readonly TYPE_PDF = 'PDF';
|
||||
private static readonly TYPE_CBX = 'CBX';
|
||||
private static readonly SETTING_GLOBAL = 'Global';
|
||||
|
||||
@@ -203,9 +200,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
this.loadSeriesNavigationAsync(book);
|
||||
}
|
||||
|
||||
const pagesObservable = this.bookType === CbxReaderComponent.TYPE_PDF
|
||||
? this.pdfReaderService.getAvailablePages(this.bookId, this.altBookType)
|
||||
: this.cbxReaderService.getAvailablePages(this.bookId, this.altBookType);
|
||||
const pagesObservable = this.cbxReaderService.getAvailablePages(this.bookId, this.altBookType);
|
||||
|
||||
pagesObservable.subscribe({
|
||||
next: (pages) => {
|
||||
@@ -239,30 +234,6 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.bookType === CbxReaderComponent.TYPE_PDF) {
|
||||
const global = userSettings.perBookSetting.pdf === CbxReaderComponent.SETTING_GLOBAL;
|
||||
this.pageViewMode = global
|
||||
? PdfPageViewMode[userSettings.newPdfReaderSetting.pageViewMode as keyof typeof PdfPageViewMode] || PdfPageViewMode.SINGLE_PAGE
|
||||
: PdfPageViewMode[bookSettings.newPdfSettings?.pageViewMode as keyof typeof PdfPageViewMode] || PdfPageViewMode[userSettings.newPdfReaderSetting.pageViewMode as keyof typeof PdfPageViewMode] || PdfPageViewMode.SINGLE_PAGE;
|
||||
|
||||
this.pageSpread = global
|
||||
? PdfPageSpread[userSettings.newPdfReaderSetting.pageSpread as keyof typeof PdfPageSpread] || PdfPageSpread.ODD
|
||||
: PdfPageSpread[bookSettings.newPdfSettings?.pageSpread as keyof typeof PdfPageSpread] || PdfPageSpread[userSettings.newPdfReaderSetting.pageSpread as keyof typeof PdfPageSpread] || PdfPageSpread.ODD;
|
||||
|
||||
this.fitMode = global
|
||||
? PdfFitMode[userSettings.newPdfReaderSetting.fitMode as keyof typeof PdfFitMode] || PdfFitMode.FIT_PAGE
|
||||
: PdfFitMode[bookSettings.newPdfSettings?.fitMode as keyof typeof PdfFitMode] || PdfFitMode[userSettings.newPdfReaderSetting.fitMode as keyof typeof PdfFitMode] || PdfFitMode.FIT_PAGE;
|
||||
|
||||
this.scrollMode = global
|
||||
? PdfScrollMode[userSettings.newPdfReaderSetting.scrollMode as keyof typeof PdfScrollMode] || PdfScrollMode.PAGINATED
|
||||
: PdfScrollMode[bookSettings.newPdfSettings?.scrollMode as keyof typeof PdfScrollMode] || PdfScrollMode[userSettings.newPdfReaderSetting.scrollMode as keyof typeof PdfScrollMode] || PdfScrollMode.PAGINATED;
|
||||
|
||||
this.backgroundColor = global
|
||||
? PdfBackgroundColor[userSettings.newPdfReaderSetting.backgroundColor as keyof typeof PdfBackgroundColor] || PdfBackgroundColor.GRAY
|
||||
: PdfBackgroundColor[bookSettings.newPdfSettings?.backgroundColor as keyof typeof PdfBackgroundColor] || PdfBackgroundColor[userSettings.newPdfReaderSetting.backgroundColor as keyof typeof PdfBackgroundColor] || PdfBackgroundColor.GRAY;
|
||||
|
||||
this.currentPage = (book.pdfProgress?.page || 1) - 1;
|
||||
}
|
||||
this.alignCurrentPageToParity();
|
||||
this.updateServiceStates();
|
||||
this.updateBookmarkState();
|
||||
@@ -610,7 +581,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
get isTwoPageView(): boolean {
|
||||
return this.pageViewMode === this.CbxPageViewMode.TWO_PAGE || this.pageViewMode === PdfPageViewMode.TWO_PAGE;
|
||||
return this.pageViewMode === this.CbxPageViewMode.TWO_PAGE;
|
||||
}
|
||||
|
||||
get hasSeries(): boolean {
|
||||
@@ -688,7 +659,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
private alignCurrentPageToParity() {
|
||||
if (!this.pages.length || !this.isTwoPageView) return;
|
||||
|
||||
const desiredOdd = this.pageSpread === CbxPageSpread.ODD || this.pageSpread === PdfPageSpread.ODD;
|
||||
const desiredOdd = this.pageSpread === CbxPageSpread.ODD;
|
||||
for (let i = this.currentPage; i >= 0; i--) {
|
||||
if ((this.pages[i] % 2 === 1) === desiredOdd) {
|
||||
this.currentPage = i;
|
||||
@@ -813,9 +784,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
private getPageImageUrl(pageIndex: number): string {
|
||||
return this.bookType === CbxReaderComponent.TYPE_PDF
|
||||
? this.pdfReaderService.getPageImageUrl(this.bookId, this.pages[pageIndex], this.altBookType)
|
||||
: this.cbxReaderService.getPageImageUrl(this.bookId, this.pages[pageIndex], this.altBookType);
|
||||
return this.cbxReaderService.getPageImageUrl(this.bookId, this.pages[pageIndex], this.altBookType);
|
||||
}
|
||||
|
||||
get infiniteScrollImageUrls(): string[] {
|
||||
@@ -823,25 +792,15 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
private updateViewerSetting(): void {
|
||||
const bookSetting: BookSetting = this.bookType === CbxReaderComponent.TYPE_CBX
|
||||
? {
|
||||
cbxSettings: {
|
||||
pageSpread: this.pageSpread as CbxPageSpread,
|
||||
pageViewMode: this.pageViewMode as CbxPageViewMode,
|
||||
fitMode: this.fitMode as CbxFitMode,
|
||||
scrollMode: this.scrollMode as CbxScrollMode,
|
||||
backgroundColor: this.backgroundColor as CbxBackgroundColor,
|
||||
}
|
||||
const bookSetting: BookSetting = {
|
||||
cbxSettings: {
|
||||
pageSpread: this.pageSpread,
|
||||
pageViewMode: this.pageViewMode,
|
||||
fitMode: this.fitMode,
|
||||
scrollMode: this.scrollMode,
|
||||
backgroundColor: this.backgroundColor,
|
||||
}
|
||||
: {
|
||||
newPdfSettings: {
|
||||
pageSpread: this.pageSpread as PdfPageSpread,
|
||||
pageViewMode: this.pageViewMode as PdfPageViewMode,
|
||||
fitMode: this.fitMode as PdfFitMode,
|
||||
scrollMode: this.scrollMode as PdfScrollMode,
|
||||
backgroundColor: this.backgroundColor as PdfBackgroundColor,
|
||||
}
|
||||
};
|
||||
};
|
||||
this.bookService.updateViewerSetting(bookSetting, this.bookId).subscribe();
|
||||
}
|
||||
|
||||
@@ -850,12 +809,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
? Math.round(((this.currentPage + 1) / this.pages.length) * 1000) / 10
|
||||
: 0;
|
||||
|
||||
if (this.bookType === CbxReaderComponent.TYPE_CBX) {
|
||||
this.bookService.saveCbxProgress(this.bookId, this.currentPage + 1, percentage, this.bookFileId).subscribe();
|
||||
}
|
||||
if (this.bookType === CbxReaderComponent.TYPE_PDF) {
|
||||
this.bookService.savePdfProgress(this.bookId, this.currentPage + 1, percentage, this.bookFileId).subscribe();
|
||||
}
|
||||
this.bookService.saveCbxProgress(this.bookId, this.currentPage + 1, percentage, this.bookFileId).subscribe();
|
||||
}
|
||||
|
||||
private updateSessionProgress(): void {
|
||||
@@ -1049,7 +1003,7 @@ export class CbxReaderComponent implements OnInit, OnDestroy {
|
||||
|
||||
private enforcePortraitSinglePageView() {
|
||||
if (this.isPhonePortrait() && this.isTwoPageView) {
|
||||
this.pageViewMode = this.bookType === CbxReaderComponent.TYPE_CBX ? CbxPageViewMode.SINGLE_PAGE : PdfPageViewMode.SINGLE_PAGE;
|
||||
this.pageViewMode = CbxPageViewMode.SINGLE_PAGE;
|
||||
this.quickSettingsService.setPageViewMode(this.pageViewMode);
|
||||
this.footerService.setTwoPageView(false);
|
||||
this.updateViewerSetting();
|
||||
|
||||
+2
-4
@@ -9,9 +9,7 @@ import {
|
||||
CbxPageSpread,
|
||||
CbxBackgroundColor,
|
||||
CbxReadingDirection,
|
||||
CbxSlideshowInterval,
|
||||
PdfPageViewMode,
|
||||
PdfPageSpread
|
||||
CbxSlideshowInterval
|
||||
} from '../../../../settings/user-management/user.service';
|
||||
import {ReaderIconComponent, ReaderIconName} from '../../../ebook-reader/shared/icon.component';
|
||||
import {CbxQuickSettingsService, CbxQuickSettingsState} from './cbx-quick-settings.service';
|
||||
@@ -85,7 +83,7 @@ export class CbxQuickSettingsComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
|
||||
get isTwoPageView(): boolean {
|
||||
return this.state.pageViewMode === CbxPageViewMode.TWO_PAGE || this.state.pageViewMode === PdfPageViewMode.TWO_PAGE;
|
||||
return this.state.pageViewMode === CbxPageViewMode.TWO_PAGE;
|
||||
}
|
||||
|
||||
get isPaginated(): boolean {
|
||||
|
||||
+8
-8
@@ -1,13 +1,13 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Subject} from 'rxjs';
|
||||
import {CbxBackgroundColor, CbxFitMode, CbxPageSpread, CbxPageViewMode, CbxScrollMode, CbxReadingDirection, CbxSlideshowInterval, PdfBackgroundColor, PdfFitMode, PdfPageSpread, PdfPageViewMode, PdfScrollMode} from '../../../../settings/user-management/user.service';
|
||||
import {CbxBackgroundColor, CbxFitMode, CbxPageSpread, CbxPageViewMode, CbxScrollMode, CbxReadingDirection, CbxSlideshowInterval} from '../../../../settings/user-management/user.service';
|
||||
|
||||
export interface CbxQuickSettingsState {
|
||||
fitMode: CbxFitMode | PdfFitMode;
|
||||
scrollMode: CbxScrollMode | PdfScrollMode;
|
||||
pageViewMode: CbxPageViewMode | PdfPageViewMode;
|
||||
pageSpread: CbxPageSpread | PdfPageSpread;
|
||||
backgroundColor: CbxBackgroundColor | PdfBackgroundColor;
|
||||
fitMode: CbxFitMode;
|
||||
scrollMode: CbxScrollMode;
|
||||
pageViewMode: CbxPageViewMode;
|
||||
pageSpread: CbxPageSpread;
|
||||
backgroundColor: CbxBackgroundColor;
|
||||
readingDirection: CbxReadingDirection;
|
||||
slideshowInterval: CbxSlideshowInterval;
|
||||
}
|
||||
@@ -77,11 +77,11 @@ export class CbxQuickSettingsService {
|
||||
this.updateState({scrollMode: mode});
|
||||
}
|
||||
|
||||
setPageViewMode(mode: CbxPageViewMode | PdfPageViewMode): void {
|
||||
setPageViewMode(mode: CbxPageViewMode): void {
|
||||
this.updateState({pageViewMode: mode});
|
||||
}
|
||||
|
||||
setPageSpread(spread: CbxPageSpread | PdfPageSpread): void {
|
||||
setPageSpread(spread: CbxPageSpread): void {
|
||||
this.updateState({pageSpread: spread});
|
||||
}
|
||||
|
||||
|
||||
+17
-52
@@ -50,59 +50,24 @@
|
||||
<div class="content">
|
||||
@switch (activeTab) {
|
||||
@case ('pages') {
|
||||
@if (isPdf && hasOutline) {
|
||||
<ul class="outline-list">
|
||||
<ng-container *ngTemplateOutlet="outlineTemplate; context: { items: pdfOutline, depth: 0 }"></ng-container>
|
||||
</ul>
|
||||
|
||||
<ng-template #outlineTemplate let-items="items" let-depth="depth">
|
||||
@for (item of items; track item.title + item.pageNumber) {
|
||||
<li class="outline-item">
|
||||
<button
|
||||
class="outline-row"
|
||||
[class.active]="isOutlineItemActive(item, items)"
|
||||
[class.has-children]="item.children && item.children.length > 0"
|
||||
[style.padding-left.px]="16 + depth * 16"
|
||||
(click)="onOutlineItemClick(item)">
|
||||
@if (item.children && item.children.length > 0) {
|
||||
<span
|
||||
class="expand-icon"
|
||||
[class.expanded]="isOutlineItemExpanded(item)"
|
||||
(click)="toggleOutlineItem(item); $event.stopPropagation()">
|
||||
<app-reader-icon name="chevron-right" [size]="14"></app-reader-icon>
|
||||
</span>
|
||||
}
|
||||
<span class="outline-title">{{ item.title }}</span>
|
||||
<span class="outline-page">{{ item.pageNumber }}</span>
|
||||
</button>
|
||||
@if (item.children && item.children.length > 0 && isOutlineItemExpanded(item)) {
|
||||
<ul class="outline-children">
|
||||
<ng-container *ngTemplateOutlet="outlineTemplate; context: { items: item.children, depth: depth + 1 }"></ng-container>
|
||||
</ul>
|
||||
}
|
||||
</li>
|
||||
}
|
||||
</ng-template>
|
||||
} @else {
|
||||
<ul class="page-list">
|
||||
@for (page of pages; track page.pageNumber) {
|
||||
<li>
|
||||
<button
|
||||
class="page-row"
|
||||
[class.active]="isPageActive(page.pageNumber)"
|
||||
(click)="onPageClick(page.pageNumber)">
|
||||
<span class="page-number">{{ page.pageNumber }}</span>
|
||||
<span class="page-label">{{ page.displayName }}</span>
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
@if (pages.length === 0 && !hasOutline) {
|
||||
<div class="empty-state">
|
||||
<app-reader-icon name="book" class="empty-icon" [size]="48" [strokeWidth]="1.5"></app-reader-icon>
|
||||
<p>No pages found</p>
|
||||
</div>
|
||||
<ul class="page-list">
|
||||
@for (page of pages; track page.pageNumber) {
|
||||
<li>
|
||||
<button
|
||||
class="page-row"
|
||||
[class.active]="isPageActive(page.pageNumber)"
|
||||
(click)="onPageClick(page.pageNumber)">
|
||||
<span class="page-number">{{ page.pageNumber }}</span>
|
||||
<span class="page-label">{{ page.displayName }}</span>
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
@if (pages.length === 0) {
|
||||
<div class="empty-state">
|
||||
<app-reader-icon name="book" class="empty-icon" [size]="48" [strokeWidth]="1.5"></app-reader-icon>
|
||||
<p>No pages found</p>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@case ('bookmarks') {
|
||||
|
||||
+1
-88
@@ -5,7 +5,6 @@ import {Subject} from 'rxjs';
|
||||
import {takeUntil} from 'rxjs/operators';
|
||||
import {CbxSidebarService, CbxSidebarTab, SidebarBookInfo} from './cbx-sidebar.service';
|
||||
import {CbxPageInfo} from '../../../../book/service/cbx-reader.service';
|
||||
import {PdfOutlineItem} from '../../../../book/service/new-pdf-reader.service';
|
||||
import {BookMark} from '../../../../../shared/service/book-mark.service';
|
||||
import {BookNoteV2} from '../../../../../shared/service/book-note-v2.service';
|
||||
import {ReaderIconComponent} from '../../../ebook-reader';
|
||||
@@ -26,9 +25,6 @@ export class CbxSidebarComponent implements OnInit, OnDestroy {
|
||||
activeTab: CbxSidebarTab = 'pages';
|
||||
bookInfo: SidebarBookInfo = { id: null, title: '', authors: '', coverUrl: null };
|
||||
pages: CbxPageInfo[] = [];
|
||||
pdfOutline: PdfOutlineItem[] | null = null;
|
||||
pdfPageCount = 0;
|
||||
expandedOutlineItems = new Set<string>();
|
||||
currentPage = 1;
|
||||
bookmarks: BookMark[] = [];
|
||||
notes: BookNoteV2[] = [];
|
||||
@@ -58,23 +54,9 @@ export class CbxSidebarComponent implements OnInit, OnDestroy {
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(pages => this.pages = pages);
|
||||
|
||||
this.sidebarService.pdfOutline$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(outline => {
|
||||
this.pdfOutline = outline;
|
||||
this.autoExpandToCurrentPage();
|
||||
});
|
||||
|
||||
this.sidebarService.pdfPageCount$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(count => this.pdfPageCount = count);
|
||||
|
||||
this.sidebarService.currentPage$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(page => {
|
||||
this.currentPage = page;
|
||||
this.autoExpandToCurrentPage();
|
||||
});
|
||||
.subscribe(page => this.currentPage = page);
|
||||
|
||||
this.sidebarService.bookmarks$
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
@@ -114,75 +96,6 @@ export class CbxSidebarComponent implements OnInit, OnDestroy {
|
||||
return this.currentPage === pageNumber;
|
||||
}
|
||||
|
||||
get isPdf(): boolean {
|
||||
return this.sidebarService.isPdf;
|
||||
}
|
||||
|
||||
get hasOutline(): boolean {
|
||||
return this.pdfOutline !== null && this.pdfOutline.length > 0;
|
||||
}
|
||||
|
||||
toggleOutlineItem(item: PdfOutlineItem): void {
|
||||
const key = `${item.title}-${item.pageNumber}`;
|
||||
if (this.expandedOutlineItems.has(key)) {
|
||||
this.expandedOutlineItems.delete(key);
|
||||
} else {
|
||||
this.expandedOutlineItems.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
isOutlineItemExpanded(item: PdfOutlineItem): boolean {
|
||||
const key = `${item.title}-${item.pageNumber}`;
|
||||
return this.expandedOutlineItems.has(key);
|
||||
}
|
||||
|
||||
onOutlineItemClick(item: PdfOutlineItem): void {
|
||||
this.sidebarService.navigateToPage(item.pageNumber);
|
||||
}
|
||||
|
||||
isOutlineItemActive(item: PdfOutlineItem, siblings: PdfOutlineItem[] | null): boolean {
|
||||
if (!siblings) return this.currentPage >= item.pageNumber;
|
||||
|
||||
const itemIndex = siblings.findIndex(s => s.title === item.title && s.pageNumber === item.pageNumber);
|
||||
const nextSibling = siblings[itemIndex + 1];
|
||||
|
||||
if (nextSibling) {
|
||||
return this.currentPage >= item.pageNumber && this.currentPage < nextSibling.pageNumber;
|
||||
} else {
|
||||
return this.currentPage >= item.pageNumber;
|
||||
}
|
||||
}
|
||||
|
||||
hasActiveChild(item: PdfOutlineItem): boolean {
|
||||
if (!item.children || item.children.length === 0) return false;
|
||||
|
||||
return item.children.some((child, index) => {
|
||||
if (this.isOutlineItemActive(child, item.children)) return true;
|
||||
if (this.hasActiveChild(child)) return true;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private autoExpandToCurrentPage(): void {
|
||||
if (!this.pdfOutline) return;
|
||||
|
||||
const expandParents = (items: PdfOutlineItem[] | null): void => {
|
||||
if (!items) return;
|
||||
|
||||
for (const item of items) {
|
||||
if (item.children && item.children.length > 0) {
|
||||
if (this.hasActiveChild(item) || this.isOutlineItemActive(item, items)) {
|
||||
const key = `${item.title}-${item.pageNumber}`;
|
||||
this.expandedOutlineItems.add(key);
|
||||
}
|
||||
expandParents(item.children);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
expandParents(this.pdfOutline);
|
||||
}
|
||||
|
||||
onBookmarkClick(cfi: string): void {
|
||||
this.sidebarService.navigateToBookmark(cfi);
|
||||
}
|
||||
|
||||
+4
-32
@@ -2,9 +2,8 @@ import {inject, Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Subject} from 'rxjs';
|
||||
import {takeUntil} from 'rxjs/operators';
|
||||
import {CbxPageInfo, CbxReaderService} from '../../../../book/service/cbx-reader.service';
|
||||
import {NewPdfReaderService, PdfOutlineItem} from '../../../../book/service/new-pdf-reader.service';
|
||||
import {UrlHelperService} from '../../../../../shared/service/url-helper.service';
|
||||
import {Book, BookType} from '../../../../book/model/book.model';
|
||||
import {Book} from '../../../../book/model/book.model';
|
||||
import {BookMark, BookMarkService, CreateBookMarkRequest} from '../../../../../shared/service/book-mark.service';
|
||||
import {BookNoteV2, BookNoteV2Service, CreateBookNoteV2Request, UpdateBookNoteV2Request} from '../../../../../shared/service/book-note-v2.service';
|
||||
|
||||
@@ -21,14 +20,12 @@ export type CbxSidebarTab = 'pages' | 'bookmarks' | 'notes';
|
||||
export class CbxSidebarService {
|
||||
private urlHelper = inject(UrlHelperService);
|
||||
private cbxReaderService = inject(CbxReaderService);
|
||||
private pdfReaderService = inject(NewPdfReaderService);
|
||||
private bookMarkService = inject(BookMarkService);
|
||||
private bookNoteV2Service = inject(BookNoteV2Service);
|
||||
|
||||
private destroy$ = new Subject<void>();
|
||||
private bookId!: number;
|
||||
private altBookType?: string;
|
||||
private bookType!: BookType;
|
||||
|
||||
private _isOpen = new BehaviorSubject<boolean>(false);
|
||||
private _activeTab = new BehaviorSubject<CbxSidebarTab>('pages');
|
||||
@@ -39,8 +36,6 @@ export class CbxSidebarService {
|
||||
coverUrl: null
|
||||
});
|
||||
private _pages = new BehaviorSubject<CbxPageInfo[]>([]);
|
||||
private _pdfOutline = new BehaviorSubject<PdfOutlineItem[] | null>(null);
|
||||
private _pdfPageCount = new BehaviorSubject<number>(0);
|
||||
private _currentPage = new BehaviorSubject<number>(1);
|
||||
private _bookmarks = new BehaviorSubject<BookMark[]>([]);
|
||||
private _notes = new BehaviorSubject<BookNoteV2[]>([]);
|
||||
@@ -54,8 +49,6 @@ export class CbxSidebarService {
|
||||
activeTab$ = this._activeTab.asObservable();
|
||||
bookInfo$ = this._bookInfo.asObservable();
|
||||
pages$ = this._pages.asObservable();
|
||||
pdfOutline$ = this._pdfOutline.asObservable();
|
||||
pdfPageCount$ = this._pdfPageCount.asObservable();
|
||||
currentPage$ = this._currentPage.asObservable();
|
||||
bookmarks$ = this._bookmarks.asObservable();
|
||||
notes$ = this._notes.asObservable();
|
||||
@@ -63,14 +56,9 @@ export class CbxSidebarService {
|
||||
editNote$ = this._editNote.asObservable();
|
||||
bookmarksChanged$ = this._bookmarksChanged.asObservable();
|
||||
|
||||
get isPdf(): boolean {
|
||||
return this.bookType === 'PDF';
|
||||
}
|
||||
|
||||
initialize(bookId: number, book: Book, destroy$: Subject<void>, altBookType?: string): void {
|
||||
this.bookId = bookId;
|
||||
this.altBookType = altBookType;
|
||||
this.bookType = (altBookType as BookType) ?? book.primaryFile?.bookType!;
|
||||
this.destroy$ = destroy$;
|
||||
|
||||
this._bookInfo.next({
|
||||
@@ -86,25 +74,9 @@ export class CbxSidebarService {
|
||||
}
|
||||
|
||||
private loadPageInfo(): void {
|
||||
if (this.bookType === 'PDF') {
|
||||
this.pdfReaderService.getPageInfo(this.bookId, this.altBookType)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(pdfInfo => {
|
||||
this._pdfPageCount.next(pdfInfo.pageCount);
|
||||
this._pdfOutline.next(pdfInfo.outline);
|
||||
if (!pdfInfo.outline || pdfInfo.outline.length === 0) {
|
||||
const pages: CbxPageInfo[] = [];
|
||||
for (let i = 1; i <= pdfInfo.pageCount; i++) {
|
||||
pages.push({pageNumber: i, displayName: `Page ${i}`});
|
||||
}
|
||||
this._pages.next(pages);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.cbxReaderService.getPageInfo(this.bookId, this.altBookType)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(pages => this._pages.next(pages));
|
||||
}
|
||||
this.cbxReaderService.getPageInfo(this.bookId, this.altBookType)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(pages => this._pages.next(pages));
|
||||
}
|
||||
|
||||
private loadBookmarks(): void {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
@if (!isLoading) {
|
||||
<ngx-extended-pdf-viewer
|
||||
[src]="bookData"
|
||||
[authorization]="authorization"
|
||||
[textLayer]="true"
|
||||
[showHandToolButton]="true"
|
||||
[height]="'auto'"
|
||||
@@ -19,7 +20,8 @@
|
||||
(pagesLoaded)="onPdfPagesLoaded($event)"
|
||||
(pageChange)="onPageChange($event)"
|
||||
(zoomChange)="onZoomChange($event)"
|
||||
(spreadChange)="onSpreadChange($event)">
|
||||
(spreadChange)="onSpreadChange($event)"
|
||||
(annotationEditorEvent)="onAnnotationEditorEvent()">
|
||||
</ngx-extended-pdf-viewer>
|
||||
|
||||
<ng-template #additionalButtons>
|
||||
@@ -47,6 +49,10 @@
|
||||
<div class="verticalToolbarSeparator hiddenSmallView"></div>
|
||||
<pdf-toggle-secondary-toolbar></pdf-toggle-secondary-toolbar>
|
||||
<div class="verticalToolbarSeparator"></div>
|
||||
<pdf-highlight-editor></pdf-highlight-editor>
|
||||
<pdf-text-editor></pdf-text-editor>
|
||||
<pdf-draw-editor></pdf-draw-editor>
|
||||
<div class="verticalToolbarSeparator"></div>
|
||||
<button class="toolbar-btn" [title]="isDarkTheme ? 'Switch to Light Mode' : 'Switch to Dark Mode'" (click)="isDarkTheme = !isDarkTheme">
|
||||
@if (isDarkTheme) {
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {ActivatedRoute} from '@angular/router';
|
||||
import {NgxExtendedPdfViewerModule, ZoomType} from 'ngx-extended-pdf-viewer';
|
||||
import {NgxExtendedPdfViewerModule, NgxExtendedPdfViewerService, pdfDefaultOptions, ZoomType} from 'ngx-extended-pdf-viewer';
|
||||
import {PageTitleService} from "../../../shared/service/page-title.service";
|
||||
import {BookService} from '../../book/service/book.service';
|
||||
import {forkJoin, Subscription} from 'rxjs';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {forkJoin, Subject, Subscription} from 'rxjs';
|
||||
import {debounceTime, map, switchMap} from 'rxjs/operators';
|
||||
import {BookSetting} from '../../book/model/book.model';
|
||||
import {UserService} from '../../settings/user-management/user.service';
|
||||
import {AuthService} from '../../../shared/service/auth.service';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {PdfAnnotationService} from '../../../shared/service/pdf-annotation.service';
|
||||
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {MessageService} from 'primeng/api';
|
||||
@@ -21,47 +24,61 @@ import {Location} from '@angular/common';
|
||||
styleUrl: './pdf-reader.component.scss',
|
||||
})
|
||||
export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
constructor() {
|
||||
pdfDefaultOptions.rangeChunkSize = 512 * 1024;
|
||||
pdfDefaultOptions.disableAutoFetch = true;
|
||||
}
|
||||
|
||||
isLoading = true;
|
||||
totalPages: number = 0;
|
||||
isDarkTheme = true;
|
||||
|
||||
rotation: 0 | 90 | 180 | 270 = 0;
|
||||
authorization = '';
|
||||
|
||||
page!: number;
|
||||
spread!: 'off' | 'even' | 'odd';
|
||||
zoom!: ZoomType;
|
||||
|
||||
bookData!: string | Blob;
|
||||
bookData!: string;
|
||||
bookId!: number;
|
||||
bookFileId?: number;
|
||||
private appSettingsSubscription!: Subscription;
|
||||
private annotationSaveSubject = new Subject<void>();
|
||||
private annotationSaveSubscription!: Subscription;
|
||||
private annotationsLoaded = false;
|
||||
|
||||
private bookService = inject(BookService);
|
||||
private userService = inject(UserService);
|
||||
private authService = inject(AuthService);
|
||||
private messageService = inject(MessageService);
|
||||
private route = inject(ActivatedRoute);
|
||||
private pageTitle = inject(PageTitleService);
|
||||
private readingSessionService = inject(ReadingSessionService);
|
||||
private location = inject(Location);
|
||||
private pdfViewerService = inject(NgxExtendedPdfViewerService);
|
||||
private pdfAnnotationService = inject(PdfAnnotationService);
|
||||
|
||||
ngOnInit(): void {
|
||||
this.annotationSaveSubscription = this.annotationSaveSubject
|
||||
.pipe(debounceTime(1500))
|
||||
.subscribe(() => this.persistAnnotations());
|
||||
|
||||
this.route.paramMap.subscribe((params) => {
|
||||
this.isLoading = true;
|
||||
this.bookId = +params.get('bookId')!;
|
||||
|
||||
this.bookService.getBookByIdFromAPI(this.bookId, false).pipe(
|
||||
switchMap((book) => {
|
||||
// Set the book file ID for progress tracking
|
||||
this.bookFileId = book.primaryFile?.id;
|
||||
|
||||
return forkJoin([
|
||||
this.bookService.getBookSetting(this.bookId, this.bookFileId!),
|
||||
this.bookService.getFileContent(this.bookId),
|
||||
this.userService.getMyself()
|
||||
]).pipe(map(([bookSetting, pdfData, myself]) => ({ book, bookSetting, pdfData, myself })));
|
||||
]).pipe(map(([bookSetting, myself]) => ({book, bookSetting, myself})));
|
||||
})
|
||||
).subscribe({
|
||||
next: ({ book, bookSetting, pdfData, myself }) => {
|
||||
next: ({book, bookSetting, myself}) => {
|
||||
const pdfMeta = book;
|
||||
const pdfPrefs = bookSetting;
|
||||
|
||||
@@ -76,7 +93,9 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
this.spread = pdfPrefs.pdfSettings?.spread || myself.userSettings.pdfReaderSetting.pageSpread || 'odd';
|
||||
}
|
||||
this.page = pdfMeta.pdfProgress?.page || 1;
|
||||
this.bookData = pdfData;
|
||||
this.bookData = `${API_CONFIG.BASE_URL}/api/v1/books/${this.bookId}/content`;
|
||||
const token = this.authService.getOidcAccessToken() || this.authService.getInternalAccessToken();
|
||||
this.authorization = token ? `Bearer ${token}` : '';
|
||||
this.isLoading = false;
|
||||
},
|
||||
error: () => {
|
||||
@@ -130,6 +149,13 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
|
||||
this.readingSessionService.startSession(this.bookId, "PDF", this.page.toString(), percentage);
|
||||
this.readingSessionService.updateProgress(this.page.toString(), percentage);
|
||||
this.loadAnnotations();
|
||||
}
|
||||
|
||||
onAnnotationEditorEvent(): void {
|
||||
if (this.annotationsLoaded) {
|
||||
this.annotationSaveSubject.next();
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
@@ -138,6 +164,9 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
this.readingSessionService.endSession(this.page.toString(), percentage);
|
||||
}
|
||||
|
||||
this.annotationSaveSubscription?.unsubscribe();
|
||||
this.persistAnnotations();
|
||||
|
||||
if (this.appSettingsSubscription) {
|
||||
this.appSettingsSubscription.unsubscribe();
|
||||
}
|
||||
@@ -151,4 +180,33 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
}
|
||||
this.location.back();
|
||||
}
|
||||
|
||||
private loadAnnotations(): void {
|
||||
this.pdfAnnotationService.getAnnotations(this.bookId).subscribe({
|
||||
next: (response) => {
|
||||
if (response?.data) {
|
||||
const annotations = JSON.parse(response.data);
|
||||
for (const annotation of annotations) {
|
||||
this.pdfViewerService.addEditorAnnotation(annotation);
|
||||
}
|
||||
}
|
||||
this.annotationsLoaded = true;
|
||||
},
|
||||
error: () => {
|
||||
this.annotationsLoaded = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private persistAnnotations(): void {
|
||||
if (!this.annotationsLoaded || !this.bookId) {
|
||||
return;
|
||||
}
|
||||
const serialized = this.pdfViewerService.getSerializedAnnotations();
|
||||
if (serialized && serialized.length > 0) {
|
||||
const cleaned = serialized.map(({id, ...rest}: any) => rest);
|
||||
const data = JSON.stringify(cleaned);
|
||||
this.pdfAnnotationService.saveAnnotations(this.bookId, data).subscribe();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user