Quote improvements

This commit is contained in:
Isaac
2024-05-24 15:52:01 +04:00
parent 5fe5812328
commit 3f7113797e
12 changed files with 199 additions and 94 deletions

View File

@@ -402,6 +402,7 @@ public enum ChatTextInputStateTextAttributeType: Codable, Equatable {
case spoiler
case quote(isCollapsed: Bool)
case codeBlock(language: String?)
case collapsedQuote(text: ChatTextInputStateText)
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
@@ -433,6 +434,8 @@ public enum ChatTextInputStateTextAttributeType: Codable, Equatable {
self = .quote(isCollapsed: try container.decodeIfPresent(Bool.self, forKey: "isCollapsed") ?? false)
case 10:
self = .codeBlock(language: try container.decodeIfPresent(String.self, forKey: "l"))
case 11:
self = .collapsedQuote(text: try container.decode(ChatTextInputStateText.self, forKey: "text"))
default:
assertionFailure()
self = .bold
@@ -470,6 +473,9 @@ public enum ChatTextInputStateTextAttributeType: Codable, Equatable {
case let .codeBlock(language):
try container.encode(10 as Int32, forKey: "t")
try container.encodeIfPresent(language, forKey: "l")
case let .collapsedQuote(text):
try container.encode(11 as Int32, forKey: "t")
try container.encode(text, forKey: "text")
}
}
}
@@ -522,6 +528,7 @@ public struct ChatTextInputStateText: Codable, Equatable {
public init(attributedText: NSAttributedString) {
self.text = attributedText.string
var parsedAttributes: [ChatTextInputStateTextAttribute] = []
attributedText.enumerateAttributes(in: NSRange(location: 0, length: attributedText.length), options: [], using: { attributes, range, _ in
for (key, value) in attributes {
@@ -550,6 +557,8 @@ public struct ChatTextInputStateText: Codable, Equatable {
case let .code(language):
parsedAttributes.append(ChatTextInputStateTextAttribute(type: .codeBlock(language: language), range: range.location ..< (range.location + range.length)))
}
} else if key == ChatTextInputAttributes.collapsedBlock, let value = value as? NSAttributedString {
parsedAttributes.append(ChatTextInputStateTextAttribute(type: .collapsedQuote(text: ChatTextInputStateText(attributedText: value)), range: range.location ..< (range.location + range.length)))
}
}
})
@@ -598,6 +607,8 @@ public struct ChatTextInputStateText: Codable, Equatable {
result.addAttribute(ChatTextInputAttributes.block, value: ChatTextInputTextQuoteAttribute(kind: .quote, isCollapsed: isCollapsed), range: NSRange(location: attribute.range.lowerBound, length: attribute.range.count))
case let .codeBlock(language):
result.addAttribute(ChatTextInputAttributes.block, value: ChatTextInputTextQuoteAttribute(kind: .code(language: language), isCollapsed: false), range: NSRange(location: attribute.range.lowerBound, length: attribute.range.count))
case let .collapsedQuote(text):
result.addAttribute(ChatTextInputAttributes.collapsedBlock, value: text.attributedText(), range: NSRange(location: attribute.range.lowerBound, length: attribute.range.count))
}
}
return result