forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffWebView.swift
More file actions
185 lines (160 loc) · 7.07 KB
/
DiffWebView.swift
File metadata and controls
185 lines (160 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import ComposableArchitecture
import ChatService
import SwiftUI
import WebKit
import Logger
import ChatAPIService
struct DiffWebView: NSViewRepresentable {
@Perception.Bindable var chat: StoreOf<Chat>
var fileEdit: FileEdit
init(chat: StoreOf<Chat>, fileEdit: FileEdit) {
self.chat = chat
self.fileEdit = fileEdit
}
func makeNSView(context: Context) -> WKWebView {
let configuration = WKWebViewConfiguration()
let userContentController = WKUserContentController()
#if DEBUG
let scriptSource = """
function captureLog(msg) { window.webkit.messageHandlers.logging.postMessage(Array.prototype.slice.call(arguments)); }
console.log = captureLog;
console.error = captureLog;
console.warn = captureLog;
console.info = captureLog;
"""
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentStart, forMainFrameOnly: true)
userContentController.addUserScript(script)
userContentController.add(context.coordinator, name: "logging")
#endif
userContentController.add(context.coordinator, name: "swiftHandler")
configuration.userContentController = userContentController
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = context.coordinator
#if DEBUG
webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
#endif
// Configure WebView
webView.wantsLayer = true
webView.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
webView.layer?.borderWidth = 1
// Make the webview auto-resize with its container
webView.autoresizingMask = [.width, .height]
webView.translatesAutoresizingMaskIntoConstraints = true
// Notify the webview of resize events explicitly
let resizeNotificationScript = WKUserScript(
source: """
window.addEventListener('resize', function() {
if (window.DiffViewer && window.DiffViewer.handleResize) {
window.DiffViewer.handleResize();
}
});
""",
injectionTime: .atDocumentEnd,
forMainFrameOnly: true
)
webView.configuration.userContentController.addUserScript(resizeNotificationScript)
/// Load web asset resources
let bundleBaseURL = Bundle.main.bundleURL.appendingPathComponent("Contents/Resources/webViewDist/diffView")
let htmlFileURL = bundleBaseURL.appendingPathComponent("diffView.html")
webView.loadFileURL(htmlFileURL, allowingReadAccessTo: bundleBaseURL)
return webView
}
func updateNSView(_ webView: WKWebView, context: Context) {
if context.coordinator.shouldUpdate(fileEdit) {
// Update content via JavaScript API
let script = """
if (typeof window.DiffViewer !== 'undefined') {
window.DiffViewer.update(
`\(escapeJSString(fileEdit.originalContentByStatus))`,
`\(escapeJSString(fileEdit.modifiedContentByStatus))`,
`\(escapeJSString(fileEdit.fileURL.absoluteString))`,
`\(fileEdit.status.rawValue)`
);
} else {
console.error("DiffViewer is not defined in update");
}
"""
webView.evaluateJavaScript(script)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var parent: DiffWebView
private var fileEdit: FileEdit
init(_ parent: DiffWebView) {
self.parent = parent
self.fileEdit = parent.fileEdit
}
func shouldUpdate(_ fileEdit: FileEdit) -> Bool {
let shouldUpdate = self.fileEdit != fileEdit
if shouldUpdate {
self.fileEdit = fileEdit
}
return shouldUpdate
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
#if DEBUG
if message.name == "logging" {
if let logs = message.body as? [Any] {
let logString = logs.map { "\($0)" }.joined(separator: " ")
Logger.client.info("WebView console: \(logString)")
}
return
}
#endif
guard message.name == "swiftHandler",
let body = message.body as? [String: Any],
let event = body["event"] as? String,
let data = body["data"] as? [String: String],
let filePath = data["filePath"],
let fileURL = URL(string: filePath)
else { return }
switch event {
case "undoButtonClicked":
self.parent.chat.send(.undoEdits(fileURLs: [fileURL]))
case "keepButtonClicked":
self.parent.chat.send(.keepEdits(fileURLs: [fileURL]))
default:
break
}
}
// Initialize content when the page has finished loading
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let script = """
if (typeof window.DiffViewer !== 'undefined') {
window.DiffViewer.init(
`\(escapeJSString(fileEdit.originalContentByStatus))`,
`\(escapeJSString(fileEdit.modifiedContentByStatus))`,
`\(escapeJSString(fileEdit.fileURL.absoluteString))`,
`\(fileEdit.status.rawValue)`
);
} else {
console.error("DiffViewer is not defined on page load");
}
"""
webView.evaluateJavaScript(script) { result, error in
if let error = error {
Logger.client.error("Error evaluating JavaScript: \(error)")
}
}
}
// Handle navigation errors
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
Logger.client.error("WebView navigation failed: \(error)")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
Logger.client.error("WebView provisional navigation failed: \(error)")
}
}
}
func escapeJSString(_ string: String) -> String {
return string
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "`", with: "\\`")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\r", with: "\\r")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "$", with: "\\$")
}