@@ -155,6 +155,7 @@ struct WebUIView: NSViewRepresentable {
155
155
nsView. load ( r)
156
156
}
157
157
}
158
+
158
159
}
159
160
160
161
class Coordinator : NSObject , WKNavigationDelegate {
@@ -189,6 +190,70 @@ struct WebUIView: NSViewRepresentable {
189
190
}
190
191
}
191
192
193
+ struct WebUIViewWithSave : NSViewRepresentable {
194
+ var urlStr : String = " "
195
+ var html : String = " "
196
+ var baseURLStr : String = " "
197
+
198
+ @Binding var savingDataTrigger : Bool
199
+ @Binding var savingData : Data ?
200
+
201
+ func makeCoordinator( ) -> Coordinator {
202
+ Coordinator ( )
203
+ }
204
+
205
+ func makeNSView( context: Context ) -> some WKWebView {
206
+ let webView = WKWebView ( )
207
+ webView. navigationDelegate = context. coordinator
208
+ return webView
209
+ }
210
+
211
+ func updateNSView( _ nsView: NSViewType , context: Context ) {
212
+ if savingData != nil {
213
+ if let data = savingData {
214
+ nsView. load ( data, mimeType: " application/x-webarchive " , characterEncodingName: " utf-8 " , baseURL: getDocumentsDirectory ( ) )
215
+ }
216
+ } else if urlStr. isEmpty {
217
+ let host = URL ( string: baseURLStr) ? . host ?? " "
218
+ nsView. loadHTMLString ( html, baseURL: URL ( string: " https:// \( host) " ) )
219
+ } else {
220
+ if let url = URL ( string: urlStr) {
221
+ let r = URLRequest ( url: url)
222
+ nsView. load ( r)
223
+ }
224
+ }
225
+
226
+ if savingDataTrigger == true {
227
+ nsView. createWebArchiveData { result in
228
+ do {
229
+ let data = try result. get ( )
230
+ savingData = data
231
+ } catch {
232
+ print ( " 创建 webarchivedata 数据失败, \( error) " )
233
+ }
234
+ }
235
+ savingDataTrigger = false
236
+ }
237
+ }
238
+
239
+ class Coordinator : NSObject , WKNavigationDelegate {
240
+
241
+ func webView( _ webView: WKWebView , decidePolicyFor navigationAction: WKNavigationAction , decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void ) {
242
+ if navigationAction. navigationType == . linkActivated {
243
+ if let url = navigationAction. request. url {
244
+ let components = URLComponents ( url: url, resolvingAgainstBaseURL: false )
245
+ if components? . scheme == " http " || components? . scheme == " https " {
246
+ NSWorkspace . shared. open ( url)
247
+ decisionHandler ( . cancel)
248
+ return
249
+ }
250
+ }
251
+ }
252
+ decisionHandler ( . allow)
253
+ }
254
+ } // end Coordinator
255
+ }
256
+
192
257
// MARK: - Time
193
258
struct GitHubApiTimeView : View {
194
259
var timeStr : String
0 commit comments