fix(ios): support image paste from composer edit menu #5610
t3code/rebuild-mobile-app-swift fromrefs/pull/5610/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/swift-ios/Features/Chat/FeatureComposerView.swift line 135OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -108,3 +115,4 @@108115 .task(id: pathSearchRequest) {109116 await updatePathSearch()110117 }118+ .onChange(of: attachmentContextID) {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) removed labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/swift-ios/Features/Chat/FeatureComposerView.swift lines 638 to 645OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -466,0 +631,21 @@631+ task.cancel()632+ }633+ tasks.removeAll()634+ tail = nil635+ }636+637+ func waitForAll() async {638+ await tail?.value639+ }640+}641+642+final class FeatureComposerUITextView: UITextView {643+ var acceptsImages = false644+ var onPasteImages: (([NSItemProvider]) -> Void)?645+646+ override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {647+ if action == (paste(_:)), acceptsImages, UIPasteboard.general.hasImages {648+ return true649+ }650+ return super.canPerformAction(action, withSender: sender)651+ }
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/swift-ios/Features/Chat/FeatureComposerView.swift line 818OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -466,0 +750,21 @@750+ textView.accessibilityIdentifier = "message-composer"751+ updateAccessibility(textView)752+ return textView753+ }754+755+ func updateUIView(_ textView: FeatureComposerUITextView, context: Context) {756+ context.coordinator.parent = self757+ textView.acceptsImages = acceptsImages758+ textView.onPasteImages = onPasteImages759+760+ let shouldApplySelection = selectionRequest.map {761+ context.coordinator.lastAppliedSelectionRequestID != $0.id762+ } ?? false763+ if textView.text != text {764+ let selectedRange = textView.selectedRange765+ textView.text = text766+ if !shouldApplySelection {767+ let location = min(selectedRange.location, textView.text.utf16.count)768+ let length = min(selectedRange.length, textView.text.utf16.count - location)769+ textView.selectedRange = NSRange(location: location, length: length)770+ }
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/swift-ios/Features/Chat/FeatureComposerView.swift lines 691 to 695OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -466,0 +675,21 @@675+ let lower = min(max(range.lowerBound, 0), text.count)676+ let lowerIndex = text.index(text.startIndex, offsetBy: lower)677+ return text[..<lowerIndex].utf16.count + replacement.utf16.count678+ }679+680+ static func cursorLocationAfterBindingUpdate(previousText: String, newText: String, selectedLocation: Int) -> Int {681+ previousText.isEmpty ? newText.utf16.count : min(selectedLocation, newText.utf16.count)682+ }683+}684+685+enum FeatureComposerPasteTextPolicy {686+ static func text(from pasteboard: UIPasteboard) -> String? {687+ let strings = (0..<pasteboard.numberOfItems).compactMap { itemIndex -> String? in688+ let itemSet = IndexSet(integer: itemIndex)689+ let typeIdentifiers = pasteboard.types(forItemSet: itemSet)?690+ .first ?? []691+ guard !typeIdentifiers.contains(where: {692+ UTType($0)?.conforms(to: .image) == true693+ }) else { return nil }694+695+ for typeIdentifier in preferredPlainTextTypes(in: typeIdentifiers) {
- apps/swift-ios/Features/Chat/FeatureComposerView.swift line 137Resolvedby saphid(GitHub profile, opens in a new tab)@@ -116,3 +130,6 @@116130 .task(id: pathSearchRequest) {117131 await updatePathSearch()118132 }133+ .onChange(of: attachmentContextID) {134+ rotateAttachmentLifecycle(to: attachmentContextID)135+ }
- apps/swift-ios/Features/Chat/FeatureComposerView.swift line 735OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -466,0 +715,21 @@715+ let string = utf8String(from: data, typeIdentifier: typeIdentifier),716+ !string.isEmpty {717+ return string718+ }719+ }720+ return nil721+ }722+ guard !strings.isEmpty else { return nil }723+ return strings.joined(separator: "\n")724+ }725+726+ private static func utf8String(from data: Data, typeIdentifier: String) -> String? {727+ guard let type = UTType(typeIdentifier) else { return nil }728+ if type.conforms(to: .utf16PlainText)729+ || type.conforms(to: .utf16ExternalPlainText) {730+ if data.starts(with: [0xFF, 0xFE]) || data.starts(with: [0xFE, 0xFF]) {731+ return String(data: data, encoding: .utf16)732+ }733+ if type.conforms(to: .utf16ExternalPlainText) {734+ return String(data: data, encoding: .utf16LittleEndian)735+ ?? String(data: data, encoding: .utf16BigEndian)
t3-code-bot[bot](GitHub profile, opens in a new tab) approved these changes
saphid(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.








Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
3b9d22c2-4390-4d41-9896-e3e89f4adb83You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share