From ae3d190799579bfdf233dedd010d923fa12374de Mon Sep 17 00:00:00 2001 From: Hidemune Takahashi <31161372+h1d3mun3@users.noreply.github.com> Date: Wed, 25 Jan 2023 01:26:56 +0900 Subject: [PATCH] Added marked text range support (#345) * :+1: Added markedTextRangeSupport. * :+1: ignore when markedTextRange is non-nil. * Update TextView revision Co-authored-by: Hidemune Takahashi Co-authored-by: Thomas Ricouard --- .../xcshareddata/swiftpm/Package.resolved | 2 +- .../Sources/Status/Editor/StatusEditorView.swift | 2 +- .../Sources/Status/Editor/StatusEditorViewModel.swift | 11 ++++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/IceCubesApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/IceCubesApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index d5e6e3f2..66c7a29e 100644 --- a/IceCubesApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/IceCubesApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -69,7 +69,7 @@ "location" : "https://github.com/Dimillian/TextView", "state" : { "branch" : "main", - "revision" : "a4ba065a69e3376aeb45b32846d50fd71a7e20e3" + "revision" : "22eec87ccac1270557b2b446fddc13c311bae5e7" } } ], diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorView.swift b/Packages/Status/Sources/Status/Editor/StatusEditorView.swift index e3208b73..db2b9b80 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorView.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorView.swift @@ -37,7 +37,7 @@ public struct StatusEditorView: View { VStack(spacing: 12) { accountHeaderView .padding(.horizontal, .layoutPadding) - TextView($viewModel.statusText, $viewModel.selectedRange) + TextView($viewModel.statusText, $viewModel.selectedRange, $viewModel.markedTextRange) .placeholder(String(localized: "status.editor.text.placeholder")) .font(Font.scaledBodyUIFont) .keyboardType(.twitter) diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index 3658f09e..dc9a4aef 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -43,6 +43,7 @@ public class StatusEditorViewModel: ObservableObject { @Published var spoilerText: String = "" @Published var selectedRange: NSRange = .init(location: 0, length: 0) + @Published var markedTextRange: UITextRange? = nil @Published var isPosting: Bool = false @Published var selectedMedias: [PhotosPickerItem] = [] { @@ -155,6 +156,7 @@ public class StatusEditorViewModel: ObservableObject { string.mutableString.insert(text, at: selectedRange.location) statusText = string selectedRange = NSRange(location: selectedRange.location + text.utf16.count, length: 0) + markedTextRange = nil } func replaceTextWith(text: String, inRange: NSRange) { @@ -163,11 +165,13 @@ public class StatusEditorViewModel: ObservableObject { string.mutableString.insert(text, at: inRange.location) statusText = string selectedRange = NSRange(location: inRange.location + text.utf16.count, length: 0) + markedTextRange = nil } func replaceTextWith(text: String) { statusText = .init(string: text) selectedRange = .init(location: text.utf16.count, length: 0) + markedTextRange = nil } func prepareStatusText() { @@ -193,7 +197,7 @@ public class StatusEditorViewModel: ObservableObject { visibility = status.visibility statusText = .init(string: mentionString) selectedRange = .init(location: mentionString.utf16.count, length: 0) - + markedTextRange = nil if !mentionString.isEmpty { self.mentionString = mentionString.trimmingCharacters(in: .whitespaces) } @@ -201,6 +205,7 @@ public class StatusEditorViewModel: ObservableObject { statusText = .init(string: "@\(account.acct) ") self.visibility = visibility selectedRange = .init(location: statusText.string.utf16.count, length: 0) + markedTextRange = nil case let .edit(status): var rawText = NSAttributedString(status.content.asSafeMarkdownAttributedString).string for mention in status.mentions { @@ -208,6 +213,7 @@ public class StatusEditorViewModel: ObservableObject { } statusText = .init(string: rawText) selectedRange = .init(location: statusText.string.utf16.count, length: 0) + markedTextRange = nil spoilerOn = !status.spoilerText.asRawText.isEmpty spoilerText = status.spoilerText.asRawText visibility = status.visibility @@ -220,11 +226,13 @@ public class StatusEditorViewModel: ObservableObject { if let url = embeddedStatusURL { statusText = .init(string: "\n\nFrom: @\(status.reblog?.account.acct ?? status.account.acct)\n\(url)") selectedRange = .init(location: 0, length: 0) + markedTextRange = nil } } } private func processText() { + guard markedTextRange == nil else { return } statusText.addAttributes([.foregroundColor: UIColor(Color.label), .underlineColor: .clear], range: NSMakeRange(0, statusText.string.utf16.count)) @@ -332,6 +340,7 @@ public class StatusEditorViewModel: ObservableObject { if !initialText.isEmpty { statusText = .init(string: initialText) selectedRange = .init(location: statusText.string.utf16.count, length: 0) + markedTextRange = nil } if !mediasImages.isEmpty { processMediasToUpload()