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

Added shouldNavigateToLink on NavigatorDelegate #331

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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ All notable changes to this project will be documented in this file. Take a look

* The `PublicationSpeechSynthesizer` (TTS) now supports background playback by default.
* You will need to enable the **Audio Background Mode** in your app's build info.
* The EPUB navigator can now render a custom reading order (contributed by [@chrfalch](https://github.com/readium/swift-toolkit/pull/332)).
* This is useful when displaying a non-linear resource.
* Support for non-linear EPUB resources with an opt-in in reading apps (contributed by @chrfalch in [#332](https://github.com/readium/swift-toolkit/pull/332) and [#331](https://github.com/readium/swift-toolkit/pull/331)).
1. Override loading non-linear resources with `VisualNavigatorDelegate.navigator(_:shouldNavigateToLink:)`.
2. Present a new `EPUBNavigatorViewController` by providing a custom `readingOrder` with only this resource to the constructor.

### Fixed

Expand Down
14 changes: 12 additions & 2 deletions Sources/Navigator/EPUB/EPUBNavigatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,11 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
}

func spreadView(_ spreadView: EPUBSpreadView, didTapOnInternalLink href: String, clickEvent: ClickEvent?) {
guard let link = publication.link(withHREF: href)?.copy(href: href) else {
log(.warning, "Cannot find link with HREF: \(href)")
return
}

// Check to see if this was a noteref link and give delegate the opportunity to display it.
if
let clickEvent = clickEvent,
Expand All @@ -994,15 +999,20 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
{
if !delegate.navigator(
self,
shouldNavigateToNoteAt: Link(href: href, type: "text/html"),
shouldNavigateToNoteAt: link,
content: note,
referrer: referrer
) {
return
}
}

go(to: Link(href: href))
// Ask if we should navigate to the link
if let delegate = delegate, !delegate.navigator(self, shouldNavigateToLink: link) {
return
}

go(to: link)
}

/// Checks if the internal link is a noteref, and retrieves both the referring text of the link and the body of the note.
Expand Down
10 changes: 10 additions & 0 deletions Sources/Navigator/VisualNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public protocol VisualNavigatorDelegate: NavigatorDelegate {
/// Called when the user released a key and it was not handled by the
/// resource.
func navigator(_ navigator: VisualNavigator, didReleaseKey event: KeyEvent)

/// Called when the user taps on an internal link.
///
/// Return `true` to navigate to the link, or `false` if you intend to
/// present the link yourself
func navigator(_ navigator: VisualNavigator, shouldNavigateToLink link: Link) -> Bool
}

public extension VisualNavigatorDelegate {
Expand All @@ -125,4 +131,8 @@ public extension VisualNavigatorDelegate {
func navigator(_ navigator: VisualNavigator, didReleaseKey event: KeyEvent) {
// Optional
}

func navigator(_ navigator: VisualNavigator, shouldNavigateToLink link: Link) -> Bool {
true
}
}