]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapTextEditor.swift
f3838a663aa83d63dc22bf688326b93176a704a5
[rbdr/map] / Map / Presentation / Base Components / MapTextEditor.swift
1 import Cocoa
2 import SwiftUI
3
4 class MapTextEditorController: NSViewController {
5
6 @Binding var text: String
7 let onChange: () -> Void
8
9 private let vertexRegex = MapParsingPatterns.vertex
10 private let edgeRegex = MapParsingPatterns.edge
11 private let blockerRegex = MapParsingPatterns.blocker
12 private let opportunityRegex = MapParsingPatterns.opportunity
13 private let noteRegex = MapParsingPatterns.note
14 private let stageRegex = MapParsingPatterns.stage
15
16 private let changeDebouncer: Debouncer = Debouncer(seconds: 1)
17
18 init(text: Binding<String>, onChange: @escaping () -> Void) {
19 self._text = text
20 self.onChange = onChange
21 super.init(nibName: nil, bundle: nil)
22 }
23
24 required init?(coder: NSCoder) {
25 fatalError("init(coder:) has not been implemented")
26 }
27
28 override func loadView() {
29 let scrollView = NSTextView.scrollableTextView()
30 let textView = scrollView.documentView as! NSTextView
31
32 scrollView.translatesAutoresizingMaskIntoConstraints = false
33
34 textView.allowsUndo = true
35 textView.delegate = self
36 textView.textStorage?.delegate = self
37 textView.string = self.text
38 textView.isEditable = true
39 textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
40 self.view = scrollView
41 }
42
43 override func viewDidAppear() {
44 self.view.window?.makeFirstResponder(self.view)
45 }
46 }
47
48 extension MapTextEditorController: NSTextViewDelegate {
49
50 func textDidChange(_ obj: Notification) {
51 if let textField = obj.object as? NSTextView {
52 self.text = textField.string
53
54
55 changeDebouncer.debounce {
56 DispatchQueue.main.async {
57 self.onChange()
58 }
59 }
60 }
61 }
62
63 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
64 {
65 let range = Range(shouldChangeTextIn, in: view.string)
66 let target = view.string[range!]
67
68 if target == "--" {
69 return false
70 }
71
72 return true
73 }
74 }
75
76 extension MapTextEditorController: NSTextStorageDelegate {
77
78 override func textStorageDidProcessEditing(_ obj: Notification) {
79 if let textStorage = obj.object as? NSTextStorage {
80 self.colorizeText(textStorage: textStorage)
81 }
82 }
83
84 private func colorizeText(textStorage: NSTextStorage) {
85 let range = NSMakeRange(0, textStorage.length)
86 var matches = vertexRegex.matches(in: textStorage.string, options: [], range: range)
87
88 for match in matches {
89 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
90 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
91 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
92 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 4))
93 }
94
95 matches = edgeRegex.matches(in: textStorage.string, options: [], range: range)
96
97 for match in matches {
98 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
99 let arrowRange = match.range(at: 2)
100 textStorage.addAttributes(
101 [.foregroundColor: NSColor.syntax.symbol],
102 range: NSMakeRange(arrowRange.lowerBound - 1, arrowRange.length + 1))
103 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 3))
104 }
105
106 matches = opportunityRegex.matches(in: textStorage.string, options: [], range: range)
107
108 for match in matches {
109 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
110 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
111 textStorage.addAttributes([.foregroundColor: NSColor.syntax.symbol], range: match.range(at: 3))
112 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 4))
113 }
114
115 matches = blockerRegex.matches(in: textStorage.string, options: [], range: range)
116
117 for match in matches {
118 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
119 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
120 }
121
122 matches = noteRegex.matches(in: textStorage.string, options: [], range: range)
123
124 for match in matches {
125 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
126 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
127 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
128 }
129
130 matches = stageRegex.matches(in: textStorage.string, options: [], range: range)
131
132 for match in matches {
133 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
134 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
135 }
136 }
137 }
138
139 struct MapTextEditor: NSViewControllerRepresentable {
140
141 @Binding var text: String
142 var onChange: () -> Void = {}
143
144 func makeNSViewController(
145 context: NSViewControllerRepresentableContext<MapTextEditor>
146 ) -> MapTextEditorController {
147 return MapTextEditorController(text: $text, onChange: onChange)
148 }
149
150 func updateNSViewController(
151 _ nsViewController: MapTextEditorController,
152 context: NSViewControllerRepresentableContext<MapTextEditor>
153 ) {}
154 }