Skip to content

Commit 58dca9f

Browse files
committed
fix: Fix focus setting issue.
1 parent 1bf66b9 commit 58dca9f

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

Example/Example/ContentView.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ struct ContentView: View {
4343
@State var theme: Themes = .vscodedark
4444
@State var enabledSearch = false
4545
@State var count: Int = 0
46+
@FocusState var input: InputFocused?
47+
@State private var textForTextField: String = ""
48+
enum InputFocused: Hashable {
49+
case text, output, test
50+
}
4651
var body: some View {
47-
VStack {
52+
VStack(spacing: 0) {
4853
// ScrollView {
4954
// Text(value)
5055
// }
@@ -66,6 +71,7 @@ struct ContentView: View {
6671
.onContentChange {
6772
print("@@@3 Content Did Change")
6873
}
74+
.focused($input, equals: .text)
6975
.frame(maxWidth: .infinity, maxHeight: .infinity)
7076
.safeAreaInset(edge: .bottom, spacing: 0) {
7177
HStack {
@@ -93,6 +99,26 @@ struct ContentView: View {
9399
} label: {
94100
Text("SET")
95101
}
102+
Button {
103+
self.input = .output
104+
} label: {
105+
Text("Focused1")
106+
}
107+
Button {
108+
self.input = .text
109+
} label: {
110+
Text("Focused text")
111+
}
112+
Button {
113+
self.input = .test
114+
} label: {
115+
Text("Focused3")
116+
}
117+
Button {
118+
self.input = nil
119+
} label: {
120+
Text("Focused4")
121+
}
96122
Spacer()
97123
Picker("Lang", selection: $language) {
98124
ForEach(Language.allCases, id: \.rawValue) {
@@ -113,6 +139,9 @@ struct ContentView: View {
113139
.padding(.vertical, 6)
114140
}
115141
}
142+
.onAppear() {
143+
input = .text
144+
}
116145
}
117146
}
118147

Sources/CodeMirror/CodeMirror.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ import SwiftUI
1010
public struct CodeMirror: View {
1111
@ObservedObject var vm: CodeMirrorVM = .init()
1212
@Binding var value: String
13+
@FocusState private var isFocused: Bool
1314
public init(value: Binding<String>) {
1415
self._value = value
1516
}
1617
public var body: some View {
1718
CodeMirrorView(vm, value: $value)
19+
.focused($isFocused)
20+
.onChange(of: isFocused, initial: true) { old, val in
21+
if old != val {
22+
vm.focused = val
23+
}
24+
}
1825
}
1926
/// Set Line Wrapping
2027
public func cmLineWrapping(_ value: Binding<Bool>) -> CodeMirror {

Sources/CodeMirror/CodeMirrorVM.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public class CodeMirrorVM: ObservableObject {
9191
@Published public var language: Language = .json
9292
@Published public var theme: Themes = .vscodelight
9393

94+
@Published public var focused = false
95+
9496
public var onLoadSuccess: (() -> Void)?
9597
public var onLoadFailed: ((Error) -> Void)?
9698
public var onContentChange: (() -> Void)?

Sources/CodeMirror/CodeMirrorView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public struct CodeMirrorView: NativeView {
113113
args: ["value": vm.language.rawValue]
114114
)
115115
)
116+
context.coordinator.queueJavascriptFunction(
117+
JavascriptFunction(
118+
functionString: vm.focused == true ? "CodeMirror.setFocus()" : "CodeMirror.setBlur()",
119+
args: [:]
120+
)
121+
)
116122
if previewValue != value {
117123
context.coordinator.queueJavascriptFunction(
118124
JavascriptFunction(

Sources/CodeMirror/web.bundle/codemirror.bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codemirrorjs/codemirror.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ function setFoldGutter(enabled) {
296296
});
297297
}
298298

299+
function setFocus() {
300+
editorView.focus();
301+
}
302+
303+
function setBlur() {
304+
editorView.dom.blur()
305+
}
306+
299307
export {
300308
setLanguage,
301309
getSupportedLanguages,
@@ -308,5 +316,7 @@ export {
308316
setLineNumber,
309317
setFoldGutter,
310318
setEnabledSearch,
319+
setFocus,
320+
setBlur,
311321
editorView,
312322
};

0 commit comments

Comments
 (0)