FXL: fix onLoad callback

Also add deselect method.
This commit is contained in:
John Factotum
2023-03-08 11:40:18 +00:00
committed by GitHub
parent 1fef33731f
commit b6acb4ff5c
2 changed files with 18 additions and 9 deletions
+16 -7
View File
@@ -54,7 +54,7 @@ class Container {
get side() {
return this.#side
}
async #createFrame(src) {
async #createFrame({ index, src }) {
const element = document.createElement('div')
const iframe = document.createElement('iframe')
element.append(iframe)
@@ -70,8 +70,8 @@ class Container {
return new Promise(resolve => {
const onload = () => {
iframe.removeEventListener('load', onload)
this.onLoad?.(iframe)
const doc = iframe.contentDocument
this.onLoad?.(doc, index)
const { width, height } = getViewport(doc, this.defaultViewport)
resolve({
element, iframe,
@@ -175,7 +175,7 @@ export class FixedLayout {
#container = new Container()
constructor({ book, onLoad, onRelocated }) {
this.book = book
this.onLoad = onLoad
this.#container.onLoad = onLoad
this.onRelocated = onRelocated
const { rendition } = book
@@ -246,11 +246,16 @@ export class FixedLayout {
this.#index = index
const spread = this.#spreads[index]
if (spread.center) {
const center = await spread.center?.load?.()
await this.#container.showSpread({ center })
const index = this.book.sections.indexOf(spread.center)
const src = await spread.center?.load?.()
await this.#container.showSpread({ center: { index, src } })
} else {
const left = await spread.left?.load?.()
const right = await spread.right?.load?.()
const indexL = this.book.sections.indexOf(spread.left)
const indexR = this.book.sections.indexOf(spread.right)
const srcL = await spread.left?.load?.()
const srcR = await spread.right?.load?.()
const left = { index: indexL, src: srcL }
const right = { index: indexR, src: srcR }
await this.#container.showSpread({ left, right, side })
}
this.onRelocated?.(null, this.index, 0, 1)
@@ -277,4 +282,8 @@ export class FixedLayout {
if (s) this.onRelocated?.(null, this.index, 0, 1)
else return this.goToSpread(this.#index - 1, this.rtl ? 'left' : 'right')
}
deselect() {
for (const frame of this.#container.element.querySelectorAll('iframe'))
frame.contentWindow.getSelection().removeAllRanges()
}
}
+2 -2
View File
@@ -172,7 +172,7 @@ export class View {
doc.documentElement.lang ||= this.language
doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection
this.renderer.setStyle(this.#css)
this.renderer.setStyle?.(this.#css)
// set handlers for links
const section = book.sections[index]
@@ -281,7 +281,7 @@ export class View {
}
}
deselect() {
return this.renderer?.deselect()
return this.renderer.deselect?.()
}
async getTOCItemOf(target) {
try {