Skip to content

Commit

Permalink
fix: keep safe update the characters
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Oct 4, 2022
1 parent bdd83d7 commit 92446ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 17 additions & 4 deletions PlantUML/PlantUMLEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -88,8 +88,6 @@ extension PlantUMLTextFieldWithCustomKeyboard {
}

self.customKeyboardMinHeight = max( self.customKeyboardMinHeight, rect.size.height)

// print( "keyboardWillShowNotification", self.minimumHeight )

return rect
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -171,7 +170,7 @@ extension PlantUMLTextFieldWithCustomKeyboard {
}

@objc public func toggleCustomKeyobard() {
print("toggleCustomKeyobard:", showCustomKeyboard)
logger.trace( "toggleCustomKeyobard: \(self.showCustomKeyboard)" )

showCustomKeyboard.toggle()

Expand All @@ -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()
}
}
Expand Down

0 comments on commit 92446ea

Please sign in to comment.