fix(reader): load correct chapter in web reader

Navigation sometimes treated stale rendered contents as successful navigation or failed silently when a section did not load properly. Ensure the target index matches the rendered content and throw on section load failure to prevent incorrect chapter display.

Closes #203
This commit is contained in:
neon
2026-06-02 21:29:29 +00:00
parent 45f252a7e6
commit 0208ba0b9e
3 changed files with 116 additions and 5 deletions
+5 -2
View File
@@ -1010,11 +1010,14 @@ export class Paginator extends HTMLElement {
}
await this.#display(
Promise.resolve(this.sections[index].load())
.then((src) => ({ index, src, anchor, onLoad, select }))
.then((src) => {
if (!src) throw new Error(`Failed to load section ${index}`)
return { index, src, anchor, onLoad, select }
})
.catch((e) => {
console.warn(e)
console.warn(new Error(`Failed to load section ${index}`))
return {}
throw e
}),
)
}
+9 -3
View File
@@ -133,7 +133,8 @@ export const makeBook = async (file) => {
book = await new EPUB(loader).init()
}
} else if (await isPDF(file)) {
const { makePDF } = await import('./pdf.js')
const pdfModule = './pdf.js'
const { makePDF } = await import(pdfModule)
book = await makePDF(file)
} else {
const { isMOBI, MOBI } = await import('./mobi.js')
@@ -536,11 +537,16 @@ export class View extends HTMLElement {
async goTo(target) {
const resolved = this.resolveNavigation(target)
const hasContent = () => (this.renderer.getContents?.()?.length ?? 0) > 0
const hasContentFor = (nextResolved) => {
const contents = this.renderer.getContents?.() ?? []
if (!contents.length) return false
if (typeof nextResolved?.index !== 'number') return true
return contents.some((content) => content?.index === nextResolved.index)
}
const tryGoTo = async (nextResolved) => {
try {
await this.renderer.goTo(nextResolved)
return hasContent()
return hasContentFor(nextResolved)
} catch (e) {
console.warn(e)
return false