From a9c9a02f0ce3d750b3d28093f4f052f5b644b1c2 Mon Sep 17 00:00:00 2001 From: Mak Ho-Cheung Date: Fri, 11 Mar 2022 22:06:58 +0800 Subject: [PATCH] feat: support readOnly option of CodeMirror close #7 --- Sources/CodeMirror-SwiftUI/Representables/CodeView.swift | 9 +++++++-- .../Representables/CodeViewController.swift | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/CodeMirror-SwiftUI/Representables/CodeView.swift b/Sources/CodeMirror-SwiftUI/Representables/CodeView.swift index 31d72fb..2adbb15 100644 --- a/Sources/CodeMirror-SwiftUI/Representables/CodeView.swift +++ b/Sources/CodeMirror-SwiftUI/Representables/CodeView.swift @@ -30,6 +30,7 @@ public struct CodeView: RepresentableView { var fontSize: Int var showInvisibleCharacters: Bool var lineWrapping: Bool + var readOnly: String var onLoadSuccess: (() -> ())? var onLoadFail: ((Error) -> ())? @@ -41,13 +42,16 @@ public struct CodeView: RepresentableView { mode: Mode, fontSize: Int = 12, showInvisibleCharacters: Bool = true, - lineWrapping: Bool = true) { + lineWrapping: Bool = true, + readOnly: String = "false" + ) { self._code = code self.mode = mode self.theme = theme self.fontSize = fontSize self.showInvisibleCharacters = showInvisibleCharacters self.lineWrapping = lineWrapping + self.readOnly = readOnly } @@ -150,7 +154,7 @@ extension CodeView { context.coordinator.setFontSize(fontSize) context.coordinator.setShowInvisibleCharacters(showInvisibleCharacters) context.coordinator.setLineWrapping(lineWrapping) - + context.coordinator.setReadonly(readOnly) return webView } @@ -163,6 +167,7 @@ extension CodeView { context.coordinator.setFontSize(fontSize) context.coordinator.setShowInvisibleCharacters(showInvisibleCharacters) context.coordinator.setLineWrapping(lineWrapping) + context.coordinator.setReadonly(readOnly) } func updateWhatsNecessary(elementGetter: (JavascriptCallback?) -> Void, diff --git a/Sources/CodeMirror-SwiftUI/Representables/CodeViewController.swift b/Sources/CodeMirror-SwiftUI/Representables/CodeViewController.swift index 06482ad..8a24cdd 100644 --- a/Sources/CodeMirror-SwiftUI/Representables/CodeViewController.swift +++ b/Sources/CodeMirror-SwiftUI/Representables/CodeViewController.swift @@ -184,8 +184,12 @@ extension CodeViewController { setMimeType("application/json") } - func setReadonly(_ value: Bool) { - callJavascript(javascriptString: "SetReadOnly(\(value));") + func setReadonly(_ value: String) { + var jsString = "SetReadOnly(\(value));" + if value == "nocursor" { + jsString = "SetReadOnly(\"\(value)\");" + } + callJavascript(javascriptString: jsString) } func getTextSelection(_ block: JavascriptCallback?) {