From b6acb4ff5cd3c45d4f5efc8f4e1c75380558145a Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Wed, 8 Mar 2023 11:40:18 +0000 Subject: [PATCH] FXL: fix `onLoad` callback Also add deselect method. --- fixed-layout.js | 23 ++++++++++++++++------- view.js | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fixed-layout.js b/fixed-layout.js index 46a8612..38a144a 100644 --- a/fixed-layout.js +++ b/fixed-layout.js @@ -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() + } } diff --git a/view.js b/view.js index 1fc1292..0977297 100644 --- a/view.js +++ b/view.js @@ -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 {