Fix PDF setting not applying bug

This commit is contained in:
aditya.chandel
2025-03-07 23:18:34 -07:00
parent 39616dabd7
commit b0ae188310
19 changed files with 157 additions and 113 deletions
@@ -11,6 +11,5 @@
(pageChange)="onPageChange($event)"
(zoomChange)="onZoomChange($event)"
(sidebarVisibleChange)="onSidebarVisibleChange($event)"
(pdfLoaded)="onPdfLoaded($event)"
(spreadChange)="onSpreadChange($event)">
</ngx-extended-pdf-viewer>
@@ -6,6 +6,7 @@ import {AppSettingsService} from '../../../core/service/app-settings.service';
import {filter, forkJoin, Subscription, take} from 'rxjs';
import {BookSetting, PdfViewerSetting} from '../../model/book.model';
import {PdfSettings} from '../../../core/model/app-settings.model';
import {UserService} from '../../../user.service';
@Component({
selector: 'app-pdf-viewer',
@@ -23,48 +24,46 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
spread!: 'off' | 'even' | 'odd';
zoom!: number | string;
private pdfLoaded = false;
bookData!: string | Blob;
bookId!: number;
private appSettingsSubscription!: Subscription;
private bookService = inject(BookService);
private appSettingsService = inject(AppSettingsService);
private userService = inject(UserService);
private route = inject(ActivatedRoute);
private individualPdfSettings: PdfViewerSetting | undefined;
private globalPdfSettings: PdfSettings | undefined;
private pdfScope: 'global' | 'individual' | undefined;
ngOnInit(): void {
this.route.paramMap.subscribe((params) => {
this.bookId = +params.get('bookId')!;
this.appSettingsService.appSettings$
.pipe(
filter((appSettings) => appSettings !== null),
take(1)
)
.subscribe((appSettings) => {
const book$ = this.bookService.getBookByIdFromAPI(this.bookId, false);
const pdf$ = this.bookService.getFileContent(this.bookId);
const bookSetting$ = this.bookService.getBookSetting(this.bookId);
const myself$ = this.userService.getMyself();
const book$ = this.bookService.getBookByIdFromAPI(this.bookId, false);
const bookSetting$ = this.bookService.getBookSetting(this.bookId);
const pdfData$ = this.bookService.getFileContent(this.bookId);
forkJoin([book$, pdf$, bookSetting$]).subscribe((results) => {
const pdf = results[0];
const pdfData = results[1];
this.individualPdfSettings = results[2]?.pdfSettings;
this.globalPdfSettings = appSettings.pdf;
this.pdfScope = appSettings?.readerSettings?.pdfScope;
this.page = pdf.pdfProgress || 1;
this.bookData = pdfData;
});
});
forkJoin([book$, bookSetting$, pdfData$, myself$]).subscribe((results) => {
const pdfMeta = results[0];
const pdfPrefs = results[1];
const pdfData = results[2];
const myself = results[3];
let globalOrIndividual = myself.bookPreferences.perBookSetting.pdf;
if (globalOrIndividual === 'Global') {
this.zoom = myself.bookPreferences.pdfReaderSetting.pageZoom || 'page-fit';
this.sidebarVisible = myself.bookPreferences.pdfReaderSetting.showSidebar ?? true;
this.spread = myself.bookPreferences.pdfReaderSetting.pageSpread || 'odd';
} else {
this.zoom = pdfPrefs.pdfSettings?.zoom || myself.bookPreferences.pdfReaderSetting.pageZoom || 'page-fit';
this.sidebarVisible = pdfPrefs.pdfSettings?.sidebarVisible ?? myself.bookPreferences.pdfReaderSetting.showSidebar ?? true;
this.spread = pdfPrefs.pdfSettings?.spread || myself.bookPreferences.pdfReaderSetting.pageSpread || 'odd';
}
this.page = pdfMeta.pdfProgress || 1;
this.bookData = pdfData;
});
});
}
onPageChange(page: number): void {
if (!this.pdfLoaded) return;
if (page !== this.page) {
this.page = page;
}
@@ -72,7 +71,6 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
onZoomChange(zoom: string | number): void {
if (!this.pdfLoaded) return;
if (zoom !== this.zoom) {
this.zoom = zoom;
this.updateViewerSetting();
@@ -80,7 +78,6 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
onSidebarVisibleChange(visible: boolean): void {
if (!this.pdfLoaded) return;
if (visible !== this.sidebarVisible) {
this.sidebarVisible = visible;
this.updateViewerSetting();
@@ -88,7 +85,6 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
onSpreadChange(spread: 'off' | 'even' | 'odd'): void {
if (!this.pdfLoaded) return;
if (spread !== this.spread) {
this.spread = spread;
this.updateViewerSetting();
@@ -96,7 +92,6 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
private updateViewerSetting(): void {
if (!this.pdfLoaded) return;
const bookSetting: BookSetting = {
pdfSettings: {
sidebarVisible: this.sidebarVisible,
@@ -117,18 +112,4 @@ export class PdfViewerComponent implements OnInit, OnDestroy {
}
this.updateProgress();
}
onPdfLoaded($event: PdfLoadedEvent) {
this.pdfLoaded = true;
this.updateProgress();
if (this.pdfScope === 'global') {
this.zoom = this.globalPdfSettings?.zoom || 'page-fit';
this.sidebarVisible = this.globalPdfSettings?.sidebar ?? true;
this.spread = this.globalPdfSettings?.spread || 'odd';
} else {
this.zoom = this.individualPdfSettings?.zoom || this.globalPdfSettings?.zoom || 'page-fit';
this.sidebarVisible = this.individualPdfSettings?.sidebarVisible ?? this.globalPdfSettings?.sidebar ?? true;
this.spread = this.individualPdfSettings?.spread || this.globalPdfSettings?.spread || 'odd';
}
}
}
@@ -48,7 +48,7 @@ export class BookPreferences implements OnInit {
readonly individualOrGlobal = ['Global', 'Individual'];
selectedSpread!: string;
selectedSpread!: 'off' | 'even' | 'odd';
selectedZoom!: string;
showSidebar = false;
selectedTheme!: string;
@@ -15,8 +15,8 @@ export interface PdfSettings {
}
export interface ReaderSettings {
pdfScope: 'global' | 'individual';
epubScope: 'global' | 'individual';
pdfScope: 'Global' | 'Individual';
epubScope: 'Global' | 'Individual';
}
export interface AppSettings {
@@ -111,7 +111,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.rendition.themes.register(name, theme);
});
if (epubScope === 'global') {
if (epubScope === 'Global') {
this.selectedTheme = globalSetting?.theme || FALLBACK_EPUB_SETTINGS.theme;
this.selectedFontType = globalSetting?.font || FALLBACK_EPUB_SETTINGS.fontType;
this.fontSize = globalSetting?.fontSize || FALLBACK_EPUB_SETTINGS.fontSize;
+17 -2
View File
@@ -1,6 +1,6 @@
import {inject, Injectable, Injector} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, Observable} from 'rxjs';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {API_CONFIG} from './config/api-config';
import {jwtDecode} from 'jwt-decode';
import {RxStompService} from './shared/websocket/rx-stomp.service';
@@ -28,7 +28,7 @@ export interface UserBookPreferences {
epub: string;
};
pdfReaderSetting: {
pageSpread: string;
pageSpread: 'off' | 'even' | 'odd';
pageZoom: string;
showSidebar: boolean;
};
@@ -78,6 +78,21 @@ export class UserService {
});
}
getMyself(): Observable<User> {
return this.http.get<User>(`${this.userUrl}/me`);
}
getUserFromAPI(): Observable<User> {
const token = localStorage.getItem('token');
if (token) {
const decoded: JwtPayload & { userId?: string } = jwtDecode(token);
if (decoded.userId) {
return this.http.get<User>(`${this.userUrl}/${decoded.userId}`);
}
}
return of(null as unknown as User);
}
getLocalUser(): User | null {
return this.userDataSubject.getValue();
}