Skip to content

Commit

Permalink
[SuperTextField] Fix new line duplication (Resolves #1468) (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelosilvestre authored Oct 17, 2023
1 parent 185cce5 commit c851667
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,11 @@ class _SuperTextFieldImeInteractorState extends State<SuperTextFieldImeInteracto
void _onPerformAction(TextInputAction action) {
switch (action) {
case TextInputAction.newline:
_textController.insertNewline();
// Do nothing for IME newline actions.
//
// Mac: Key presses flow, unhandled, to the OS and turn into IME selectors. We handle newlines there.
// Windows/Linux: Key presses flow, unhandled, to the OS and turn into text deltas. We handle newlines there.
// Android/iOS: This text field implementation is only for desktop, mobile is handled elsewhere.
break;
case TextInputAction.done:
widget.focusNode.unfocus();
Expand Down Expand Up @@ -1761,7 +1765,6 @@ const defaultTextFieldImeKeyboardHandlers = <TextFieldKeyboardHandler>[
// handlers, passing the key combo to the OS on Mac. Place all custom Mac key
// combos above this handler.
DefaultSuperTextFieldKeyboardHandlers.sendKeyEventToMacOs,
DefaultSuperTextFieldKeyboardHandlers.insertNewlineWhenEnterIsPressed,
DefaultSuperTextFieldKeyboardHandlers.moveCaretToStartOrEnd,
DefaultSuperTextFieldKeyboardHandlers.moveUpDownLeftAndRightWithArrowKeys,
DefaultSuperTextFieldKeyboardHandlers.moveToLineStartWithHome,
Expand Down
2 changes: 1 addition & 1 deletion super_editor/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
# Dependencies for testing tools that we ship with super_editor
flutter_test:
sdk: flutter
flutter_test_robots: 0.0.21
flutter_test_robots: 0.0.22

dependency_overrides:
# # Override to local mono-repo path so devs can test this repo
Expand Down
93 changes: 15 additions & 78 deletions super_editor/test/super_textfield/super_textfield_ime_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void main() {
});

group('inserts line', () {
testWidgetsOnWindowsAndLinux('when ENTER is pressed in middle of text', (tester) async {
testWidgetsOnDesktop('when ENTER is pressed in middle of text', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
Expand All @@ -240,31 +240,13 @@ void main() {
);
await tester.placeCaretInSuperTextField(8);

await tester.pressEnter();
await tester.pressEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is \nsome text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 9));
});

testWidgetsOnMac('when ENTER is pressed in middle of text (on MAC)', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
text: AttributedText('this is some text'),
),
);
await tester.placeCaretInSuperTextField(8);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();

expect(SuperTextFieldInspector.findText().text, "this is \nsome text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 9));
});

testWidgetsOnWindowsAndLinux('when ENTER is pressed at beginning of text', (tester) async {
testWidgetsOnDesktop('when ENTER is pressed at beginning of text', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
Expand All @@ -273,46 +255,13 @@ void main() {
);
await tester.placeCaretInSuperTextField(0);

await tester.pressEnter();
await tester.pressEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "\nthis is some text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 1));
});

testWidgetsOnMac('when ENTER is pressed at beginning of text (on MAC)', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
text: AttributedText('this is some text'),
),
);
await tester.placeCaretInSuperTextField(0);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();

expect(SuperTextFieldInspector.findText().text, "\nthis is some text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 1));
});

testWidgetsOnWindowsAndLinux('when ENTER is pressed at end of text', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
text: AttributedText('this is some text'),
),
);
await tester.placeCaretInSuperTextField(17);

await tester.pressEnter();

expect(SuperTextFieldInspector.findText().text, "this is some text\n");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 18));
});

testWidgetsOnMac('when ENTER is pressed at end of text (on MAC)', (tester) async {
testWidgetsOnDesktop('when ENTER is pressed at end of text', (tester) async {
await _pumpSuperTextField(
tester,
AttributedTextEditingController(
Expand All @@ -321,16 +270,13 @@ void main() {
);
await tester.placeCaretInSuperTextField(17);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
await tester.pressEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is some text\n");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 18));
});

// TODO: Make this a Windows + Linux test when Flutter supports numpad enter on windows
// TODO: Merge this with the testWidgetsOnMac below when Flutter supports numpad enter on windows
testWidgetsOnLinux('when NUMPAD ENTER is pressed in middle of text', (tester) async {
await _pumpSuperTextField(
tester,
Expand All @@ -340,7 +286,7 @@ void main() {
);
await tester.placeCaretInSuperTextField(8);

await tester.pressNumpadEnter();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is \nsome text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 9));
Expand All @@ -355,16 +301,13 @@ void main() {
);
await tester.placeCaretInSuperTextField(8);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is \nsome text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 9));
});

// TODO: Make this a Windows + Linux test when Flutter supports numpad enter on windows
// TODO: Merge this with the testWidgetsOnMac below when Flutter supports numpad enter on windows
testWidgetsOnLinux('when NUMPAD ENTER is pressed at beginning of text', (tester) async {
await _pumpSuperTextField(
tester,
Expand All @@ -374,7 +317,7 @@ void main() {
);
await tester.placeCaretInSuperTextField(0);

await tester.pressNumpadEnter();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "\nthis is some text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 1));
Expand All @@ -389,16 +332,13 @@ void main() {
);
await tester.placeCaretInSuperTextField(0);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "\nthis is some text");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 1));
});

// TODO: Make this a Windows + Linux test when Flutter supports numpad enter on windows
// TODO: Merge this with the testWidgetsOnMac below when Flutter supports numpad enter on windows
testWidgetsOnLinux('when NUMPAD ENTER is pressed at end of text', (tester) async {
await _pumpSuperTextField(
tester,
Expand All @@ -408,7 +348,7 @@ void main() {
);
await tester.placeCaretInSuperTextField(17);

await tester.pressNumpadEnter();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is some text\n");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 18));
Expand All @@ -423,10 +363,7 @@ void main() {
);
await tester.placeCaretInSuperTextField(17);

// Simulate the user pressing ENTER.
// On macOS, pressing ENTER generates a new line input action.
await tester.testTextInput.receiveAction(TextInputAction.newline);
await tester.pump();
await tester.pressNumpadEnterAdaptive(getter: imeClientGetter);

expect(SuperTextFieldInspector.findText().text, "this is some text\n");
expect(SuperTextFieldInspector.findSelection(), const TextSelection.collapsed(offset: 18));
Expand Down

0 comments on commit c851667

Please sign in to comment.