Andrew-Chen-Wang / RichEditorView

Rich Text Editor in Swift. Newly Featured Code and Swift 5 compatible of cjwirth/RichEditorView.
BSD 3-Clause "New" or "Revised" License
134 stars 59 forks source link

Handle URL Click in the editor #15

Open tomcruise-007 opened 3 years ago

tomcruise-007 commented 3 years ago

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 commented 3 years ago

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

Andrew-Chen-Wang commented 3 years ago

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 commented 3 years ago

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

Andrew-Chen-Wang commented 3 years ago

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.

fukemy commented 3 years ago

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 commented 3 years ago

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: https://github.com/Andrew-Chen-Wang/RichEditorView/blob/ed734f6f9bb2bb2e09efe183764b488d50d3bcf1/Sources/RichEditorView/RichEditorView.swift#L30-L32

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:

https://github.com/Andrew-Chen-Wang/RichEditorView/blob/ed734f6f9bb2bb2e09efe183764b488d50d3bcf1/Sources/RichEditorView/RichEditorView.swift#L483-L491

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 commented 3 years ago

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