From 92446ea9e52c05e7b44a8e7f15fe08936ab6187e Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Tue, 4 Oct 2022 12:06:27 +0200 Subject: [PATCH] fix: keep safe update the characters --- PlantUML/PlantUMLEditorView.swift | 21 +++++++++++++++---- .../PlantUMLTextField+Representable.swift | 13 ++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/PlantUML/PlantUMLEditorView.swift b/PlantUML/PlantUMLEditorView.swift index ec57901..ee85960 100644 --- a/PlantUML/PlantUMLEditorView.swift +++ b/PlantUML/PlantUMLEditorView.swift @@ -162,21 +162,34 @@ extension PlantUMLEditorView { document.text = diagram.description } + @available(*, deprecated, message: "no longer valid!") internal func indexFromFocusedItem() -> Int? { + logger.trace( "indexFromFocusedItem" ) + + var result:Int? = nil + if case .row(let id) = focusedItem { - return diagram.items.firstIndex { $0.id == id } + result = diagram.items.firstIndex { $0.id == id } + logger.trace( "indexFromFocusedItem: \(result ?? -1 )" ) + + return result } - return nil + + return result } - func updateItem( newValue value: String, additionalValues values: [String]? ) { - guard let offset = indexFromFocusedItem() else { + func updateItem( item: SyntaxStructure, withValue value: String, andAdditionalValues values: [String]? ) { + + guard let offset = diagram.items.firstIndex( where: { $0.id == item.id } ) else { + logger.debug( "update failed!" ) return } diagram.items[ offset ].rawValue = value + logger.debug( "update at \(offset): \(value)" ) + if let values = values { addItemsBelow(theOffset: offset, values: values) } diff --git a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift index 33b6d50..51cf7b7 100644 --- a/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift +++ b/PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLTextField+Representable.swift @@ -16,7 +16,7 @@ public enum AppendActionPosition { } public struct PlantUMLTextFieldWithCustomKeyboard : UIViewRepresentable { - public typealias ChangeHandler = ( String, [String]? ) -> Void + public typealias ChangeHandler = ( SyntaxStructure, String, [String]? ) -> Void public typealias AddNewActionHandler = ( SyntaxStructure, AppendActionPosition, String? ) -> Void public typealias UIViewType = UITextField private let textField = UITextField() @@ -88,8 +88,6 @@ extension PlantUMLTextFieldWithCustomKeyboard { } self.customKeyboardMinHeight = max( self.customKeyboardMinHeight, rect.size.height) - - // print( "keyboardWillShowNotification", self.minimumHeight ) return rect } @@ -137,9 +135,10 @@ extension PlantUMLTextFieldWithCustomKeyboard { } public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { - // print( "shouldChangeCharactersIn", range, string ) if let text = textField.text, let range = Range(range, in: text) { - owner.onChange( text.replacingCharacters(in: range, with: string), nil ) + owner.onChange( owner.item, + text.replacingCharacters(in: range, with: string), + nil ) } return true @@ -171,7 +170,7 @@ extension PlantUMLTextFieldWithCustomKeyboard { } @objc public func toggleCustomKeyobard() { - print("toggleCustomKeyobard:", showCustomKeyboard) + logger.trace( "toggleCustomKeyobard: \(self.showCustomKeyboard)" ) showCustomKeyboard.toggle() @@ -193,7 +192,7 @@ extension PlantUMLTextFieldWithCustomKeyboard { owner.textField.replace(range, withText: symbol.value ) if let text = owner.textField.text { owner.textField.sendActions(for: .valueChanged) - owner.onChange( text, symbol.additionalValues ) + owner.onChange( owner.item, text, symbol.additionalValues ) toggleCustomKeyobard() } }