-
Notifications
You must be signed in to change notification settings - Fork 1
/
HyperTexiOS.swift
103 lines (90 loc) · 2.84 KB
/
HyperTexiOS.swift
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
import SwiftUI
import WebKit
@main
struct HyperTexApp: App {
@State private var fileURL: URL?
@State private var urlString: String = ""
var body: some Scene {
WindowGroup {
ContentView(fileURL: $fileURL, urlString: $urlString)
}
.commands {
/* CommandMenu("DEBUG") {
Button("MORE DEBUG") { print("triggered debug") }
} */
CommandGroup(replacing: .newItem) {
Button("Open") { openFile() }
}
}
}
private func openFile() {
let openPanel = NSOpenPanel()
openPanel.allowedContentTypes = [.html]
openPanel.begin { result in
if result == .OK, let selectedFileURL = openPanel.url {
self.fileURL = selectedFileURL
}
}
}
}
struct ContentView: View {
@Binding var fileURL: URL?
@Binding var urlString: String
var body: some View {
VStack {
WebView(fileURL: $fileURL)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.toolbar {
/* ToolbarItem(placement: .primaryAction) {
TextField("Enter a file path", text: $urlString, onCommit: {
if let url = URL(string: urlString) {
self.fileURL = url
}
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(width: 256)
} */
ToolbarItem(placement: .primaryAction) {
Button("Open") { openFile() }
}
/* ToolbarItem(placement: .primaryAction) {
Button("Refresh") { fileURL = fileURL }
.disabled(fileURL == nil)
}
ToolbarItem(placement: .primaryAction) {
Button("Inspector") { openInTextEditor() }
.disabled(fileURL == nil)
}
ToolbarItem(placement: .primaryAction) {
Button("DEBUG") {print("triggered debug")}
} */
}
}
private func openFile() {
let openPanel = NSOpenPanel()
openPanel.allowedContentTypes = [.html]
openPanel.begin { result in
if result == .OK, let selectedFileURL = openPanel.url {
self.fileURL = selectedFileURL
}
}
}
private func openInTextEditor() {
if let fileURL = fileURL {
NSWorkspace.shared.open(fileURL)
}
}
}
struct WebView: NSViewRepresentable {
@Binding var fileURL: URL?
func makeNSView(context: Context) -> WKWebView {
let webView = WKWebView()
return webView
}
func updateNSView(_ nsView: WKWebView, context: Context) {
if let fileURL = fileURL {
nsView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
}
}
}