mirror of
https://github.com/block/buzz.git
synced 2026-11-25 10:42:26 +00:00
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 <noreply@anthropic.com> Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
@@ -46,6 +46,14 @@ class _MarkdownEditingController extends TextEditingController {
|
|||||||
List<TextRange> _chipRanges = const [];
|
List<TextRange> _chipRanges = const [];
|
||||||
final List<TextRange> _pendingChipRanges = [];
|
final List<TextRange> _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
|
/// Where [buildTextSpan] last ran, so the chip ranges can be brought up
|
||||||
/// to date when edits arrive faster than frames.
|
/// to date when edits arrive faster than frames.
|
||||||
BuildContext? _lastBuildContext;
|
BuildContext? _lastBuildContext;
|
||||||
@@ -182,6 +190,7 @@ class _MarkdownEditingController extends TextEditingController {
|
|||||||
|
|
||||||
TextEditingValue _snapSelectionToChips(TextEditingValue next) {
|
TextEditingValue _snapSelectionToChips(TextEditingValue next) {
|
||||||
_refreshChipRanges(next.text);
|
_refreshChipRanges(next.text);
|
||||||
|
_platformSelection = _snappedSelection = _snappedText = null;
|
||||||
final selection = next.selection;
|
final selection = next.selection;
|
||||||
if ((_chipRanges.isEmpty && _revealedChip == null) ||
|
if ((_chipRanges.isEmpty && _revealedChip == null) ||
|
||||||
next.text != _chipRangesText ||
|
next.text != _chipRangesText ||
|
||||||
@@ -230,6 +239,9 @@ class _MarkdownEditingController extends TextEditingController {
|
|||||||
: selection.baseOffset <= selection.extentOffset
|
: selection.baseOffset <= selection.extentOffset
|
||||||
? TextSelection(baseOffset: start, extentOffset: end)
|
? TextSelection(baseOffset: start, extentOffset: end)
|
||||||
: TextSelection(baseOffset: end, extentOffset: start);
|
: TextSelection(baseOffset: end, extentOffset: start);
|
||||||
|
_platformSelection = selection;
|
||||||
|
_snappedSelection = snapped;
|
||||||
|
_snappedText = next.text;
|
||||||
return next.copyWith(selection: snapped);
|
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
|
/// 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
|
/// URL or `@label` as one glyph, so backspace right after it would
|
||||||
/// otherwise remove one invisible character of the source and leave a
|
/// otherwise remove one invisible character of the source and leave a
|
||||||
/// broken token behind. Edits over a selection replace exactly that
|
/// broken token behind. The deletion arrives either against a collapsed
|
||||||
/// selection, so Cut never removes more than it copies.
|
/// 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 expandChipDeletion(
|
||||||
TextEditingValue oldValue,
|
TextEditingValue oldValue,
|
||||||
TextEditingValue newValue,
|
TextEditingValue newValue,
|
||||||
@@ -506,19 +525,20 @@ class _MarkdownEditingController extends TextEditingController {
|
|||||||
if (_chipRanges.isEmpty ||
|
if (_chipRanges.isEmpty ||
|
||||||
_chipRangesText != oldValue.text ||
|
_chipRangesText != oldValue.text ||
|
||||||
!oldSelection.isValid ||
|
!oldSelection.isValid ||
|
||||||
!oldSelection.isCollapsed ||
|
|
||||||
!_withinChip(oldValue.composing, _chipRanges) ||
|
!_withinChip(oldValue.composing, _chipRanges) ||
|
||||||
!_withinChip(newValue.composing, _chipRanges)) {
|
!_withinChip(newValue.composing, _chipRanges)) {
|
||||||
return newValue;
|
return newValue;
|
||||||
}
|
}
|
||||||
final newSelection = newValue.selection;
|
final newSelection = newValue.selection;
|
||||||
if (!newSelection.isValid || !newSelection.isCollapsed) return newValue;
|
if (!newSelection.isValid || !newSelection.isCollapsed) return newValue;
|
||||||
final deleted = _caretDeletion(
|
final deleted = oldSelection.isCollapsed
|
||||||
oldValue.text,
|
? _caretDeletion(
|
||||||
newValue.text,
|
oldValue.text,
|
||||||
oldSelection.baseOffset,
|
newValue.text,
|
||||||
newSelection.baseOffset,
|
oldSelection.baseOffset,
|
||||||
);
|
newSelection.baseOffset,
|
||||||
|
)
|
||||||
|
: _platformSelectionDeletion(oldValue, newValue);
|
||||||
if (deleted == null) return newValue;
|
if (deleted == null) return newValue;
|
||||||
// A link the caret has revealed edits as raw text, even before the next
|
// A link the caret has revealed edits as raw text, even before the next
|
||||||
// frame drops its chip.
|
// frame drops its chip.
|
||||||
@@ -597,6 +617,30 @@ class _MarkdownEditingController extends TextEditingController {
|
|||||||
return null;
|
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<InlineSpan> _buildMarkdownSpans(
|
List<InlineSpan> _buildMarkdownSpans(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
String source,
|
String source,
|
||||||
|
|||||||
@@ -43,12 +43,22 @@ void mentionChipTests() {
|
|||||||
return tester.widget<TextField>(find.byType(TextField)).controller!;
|
return tester.widget<TextField>(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) {
|
void backspace(WidgetTester tester, TextEditingController controller) {
|
||||||
|
final text = controller.text;
|
||||||
final caret = controller.selection.baseOffset;
|
final caret = controller.selection.baseOffset;
|
||||||
tester.testTextInput.updateEditingValue(
|
tester.testTextInput.updateEditingValue(
|
||||||
TextEditingValue(
|
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),
|
selection: TextSelection.collapsed(offset: caret - 1),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user