Neon-Vision-Editor/Neon Vision Editor/UI/MarkdownPreviewWebView.swift

33 lines
1,010 B
Swift

#if os(macOS)
import SwiftUI
import WebKit
struct MarkdownPreviewWebView: NSViewRepresentable {
let html: String
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeNSView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
configuration.defaultWebpagePreferences.allowsContentJavaScript = false
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.setValue(false, forKey: "drawsBackground")
webView.allowsBackForwardNavigationGestures = false
webView.loadHTMLString(html, baseURL: nil)
context.coordinator.lastHTML = html
return webView
}
func updateNSView(_ webView: WKWebView, context: Context) {
guard context.coordinator.lastHTML != html else { return }
webView.loadHTMLString(html, baseURL: nil)
context.coordinator.lastHTML = html
}
final class Coordinator {
var lastHTML: String = ""
}
}
#endif