Skip to content

Commit 5becc6d

Browse files
committed
fix: Fix value binding issue in the editor
1 parent 0402e06 commit 5becc6d

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

Example/Example/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct ContentView: View {
5050
}
5151
var body: some View {
5252
VStack(spacing: 0) {
53-
TextField("Placeholder", text: $textForTextField)
53+
// TextField("Placeholder", text: $textForTextField)
5454
// ScrollView {
5555
// Text(value)
5656
// }

Example/Example/ExampleApp.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftUI
9+
import CodeMirror
910

1011
@main
1112
struct ExampleApp: App {
@@ -15,3 +16,12 @@ struct ExampleApp: App {
1516
}
1617
}
1718
}
19+
20+
struct ContentView2: View {
21+
@State var value: String = ""
22+
var body: some View {
23+
CodeMirror(value: $value, prompt: String(localized: "Please enter text"))
24+
.cmLanguage(.constant(.html))
25+
.cmLineWrapping(.constant(true))
26+
}
27+
}

Sources/CodeMirror/CodeMirrorView.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import WebKit
1616
public struct CodeMirrorView: NativeView {
1717
@ObservedObject public var vm: CodeMirrorVM
1818
@Binding var value: String
19-
@State var previewValue: String = ""
19+
var prevValue: String = ""
2020
public init(
2121
_ viewModel: CodeMirrorVM,
2222
value: Binding<String>
2323
) {
2424
self.vm = viewModel
2525
self._value = value
26-
self.previewValue = value.wrappedValue
26+
self.prevValue = value.wrappedValue
2727
}
2828
#if canImport(AppKit)
2929
public func makeNSView(context: Context) -> WKWebView {
@@ -125,7 +125,7 @@ public struct CodeMirrorView: NativeView {
125125
args: [:]
126126
)
127127
)
128-
if previewValue != value {
128+
if prevValue != value {
129129
context.coordinator.queueJavascriptFunction(
130130
JavascriptFunction(
131131
functionString: "CodeMirror.setContent(value)",
@@ -220,10 +220,9 @@ extension Coordinator: WKScriptMessageHandler {
220220
callPendingFunctions()
221221
case ScriptMessageName.codeMirrorContentDidChange:
222222
parent.vm.onContentChange?()
223-
Task {
224-
guard let content = try await parent.vm.getContent() else { return }
225-
parent.value = content
226-
parent.previewValue = content
223+
if let messageBody = message.body as? String {
224+
parent.value = messageBody
225+
parent.prevValue = messageBody
227226
}
228227
default:
229228
print("CodeMirrorWebView receive \(message.name) \(message.body)")

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.

Sources/CodeMirror/web.bundle/index.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
if (window.webkit) {
1818
window.webkit.messageHandlers.codeMirrorDidReady.postMessage(null);
1919
CodeMirror.setListener(() => {
20-
window.webkit.messageHandlers.codeMirrorContentDidChange.postMessage(
21-
null
22-
);
20+
let code = CodeMirror.getContent();
21+
window.webkit.messageHandlers.codeMirrorContentDidChange.postMessage(code);
2322
});
2423
}
2524
</script>

codemirrorjs/codemirror.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ const editorView = new CodeMirror.EditorView({
218218
lineNumber.of([]),
219219
baseTheme,
220220
theme.of(oneDark),
221-
language.of(json()),
221+
language.of(),
222+
// language.of(json()),
222223
listener.of([]),
223224
],
224225
parent: document.body,

0 commit comments

Comments
 (0)