Syntax highlight

This commit is contained in:
Ali
2023-10-31 16:47:10 +04:00
parent 6bc7b4f663
commit eb15434a8e
138 changed files with 49922 additions and 218 deletions

View File

@@ -19,10 +19,9 @@ public struct ChatTextInputAttributes {
public static let textUrl = NSAttributedString.Key(rawValue: "Attribute__TextUrl")
public static let spoiler = NSAttributedString.Key(rawValue: "Attribute__Spoiler")
public static let customEmoji = NSAttributedString.Key(rawValue: "Attribute__CustomEmoji")
public static let code = NSAttributedString.Key(rawValue: "Attribute__Code")
public static let quote = NSAttributedString.Key(rawValue: "Attribute__Blockquote")
public static let block = NSAttributedString.Key(rawValue: "Attribute__Blockquote")
public static let allAttributes = [ChatTextInputAttributes.bold, ChatTextInputAttributes.italic, ChatTextInputAttributes.monospace, ChatTextInputAttributes.strikethrough, ChatTextInputAttributes.underline, ChatTextInputAttributes.textMention, ChatTextInputAttributes.textUrl, ChatTextInputAttributes.spoiler, ChatTextInputAttributes.customEmoji, ChatTextInputAttributes.code, ChatTextInputAttributes.quote]
public static let allAttributes = [ChatTextInputAttributes.bold, ChatTextInputAttributes.italic, ChatTextInputAttributes.monospace, ChatTextInputAttributes.strikethrough, ChatTextInputAttributes.underline, ChatTextInputAttributes.textMention, ChatTextInputAttributes.textUrl, ChatTextInputAttributes.spoiler, ChatTextInputAttributes.customEmoji, ChatTextInputAttributes.block]
}
public let originalTextAttributeKey = NSAttributedString.Key(rawValue: "Attribute__OriginalText")
@@ -149,15 +148,9 @@ public func textAttributedStringForStateText(_ stateText: NSAttributedString, fo
} else if key == ChatTextInputAttributes.customEmoji {
result.addAttribute(key, value: value, range: range)
result.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.clear, range: range)
} else if key == ChatTextInputAttributes.quote {
} else if key == ChatTextInputAttributes.block {
fontAttributes.insert(.blockQuote)
result.addAttribute(key, value: value, range: range)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 8.0
paragraphStyle.headIndent = 8.0
//paragraphStyle.paragraphSpacing = 8.0
//paragraphStyle.paragraphSpacingBefore = 8.0
//result.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
}
}
@@ -244,7 +237,16 @@ public final class ChatTextInputTextUrlAttribute: NSObject {
}
public final class ChatTextInputTextQuoteAttribute: NSObject {
override public init() {
public enum Kind: Equatable {
case quote
case code(language: String?)
}
public let kind: Kind
public init(kind: Kind) {
self.kind = kind
super.init()
}
@@ -253,7 +255,9 @@ public final class ChatTextInputTextQuoteAttribute: NSObject {
return false
}
let _ = other
if self.kind != other.kind {
return false
}
return true
}
@@ -587,7 +591,7 @@ private func quoteRangesEqual(_ lhs: [(NSRange, ChatTextInputTextQuoteAttribute)
private func refreshBlockQuotes(text: NSString, initialAttributedText: NSAttributedString, attributedText: NSMutableAttributedString, fullRange: NSRange) {
var quoteRanges: [(NSRange, ChatTextInputTextQuoteAttribute)] = []
initialAttributedText.enumerateAttributes(in: fullRange, using: { dict, range, _ in
if let value = dict[ChatTextInputAttributes.quote] as? ChatTextInputTextQuoteAttribute {
if let value = dict[ChatTextInputAttributes.block] as? ChatTextInputTextQuoteAttribute {
quoteRanges.append((range, value))
}
})
@@ -638,10 +642,9 @@ private func refreshBlockQuotes(text: NSString, initialAttributedText: NSAttribu
}
if !quoteRangesEqual(quoteRanges, initialQuoteRanges) {
attributedText.removeAttribute(ChatTextInputAttributes.quote, range: fullRange)
attributedText.removeAttribute(ChatTextInputAttributes.block, range: fullRange)
for (range, attribute) in quoteRanges {
let _ = attribute
attributedText.addAttribute(ChatTextInputAttributes.quote, value: ChatTextInputTextQuoteAttribute(), range: range)
attributedText.addAttribute(ChatTextInputAttributes.block, value: ChatTextInputTextQuoteAttribute(kind: attribute.kind), range: range)
}
}
}
@@ -695,7 +698,7 @@ public func refreshChatTextInputAttributes(textView: UITextView, primaryTextColo
textView.textStorage.removeAttribute(ChatTextInputAttributes.textUrl, range: fullRange)
textView.textStorage.removeAttribute(ChatTextInputAttributes.spoiler, range: fullRange)
textView.textStorage.removeAttribute(ChatTextInputAttributes.customEmoji, range: fullRange)
textView.textStorage.removeAttribute(ChatTextInputAttributes.quote, range: fullRange)
textView.textStorage.removeAttribute(ChatTextInputAttributes.block, range: fullRange)
textView.textStorage.addAttribute(NSAttributedString.Key.font, value: Font.regular(baseFontSize), range: fullRange)
textView.textStorage.addAttribute(NSAttributedString.Key.foregroundColor, value: primaryTextColor, range: fullRange)
@@ -739,15 +742,9 @@ public func refreshChatTextInputAttributes(textView: UITextView, primaryTextColo
} else if key == ChatTextInputAttributes.customEmoji, let value = value as? ChatTextInputTextCustomEmojiAttribute {
textView.textStorage.addAttribute(key, value: value, range: range)
textView.textStorage.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.clear, range: range)
} else if key == ChatTextInputAttributes.quote {
} else if key == ChatTextInputAttributes.block {
fontAttributes.insert(.blockQuote)
textView.textStorage.addAttribute(key, value: value, range: range)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 8.0
paragraphStyle.headIndent = 8.0
//paragraphStyle.paragraphSpacing = 8.0
//paragraphStyle.paragraphSpacingBefore = 8.0
//textView.textStorage.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
}
}
@@ -858,13 +855,6 @@ public func refreshGenericTextInputAttributes(_ textView: UITextView, theme: Pre
} else {
textView.textStorage.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.clear, range: range)
}
} else if key == ChatTextInputAttributes.quote {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 8.0
paragraphStyle.headIndent = 8.0
//paragraphStyle.paragraphSpacing = 8.0
//paragraphStyle.paragraphSpacingBefore = 8.0
//textView.textStorage.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
}
}
@@ -1056,8 +1046,21 @@ public func convertMarkdownToAttributes(_ text: NSAttributedString) -> NSAttribu
stringOffset -= match.range(at: 2).length + match.range(at: 4).length
let substring = string.substring(with: match.range(at: 1)) + text + string.substring(with: match.range(at: 5))
result.append(NSAttributedString(string: substring, attributes: [ChatTextInputAttributes.code: true as NSNumber]))
var substring = (string.substring(with: match.range(at: 1)).trimmingCharacters(in: .whitespacesAndNewlines) + text + string.substring(with: match.range(at: 5))) as NSString
var language: String?
let newlineRange = substring.range(of: "\n")
if newlineRange.location != NSNotFound {
if newlineRange.location != 0 {
language = substring.substring(with: NSRange(location: 0, length: newlineRange.location))
}
substring = substring.substring(with: NSRange(location: newlineRange.upperBound, length: substring.length - newlineRange.upperBound)) as NSString
}
if substring.hasSuffix("\n") {
substring = substring.substring(with: NSRange(location: 0, length: substring.length - 1)) as NSString
}
result.append(NSAttributedString(string: substring as String, attributes: [ChatTextInputAttributes.block: ChatTextInputTextQuoteAttribute(kind: .code(language: language))]))
offsetRanges.append((NSMakeRange(matchIndex + match.range(at: 1).length, text.count), 6))
}
}
@@ -1090,7 +1093,7 @@ public func convertMarkdownToAttributes(_ text: NSAttributedString) -> NSAttribu
case "`":
textInputAttribute = ChatTextInputAttributes.monospace
case "```":
textInputAttribute = ChatTextInputAttributes.code
textInputAttribute = ChatTextInputAttributes.block
case "**":
textInputAttribute = ChatTextInputAttributes.bold
case "__":