@devries-cameron20 / t3code Mirror · read-only / commits / 5070931521
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(mobile): cutting text in the iOS composer can be undone (#37158)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 15, 2026 at 14:12 UTC 507093152148c0d31c09bcd40e3ce4b5214a8557
2 files changed +49 -16
apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift
+24 -16
@@ -47,12 +47,12 @@ private enum ComposerEnterBehavior: String {
47 }
48
49 private final class ComposerTextAttachment: NSTextAttachment {
50 - let source: String
51 - let label: String
50 + let token: ComposerTokenPayload
51 + var source: String { token.source }
52 + var label: String { token.label }
53
53 - init(source: String, label: String, image: UIImage, size: CGSize, baselineOffset: CGFloat) {
54 - self.source = source
55 - self.label = label
54 + init(token: ComposerTokenPayload, image: UIImage, size: CGSize, baselineOffset: CGFloat) {
55 + self.token = token
56 super.init(data: nil, ofType: nil)
57 self.image = image
58 bounds = CGRect(x: 0, y: baselineOffset, width: size.width, height: size.height)
@@ -89,7 +89,6 @@ private final class ComposerTextView: UITextView {
89 var onPasteContext: (([String: String]) -> Void)?
90 var onPasteText: ((String, NSRange) -> Void)?
91 var clipboardFragment = ""
92 - var onAttributedMutation: (() -> Void)?
92 var onSubmit: ((Bool) -> Void)?
93 var isReadOnly = false
94 var textPasteThresholdBytes = 0
@@ -363,9 +362,7 @@ private final class ComposerTextView: UITextView {
362 return super.cut(sender)
363 }
364 copy(sender)
366 - textStorage.replaceCharacters(in: selectedRange, with: "")
367 - selectedRange = NSRange(location: selectedRange.location, length: 0)
368 - onAttributedMutation?()
365 + replaceDisplayRange(selectedRange)
366 }
367
368 func serializedText() -> String {
@@ -564,9 +561,6 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
561 "selection": ["start": selection.start, "end": selection.end],
562 ])
563 }
567 - textView.onAttributedMutation = { [weak self] in
568 - self?.emitTextChange()
569 - }
564 textView.onSubmit = { [weak self] alternate in
565 self?.onComposerSubmit(["alternate": alternate])
566 }
@@ -737,8 +731,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
731 if tokensJson != document.tokensJson {
732 tokensJson = document.tokensJson
733 let nextTokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? []
740 - // Typing before a chip only moves its offsets; rebuilding then makes iOS autocorrect behind the caret.
741 - tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(tokens, by: { $0.rendersLike($1) })
734 + // Rebuild only when a chip on screen would look different: typing before a chip moves its
735 + // offsets, and Undo restores the chip it removed. A rebuild there makes iOS autocorrect
736 + // behind the caret, and replacing the text drops UIKit's Redo.
737 + tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(renderedChipTokens(), by: { $0.rendersLike($1) })
738 tokens = nextTokens
739 }
740 let previousSelection = sourceSelection()
@@ -1052,8 +1048,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
1048 font: chipFont
1049 )
1050 let attachment = ComposerTextAttachment(
1055 - source: token.source,
1056 - label: token.label,
1051 + token: token,
1052 image: image,
1053 size: image.size,
1054 baselineOffset: chipFont.ascender - image.size.height
@@ -1353,6 +1348,19 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
1348 }
1349 }
1350
1351 + private func renderedChipTokens() -> [ComposerTokenPayload] {
1352 + var rendered: [ComposerTokenPayload] = []
1353 + textView.textStorage.enumerateAttribute(
1354 + .attachment,
1355 + in: NSRange(location: 0, length: textView.textStorage.length)
1356 + ) { value, _, _ in
1357 + if let attachment = value as? ComposerTextAttachment {
1358 + rendered.append(attachment.token)
1359 + }
1360 + }
1361 + return rendered
1362 + }
1363 +
1364 private func documentMatchesExpectedTokens() -> Bool {
1365 let source = value as NSString
1366 let expectedChips = tokens.compactMap { token -> (Int, String)? in
apps/mobile/modules/t3-composer-editor/tests/ios/main.swift
+25
@@ -146,6 +146,31 @@ do {
146 check(view.editor.accessibilityLabel == "Ask anything", "VoiceOver label: \(String(describing: view.editor.accessibilityLabel))")
147 }
148
149 +// Cutting a chip is undoable: Undo restores the chip and tells the parent, Redo removes it again.
150 +// The parent echoes each edit with its re-derived chips; those echoes must not rebuild the text.
151 +do {
152 + let view = makeEditor("hi @README.md ok", tokensJson: mention("README.md", at: 3))
153 + func echo() {
154 + let revision = view.onComposerChange.payloads.last?["eventCount"] as? Int ?? 0
155 + let chips = view.text.contains("@README.md") ? mention("README.md", at: 3) : "[]"
156 + view.setControlledDocumentJson(document(view.text, revision: revision, isNativeEcho: true, tokensJson: chips))
157 + }
158 + view.editor.selectedRange = NSRange(location: 3, length: 2)
159 + view.editor.cut(nil)
160 + check(view.text == "hi ok", "Cut left the selection behind: \(view.text)")
161 + check(view.onComposerChange.payloads.last?["value"] as? String == "hi ok", "Cut did not emit the new value")
162 + echo()
163 + view.editor.undoManager?.undo()
164 + check(view.text == "hi @README.md ok", "Undo did not restore the cut text: \(view.text)")
165 + let restored = view.chip(at: 3)
166 + check(restored is ComposerTextAttachment, "Undo did not restore the cut chip")
167 + check(view.onComposerChange.payloads.last?["value"] as? String == "hi @README.md ok", "Undo did not emit the restored value")
168 + echo()
169 + check(view.chip(at: 3) === restored, "The parent's echo of the restored chip rebuilt the text and dropped Redo")
170 + view.editor.undoManager?.redo()
171 + check(view.text == "hi ok", "Redo did not cut again: \(view.text)")
172 +}
173 +
174 print("composer editor tests passed")
175
176 private let dropEditor = ComposerTextView()