This commit is contained in:
Balazs Perlaki-Horvath 2023-11-14 09:52:07 +01:00 committed by Kelson
parent 8f3cd6f371
commit 21b33a1c76

View File

@ -8,13 +8,12 @@
import WebKit import WebKit
final class BrowserUIDelegate: NSObject, WKUIDelegate { final class BrowserUIDelegate: NSObject, WKUIDelegate {
@Published private(set) var externalURL: URL? @Published private(set) var externalURL: URL?
#if os(macOS) #if os(macOS)
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, func webView(_: WKWebView, createWebViewWith _: WKWebViewConfiguration,
for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { for navigationAction: WKNavigationAction, windowFeatures _: WKWindowFeatures) -> WKWebView?
{
guard navigationAction.targetFrame == nil else { return nil } guard navigationAction.targetFrame == nil else { return nil }
guard let newUrl = navigationAction.request.url else { return nil } guard let newUrl = navigationAction.request.url else { return nil }
@ -37,19 +36,20 @@ final class BrowserUIDelegate: NSObject, WKUIDelegate {
currentWindow.addTabbedWindow(newWindow, ordered: .above) currentWindow.addTabbedWindow(newWindow, ordered: .above)
return nil return nil
} }
#endif #endif
#if os(iOS) #if os(iOS)
func webView(_ webView: WKWebView, func webView(_ webView: WKWebView,
contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo, contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo,
completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) { completionHandler: @escaping (UIContextMenuConfiguration?) -> Void)
{
guard let url = elementInfo.linkURL, url.isKiwixURL else { completionHandler(nil); return } guard let url = elementInfo.linkURL, url.isKiwixURL else { completionHandler(nil); return }
let configuration = UIContextMenuConfiguration( let configuration = UIContextMenuConfiguration(
previewProvider: { previewProvider: {
let webView = WKWebView(frame: .zero, configuration: WebViewConfiguration()) let webView = WKWebView(frame: .zero, configuration: WebViewConfiguration())
webView.load(URLRequest(url: url)) webView.load(URLRequest(url: url))
return WebViewController(webView: webView) return WebViewController(webView: webView)
}, actionProvider: { suggestedActions in }, actionProvider: { _ in
var actions = [UIAction]() var actions = [UIAction]()
// open url // open url
@ -86,5 +86,5 @@ final class BrowserUIDelegate: NSObject, WKUIDelegate {
) )
completionHandler(configuration) completionHandler(configuration)
} }
#endif #endif
} }