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

Handle URL Click in the editor #15

Open
tomcruise-007 opened this issue Apr 16, 2021 · 7 comments
Open

Handle URL Click in the editor #15

tomcruise-007 opened this issue Apr 16, 2021 · 7 comments
Labels
solution-given A solution has been provided wontfix This will not be worked on

Comments

@tomcruise-007
Copy link

Hi,
I was able to insert link in the editor. But couldn't implement autolink action on it, making the link clickable. It seems that there was no callback triggered while we click on the URL from js file. Any workarounds for this?. I hope you will found a suitable solution for this issue.

@tomcruise-007
Copy link
Author

Do you recognize the issue that i have mentioned in the above comment? Could you please reply to it.

@Andrew-Chen-Wang
Copy link
Owner

Apologies; I put the eyes emoji to let you know I've read it. I'm in the middle of finals and will report back on May 1st.

@tomcruise-007
Copy link
Author

Thank you for your reply. I'm looking forward to it.

@Andrew-Chen-Wang
Copy link
Owner

HI @tomcruise-007 I just remembered why I don't allow it. This view is technically an "editor" so if you had an accidental click on the URL, then you would've been taken somewhere else.

@Andrew-Chen-Wang Andrew-Chen-Wang added the wontfix This will not be worked on label Apr 27, 2021
@fukemy
Copy link

fukemy commented Aug 24, 2021

i think it's really bad if user can not click into link, can u help about this problem. Sometimes we use this as "View", not editor

@Andrew-Chen-Wang
Copy link
Owner

As a "view", gotcha. IIRC, I thought if you disabled the editor and only showed the editor, users would be able to click the link. However, it would not appear in SFSafariViewController; instead, it'd show up in the WKWebView and you wouldn't be able to get back to the editor view. So in the following, I'll show how to present SFSafariViewController.

It's been awhile since I've looked at this package, but here are my two guesses for what to do:

Disclaimer: it's been nearly a year since I've properly written in Swift, so adjust accordingly if I get concepts wrong.

Guess 1:

Use this:

/// Called when the internal WKWebView begins loading a URL that it does not know how to respond to
/// For example, if there is an external link, and then the user taps it
@objc optional func richEditor(_ editor: RichEditorView, shouldInteractWith url: URL) -> Bool

Basically, your VC needs to inherit the delegate. Use the delegate to define that function. Finally, the contents of the code

@objc optional func richEditor(_ editor: RichEditorView, shouldInteractWith url: URL) -> Bool {
    let safariVC = SFSafariViewController(url);
    present(safariVC);
    return true;
}

Guess 2:

Assuming we only need to use SFSafariViewController, first import it. Then create a new class inheriting the editor view. Take a look at this func's bottom portion of the code:

// User is tapping on a link, so we should react accordingly
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url {
if delegate?.richEditor?(self, shouldInteractWith: url) ?? false {
return decisionHandler(WKNavigationActionPolicy.allow);
}
}
}
return decisionHandler(WKNavigationActionPolicy.allow);

So we override this func. We now need to decide if the given url is a link:

override public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

if !navigationAction.request.url?.absoluteString.hasPrefix(callbackPrefix) && navigationAction.navigationType == .linkActivated {
    if let url = navigationAction.request.url {
        let safariVC = SFSafariViewController(url);
        present(safariVC);
        return decisionHandler(WKNavigationActionPolicy.cancel);
    }
}
return super().webView(webView, navigationAction, decisionHandler);

}

Again, please adjust accordingly. If you don't mind, too, please post your code so that others know what to do as well.

@fukemy
Copy link

fukemy commented Aug 24, 2021

yeah, i just use your solution 1, thanks so much <3

@Andrew-Chen-Wang Andrew-Chen-Wang added the solution-given A solution has been provided label Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution-given A solution has been provided wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants