-
Notifications
You must be signed in to change notification settings - Fork 47
Conversation
Cross-posting from readium/swift-toolkit#139
JS Click HandlerAs was mentioned in readium/swift-toolkit#139 (1, 2) it may be desirable moving forward to generically allow the App to intercept & control what occurs with tapping hyperlinks, and yet I didn't go this route; I wanted to explain my logic a bit more. As I said above, the JS bridge doesn't normally allow Swift to return a value to JS, so you can't do this: function onClick(event) {
let eventWillBeHandledBySwift = webkit.messageHandlers.tap.postMessage(event);
if (eventWillBeHandledBySwift) {
event.preventDefault();
event.stopPropagation();
return;
}
// ...
} Any other options? There's this method I found: // js
function callNativeCode() {
webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript");
}
// this function will be later called by native code
function callNativeCodeReturn(returnedValue) {
// do your stuff with returned value here
} // swift
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if(message.name == "callbackHandler") {
print("JavaScript is sending a message \(message.body)")
webView.evaluateJavaScript("callNativeCodeReturn(0)")
}
} ...Gross, and probably not terribly performant. Even if you use promises layered on this, which some people suggested. You can also override the On the other end of this, we have the actual link navigation which we can intercept with You could go the route of what was done in Android, whereby the Swift click handler sets a little semaphore variable that the navigation-policy delegate method uses to know if it should proceed...but that also feels icky (no offense). The above is why I opted to purely detect this specific scenario in the JS: it's not generic, but it seemed the least hacky and most performant. However, it doesn't allow the signature suggested by @mickael-menu func navigator(_ navigator: Navigator, shouldNavigateToNoteAt link: Link, content: String) -> Bool Having that NavigatorDelegateAs I said, I feel like making the I could make it more specific by adding I could also attempt to call Maybe this other thing I mentioned of somehow subclassing the |
… navigator, made touch handling always send data across the bridge, noteref delegate method returns bool.
7514e06
to
cc62715
Compare
Cross-posting:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tooolbox Thanks again for this contribution, it will be very useful! I left a few comments in the PR.
Also I don't see any PR in r2-testapp-swift
with the footnote pop-up, did you intend to send one?
Great review, appreciated 👍
I will need to see about extracting my implementation and making a feature branch to give you guys. Do you need to see that before merging this one? |
Not really, but it would help test the feature. Maybe we can wait until you're ready before merging this PR? I don't expect any changes in the files you modified in the short run. |
Yeah, I can see how that would help with testing, so I'll get a PR together this afternoon. 🚀 |
Okay @mickael-menu I'm done with everything we discussed, plus a little more that I noticed during my tests, mainly to handle noterefs that are just Matching PR in the App is available here. Whew. |
@tooolbox Thanks, great job! I'll test and merge it next week, but the code looks good. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tooolbox Thanks again for your contribution 👍
This PR is based on extensive discussion and closes readium/swift-toolkit#139
(Not ready for formal review yet.)