Put compose text view attributed string behavior behind feature flag
This commit is contained in:
parent
e9e08bdadd
commit
96f5ea8af1
@ -139,31 +139,40 @@ private final class WrappedTextViewCoordinator: NSObject {
|
||||
attributedText.string.replacingOccurrences(of: "\u{FFFC}", with: "")
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func attributedTextFromPlain(_ text: String) -> NSAttributedString {
|
||||
let str = NSMutableAttributedString(string: text)
|
||||
str.addAttributes([
|
||||
.foregroundColor: UIColor.label,
|
||||
.font: UIFontMetrics.default.scaledFont(for: .systemFont(ofSize: 20)),
|
||||
], range: NSRange(location: 0, length: str.length))
|
||||
let mentionMatches = CharacterCounter.mention.matches(in: text, range: NSRange(location: 0, length: str.length))
|
||||
for match in mentionMatches.reversed() {
|
||||
str.insert(NSAttributedString(attachment: Self.attachment), at: match.range.location)
|
||||
let range = NSRange(location: match.range.location, length: match.range.length + 1)
|
||||
str.addAttributes([
|
||||
.mention: true,
|
||||
.foregroundColor: UIColor.tintColor,
|
||||
], range: range)
|
||||
if Preferences.shared.hasFeatureFlag(.composeTextAttributes) {
|
||||
let mentionMatches = CharacterCounter.mention.matches(in: text, range: NSRange(location: 0, length: str.length))
|
||||
for match in mentionMatches.reversed() {
|
||||
str.insert(NSAttributedString(attachment: Self.attachment), at: match.range.location)
|
||||
let range = NSRange(location: match.range.location, length: match.range.length + 1)
|
||||
str.addAttributes([
|
||||
.mention: true,
|
||||
.foregroundColor: UIColor.tintColor,
|
||||
], range: range)
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func updateTextViewTextIfNecessary(_ text: String, textView: UITextView) {
|
||||
if text != plainTextFromAttributed(textView.attributedText) {
|
||||
textView.attributedText = attributedTextFromPlain(text)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
private func updateAttributes(in textView: UITextView) {
|
||||
guard Preferences.shared.hasFeatureFlag(.composeTextAttributes) else {
|
||||
return
|
||||
}
|
||||
|
||||
let str = NSMutableAttributedString(attributedString: textView.attributedText!)
|
||||
var changed = false
|
||||
var cursorOffset = 0
|
||||
|
@ -10,5 +10,6 @@ import Foundation
|
||||
public enum FeatureFlag: String, Codable {
|
||||
case iPadBrowserNavigation = "ipad-browser-navigation"
|
||||
case composeRewrite = "compose-rewrite"
|
||||
case composeTextAttributes = "compose-text-attributes"
|
||||
case pushNotifCustomEmoji = "push-notif-custom-emoji"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user