mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
78 lines
3.4 KiB
Swift
78 lines
3.4 KiB
Swift
import Foundation
|
|
import TextFormat
|
|
import AccountContext
|
|
|
|
func chatTextInputAddFormattingAttribute(_ state: ChatTextInputState, attribute: NSAttributedString.Key) -> ChatTextInputState {
|
|
if !state.selectionRange.isEmpty {
|
|
let nsRange = NSRange(location: state.selectionRange.lowerBound, length: state.selectionRange.count)
|
|
var addAttribute = true
|
|
var attributesToRemove: [NSAttributedString.Key] = []
|
|
state.inputText.enumerateAttributes(in: nsRange, options: .longestEffectiveRangeNotRequired) { attributes, range, stop in
|
|
for (key, _) in attributes {
|
|
if key == attribute && range == nsRange {
|
|
addAttribute = false
|
|
attributesToRemove.append(key)
|
|
}
|
|
}
|
|
}
|
|
|
|
let result = NSMutableAttributedString(attributedString: state.inputText)
|
|
for attribute in attributesToRemove {
|
|
result.removeAttribute(attribute, range: nsRange)
|
|
}
|
|
if addAttribute {
|
|
result.addAttribute(attribute, value: true as Bool, range: nsRange)
|
|
}
|
|
return ChatTextInputState(inputText: result, selectionRange: state.selectionRange)
|
|
} else {
|
|
return state
|
|
}
|
|
}
|
|
|
|
func chatTextInputClearFormattingAttributes(_ state: ChatTextInputState) -> ChatTextInputState {
|
|
if !state.selectionRange.isEmpty {
|
|
let nsRange = NSRange(location: state.selectionRange.lowerBound, length: state.selectionRange.count)
|
|
var attributesToRemove: [NSAttributedString.Key] = []
|
|
state.inputText.enumerateAttributes(in: nsRange, options: .longestEffectiveRangeNotRequired) { attributes, range, stop in
|
|
for (key, _) in attributes {
|
|
attributesToRemove.append(key)
|
|
}
|
|
}
|
|
|
|
let result = NSMutableAttributedString(attributedString: state.inputText)
|
|
for attribute in attributesToRemove {
|
|
result.removeAttribute(attribute, range: nsRange)
|
|
}
|
|
return ChatTextInputState(inputText: result, selectionRange: state.selectionRange)
|
|
} else {
|
|
return state
|
|
}
|
|
}
|
|
|
|
func chatTextInputAddLinkAttribute(_ state: ChatTextInputState, url: String) -> ChatTextInputState {
|
|
if !state.selectionRange.isEmpty {
|
|
let nsRange = NSRange(location: state.selectionRange.lowerBound, length: state.selectionRange.count)
|
|
var linkRange = nsRange
|
|
var attributesToRemove: [(NSAttributedString.Key, NSRange)] = []
|
|
state.inputText.enumerateAttributes(in: nsRange, options: .longestEffectiveRangeNotRequired) { attributes, range, stop in
|
|
for (key, _) in attributes {
|
|
if key == ChatTextInputAttributes.textUrl {
|
|
attributesToRemove.append((key, range))
|
|
linkRange = linkRange.union(range)
|
|
} else {
|
|
attributesToRemove.append((key, nsRange))
|
|
}
|
|
}
|
|
}
|
|
|
|
let result = NSMutableAttributedString(attributedString: state.inputText)
|
|
for (attribute, range) in attributesToRemove {
|
|
result.removeAttribute(attribute, range: range)
|
|
}
|
|
result.addAttribute(ChatTextInputAttributes.textUrl, value: ChatTextInputTextUrlAttribute(url: url), range: nsRange)
|
|
return ChatTextInputState(inputText: result, selectionRange: state.selectionRange)
|
|
} else {
|
|
return state
|
|
}
|
|
}
|