Skip to content

Commit 093777f

Browse files
committed
feat: onContentChange event returns the modified text.
1 parent 157b12b commit 093777f

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
lines changed

Example/Example/ContentView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct ContentView: View {
5050
.onLoadFailed { error in
5151
print("@@@2 \(#function) \(error)")
5252
}
53-
.onContentChange {
54-
print("@@@3 Content Did Change")
53+
.onContentChange { value in
54+
print("@@@3 Content Did Change", value)
5555
}
5656
.focused($input, equals: .text)
5757
.frame(maxWidth: .infinity, maxHeight: .infinity)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct ContentView: View {
8282
.onLoadFailed { error in
8383
print("@@@2 \(#function) \(error)")
8484
}
85-
.onContentChange {
85+
.onContentChange { value in
8686
print("@@@3 Content Did Change")
8787
}
8888
}
@@ -299,7 +299,7 @@ ToolbarItem {
299299
onLoadFailed: { error in
300300
print("@@@2 \(#function) \(error)")
301301
},
302-
onContentChange: {
302+
onContentChange: { value in
303303
print("@@@3 Content Did Change")
304304
}
305305
)

Sources/CodeMirror/CodeMirror.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public struct CodeMirror: View {
6767
vm.onLoadFailed = action
6868
return self as CodeMirror
6969
}
70-
public func onContentChange(perform action: (() -> Void)? = nil) -> CodeMirror {
70+
public func onContentChange(perform action: ((_ value: String) -> Void)? = nil) -> CodeMirror {
7171
vm.onContentChange = action
7272
return self as CodeMirror
7373
}

Sources/CodeMirror/CodeMirrorVM.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class CodeMirrorVM: ObservableObject {
9696

9797
public var onLoadSuccess: (() -> Void)?
9898
public var onLoadFailed: ((Error) -> Void)?
99-
public var onContentChange: (() -> Void)?
99+
public var onContentChange: ((_ value: String) -> Void)?
100100

101101
internal var executeJS: ((JavascriptFunction, JavascriptCallback?) -> Void)!
102102

Sources/CodeMirror/CodeMirrorView.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import WebKit
1616
public struct CodeMirrorView: NativeView {
1717
@ObservedObject public var vm: CodeMirrorVM
1818
@Binding var value: String
19-
var prevValue: String = ""
2019
public init(
2120
_ viewModel: CodeMirrorVM,
2221
value: Binding<String>
@@ -124,14 +123,12 @@ public struct CodeMirrorView: NativeView {
124123
args: [:]
125124
)
126125
)
127-
if prevValue != value {
128-
context.coordinator.queueJavascriptFunction(
129-
JavascriptFunction(
130-
functionString: "CodeMirror.setContent(value)",
131-
args: ["value": value]
132-
)
126+
context.coordinator.queueJavascriptFunction(
127+
JavascriptFunction(
128+
functionString: "CodeMirror.setContent(value)",
129+
args: ["value": value]
133130
)
134-
}
131+
)
135132
}
136133
public func makeCoordinator() -> Coordinator {
137134
let coordinator = Coordinator(parent: self, viewModel: vm)
@@ -218,10 +215,9 @@ extension Coordinator: WKScriptMessageHandler {
218215
pageLoaded = true
219216
callPendingFunctions()
220217
case ScriptMessageName.codeMirrorContentDidChange:
221-
parent.vm.onContentChange?()
222218
if let messageBody = message.body as? String {
223219
parent.value = messageBody
224-
parent.prevValue = messageBody
220+
parent.vm.onContentChange?(messageBody)
225221
}
226222
default:
227223
print("CodeMirrorWebView receive \(message.name) \(message.body)")

0 commit comments

Comments
 (0)