]> git.r.bdr.sh - rbdr/map/blob - Map/Views/MapTextEditor.swift
Add code for first release
[rbdr/map] / Map / Views / MapTextEditor.swift
1 import Cocoa
2 import SwiftUI
3
4 class MapTextEditorController: NSViewController {
5
6 @Binding var text: String
7
8 init(text: Binding<String>) {
9 self._text = text
10 super.init(nibName: nil, bundle: nil)
11 }
12
13 required init?(coder: NSCoder) {
14 fatalError("init(coder:) has not been implemented")
15 }
16
17 override func loadView() {
18 let scrollView = NSTextView.scrollableTextView()
19 let textView = scrollView.documentView as! NSTextView
20
21 scrollView.translatesAutoresizingMaskIntoConstraints = false
22
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
28 }
29
30 override func viewDidAppear() {
31 self.view.window?.makeFirstResponder(self.view)
32 }
33 }
34
35 extension MapTextEditorController: NSTextViewDelegate {
36
37 func textDidChange(_ obj: Notification) {
38 if let textField = obj.object as? NSTextView {
39 self.text = textField.string
40 }
41 }
42
43 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
44 {
45 let range = Range(shouldChangeTextIn, in: view.string)
46 let target = view.string[range!]
47
48 if target == "--" {
49 return false
50 }
51
52 return true
53 }
54 }
55
56 struct MapTextEditor: NSViewControllerRepresentable {
57
58 @Binding var text: String
59
60 func makeNSViewController(
61 context: NSViewControllerRepresentableContext<MapTextEditor>
62 ) -> MapTextEditorController {
63 return MapTextEditorController(text: $text)
64 }
65
66 func updateNSViewController(
67 _ nsViewController: MapTextEditorController,
68 context: NSViewControllerRepresentableContext<MapTextEditor>
69 ) {
70 }
71 }