Improve EPUB Reader: UI Enhancements (Line Height, Letter Spacing, Flow, Fonts)

This commit is contained in:
aditya.chandel
2025-06-25 01:10:16 -06:00
committed by Aditya Chandel
parent fc328a7711
commit df5ebbe7c4
27 changed files with 767 additions and 499 deletions
@@ -0,0 +1,44 @@
import {EpubCFI} 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 getCfiFromHref(book: any, href: string): string | null {
const [_, id] = href.split('#');
const section = book.spine.get(href);
if (!section || !section.document) {
console.warn('Section or section.document is undefined for href:', href);
return null;
}
const el = id ? section.document.getElementById(id) : section.document.body;
if (!el) {
console.warn('Element not found in section.document for href:', href);
return null;
}
return section.cfiFromElement(el);
}
export function getChapter(book: any, location: any) {
const locationHref = location.start.href;
return flatten(book.navigation.toc)
.filter((chapter: any) => {
return book.canonical(chapter.href).includes(book.canonical(locationHref));
})
.reduce((result: any | null, chapter: any) => {
const chapterCfi = getCfiFromHref(book, chapter.href);
if (!chapterCfi) {
return result;
}
const locationAfterChapter = EpubCFI.prototype.compare(location.start.cfi, chapterCfi) > 0;
return locationAfterChapter ? chapter : result;
}, null);
}