mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
FXL: fix onLoad callback
Also add deselect method.
This commit is contained in:
+16
-7
@@ -54,7 +54,7 @@ class Container {
|
|||||||
get side() {
|
get side() {
|
||||||
return this.#side
|
return this.#side
|
||||||
}
|
}
|
||||||
async #createFrame(src) {
|
async #createFrame({ index, src }) {
|
||||||
const element = document.createElement('div')
|
const element = document.createElement('div')
|
||||||
const iframe = document.createElement('iframe')
|
const iframe = document.createElement('iframe')
|
||||||
element.append(iframe)
|
element.append(iframe)
|
||||||
@@ -70,8 +70,8 @@ class Container {
|
|||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const onload = () => {
|
const onload = () => {
|
||||||
iframe.removeEventListener('load', onload)
|
iframe.removeEventListener('load', onload)
|
||||||
this.onLoad?.(iframe)
|
|
||||||
const doc = iframe.contentDocument
|
const doc = iframe.contentDocument
|
||||||
|
this.onLoad?.(doc, index)
|
||||||
const { width, height } = getViewport(doc, this.defaultViewport)
|
const { width, height } = getViewport(doc, this.defaultViewport)
|
||||||
resolve({
|
resolve({
|
||||||
element, iframe,
|
element, iframe,
|
||||||
@@ -175,7 +175,7 @@ export class FixedLayout {
|
|||||||
#container = new Container()
|
#container = new Container()
|
||||||
constructor({ book, onLoad, onRelocated }) {
|
constructor({ book, onLoad, onRelocated }) {
|
||||||
this.book = book
|
this.book = book
|
||||||
this.onLoad = onLoad
|
this.#container.onLoad = onLoad
|
||||||
this.onRelocated = onRelocated
|
this.onRelocated = onRelocated
|
||||||
|
|
||||||
const { rendition } = book
|
const { rendition } = book
|
||||||
@@ -246,11 +246,16 @@ export class FixedLayout {
|
|||||||
this.#index = index
|
this.#index = index
|
||||||
const spread = this.#spreads[index]
|
const spread = this.#spreads[index]
|
||||||
if (spread.center) {
|
if (spread.center) {
|
||||||
const center = await spread.center?.load?.()
|
const index = this.book.sections.indexOf(spread.center)
|
||||||
await this.#container.showSpread({ center })
|
const src = await spread.center?.load?.()
|
||||||
|
await this.#container.showSpread({ center: { index, src } })
|
||||||
} else {
|
} else {
|
||||||
const left = await spread.left?.load?.()
|
const indexL = this.book.sections.indexOf(spread.left)
|
||||||
const right = await spread.right?.load?.()
|
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 })
|
await this.#container.showSpread({ left, right, side })
|
||||||
}
|
}
|
||||||
this.onRelocated?.(null, this.index, 0, 1)
|
this.onRelocated?.(null, this.index, 0, 1)
|
||||||
@@ -277,4 +282,8 @@ export class FixedLayout {
|
|||||||
if (s) this.onRelocated?.(null, this.index, 0, 1)
|
if (s) this.onRelocated?.(null, this.index, 0, 1)
|
||||||
else return this.goToSpread(this.#index - 1, this.rtl ? 'left' : 'right')
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ export class View {
|
|||||||
doc.documentElement.lang ||= this.language
|
doc.documentElement.lang ||= this.language
|
||||||
doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection
|
doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection
|
||||||
|
|
||||||
this.renderer.setStyle(this.#css)
|
this.renderer.setStyle?.(this.#css)
|
||||||
|
|
||||||
// set handlers for links
|
// set handlers for links
|
||||||
const section = book.sections[index]
|
const section = book.sections[index]
|
||||||
@@ -281,7 +281,7 @@ export class View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
deselect() {
|
deselect() {
|
||||||
return this.renderer?.deselect()
|
return this.renderer.deselect?.()
|
||||||
}
|
}
|
||||||
async getTOCItemOf(target) {
|
async getTOCItemOf(target) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user