From 68300149bf8caa5bd994918f82e3af8fac98822c Mon Sep 17 00:00:00 2001 From: Mayur Mahajan <47064215+MayurSMahajan@users.noreply.github.com> Date: Thu, 13 Jul 2023 16:55:14 +0530 Subject: [PATCH] feat: support for shift+home/end keys (#306) --- .../arrow_left_command.dart | 16 +- .../arrow_right_command.dart | 14 +- .../arrow_left_command_test.dart | 124 +++++++++++ .../arrow_right_command_test.dart | 128 ++++++++++++ .../end_command_test.dart | 38 ++++ .../home_command_test.dart | 38 ++++ .../arrow_keys_handler_test.dart | 194 ++++++++++++------ .../shortcut_event/shortcut_event_test.dart | 12 +- 8 files changed, 483 insertions(+), 81 deletions(-) diff --git a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_left_command.dart b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_left_command.dart index b37b4dab9..be656bb10 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_left_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_left_command.dart @@ -42,7 +42,7 @@ CommandShortcutEventHandler _arrowLeftCommandHandler = (editorState) { // move the cursor to the beginning of the block final CommandShortcutEvent moveCursorToBeginCommand = CommandShortcutEvent( key: 'move the cursor at the start of line', - command: 'ctrl+arrow left,home', + command: 'home', macOSCommand: 'cmd+arrow left', handler: _moveCursorToBeginCommandHandler, ); @@ -64,7 +64,8 @@ CommandShortcutEventHandler _moveCursorToBeginCommandHandler = (editorState) { // move the cursor to the left word final CommandShortcutEvent moveCursorToLeftWordCommand = CommandShortcutEvent( key: 'move the cursor to the left word', - command: 'alt+arrow left', + command: 'ctrl+arrow left', + macOSCommand: 'alt+arrow left', handler: _moveCursorToLeftWordCommandHandler, ); @@ -86,7 +87,8 @@ CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = final CommandShortcutEvent moveCursorLeftWordSelectCommand = CommandShortcutEvent( key: 'move the cursor to select the left word', - command: 'alt+shift+arrow left', + command: 'ctrl+shift+arrow left', + macOSCommand: 'alt+shift+arrow left', handler: _moveCursorLeftWordSelectCommandHandler, ); @@ -116,7 +118,7 @@ CommandShortcutEventHandler _moveCursorLeftWordSelectCommandHandler = }; // arrow left key + shift -// +// selects only one character final CommandShortcutEvent moveCursorLeftSelectCommand = CommandShortcutEvent( key: 'move the cursor left select', command: 'shift+arrow left', @@ -144,10 +146,10 @@ CommandShortcutEventHandler _moveCursorLeftSelectCommandHandler = return KeyEventResult.handled; }; -// arrow left key + shift + ctrl or cmd +// final CommandShortcutEvent moveCursorBeginSelectCommand = CommandShortcutEvent( - key: 'move the cursor left select line', - command: 'ctrl+shift+arrow left', + key: 'move cursor to select till start of line', + command: 'shift+home', macOSCommand: 'cmd+shift+arrow left', handler: _moveCursorBeginSelectCommandHandler, ); diff --git a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_right_command.dart b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_right_command.dart index 110bd2909..8ee06b75e 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_right_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/arrow_right_command.dart @@ -42,7 +42,7 @@ CommandShortcutEventHandler _arrowRightCommandHandler = (editorState) { // move the cursor to the end of the block final CommandShortcutEvent moveCursorToEndCommand = CommandShortcutEvent( key: 'move the cursor to the end of line', - command: 'ctrl+arrow right,end', + command: 'end', macOSCommand: 'cmd+arrow right', handler: _moveCursorToEndCommandHandler, ); @@ -64,7 +64,8 @@ CommandShortcutEventHandler _moveCursorToEndCommandHandler = (editorState) { // move the cursor to the right word final CommandShortcutEvent moveCursorToRightWordCommand = CommandShortcutEvent( key: 'move the cursor to the right word', - command: 'alt+arrow right', + command: 'ctrl+arrow right', + macOSCommand: 'alt+arrow right', handler: _moveCursorToRightWordCommandHandler, ); @@ -86,7 +87,8 @@ CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = final CommandShortcutEvent moveCursorRightWordSelectCommand = CommandShortcutEvent( key: 'move the cursor to select the right word', - command: 'alt+shift+arrow right', + command: 'ctrl+shift+arrow right', + macOSCommand: 'alt+shift+arrow right', handler: _moveCursorRightWordSelectCommandHandler, ); @@ -116,7 +118,7 @@ CommandShortcutEventHandler _moveCursorRightWordSelectCommandHandler = }; // arrow right key + shift -// +// selects only one character final CommandShortcutEvent moveCursorRightSelectCommand = CommandShortcutEvent( key: 'move the cursor right select', command: 'shift+arrow right', @@ -146,8 +148,8 @@ CommandShortcutEventHandler _moveCursorRightSelectCommandHandler = // arrow right key + shift + ctrl or cmd final CommandShortcutEvent moveCursorEndSelectCommand = CommandShortcutEvent( - key: 'move the cursor right select', - command: 'ctrl+shift+arrow right', + key: 'move cursor to select till end of line', + command: 'shift+end', macOSCommand: 'cmd+shift+arrow right', handler: _moveCursorEndSelectCommandHandler, ); diff --git a/test/new/service/shortcuts/command_shortcut_events/arrow_left_command_test.dart b/test/new/service/shortcuts/command_shortcut_events/arrow_left_command_test.dart index 51cef4da6..4580539a2 100644 --- a/test/new/service/shortcuts/command_shortcut_events/arrow_left_command_test.dart +++ b/test/new/service/shortcuts/command_shortcut_events/arrow_left_command_test.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -150,5 +152,127 @@ void main() async { ); } }); + + // Before + // Welcome| to AppFlowy Editor 🔥! + // After + // Welcom|e| to AppFlowy Editor 🔥! + testWidgets('press shift + arrow left to select left character', + (tester) async { + final editor = tester.editor + ..addParagraph( + initialText: text, + ); + await editor.startTesting(); + + const initialOffset = 'Welcome'.length; + final selection = Selection.collapse([0], initialOffset); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowLeft, + isShiftPressed: true, + ); + + expect( + editor.selection, + Selection.single( + path: [0], + startOffset: initialOffset, + endOffset: initialOffset - 1, + ), + ); + + await editor.dispose(); + }); + + // Before + // Welcome to AppFlowy Editor| 🔥! + // After on Mac + // |Welcome to AppFlowy Editor 🔥! + // After on Windows & Linux + // Welcome to AppFlowy |Editor 🔥! + testWidgets('''press the ctrl+arrow left key, + on windows & linux it should move to the start of a word, + on mac it should move the cursor to the start of the line + ''', (tester) async { + final editor = tester.editor + ..addParagraphs( + 2, + initialText: text, + ); + await editor.startTesting(); + + const initialOffset = 26; + final selection = Selection.collapse( + [1], + initialOffset, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowLeft, + isControlPressed: Platform.isWindows || Platform.isLinux, + isMetaPressed: Platform.isMacOS, + ); + + const expectedOffset = initialOffset - "Editor".length; + if (Platform.isMacOS) { + expect(editor.selection, Selection.collapse([1], 0)); + } else { + expect(editor.selection, Selection.collapse([1], expectedOffset)); + } + + await editor.dispose(); + }); + + testWidgets('''press the ctrl+shift+arrow left key, + on windows & linux it should move to the start of a word and select it, + on mac it should move the cursor to the start of the line and select it + ''', (tester) async { + final editor = tester.editor + ..addParagraphs( + 2, + initialText: text, + ); + await editor.startTesting(); + const initialOffset = 26; + + final selection = Selection.collapse( + [1], + initialOffset, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowLeft, + isControlPressed: Platform.isWindows || Platform.isLinux, + isMetaPressed: Platform.isMacOS, + isShiftPressed: true, + ); + + const expectedOffset = initialOffset - "Editor".length; + if (Platform.isMacOS) { + expect( + editor.selection, + Selection.single( + path: [1], + startOffset: initialOffset, + endOffset: 0, + ), + ); + } else { + expect( + editor.selection, + Selection.single( + path: [1], + startOffset: initialOffset, + endOffset: expectedOffset, + ), + ); + } + + await editor.dispose(); + }); }); } diff --git a/test/new/service/shortcuts/command_shortcut_events/arrow_right_command_test.dart b/test/new/service/shortcuts/command_shortcut_events/arrow_right_command_test.dart index 46826d4b2..179c0f804 100644 --- a/test/new/service/shortcuts/command_shortcut_events/arrow_right_command_test.dart +++ b/test/new/service/shortcuts/command_shortcut_events/arrow_right_command_test.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -136,5 +138,131 @@ void main() async { ); } }); + + // Before + // Welcom|e to AppFlowy Editor 🔥! + // After + // Welcom|e| to AppFlowy Editor 🔥! + testWidgets('press shift + arrow right to select right character', + (tester) async { + final editor = tester.editor + ..addParagraph( + initialText: text, + ); + await editor.startTesting(); + + const initialOffset = 'Welcom'.length; + final selection = Selection.collapse([0], initialOffset); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowRight, + isShiftPressed: true, + ); + + expect( + editor.selection, + Selection.single( + path: [0], + startOffset: initialOffset, + endOffset: initialOffset + 1, + ), + ); + + await editor.dispose(); + }); + + // Before + // |Welcome to AppFlowy Editor 🔥! + // After on Mac + // Welcome to AppFlowy Editor 🔥!| + // After on Windows & Linux + // Welcome| to AppFlowy Editor 🔥! + testWidgets('''press the ctrl+arrow right key, + on windows & linux it should move to the end of a word, + on mac it should move the cursor to the end of the line + ''', (tester) async { + final editor = tester.editor + ..addParagraphs( + 2, + initialText: text, + ); + await editor.startTesting(); + + final selection = Selection.collapse( + [1], + 0, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowRight, + isControlPressed: Platform.isWindows || Platform.isLinux, + isMetaPressed: Platform.isMacOS, + ); + + const expectedOffset = 'Welcome'.length; + if (Platform.isMacOS) { + expect(editor.selection, Selection.collapse([1], text.length)); + } else { + expect(editor.selection, Selection.collapse([1], expectedOffset)); + } + + await editor.dispose(); + }); + + // Before + // |Welcome to AppFlowy Editor 🔥! + // After on Mac + // |Welcome to AppFlowy Editor 🔥!| + // After on Windows & Linux + // |Welcome| to AppFlowy Editor 🔥! + testWidgets('''press the ctrl+shift+arrow right key, + on windows & linux it should move to the end of a word and select it, + on mac it should move the cursor to the end of the line and select it + ''', (tester) async { + final editor = tester.editor + ..addParagraphs( + 2, + initialText: text, + ); + await editor.startTesting(); + + final selection = Selection.collapse( + [1], + 0, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.arrowRight, + isControlPressed: Platform.isWindows || Platform.isLinux, + isMetaPressed: Platform.isMacOS, + isShiftPressed: true, + ); + + const expectedOffset = 'Welcome'.length; + if (Platform.isMacOS) { + expect( + editor.selection, + Selection.single( + path: [1], + startOffset: 0, + endOffset: text.length, + ), + ); + } else { + expect( + editor.selection, + Selection.single( + path: [1], + startOffset: 0, + endOffset: expectedOffset, + ), + ); + } + + await editor.dispose(); + }); }); } diff --git a/test/new/service/shortcuts/command_shortcut_events/end_command_test.dart b/test/new/service/shortcuts/command_shortcut_events/end_command_test.dart index 6abca9f3d..1d70c86b5 100644 --- a/test/new/service/shortcuts/command_shortcut_events/end_command_test.dart +++ b/test/new/service/shortcuts/command_shortcut_events/end_command_test.dart @@ -69,5 +69,43 @@ void main() async { await editor.dispose(); }); + + //shift+end is only supported in windows and linux + // Before + // |Welcome to AppFlowy Editor 🔥! + // After + // |Welcome to AppFlowy Editor 🔥!| + testWidgets('press the shift + home to select till beginning of line', + (tester) async { + if (!Platform.isMacOS) { + final editor = tester.editor + ..addParagraph( + initialText: text, + ); + await editor.startTesting(); + + final selection = Selection.collapse( + [0], + 0, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.end, + isShiftPressed: true, + ); + + expect( + editor.selection, + Selection.single( + path: [0], + startOffset: 0, + endOffset: text.length, + ), + ); + + await editor.dispose(); + } + }); }); } diff --git a/test/new/service/shortcuts/command_shortcut_events/home_command_test.dart b/test/new/service/shortcuts/command_shortcut_events/home_command_test.dart index 521a23bc2..b1acb50e4 100644 --- a/test/new/service/shortcuts/command_shortcut_events/home_command_test.dart +++ b/test/new/service/shortcuts/command_shortcut_events/home_command_test.dart @@ -70,5 +70,43 @@ void main() async { await editor.dispose(); }); + + //shift+home is only supported in windows and linux + // Before + // Welcome to AppFlowy Editor 🔥!| + // After + // |Welcome to AppFlowy Editor 🔥!| + testWidgets('press the shift + home to select till beginning of line', + (tester) async { + if (!Platform.isMacOS) { + final editor = tester.editor + ..addParagraph( + initialText: text, + ); + await editor.startTesting(); + + final selection = Selection.collapse( + [0], + text.length, + ); + await editor.updateSelection(selection); + + await editor.pressKey( + key: LogicalKeyboardKey.home, + isShiftPressed: true, + ); + + expect( + editor.selection, + Selection.single( + path: [0], + startOffset: text.length, + endOffset: 0, + ), + ); + + await editor.dispose(); + } + }); }); } diff --git a/test/service/internal_key_event_handlers/arrow_keys_handler_test.dart b/test/service/internal_key_event_handlers/arrow_keys_handler_test.dart index e3f0dc1e2..07beb46f0 100644 --- a/test/service/internal_key_event_handlers/arrow_keys_handler_test.dart +++ b/test/service/internal_key_event_handlers/arrow_keys_handler_test.dart @@ -116,8 +116,10 @@ void main() async { await editor.dispose(); }); - testWidgets('Presses alt + arrow right key, move the cursor one word right', - (tester) async { + testWidgets('''move the cursor one word right + MacOS: presses alt + arrow right key, + Windows & Linux: presses ctrl + arrow right key, + ''', (tester) async { const text = 'Welcome to Appflowy'; final editor = tester.editor..addParagraphs(2, initialText: text); await editor.startTesting(); @@ -127,10 +129,7 @@ void main() async { selection, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowRight, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor); expect( editor.selection, @@ -143,10 +142,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowRight, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor); + expect( editor.selection, Selection.collapse( @@ -158,10 +155,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowRight, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor); + expect( editor.selection, Selection.collapse( @@ -173,8 +168,10 @@ void main() async { await editor.dispose(); }); - testWidgets('Presses alt + arrow left key, move the cursor one word left', - (tester) async { + testWidgets('''move the cursor one word left + MacOS: presses alt + arrow left key, + Windows & Linux: presses ctrl + arrow left key, + ''', (tester) async { const text = 'Welcome to Appflowy'; final editor = tester.editor..addParagraphs(2, initialText: text); await editor.startTesting(); @@ -184,10 +181,8 @@ void main() async { selection, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowLeft, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor, toRight: false); + expect( editor.selection, Selection.collapse( @@ -199,10 +194,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowLeft, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor, toRight: false); + expect( editor.selection, Selection.collapse( @@ -214,10 +207,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, ); - await editor.pressKey( - key: LogicalKeyboardKey.arrowLeft, - isAltPressed: true, - ); + await _pressShortcutToNavigateAroundWord(editor, toRight: false); + expect( editor.selection, Selection.collapse( @@ -458,6 +449,17 @@ void main() async { await editor.dispose(); }); + //Before: + //Welcome |to Appflowy 😁 + //Welcome to Appflowy 😁 + //After On Mac + //Welcome |to Appflowy 😁 + //Welcome to Appflowy 😁| + //expands the selection to include till end of line + //After On Windows + //Welcome |to Appflowy 😁 + //Welcome to| Appflowy 😁 + //expands the selection to include till end of next word testWidgets('Presses shift + arrow down and meta/ctrl + shift + right', (tester) async { const text = 'Welcome to Appflowy 😁'; @@ -475,15 +477,35 @@ void main() async { isControlPressed: Platform.isWindows || Platform.isLinux, isMetaPressed: Platform.isMacOS, ); - expect( - editor.selection, - selection.copyWith( - end: Position(path: [1], offset: text.length), - ), - ); + if (Platform.isMacOS) { + expect( + editor.selection, + selection.copyWith( + end: Position(path: [1], offset: text.length), + ), + ); + } else { + expect( + editor.selection, + selection.copyWith( + end: Position(path: [1], offset: 'Welcome to'.length), + ), + ); + } await editor.dispose(); }); + //Before: + //Welcome to Appflowy 😁 + //Welcome |to Appflowy 😁 + //After On Mac + //|Welcome to Appflowy 😁 + //Welcome |to Appflowy 😁 + //expands the selection to include till end of line + //After On Windows + //Welcome| to Appflowy 😁 + //Welcome |to Appflowy 😁 + //expands the selection to include till end of next word testWidgets('Presses shift + arrow up and meta/ctrl + shift + left', (tester) async { const text = 'Welcome to Appflowy 😁'; @@ -501,16 +523,27 @@ void main() async { isControlPressed: Platform.isWindows || Platform.isLinux, isMetaPressed: Platform.isMacOS, ); - expect( - editor.selection, - selection.copyWith( - end: Position(path: [0], offset: 0), - ), - ); + if (Platform.isMacOS) { + expect( + editor.selection, + selection.copyWith( + end: Position(path: [0], offset: 0), + ), + ); + } else { + expect( + editor.selection, + selection.copyWith( + end: Position(path: [0], offset: 'Welcome'.length), + ), + ); + } await editor.dispose(); }); - testWidgets('Presses shift + alt + arrow left to select a word', + // press alt on MacOS to select a word + // press ctrl on Windows and Linux to select a word + testWidgets('Presses shift + alt/ctrl + arrow left to select a word', (tester) async { const text = 'Welcome to Appflowy 😁'; final editor = tester.editor..addParagraphs(2, initialText: text); @@ -520,7 +553,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // expect( @@ -532,7 +566,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // < to> expect( @@ -544,7 +579,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // expect( @@ -556,7 +592,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowLeft, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // <😁> // @@ -569,7 +606,9 @@ void main() async { await editor.dispose(); }); - testWidgets('Presses shift + alt + arrow right to select a word', + // press alt on MacOS to select a word + // press ctrl on Windows and Linux to select a word + testWidgets('Presses shift + alt/ctrl + arrow right to select a word', (tester) async { const text = 'Welcome to Appflowy 😁'; final editor = tester.editor..addParagraphs(2, initialText: text); @@ -579,7 +618,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // < > expect( @@ -591,7 +631,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // < Appflowy> expect( @@ -603,12 +644,14 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); await editor.pressKey( key: LogicalKeyboardKey.arrowRight, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // < Appflowy 😁> expect( @@ -620,7 +663,8 @@ void main() async { await editor.pressKey( key: LogicalKeyboardKey.arrowRight, isShiftPressed: true, - isAltPressed: true, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, ); // < Appflowy 😁> // <> @@ -643,12 +687,14 @@ Future _testPressArrowKeyWithMetaInSelection( final editor = tester.editor..addParagraphs(2, initialText: text); await editor.startTesting(); + final initialSelection = Selection.single(path: [0], startOffset: 8); + final selectionAtBeginning = Selection.single(path: [0], startOffset: 0); + final selectionAtEnd = Selection.single(path: [0], startOffset: text.length); + final selectionAtEndOfWelcome = Selection.single(path: [0], startOffset: 7); + Selection selection; if (isSingle) { - selection = Selection.single( - path: [0], - startOffset: 8, - ); + selection = initialSelection; } else { if (isBackward) { selection = Selection.single( @@ -664,6 +710,7 @@ Future _testPressArrowKeyWithMetaInSelection( ); } } + await editor.updateSelection(selection); await editor.pressKey( @@ -672,10 +719,15 @@ Future _testPressArrowKeyWithMetaInSelection( isMetaPressed: Platform.isMacOS, ); - expect( - editor.selection, - Selection.single(path: [0], startOffset: 0), - ); + if (Platform.isMacOS) { + expect(editor.selection, selectionAtBeginning); + } else if (isSingle) { + expect(editor.selection, selectionAtEndOfWelcome); + } else { + expect(editor.selection, initialSelection); + } + + await editor.updateSelection(selectionAtBeginning); await editor.pressKey( key: LogicalKeyboardKey.arrowRight, @@ -683,10 +735,13 @@ Future _testPressArrowKeyWithMetaInSelection( isMetaPressed: Platform.isMacOS, ); - expect( - editor.selection, - Selection.single(path: [0], startOffset: text.length), - ); + if (Platform.isMacOS) { + expect(editor.selection, selectionAtEnd); + } else { + expect(editor.selection, selectionAtEndOfWelcome); + } + + await editor.updateSelection(selectionAtEnd); await editor.pressKey( key: LogicalKeyboardKey.arrowUp, @@ -696,7 +751,7 @@ Future _testPressArrowKeyWithMetaInSelection( expect( editor.selection, - Selection.single(path: [0], startOffset: 0), + selectionAtBeginning, ); await editor.pressKey( @@ -737,3 +792,14 @@ Future _testPressArrowKeyInNotCollapsedSelection( await editor.dispose(); } + +Future _pressShortcutToNavigateAroundWord( + TestableEditor editor, { + bool toRight = true, +}) async { + await editor.pressKey( + key: toRight ? LogicalKeyboardKey.arrowRight : LogicalKeyboardKey.arrowLeft, + isAltPressed: Platform.isMacOS, + isControlPressed: Platform.isWindows || Platform.isLinux, + ); +} diff --git a/test/service/shortcut_event/shortcut_event_test.dart b/test/service/shortcut_event/shortcut_event_test.dart index 94362eea1..037c3cc94 100644 --- a/test/service/shortcut_event/shortcut_event_test.dart +++ b/test/service/shortcut_event/shortcut_event_test.dart @@ -42,10 +42,12 @@ void main() async { await editor.updateSelection(selection); await editor.pressKey( - key: LogicalKeyboardKey.arrowLeft, - isControlPressed: Platform.isWindows || Platform.isLinux, + key: Platform.isMacOS + ? LogicalKeyboardKey.arrowLeft + : LogicalKeyboardKey.home, isMetaPressed: Platform.isMacOS, ); + expect( editor.selection, Selection.single(path: [1], startOffset: 0), @@ -83,10 +85,12 @@ void main() async { await editor.updateSelection(selection); await editor.pressKey( - key: LogicalKeyboardKey.arrowRight, - isControlPressed: Platform.isWindows || Platform.isLinux, + key: Platform.isMacOS + ? LogicalKeyboardKey.arrowRight + : LogicalKeyboardKey.end, isMetaPressed: Platform.isMacOS, ); + expect( editor.selection, Selection.single(path: [1], startOffset: text.length),