Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue loading fixed-layout EPUBs #493

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading