From bf34af139f51519265ab74cb5724674fd8ab5a5e Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Thu, 19 Nov 2026 15:39:07 +1100 Subject: [PATCH] fix(mobile): remove a mention chip whole on an iOS Backspace On the iPhone simulator the second Backspace after a picked `@Bob ` still turned the chip into plain `@Bo` with the chooser open, and the agent chip into `@Scou`, from the hardware key and the on-screen delete key alike (recommendation 2, second half). The widget test passed because it fed the deletion as one collapsed-caret update, the shape Android sends. A scratch harness on the simulator showed iOS reports a Backspace as two editing updates: the character before the caret is selected first (`@Bob` 4,4 to 3,4), then that selection is removed with the caret at its start (`@Bo` 3,3). The controller snapped the first update to the whole chip (0,4), so the deletion formatter saw a non-collapsed old selection and, by the rule that an edit over a selection replaces exactly that selection, left `@Bo` alone. The controller now records the platform's own selection whenever a snap widens it to a chip. A deletion that removes exactly that recorded range, caret at its start, is the platform deleting what it selected, and it widens to the whole chip and its binding like a caret deletion does. Any other edit over a selection is unchanged: typed text replaces the snapped selection and Cut removes only what it copies, since the two are otherwise indistinguishable (`@Helper Bot typing` selected and `@` typed reads the same as deleting everything after the `@`). The first press still removes only the trailing space, as buzz-app does. Tests: the chip journey's Backspace helper now sends the two updates iOS sends, so it fails on the old code with `hi @Bo` and passes with the fix, for the person and the agent chip; the selection-replacement test keeps its typed text. `dart format`, `flutter analyze`, `just file-size-check` and the full mobile `flutter test` suite pass. On the iPhone 17 simulator (iOS 27.2) with the harness, two hardware Backspaces after `@Bob ` and after `@Scout ` leave an empty composer with the chooser closed, and the on-screen delete key does the same. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Matt Toohey --- .../markdown_editing_controller.dart | 62 ++++++++++++++++--- .../compose_bar_test/mention_chip_tests.dart | 14 ++++- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart b/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart index 1197b65dd1..fb4777f878 100644 --- a/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart +++ b/mobile/lib/features/channels/compose_bar/markdown_editing_controller.dart @@ -46,6 +46,14 @@ class _MarkdownEditingController extends TextEditingController { List _chipRanges = const []; final List _pendingChipRanges = []; + /// The last selection the platform set that reached into a chip, and the + /// chip-wide selection it was snapped to in [_snappedText]. iOS selects + /// the character before the caret right before Backspace removes it; the + /// removal then matches this range rather than the snapped selection. + TextSelection? _platformSelection; + TextSelection? _snappedSelection; + String? _snappedText; + /// Where [buildTextSpan] last ran, so the chip ranges can be brought up /// to date when edits arrive faster than frames. BuildContext? _lastBuildContext; @@ -182,6 +190,7 @@ class _MarkdownEditingController extends TextEditingController { TextEditingValue _snapSelectionToChips(TextEditingValue next) { _refreshChipRanges(next.text); + _platformSelection = _snappedSelection = _snappedText = null; final selection = next.selection; if ((_chipRanges.isEmpty && _revealedChip == null) || next.text != _chipRangesText || @@ -230,6 +239,9 @@ class _MarkdownEditingController extends TextEditingController { : selection.baseOffset <= selection.extentOffset ? TextSelection(baseOffset: start, extentOffset: end) : TextSelection(baseOffset: end, extentOffset: start); + _platformSelection = selection; + _snappedSelection = snapped; + _snappedText = next.text; return next.copyWith(selection: snapped); } @@ -495,8 +507,15 @@ class _MarkdownEditingController extends TextEditingController { /// hidden source so it removes the whole chip. Each chip draws its whole /// URL or `@label` as one glyph, so backspace right after it would /// otherwise remove one invisible character of the source and leave a - /// broken token behind. Edits over a selection replace exactly that - /// selection, so Cut never removes more than it copies. + /// broken token behind. The deletion arrives either against a collapsed + /// caret (Android, hardware keys in tests) or, as iOS reports it from the + /// hardware key and the on-screen delete key alike, as two updates: the + /// character before the caret is selected first, then that selection is + /// removed. The selection step snaps to the whole chip, so the removal is + /// recognised by matching the platform's own selection rather than the + /// snapped one. Any other edit over a selection replaces exactly that + /// selection, so typed text is kept and Cut never removes more than it + /// copies. TextEditingValue expandChipDeletion( TextEditingValue oldValue, TextEditingValue newValue, @@ -506,19 +525,20 @@ class _MarkdownEditingController extends TextEditingController { if (_chipRanges.isEmpty || _chipRangesText != oldValue.text || !oldSelection.isValid || - !oldSelection.isCollapsed || !_withinChip(oldValue.composing, _chipRanges) || !_withinChip(newValue.composing, _chipRanges)) { return newValue; } final newSelection = newValue.selection; if (!newSelection.isValid || !newSelection.isCollapsed) return newValue; - final deleted = _caretDeletion( - oldValue.text, - newValue.text, - oldSelection.baseOffset, - newSelection.baseOffset, - ); + final deleted = oldSelection.isCollapsed + ? _caretDeletion( + oldValue.text, + newValue.text, + oldSelection.baseOffset, + newSelection.baseOffset, + ) + : _platformSelectionDeletion(oldValue, newValue); if (deleted == null) return newValue; // A link the caret has revealed edits as raw text, even before the next // frame drops its chip. @@ -597,6 +617,30 @@ class _MarkdownEditingController extends TextEditingController { return null; } + /// The range the platform removed when [oldValue] carries the snapped + /// form of a selection the platform set inside a chip and [newValue] is + /// [oldValue] with exactly that platform selection gone, caret at its + /// start. Null for any other edit, including text typed over a selection. + TextRange? _platformSelectionDeletion( + TextEditingValue oldValue, + TextEditingValue newValue, + ) { + final platform = _platformSelection; + if (platform == null || + _snappedText != oldValue.text || + _snappedSelection != oldValue.selection) { + return null; + } + final oldText = oldValue.text; + final start = platform.start; + final end = platform.end; + if (newValue.selection.baseOffset != start || + newValue.text != oldText.replaceRange(start, end, '')) { + return null; + } + return TextRange(start: start, end: end); + } + List _buildMarkdownSpans( BuildContext context, String source, diff --git a/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart b/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart index 4d95ad2209..a9440b9721 100644 --- a/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart +++ b/mobile/test/features/channels/compose_bar_test/mention_chip_tests.dart @@ -43,12 +43,22 @@ void mentionChipTests() { return tester.widget(find.byType(TextField)).controller!; } - /// The platform reports a backspace: one character before the caret gone. + /// The platform reports a backspace the way iOS does, from the hardware + /// key and the on-screen delete key alike: the character before the caret + /// is selected first, then that selection is removed with the caret at + /// its start. void backspace(WidgetTester tester, TextEditingController controller) { + final text = controller.text; final caret = controller.selection.baseOffset; tester.testTextInput.updateEditingValue( TextEditingValue( - text: controller.text.replaceRange(caret - 1, caret, ''), + text: text, + selection: TextSelection(baseOffset: caret - 1, extentOffset: caret), + ), + ); + tester.testTextInput.updateEditingValue( + TextEditingValue( + text: text.replaceRange(caret - 1, caret, ''), selection: TextSelection.collapsed(offset: caret - 1), ), );