refactor: cleanup processFocusedWordForMention

This commit is contained in:
William Casarin
2023-06-28 17:08:28 +02:00
parent 3b07a207c4
commit c8f18958a2

View File

@@ -70,17 +70,21 @@ struct TextViewWrapper: UIViewRepresentable {
} }
private func processFocusedWordForMention(textView: UITextView) { private func processFocusedWordForMention(textView: UITextView) {
if let selectedRange = textView.selectedTextRange { var val: (String?, NSRange?) = (nil, nil)
var val: (String?, NSRange?)
if let wordRange = textView.tokenizer.rangeEnclosingPosition(selectedRange.start, with: .word, inDirection: .init(rawValue: UITextLayoutDirection.left.rawValue)) { guard let selectedRange = textView.selectedTextRange else { return }
if let startPosition = textView.position(from: wordRange.start, offset: -1),
let cursorPosition = textView.position(from: selectedRange.start, offset: 0) { let wordRange = textView.tokenizer.rangeEnclosingPosition(selectedRange.start, with: .word, inDirection: .init(rawValue: UITextLayoutDirection.left.rawValue))
let word = textView.text(in: textView.textRange(from: startPosition, to: cursorPosition)!)
val = (word, convertToNSRange(startPosition, cursorPosition, textView)) if let wordRange,
} let startPosition = textView.position(from: wordRange.start, offset: -1),
} let cursorPosition = textView.position(from: selectedRange.start, offset: 0)
getFocusWordForMention?(val.0, val.1) {
let word = textView.text(in: textView.textRange(from: startPosition, to: cursorPosition)!)
val = (word, convertToNSRange(startPosition, cursorPosition, textView))
} }
getFocusWordForMention?(val.0, val.1)
} }
private func convertToNSRange( _ startPosition: UITextPosition, _ endPosition: UITextPosition, _ textView: UITextView) -> NSRange? { private func convertToNSRange( _ startPosition: UITextPosition, _ endPosition: UITextPosition, _ textView: UITextView) -> NSRange? {