| 1 | import SwiftUI |
| 2 | import WebKit |
| 3 | |
| 4 | @MainActor final class PreviewBrowser: NSObject, PreviewBrowserResource, WKNavigationDelegate, WKUIDelegate { |
| 5 | private(set) var webView: WKWebView? |
| 6 | private(set) var dataStore: WKWebsiteDataStore? |
| 7 | private let destination: PreviewDestination |
| 8 | private var initial = true |
| 9 | init(destination: PreviewDestination, request: URLRequest) { |
| 10 | self.destination = destination |
| 11 | super.init() |
| 12 | let configuration = WKWebViewConfiguration() |
| 13 | let store = WKWebsiteDataStore.nonPersistent() |
| 14 | configuration.websiteDataStore = store |
| 15 | configuration.preferences.javaScriptCanOpenWindowsAutomatically = false |
| 16 | let web = WKWebView(frame: .zero, configuration: configuration) |
| 17 | web.navigationDelegate = self; web.uiDelegate = self |
| 18 | web.allowsBackForwardNavigationGestures = false |
| 19 | web.allowsLinkPreview = false |
| 20 | dataStore = store; webView = web |
| 21 | web.load(request) |
| 22 | } |
| 23 | func close() { |
| 24 | webView?.stopLoading(); webView?.navigationDelegate = nil; webView?.uiDelegate = nil |
| 25 | webView?.removeFromSuperview(); webView = nil |
| 26 | dataStore?.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), modifiedSince: .distantPast) {} |
| 27 | dataStore = nil |
| 28 | } |
| 29 | func reload() { webView?.load(URLRequest(url: destination.wrapper)) } |
| 30 | func webView(_ webView: WKWebView, decidePolicyFor action: WKNavigationAction, decisionHandler: @escaping @MainActor (WKNavigationActionPolicy) -> Void) { |
| 31 | guard let frame = action.targetFrame, !action.shouldPerformDownload else { decisionHandler(.cancel); return } |
| 32 | let allowed = destination.allows(action.request, mainFrame: frame.isMainFrame, initial: initial) |
| 33 | if frame.isMainFrame { initial = false } |
| 34 | decisionHandler(allowed ? .allow : .cancel) |
| 35 | } |
| 36 | func webView(_ webView: WKWebView, decidePolicyFor response: WKNavigationResponse, decisionHandler: @escaping @MainActor (WKNavigationResponsePolicy) -> Void) { |
| 37 | guard response.canShowMIMEType, let url = response.response.url else { decisionHandler(.cancel); return } |
| 38 | // Initial exchange errors are HTML at the exchange URL. Its successful |
| 39 | // response redirects to the exact wrapper; no other top document loads. |
| 40 | let allowed = !response.isForMainFrame || url == destination.wrapper || url == destination.exchangeURL |
| 41 | decisionHandler(allowed ? .allow : .cancel) |
| 42 | } |
| 43 | func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for action: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { nil } |
| 44 | } |
| 45 | |
| 46 | struct PreviewSheetPresenter: ViewModifier { |
| 47 | let session: EquipoSession |
| 48 | func body(content: Content) -> some View { |
| 49 | content.sheet(isPresented: Binding(get: { session.previews.page != nil }, set: { if !$0 { session.previews.dismiss() } })) { |
| 50 | if let page = session.previews.page, let browser = page.resource as? PreviewBrowser, let web = browser.webView { |
| 51 | PreviewSheetContent(web: web, dismiss: session.previews.dismiss) |
| 52 | #if os(macOS) |
| 53 | .frame(minWidth: 640, minHeight: 520) |
| 54 | #endif |
| 55 | .onDisappear { session.previews.dismiss() } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | struct PreviewSheetContent: View { |
| 61 | let web: WKWebView |
| 62 | let dismiss: () -> Void |
| 63 | var body: some View { |
| 64 | VStack(spacing: 0) { |
| 65 | HStack { |
| 66 | Text(EquipoStrings.text("work.specialist.preview")).font(.headline) |
| 67 | Spacer() |
| 68 | Button(EquipoStrings.text("team.close"), action: dismiss).frame(minHeight: 44) |
| 69 | }.padding(.horizontal) |
| 70 | Divider() |
| 71 | PreviewWebView(web: web) |
| 72 | }.background(Palette.background) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #if os(iOS) |
| 77 | private struct PreviewWebView: UIViewRepresentable { |
| 78 | let web: WKWebView |
| 79 | func makeUIView(context: Context) -> WKWebView { web } |
| 80 | func updateUIView(_ view: WKWebView, context: Context) {} |
| 81 | } |
| 82 | #else |
| 83 | private struct PreviewWebView: NSViewRepresentable { |
| 84 | let web: WKWebView |
| 85 | func makeNSView(context: Context) -> WKWebView { web } |
| 86 | func updateNSView(_ view: WKWebView, context: Context) {} |
| 87 | } |
| 88 | #endif |
| 89 | |