4 class MapTextEditorController: NSViewController {
6 @Binding var text: String
8 init(text: Binding<String>) {
10 super.init(nibName: nil, bundle: nil)
13 required init?(coder: NSCoder) {
14 fatalError("init(coder:) has not been implemented")
17 override func loadView() {
18 let scrollView = NSTextView.scrollableTextView()
19 let textView = scrollView.documentView as! NSTextView
21 scrollView.translatesAutoresizingMaskIntoConstraints = false
23 textView.delegate = self
24 textView.string = self.text
25 textView.isEditable = true
26 textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
27 self.view = scrollView
30 override func viewDidAppear() {
31 self.view.window?.makeFirstResponder(self.view)
35 extension MapTextEditorController: NSTextViewDelegate {
37 func textDidChange(_ obj: Notification) {
38 if let textField = obj.object as? NSTextView {
39 self.text = textField.string
43 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
45 let range = Range(shouldChangeTextIn, in: view.string)
46 let target = view.string[range!]
56 struct MapTextEditor: NSViewControllerRepresentable {
58 @Binding var text: String
60 func makeNSViewController(
61 context: NSViewControllerRepresentableContext<MapTextEditor>
62 ) -> MapTextEditorController {
63 return MapTextEditorController(text: $text)
66 func updateNSViewController(
67 _ nsViewController: MapTextEditorController,
68 context: NSViewControllerRepresentableContext<MapTextEditor>