Skip to content

Commit

Permalink
Fix issue loading fixed-layout EPUBs (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored Oct 16, 2024
1 parent 7ffbdd6 commit 1711595
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. Take a look
#### Navigator

* [#459](https://github.com/readium/swift-toolkit/issues/459) Fixed the stack overflow issue that occurred when running the text-to-speech on an EPUB file with many empty resources.
* [#490](https://github.com/readium/swift-toolkit/issues/490) Fixed issue loading fixed-layout EPUBs.


## [3.0.0-alpha.2]
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Sources/Navigator/EPUB/EPUBFixedSpreadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ final class EPUBFixedSpreadView: EPUBSpreadView {
}

override func evaluateScript(_ script: String, inHREF href: AnyURL? = nil) async -> Result<Any, any Error> {
await spreadLoaded()
let href = href?.string ?? ""
let script = "spread.eval('\(href)', `\(script.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "`", with: "\\`"))`);"
return await super.evaluateScript(script)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Navigator/EPUB/EPUBNavigatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ open class EPUBNavigatorViewController: UIViewController,

applySettings()

await reloadSpreads(at: currentLocation, force: false)
await _reloadSpreads(at: currentLocation, force: false)

onInitializedCallbacks.complete()
}
Expand Down Expand Up @@ -536,11 +536,11 @@ open class EPUBNavigatorViewController: UIViewController,
private var reloadSpreadsContinuations = [CheckedContinuation<Void, Never>]()
private var needsReloadSpreads = false

@MainActor
private func reloadSpreads(at locator: Locator? = nil, force: Bool) async {
guard isViewLoaded else {
guard state != .initializing, isViewLoaded else {
return
}

guard !needsReloadSpreads else {
await withCheckedContinuation { continuation in
reloadSpreadsContinuations.append(continuation)
Expand Down
20 changes: 20 additions & 0 deletions Sources/Navigator/EPUB/EPUBSpreadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,33 @@ class EPUBSpreadView: UIView, Loggable, PageView {
applySettings()
spreadDidLoad()
delegate?.spreadViewDidLoad(self)
onSpreadLoadedCallbacks.complete()
}

/// To be overriden to customize the behavior after the spread is loaded.
func spreadDidLoad() {
showSpread()
}

private let onSpreadLoadedCallbacks = CompletionList()

/// Awaits for the spread to be fully loaded.
func spreadLoaded() async {
await withCheckedContinuation { continuation in
whenSpreadLoaded {
continuation.resume()
}
}
}

/// Executes the given `callback` when the spread is fully loaded.
func whenSpreadLoaded(_ callback: @escaping () -> Void) {
let callback = onSpreadLoadedCallbacks.add(callback)
if spreadLoaded {
callback()
}
}

func showSpread() {
trace()
activityIndicatorView?.stopAnimating()
Expand Down
4 changes: 4 additions & 0 deletions Sources/Navigator/EPUB/Scripts/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function update(position) {
window.addEventListener("scroll", onScroll);

function onScroll() {
if (readium.isFixedLayout) {
return;
}

last_known_scrollY_position =
window.scrollY / document.scrollingElement.scrollHeight;
// Using Math.abs because for RTL books, the value will be negative.
Expand Down

0 comments on commit 1711595

Please sign in to comment.