Editor: Fixes to the first time sizing + paste images

This commit is contained in:
Thomas Ricouard 2023-01-13 12:23:48 +01:00
parent ba64015f18
commit 1eb3dfb5a5
2 changed files with 33 additions and 15 deletions

View file

@ -5,8 +5,8 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/divadretlaw/EmojiText", "location" : "https://github.com/divadretlaw/EmojiText",
"state" : { "state" : {
"revision" : "f349e481499d2c832ab9d2dc28af238e53b1f9b4", "revision" : "3d8f6196de59634352ed317cf6ab4e292eeaaf44",
"version" : "1.1.0" "version" : "1.2.0"
} }
}, },
{ {
@ -32,8 +32,8 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/kean/Nuke", "location" : "https://github.com/kean/Nuke",
"state" : { "state" : {
"revision" : "81f6a3dea0c8ce3b87389c241c48601be07af0b1", "revision" : "2e9337168d08acccf72c039bf9324be24a1cf7d7",
"version" : "11.5.1" "version" : "11.5.3"
} }
}, },
{ {
@ -50,8 +50,8 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/scinfu/SwiftSoup.git", "location" : "https://github.com/scinfu/SwiftSoup.git",
"state" : { "state" : {
"revision" : "6778575285177365cbad3e5b8a72f2a20583cfec", "revision" : "f707b8680cddb96dc1855632340a572ef37bbb98",
"version" : "2.4.3" "version" : "2.5.3"
} }
}, },
{ {
@ -69,7 +69,7 @@
"location" : "https://github.com/Dimillian/TextView", "location" : "https://github.com/Dimillian/TextView",
"state" : { "state" : {
"branch" : "main", "branch" : "main",
"revision" : "52206e6bcb6de34b2ef6250c4c5111e9b845fb0b" "revision" : "8bddc899350d74f36ad17efc07d35152929bab64"
} }
} }
], ],

View file

@ -23,7 +23,7 @@ public class StatusEditorViewModel: ObservableObject {
@Published var statusText = NSMutableAttributedString(string: "") { @Published var statusText = NSMutableAttributedString(string: "") {
didSet { didSet {
highlightMeta() processText()
checkEmbed() checkEmbed()
} }
} }
@ -135,7 +135,10 @@ public class StatusEditorViewModel: ObservableObject {
mentionString = "@\(status.reblog?.account.acct ?? status.account.acct)" mentionString = "@\(status.reblog?.account.acct ?? status.account.acct)"
} }
for mention in status.mentions where mention.acct != currentAccount?.acct { for mention in status.mentions where mention.acct != currentAccount?.acct {
mentionString += " @\(mention.acct)" if !mentionString.isEmpty {
mentionString += " "
}
mentionString += "@\(mention.acct)"
} }
mentionString += " " mentionString += " "
replyToStatus = status replyToStatus = status
@ -162,7 +165,7 @@ public class StatusEditorViewModel: ObservableObject {
} }
} }
private func highlightMeta() { private func processText() {
statusText.addAttributes([.foregroundColor: UIColor(Color.label)], statusText.addAttributes([.foregroundColor: UIColor(Color.label)],
range: NSMakeRange(0, statusText.string.utf16.count)) range: NSMakeRange(0, statusText.string.utf16.count))
let hashtagPattern = "(#+[a-zA-Z0-9(_)]{1,})" let hashtagPattern = "(#+[a-zA-Z0-9(_)]{1,})"
@ -174,16 +177,17 @@ public class StatusEditorViewModel: ObservableObject {
let mentionRegex = try NSRegularExpression(pattern: mentionPattern, options: []) let mentionRegex = try NSRegularExpression(pattern: mentionPattern, options: [])
let urlRegex = try NSRegularExpression(pattern: urlPattern, options: []) let urlRegex = try NSRegularExpression(pattern: urlPattern, options: [])
let range = NSMakeRange(0, statusText.string.utf16.count)
var ranges = hashtagRegex.matches(in: statusText.string, var ranges = hashtagRegex.matches(in: statusText.string,
options: [], options: [],
range: NSMakeRange(0, statusText.string.utf16.count)).map { $0.range } range: range).map { $0.range }
ranges.append(contentsOf: mentionRegex.matches(in: statusText.string, ranges.append(contentsOf: mentionRegex.matches(in: statusText.string,
options: [], options: [],
range: NSMakeRange(0, statusText.string.utf16.count)).map {$0.range}) range: range).map {$0.range})
let urlRanges = urlRegex.matches(in: statusText.string, let urlRanges = urlRegex.matches(in: statusText.string,
options: [], options: [],
range: NSMakeRange(0, statusText.string.utf16.count)).map { $0.range } range:range).map { $0.range }
var foundSuggestionRange: Bool = false var foundSuggestionRange: Bool = false
for nsRange in ranges { for nsRange in ranges {
@ -207,6 +211,20 @@ public class StatusEditorViewModel: ObservableObject {
.underlineColor: UIColor(theme?.tintColor ?? .brand)], .underlineColor: UIColor(theme?.tintColor ?? .brand)],
range: NSRange(location: range.location, length: range.length)) range: NSRange(location: range.location, length: range.length))
} }
var attachementsToRemove: [NSRange] = []
statusText.enumerateAttribute(.attachment, in: range) { attachement, raneg, _ in
if let attachement = attachement as? NSTextAttachment, let image = attachement.image {
attachementsToRemove.append(range)
mediasImages.append(.init(image: image, mediaAttachement: nil, error: nil))
}
}
if !attachementsToRemove.isEmpty {
processMediasToUpload()
for range in attachementsToRemove {
statusText.removeAttribute(.attachment, range: range)
}
}
} catch { } catch {
} }
@ -303,12 +321,12 @@ public class StatusEditorViewModel: ObservableObject {
} }
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
self?.mediasImages = medias self?.mediasImages = medias
self?.processUpload() self?.processMediasToUpload()
} }
} }
} }
private func processUpload() { private func processMediasToUpload() {
uploadTask?.cancel() uploadTask?.cancel()
let mediasCopy = mediasImages let mediasCopy = mediasImages
uploadTask = Task { uploadTask = Task {