Add support for displaying subchapters in EPUB reader sidebar (#1619)

This commit is contained in:
Aditya Chandel
2025-11-24 19:01:45 -07:00
committed by GitHub
parent 4d9362a1b4
commit 2554343301
3 changed files with 54 additions and 12 deletions
@@ -15,7 +15,10 @@
<p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Chapters"> <p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Chapters">
<ul class="chapter-list"> <ul class="chapter-list">
@for (chapter of chapters; track chapter) { @for (chapter of chapters; track chapter) {
<li (click)="navigateToChapter(chapter); $event.stopPropagation()" class="chapter-item"> <li (click)="navigateToChapter(chapter); $event.stopPropagation()"
class="chapter-item"
[class.current-chapter]="chapter.href === currentChapterHref"
[style.padding-left.rem]="chapter.level * 1.5 + 0.75">
{{ chapter.label }} {{ chapter.label }}
</li> </li>
} }
@@ -140,16 +140,27 @@
} }
.chapter-item { .chapter-item {
padding: 0.5px 0.5px; padding: 0;
font-size: 14px; font-size: 14px;
color: var(--text-color); color: var(--text-color);
cursor: pointer; cursor: pointer;
transition: background-color 0.3s ease, color 0.3s ease; transition: background-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease;
border-radius: 5px; border-radius: 0.35rem;
background-color: transparent;
display: block;
&:hover {
color: var(--primary-color);
}
} }
.chapter-item:hover { .current-chapter {
color: var(--primary-color); color: var(--primary-color);
font-weight: bold;
background-color: color-mix(in srgb, var(--primary-color) 15%, transparent);
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary-color) 35%, transparent);
padding: 0.35rem 0.75rem;
border-radius: 0.45rem;
} }
.location-indicator { .location-indicator {
@@ -31,8 +31,9 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
@ViewChild('epubContainer', {static: false}) epubContainer!: ElementRef; @ViewChild('epubContainer', {static: false}) epubContainer!: ElementRef;
isLoading = true; isLoading = true;
chapters: { label: string; href: string }[] = []; chapters: { label: string; href: string; level: number }[] = [];
currentChapter = ''; currentChapter = '';
currentChapterHref: string | null = null;
isDrawerVisible = false; isDrawerVisible = false;
isSettingsDrawerVisible = false; isSettingsDrawerVisible = false;
@@ -104,10 +105,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.book = ePub(fileReader.result as ArrayBuffer); this.book = ePub(fileReader.result as ArrayBuffer);
this.book.loaded.navigation.then((nav: any) => { this.book.loaded.navigation.then((nav: any) => {
this.chapters = nav.toc.map((chapter: any) => ({ this.chapters = this.extractChapters(nav.toc, 0);
label: chapter.label,
href: chapter.href,
}));
}); });
const settingScope = myself.userSettings.perBookSetting.epub; const settingScope = myself.userSettings.perBookSetting.epub;
@@ -163,6 +161,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
: this.rendition.display(); : this.rendition.display();
displayPromise.then(() => { displayPromise.then(() => {
this.updateCurrentChapter(this.rendition.currentLocation());
this.setupKeyListener(); this.setupKeyListener();
this.trackProgress(); this.trackProgress();
this.setupTouchListener(); this.setupTouchListener();
@@ -345,7 +344,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
} }
} }
navigateToChapter(chapter: { label: string; href: string }): void { navigateToChapter(chapter: { label: string; href: string; level: number }): void {
if (this.book && chapter.href) { if (this.book && chapter.href) {
this.book.rendition.display(chapter.href); this.book.rendition.display(chapter.href);
} }
@@ -362,6 +361,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
private trackProgress(): void { private trackProgress(): void {
if (!this.book || !this.rendition) return; if (!this.book || !this.rendition) return;
this.rendition.on('relocated', (location: any) => { this.rendition.on('relocated', (location: any) => {
this.updateCurrentChapter(location);
const cfi = location.end.cfi; const cfi = location.end.cfi;
const currentIndex = location.start.index; const currentIndex = location.start.index;
const totalSpineItems = this.book.spine.items.length; const totalSpineItems = this.book.spine.items.length;
@@ -381,7 +381,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.progressPercentage = Math.round(percentage * 1000) / 10; this.progressPercentage = Math.round(percentage * 1000) / 10;
} }
this.currentChapter = getChapter(this.book, location)?.label;
this.bookService.saveEpubProgress(this.epub.id, cfi, Math.round(percentage * 1000) / 10).subscribe(); this.bookService.saveEpubProgress(this.epub.id, cfi, Math.round(percentage * 1000) / 10).subscribe();
}); });
@@ -444,4 +443,33 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}, 3000); }, 3000);
} }
} }
private updateCurrentChapter(location: any): void {
if (!location) return;
const chapter = getChapter(this.book, location);
if (chapter) {
if (chapter.label) {
this.currentChapter = chapter.label;
}
this.currentChapterHref = chapter.href;
}
}
private extractChapters(toc: any[], level: number): { label: string; href: string; level: number }[] {
const chapters: { label: string; href: string; level: number }[] = [];
for (const item of toc) {
chapters.push({
label: item.label,
href: item.href,
level: level
});
if (item.subitems && item.subitems.length > 0) {
chapters.push(...this.extractChapters(item.subitems, level + 1));
}
}
return chapters;
}
} }