refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components (#2050)
* refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components - Update method signatures, variables, and interfaces to use 'unknown' instead of 'any' - Refine event and observable typings for better type inference - Add or adjust explicit types for function parameters and return values - Update Chart.js tooltip and context typings for chart services - Improve type safety in Book, BookState, and related models - Adjust template bindings and method calls to match stricter typings - Add SecureSrcDirective for secure image loading with fallback and error handling Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * chore(angular): remove proxy configuration from development build settings Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(book-browser): streamline entity and book state management with reactive streams - Refactor entity type and entity fetching logic to use observables and combine route params - Simplify book state handling by consolidating filtering and fetching into a single reactive pipeline - Remove redundant applySortOption logic and related subscriptions - Fix book card index binding in template for improved rendering performance - Ensure proper cleanup of epub rendition in epub-reader component Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(book-browser, book-uploader): improve permission checks and enhance file upload handling - Use optional chaining for user permission checks in book-browser.component.html to prevent runtime errors when user data is undefined - Refactor book-uploader.component.ts to pass destination and IDs explicitly to uploadBatch, reconstruct files for strict type handling, and ensure proper upload flow and UI feedback Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(charts, services): improve type safety for book state validation and update form field checks - Refactor isValidBookState methods across chart components and services to use type predicates and stricter type checks for BookState - Update metadata-picker.component to simplify array field checks for authors, categories, moods, and tags - Adjust magic-shelf-utils to remove unnecessary fallback in object spread - Update notification-event.service to use correct timeout type for highlightTimeout Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(theme): ensure safe merging of theme properties and improve type definition for themesMap Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * refactor(epub-reader): streamline book and rendition initialization logic Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+1
-5
@@ -1,11 +1,9 @@
|
||||
<div #epubContainer class="epub-viewer-container">
|
||||
<div class="epub-viewer-container">
|
||||
@if (isLoading) {
|
||||
<div class="spinner-wrapper">
|
||||
<p-progressSpinner></p-progressSpinner>
|
||||
</div>
|
||||
}
|
||||
@if (!isLoading) {
|
||||
<div>
|
||||
@if (isDrawerVisible) {
|
||||
<div class="drawer-backdrop" (click)="isDrawerVisible = false; startHeaderAutoHide()"></div>
|
||||
}
|
||||
@@ -302,8 +300,6 @@
|
||||
@if (selectedFlow !== 'scrolled') {
|
||||
<button class="epub-controls-right" [class.visible]="showControls" [class.dark-theme]="selectedTheme === 'black' || selectedTheme === 'grey'" (click)="nextPage(); onBookTouch()">→</button>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<app-bookmark-edit-dialog
|
||||
[(visible)]="showEditBookmarkDialog"
|
||||
|
||||
+90
-75
@@ -73,6 +73,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
showEditBookmarkDialog = false;
|
||||
|
||||
private book: any;
|
||||
private epubArrayBuffer: ArrayBuffer | null = null;
|
||||
private rendition: any;
|
||||
private keyListener: (e: KeyboardEvent) => void = () => {
|
||||
};
|
||||
@@ -146,11 +147,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
this.pageTitle.setBookPageTitle(epub);
|
||||
|
||||
fileReader.onload = () => {
|
||||
this.book = ePub(fileReader.result as ArrayBuffer);
|
||||
|
||||
this.book.loaded.navigation.then((nav: any) => {
|
||||
this.chapters = this.extractChapters(nav.toc, 0);
|
||||
});
|
||||
this.epubArrayBuffer = fileReader.result as ArrayBuffer;
|
||||
|
||||
const settingScope = myself.userSettings.perBookSetting.epub;
|
||||
const globalSettings = myself.userSettings.epubReaderSetting;
|
||||
@@ -171,48 +168,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
if (resolvedFlow != null) this.selectedFlow = resolvedFlow;
|
||||
if (resolvedSpread != null) this.selectedSpread = resolvedSpread;
|
||||
|
||||
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
|
||||
flow: this.selectedFlow ?? 'paginated',
|
||||
manager: this.selectedFlow === 'scrolled' ? 'continuous' : 'default',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
spread: this.selectedFlow === 'paginated' && !this.isMobileDevice() ? (this.selectedSpread === 'single' ? 'none' : this.selectedSpread) : 'none',
|
||||
allowScriptedContent: true,
|
||||
});
|
||||
|
||||
const baseTheme = EpubThemeUtil.themesMap.get(this.selectedTheme ?? 'black') || {};
|
||||
const combinedTheme = {
|
||||
...baseTheme,
|
||||
body: {
|
||||
...baseTheme.body,
|
||||
...(this.selectedFontType ? {'font-family': this.selectedFontType} : {}),
|
||||
...(this.lineHeight != null ? {'line-height': this.lineHeight} : {}),
|
||||
...(this.letterSpacing != null ? {'letter-spacing': `${this.letterSpacing}em`} : {}),
|
||||
},
|
||||
'*': {
|
||||
...baseTheme['*'],
|
||||
...(this.lineHeight != null ? {'line-height': this.lineHeight} : {}),
|
||||
...(this.letterSpacing != null ? {'letter-spacing': `${this.letterSpacing}em`} : {}),
|
||||
},
|
||||
};
|
||||
|
||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||
this.rendition.themes.register('custom', combinedTheme);
|
||||
this.rendition.themes.select('custom');
|
||||
|
||||
const displayPromise = this.epub?.epubProgress?.cfi
|
||||
? this.rendition.display(this.epub.epubProgress.cfi)
|
||||
: this.rendition.display();
|
||||
|
||||
displayPromise.then(() => {
|
||||
this.updateCurrentChapter(this.rendition.currentLocation());
|
||||
this.setupKeyListener();
|
||||
this.trackProgress();
|
||||
this.setupTouchListener();
|
||||
this.isLoading = false;
|
||||
this.startHeaderAutoHide();
|
||||
});
|
||||
|
||||
this.initBook();
|
||||
this.initRendition();
|
||||
};
|
||||
|
||||
fileReader.readAsArrayBuffer(epubData);
|
||||
@@ -269,21 +226,10 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
if (!this.rendition || !this.book) return;
|
||||
|
||||
const cfi = this.rendition.currentLocation()?.start?.cfi;
|
||||
this.rendition.destroy();
|
||||
const locations = this.book.locations.save();
|
||||
|
||||
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
|
||||
flow: this.selectedFlow,
|
||||
manager: this.selectedFlow === 'scrolled' ? 'continuous' : 'default',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
spread: this.selectedFlow === 'paginated' && !this.isMobileDevice() ? (this.selectedSpread === 'single' ? 'none' : this.selectedSpread) : 'none',
|
||||
allowScriptedContent: true,
|
||||
});
|
||||
|
||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||
this.applyCombinedTheme();
|
||||
this.setupKeyListener();
|
||||
this.rendition.display(cfi || undefined);
|
||||
this.initBook(locations);
|
||||
this.initRendition(cfi);
|
||||
this.updateViewerSetting();
|
||||
}
|
||||
|
||||
@@ -291,21 +237,10 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
if (!this.rendition || !this.book || this.selectedFlow === 'scrolled' || this.isMobileDevice()) return;
|
||||
|
||||
const cfi = this.rendition.currentLocation()?.start?.cfi;
|
||||
this.rendition.destroy();
|
||||
const locations = this.book.locations.save();
|
||||
|
||||
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
|
||||
flow: this.selectedFlow,
|
||||
manager: 'default',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
spread: this.selectedSpread === 'single' ? 'none' : this.selectedSpread,
|
||||
allowScriptedContent: true,
|
||||
});
|
||||
|
||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||
this.applyCombinedTheme();
|
||||
this.setupKeyListener();
|
||||
this.rendition.display(cfi || undefined);
|
||||
this.initBook(locations);
|
||||
this.initRendition(cfi);
|
||||
this.updateViewerSetting();
|
||||
}
|
||||
|
||||
@@ -645,6 +580,9 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
|
||||
this.book.ready.then(() => {
|
||||
if (this.locationsReady && this.book.locations.total > 0) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return this.book.locations.generate(1600);
|
||||
}).then(() => {
|
||||
this.locationsReady = true;
|
||||
@@ -665,6 +603,83 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
private initBook(restoreLocations?: string): void {
|
||||
if (!this.epubArrayBuffer) return;
|
||||
|
||||
if (this.book) {
|
||||
this.book.destroy();
|
||||
}
|
||||
|
||||
this.book = ePub(this.epubArrayBuffer);
|
||||
|
||||
this.book.loaded.navigation.then((nav: any) => {
|
||||
this.chapters = this.extractChapters(nav.toc, 0);
|
||||
});
|
||||
|
||||
if (restoreLocations) {
|
||||
this.book.ready.then(() => {
|
||||
this.book.locations.load(restoreLocations);
|
||||
this.locationsReady = true;
|
||||
});
|
||||
} else {
|
||||
this.locationsReady = false;
|
||||
}
|
||||
}
|
||||
|
||||
private initRendition(targetCfi?: string): void {
|
||||
if (!this.book) return;
|
||||
|
||||
if (this.rendition) {
|
||||
try {
|
||||
this.rendition.destroy();
|
||||
} catch (e) {
|
||||
console.warn('Error destroying rendition', e);
|
||||
}
|
||||
}
|
||||
|
||||
this.rendition = this.book.renderTo(this.epubContainer.nativeElement, {
|
||||
flow: this.selectedFlow ?? 'paginated',
|
||||
manager: this.selectedFlow === 'scrolled' ? 'continuous' : 'default',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
spread: this.selectedFlow === 'paginated' && !this.isMobileDevice() ? (this.selectedSpread === 'single' ? 'none' : this.selectedSpread) : 'none',
|
||||
allowScriptedContent: true,
|
||||
});
|
||||
|
||||
const baseTheme = EpubThemeUtil.themesMap.get(this.selectedTheme ?? 'black') || {};
|
||||
const combinedTheme = {
|
||||
...baseTheme,
|
||||
body: {
|
||||
...(baseTheme.body || {}),
|
||||
...(this.selectedFontType ? {'font-family': this.selectedFontType} : {}),
|
||||
...(this.lineHeight != null ? {'line-height': this.lineHeight} : {}),
|
||||
...(this.letterSpacing != null ? {'letter-spacing': `${this.letterSpacing}em`} : {}),
|
||||
},
|
||||
'*': {
|
||||
...(baseTheme['*'] || {}),
|
||||
...(this.lineHeight != null ? {'line-height': this.lineHeight} : {}),
|
||||
...(this.letterSpacing != null ? {'letter-spacing': `${this.letterSpacing}em`} : {}),
|
||||
},
|
||||
};
|
||||
|
||||
this.rendition.themes.override('font-size', `${this.fontSize}%`);
|
||||
this.rendition.themes.register('custom', combinedTheme);
|
||||
this.rendition.themes.select('custom');
|
||||
|
||||
const displayPromise = targetCfi
|
||||
? this.rendition.display(targetCfi)
|
||||
: (this.epub?.epubProgress?.cfi ? this.rendition.display(this.epub.epubProgress.cfi) : this.rendition.display());
|
||||
|
||||
displayPromise.then(() => {
|
||||
this.updateCurrentChapter(this.rendition.currentLocation());
|
||||
this.setupKeyListener();
|
||||
this.trackProgress();
|
||||
this.setupTouchListener();
|
||||
this.isLoading = false;
|
||||
this.startHeaderAutoHide();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (this.readingSessionService.isSessionActive()) {
|
||||
this.readingSessionService.endSession(
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import {EpubCFI} from 'epubjs';
|
||||
import { Book, EpubCFI, Location, TocItem } from 'epubjs';
|
||||
|
||||
export const FALLBACK_EPUB_SETTINGS = {
|
||||
maxFontSize: 300,
|
||||
minFontSize: 50
|
||||
};
|
||||
|
||||
export function flatten(chapters: any) {
|
||||
return [].concat.apply([], chapters.map((chapter: any) => [].concat.apply([chapter], flatten(chapter.subitems))));
|
||||
export function flatten(chapters: TocItem[]): TocItem[] {
|
||||
if (!chapters || !Array.isArray(chapters)) return [];
|
||||
return chapters.flatMap((chapter: TocItem) => [chapter, ...flatten(chapter.subitems || [])]);
|
||||
}
|
||||
|
||||
export function getCfiFromHref(book: any, href: string): string | null {
|
||||
export function getCfiFromHref(book: Book, href: string): string | null {
|
||||
const [_, id] = href.split('#');
|
||||
const section = book.spine.get(href);
|
||||
|
||||
@@ -27,18 +28,20 @@ export function getCfiFromHref(book: any, href: string): string | null {
|
||||
return section.cfiFromElement(el);
|
||||
}
|
||||
|
||||
export function getChapter(book: any, location: any) {
|
||||
export function getChapter(book: Book, location: Location): TocItem | null {
|
||||
const locationHref = location.start.href;
|
||||
return flatten(book.navigation.toc)
|
||||
.filter((chapter: any) => {
|
||||
return book.canonical(chapter.href).includes(book.canonical(locationHref));
|
||||
return flatten(book.navigation.toc || [])
|
||||
.filter((chapter: TocItem) => {
|
||||
return chapter.href && book.canonical && typeof book.canonical === 'function' &&
|
||||
book.canonical(chapter.href).includes(book.canonical(locationHref));
|
||||
})
|
||||
.reduce((result: any | null, chapter: any) => {
|
||||
.reduce((result: TocItem | null, chapter: TocItem) => {
|
||||
if (!chapter.href) return result;
|
||||
const chapterCfi = getCfiFromHref(book, chapter.href);
|
||||
if (!chapterCfi) {
|
||||
return result;
|
||||
}
|
||||
const locationAfterChapter = EpubCFI.prototype.compare(location.start.cfi, chapterCfi) > 0;
|
||||
const locationAfterChapter = (EpubCFI.prototype as any).compare(location.start.cfi, chapterCfi) > 0;
|
||||
return locationAfterChapter ? chapter : result;
|
||||
}, null);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,26 @@
|
||||
interface EpubThemeObject {
|
||||
body?: Record<string, string>;
|
||||
p?: Record<string, string>;
|
||||
'h1, h2, h3, h4, h5, h6'?: Record<string, string>;
|
||||
a?: Record<string, string>;
|
||||
img?: Record<string, string>;
|
||||
code?: Record<string, string>;
|
||||
'*'?: Record<string, string>;
|
||||
[key: string]: Record<string, string> | undefined;
|
||||
}
|
||||
|
||||
interface EpubRenditionThemes {
|
||||
register(name: string, theme: EpubThemeObject): void;
|
||||
select(name: string): void;
|
||||
}
|
||||
|
||||
interface EpubRenditionMinimal {
|
||||
themes: {
|
||||
register(name: string, theme: Record<string, Record<string, string>>): void;
|
||||
select(name: string): void;
|
||||
};
|
||||
}
|
||||
|
||||
export enum EpubTheme {
|
||||
WHITE = 'white',
|
||||
BLACK = 'black',
|
||||
@@ -17,7 +40,7 @@ export enum EpubTheme {
|
||||
}
|
||||
|
||||
export class EpubThemeUtil {
|
||||
static readonly themesMap = new Map<string, any>([
|
||||
static readonly themesMap = new Map<string, EpubThemeObject>([
|
||||
[EpubTheme.BLACK, {
|
||||
"body": {"background-color": "#000000", "color": "#f9f9f9"},
|
||||
"p": {"color": "#f9f9f9"},
|
||||
@@ -177,27 +200,27 @@ export class EpubThemeUtil {
|
||||
}
|
||||
}
|
||||
|
||||
static applyTheme(rendition: any, themeKey: string, fontFamily?: string, fontSize?: number, lineHeight?: number, letterSpacing?: number): void {
|
||||
static applyTheme(rendition: EpubRenditionMinimal, themeKey: string, fontFamily?: string, fontSize?: number, lineHeight?: number, letterSpacing?: number): void {
|
||||
if (!rendition) return;
|
||||
|
||||
const baseTheme = this.themesMap.get(themeKey ?? 'black') ?? {};
|
||||
const combined = {
|
||||
const baseTheme = this.themesMap.get(themeKey ?? 'black') as EpubThemeObject ?? {};
|
||||
const combined: EpubThemeObject = {
|
||||
...baseTheme,
|
||||
body: {
|
||||
...baseTheme.body,
|
||||
'font-family': fontFamily,
|
||||
...(baseTheme.body || {}),
|
||||
...(fontFamily && { 'font-family': fontFamily }),
|
||||
'font-size': `${fontSize ?? 100}%`,
|
||||
'line-height': lineHeight,
|
||||
'letter-spacing': `${letterSpacing}em`
|
||||
...(lineHeight && { 'line-height': lineHeight.toString() }),
|
||||
'letter-spacing': `${letterSpacing ?? 0}em`
|
||||
},
|
||||
'*': {
|
||||
...baseTheme['*'],
|
||||
'line-height': lineHeight,
|
||||
'letter-spacing': `${letterSpacing}em`
|
||||
...(baseTheme['*'] || {}),
|
||||
...(lineHeight && { 'line-height': lineHeight.toString() }),
|
||||
'letter-spacing': `${letterSpacing ?? 0}em`
|
||||
}
|
||||
};
|
||||
|
||||
rendition.themes.register('custom', combined);
|
||||
rendition.themes.register('custom', combined as Record<string, Record<string, string>>);
|
||||
rendition.themes.select('custom');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
|
||||
this.bookService.savePdfProgress(this.bookId, this.page, percentage).subscribe();
|
||||
}
|
||||
|
||||
onPdfPagesLoaded(event: any): void {
|
||||
onPdfPagesLoaded(event: { pagesCount: number }): void {
|
||||
this.totalPages = event.pagesCount;
|
||||
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
|
||||
this.readingSessionService.startSession(this.bookId, "PDF", this.page.toString(), percentage);
|
||||
|
||||
Reference in New Issue
Block a user