diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-06 12:45:37 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-06 12:45:37 +0200 |
| commit | 54f89bcd1a7fb0b92b65d8c0ea3847d635e11133 (patch) | |
| tree | da6c1e3f015c4b21a2e32854cadb09e71f9079e7 /Map/Presentation/Base Components | |
| parent | 3cd8f1f7200109d505b5e105d5ebe53f0c93d337 (diff) | |
Allow adding and removing templates
Diffstat (limited to 'Map/Presentation/Base Components')
| -rw-r--r-- | Map/Presentation/Base Components/EvolutionPicker/EvolutionPickerMenu.swift | 1 | ||||
| -rw-r--r-- | Map/Presentation/Base Components/MapTextEditor.swift | 38 |
2 files changed, 28 insertions, 11 deletions
diff --git a/Map/Presentation/Base Components/EvolutionPicker/EvolutionPickerMenu.swift b/Map/Presentation/Base Components/EvolutionPicker/EvolutionPickerMenu.swift index 9478346..94a87d7 100644 --- a/Map/Presentation/Base Components/EvolutionPicker/EvolutionPickerMenu.swift +++ b/Map/Presentation/Base Components/EvolutionPicker/EvolutionPickerMenu.swift @@ -3,7 +3,6 @@ import SwiftUI struct EvolutionPickerMenu: View { @Binding var selectedEvolution: StageType - private func select(_ stageType: StageType) { selectedEvolution = stageType diff --git a/Map/Presentation/Base Components/MapTextEditor.swift b/Map/Presentation/Base Components/MapTextEditor.swift index 18b2fbc..4790a70 100644 --- a/Map/Presentation/Base Components/MapTextEditor.swift +++ b/Map/Presentation/Base Components/MapTextEditor.swift @@ -17,7 +17,8 @@ import SwiftUI class MapTextEditorController: NSViewController { - @Binding var document: MapDocument + @Binding var text: String + var highlightRanges: [Range<String.Index>] { didSet { updateHighlights() @@ -44,10 +45,10 @@ class MapTextEditorController: NSViewController { private let changeDebouncer: Debouncer = Debouncer(seconds: 1) init( - document: Binding<MapDocument>, highlightRanges: [Range<String.Index>], selectedRange: Int, + text: Binding<String>, highlightRanges: [Range<String.Index>], selectedRange: Int, onChange: @escaping () -> Void ) { - self._document = document + self._text = text self.onChange = onChange self.highlightRanges = highlightRanges self.selectedRange = selectedRange @@ -68,7 +69,7 @@ class MapTextEditorController: NSViewController { textView.allowsUndo = true textView.delegate = self textView.textStorage?.delegate = self - textView.string = self.document.text + textView.string = self.text textView.isEditable = true textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular) self.view = scrollView @@ -79,12 +80,12 @@ class MapTextEditorController: NSViewController { updateHighlights() } - private var textView: NSTextView? { + var currentTextView: NSTextView? { return (view as? NSScrollView)?.documentView as? NSTextView } private func updateHighlights() { - if let textView { + if let textView = currentTextView { if let textStorage = textView.textStorage { textStorage.removeAttribute( .backgroundColor, range: NSRange(location: 0, length: textStorage.length)) @@ -105,7 +106,7 @@ class MapTextEditorController: NSViewController { } private func focusOnResult() { - if let textView { + if let textView = currentTextView { if let textStorage = textView.textStorage { if selectedRange < highlightRanges.count { let range = highlightRanges[selectedRange] @@ -121,7 +122,7 @@ extension MapTextEditorController: NSTextViewDelegate { func textDidChange(_ obj: Notification) { if let textField = obj.object as? NSTextView { - self.document.text = textField.string + self.text = textField.string changeDebouncer.debounce { DispatchQueue.main.async { @@ -235,16 +236,26 @@ extension MapTextEditorController: NSTextStorageDelegate { struct MapTextEditor: NSViewControllerRepresentable { - @Binding var document: MapDocument + @Binding var text: String var highlightRanges: [Range<String.Index>] var selectedRange: Int var onChange: () -> Void = {} + init( + text: Binding<String>, highlightRanges: [Range<String.Index>] = [], selectedRange: Int = 0, + onChange: @escaping () -> Void = {} + ) { + self._text = text + self.highlightRanges = highlightRanges + self.selectedRange = selectedRange + self.onChange = onChange + } + func makeNSViewController( context: NSViewControllerRepresentableContext<MapTextEditor> ) -> MapTextEditorController { return MapTextEditorController( - document: $document, highlightRanges: highlightRanges, selectedRange: selectedRange, + text: $text, highlightRanges: highlightRanges, selectedRange: selectedRange, onChange: onChange) } @@ -256,5 +267,12 @@ struct MapTextEditor: NSViewControllerRepresentable { if nsViewController.selectedRange != selectedRange { nsViewController.selectedRange = selectedRange } + + // Update text view content if text has changed + if let textView = nsViewController.currentTextView { + if textView.string != text { + textView.string = text + } + } } } |