Fix epub viewer

This commit is contained in:
aditya.chandel
2025-01-17 16:18:16 -07:00
parent 5937574eb8
commit d1cd13f0bc
@@ -28,32 +28,33 @@ const FALLBACK_EPUB_SETTINGS = {
imports: [Drawer, Button, NgForOf, FormsModule, Divider, Select],
})
export class EpubViewerComponent implements OnInit, OnDestroy {
@ViewChild('epubContainer', { static: true }) epubContainer!: ElementRef;
@ViewChild('epubContainer', {static: true}) epubContainer!: ElementRef;
chapters: { label: string; href: string }[] = [];
isDrawerVisible = false;
isSettingsDrawerVisible = false;
private book: any;
private rendition: any;
private keyListener: (e: KeyboardEvent) => void = () => {};
private keyListener: (e: KeyboardEvent) => void = () => {
};
fontSize = FALLBACK_EPUB_SETTINGS.fontSize;
selectedFontType = FALLBACK_EPUB_SETTINGS.fontType;
selectedTheme = FALLBACK_EPUB_SETTINGS.theme;
fontTypes: any[] = [
{ label: 'Serif', value: 'serif' },
{ label: 'Sans Serif', value: 'sans-serif' },
{ label: 'Roboto', value: 'roboto' },
{ label: 'Cursive', value: 'cursive' },
{ label: 'Monospace', value: 'monospace' },
{label: 'Serif', value: 'serif'},
{label: 'Sans Serif', value: 'sans-serif'},
{label: 'Roboto', value: 'roboto'},
{label: 'Cursive', value: 'cursive'},
{label: 'Monospace', value: 'monospace'},
];
themes: any[] = [
{ label: 'White', value: 'white' },
{ label: 'Black', value: 'black' },
{ label: 'Grey', value: 'grey' },
{ label: 'Sepia', value: 'sepia' },
{label: 'White', value: 'white'},
{label: 'Black', value: 'black'},
{label: 'Grey', value: 'grey'},
{label: 'Sepia', value: 'sepia'},
];
private epubService = inject(EpubService);
@@ -79,9 +80,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
const individualSetting = results[2]?.epubSettings;
const globalSetting = appSettings.epub;
console.log(individualSetting)
console.log(globalSetting)
this.epub = epub;
const fileReader = new FileReader();
@@ -102,11 +100,24 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
allowScriptedContent: true,
});
if (this.epub?.epubProgress) {
this.rendition.display(this.epub.epubProgress);
} else {
this.rendition.display();
}
this.themesMap.forEach((theme, name) => {
this.rendition.themes.register(name, theme);
});
this.selectedTheme = individualSetting?.theme || globalSetting?.theme || FALLBACK_EPUB_SETTINGS.theme;
this.selectedFontType = individualSetting?.font || globalSetting?.font || FALLBACK_EPUB_SETTINGS.fontType;
this.fontSize = individualSetting?.fontSize || globalSetting?.fontSize || FALLBACK_EPUB_SETTINGS.fontSize;
this.applyViewerSettings();
this.rendition.themes.select(this.selectedTheme);
this.rendition.themes.fontSize(`${this.fontSize}%`);
this.rendition.themes.font(this.selectedFontType);
this.setupKeyListener();
this.trackProgress();
};
@@ -117,10 +128,14 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
});
}
private applyViewerSettings(): void {
this.changeFontType();
this.updateFontSize();
this.changeThemes();
changeThemes(): void {
if (this.rendition) {
console.log(this.selectedTheme)
this.rendition.themes.select(this.selectedTheme);
this.rendition.clear();
this.rendition.start();
this.updateViewerSetting();
}
}
updateFontSize(): void {
@@ -130,6 +145,13 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
}
}
changeFontType(): void {
if (this.rendition) {
this.rendition.themes.font(this.selectedFontType);
this.updateViewerSetting();
}
}
increaseFontSize(): void {
this.fontSize = Math.min(this.fontSize + 10, FALLBACK_EPUB_SETTINGS.maxFontSize);
this.updateFontSize();
@@ -140,22 +162,6 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
this.updateFontSize();
}
changeFontType(): void {
if (this.rendition) {
this.rendition.themes.font(this.selectedFontType);
this.updateViewerSetting();
}
}
changeThemes(): void {
if (this.rendition) {
this.rendition.themes.select(this.selectedTheme);
this.rendition.clear();
this.rendition.start();
this.updateViewerSetting();
}
}
private updateViewerSetting(): void {
const bookSetting: BookSetting = {
epubSettings: {
@@ -169,12 +175,20 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
private setupKeyListener(): void {
this.keyListener = (e: KeyboardEvent) => {
if (e.key === 'ArrowLeft') {
this.prevPage();
} else if (e.key === 'ArrowRight') {
this.nextPage();
switch (e.key) {
case 'ArrowLeft':
this.prevPage();
break;
case 'ArrowRight':
this.nextPage();
break;
default:
break;
}
};
if (this.rendition) {
this.rendition.on('keyup', this.keyListener);
}
document.addEventListener('keyup', this.keyListener);
}
@@ -218,4 +232,59 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
}
document.removeEventListener('keyup', this.keyListener);
}
themesMap = new Map<string, any>([
[
'black', {
"body": {"background-color": "#000000", "color": "#f9f9f9"},
"p": {"color": "#f9f9f9"},
"h1, h2, h3, h4, h5, h6": {"color": "#f9f9f9"},
"a": {"color": "#f9f9f9"},
"img": {
"-webkit-filter": "invert(1) hue-rotate(180deg)",
"filter": "invert(1) hue-rotate(180deg)"
},
"code": {"color": "#00ff00", "background-color": "black"}
}
],
[
'sepia', {
"body": {"background-color": "#f4ecd8", "color": "#6e4b3a"},
"p": {"color": "#6e4b3a"},
"h1, h2, h3, h4, h5, h6": {"color": "#6e4b3a"},
"a": {"color": "#8b4513"},
"img": {
"-webkit-filter": "sepia(1) contrast(1.5)",
"filter": "sepia(1) contrast(1.5)"
},
"code": {"color": "#8b0000", "background-color": "#f4ecd8"}
}
],
[
'white', {
"body": {"background-color": "#ffffff", "color": "#000000"},
"p": {"color": "#000000"},
"h1, h2, h3, h4, h5, h6": {"color": "#000000"},
"a": {"color": "#000000"},
"img": {
"-webkit-filter": "none",
"filter": "none"
},
"code": {"color": "#d14", "background-color": "#f5f5f5"}
}
],
[
'grey', {
"body": {"background-color": "#404040", "color": "#d3d3d3"},
"p": {"color": "#d3d3d3"},
"h1, h2, h3, h4, h5, h6": {"color": "#d3d3d3"},
"a": {"color": "#1e90ff"},
"img": {
"filter": "none"
},
"code": {"color": "#d14", "background-color": "#585858"}
}
]
]);
}