From d5d0f8da1e4dc9a9d300e54d295b218581054707 Mon Sep 17 00:00:00 2001 From: Angelo Silvestre Date: Wed, 27 Nov 2024 15:11:33 -0300 Subject: [PATCH] [SuperEditor][Web] Fix option + arrow key selection changes (Resolves #2415) (#2418) --- .../document_keyboard_actions.dart | 3 +- ...pereditor_input_keyboard_actions_test.dart | 122 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/super_editor/lib/src/default_editor/document_hardware_keyboard/document_keyboard_actions.dart b/super_editor/lib/src/default_editor/document_hardware_keyboard/document_keyboard_actions.dart index 59cf22840e..ffca2c033a 100644 --- a/super_editor/lib/src/default_editor/document_hardware_keyboard/document_keyboard_actions.dart +++ b/super_editor/lib/src/default_editor/document_hardware_keyboard/document_keyboard_actions.dart @@ -627,7 +627,8 @@ ExecutionInstruction doNothingWithLeftRightArrowKeysAtMiddleOfTextOnWeb({ return ExecutionInstruction.continueExecution; } - if (defaultTargetPlatform == TargetPlatform.windows && HardwareKeyboard.instance.isAltPressed) { + if ((defaultTargetPlatform == TargetPlatform.windows || CurrentPlatform.isApple) && + HardwareKeyboard.instance.isAltPressed) { return ExecutionInstruction.continueExecution; } diff --git a/super_editor/test/super_editor/supereditor_input_keyboard_actions_test.dart b/super_editor/test/super_editor/supereditor_input_keyboard_actions_test.dart index 05668e6675..f199d48f47 100644 --- a/super_editor/test/super_editor/supereditor_input_keyboard_actions_test.dart +++ b/super_editor/test/super_editor/supereditor_input_keyboard_actions_test.dart @@ -523,6 +523,128 @@ void main() { ), ); }); + + testWidgetsOnMacDesktopAndWeb( + 'SHIFT + OPTION + LEFT ARROW: deselects word at end of line after selecting the whole line from start to end', + (tester) async { + await tester // + .createDocument() + .withCustomContent( + MutableDocument( + nodes: [ + ParagraphNode( + id: '1', + text: AttributedText('This is a paragraph'), + ), + ], + ), + ) + .pump(); + + // Place caret at the beginning of the paragraph. + await tester.placeCaretInParagraph('1', 0); + + // Press CMD + SHIFT + RIGHT ARROW to expand the selection to the end of the line. + await tester.pressShiftCmdRightArrow(); + + // Ensure that the whole line is selected. + expect( + SuperEditorInspector.findDocumentSelection(), + selectionEquivalentTo( + const DocumentSelection( + base: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 0), + ), + extent: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 19), + ), + ), + ), + ); + + // Press SHIFT + OPTION + LEFT ARROW to remove the last word from the selection. + await tester.pressShiftAltLeftArrow(); + + // Ensure that the last word was removed from the selection. + expect( + SuperEditorInspector.findDocumentSelection(), + selectionEquivalentTo( + const DocumentSelection( + base: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 0), + ), + extent: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 10), + ), + ), + ), + ); + }); + + testWidgetsOnMacDesktopAndWeb( + 'SHIFT + OPTION + RIGHT ARROW: deselects word at start of line after selecting the whole line from end to start', + (tester) async { + await tester // + .createDocument() + .withCustomContent( + MutableDocument( + nodes: [ + ParagraphNode( + id: '1', + text: AttributedText('This is a paragraph'), + ), + ], + ), + ) + .pump(); + + // Place caret at the end of the paragraph. + await tester.placeCaretInParagraph('1', 19); + + // Press CMD + SHIFT + LEFT ARROW to expand the selection to the beginning of the line. + await tester.pressShiftCmdLeftArrow(); + + // Ensure that the whole line is selected. + expect( + SuperEditorInspector.findDocumentSelection(), + selectionEquivalentTo( + const DocumentSelection( + base: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 19), + ), + extent: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 0), + ), + ), + ), + ); + + // Press SHIFT + OPTION + RIGHT ARROW to remove the first word from the selection. + await tester.pressShiftAltRightArrow(); + + // Ensure that the first word was removed from the selection. + expect( + SuperEditorInspector.findDocumentSelection(), + selectionEquivalentTo( + const DocumentSelection( + base: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 19), + ), + extent: DocumentPosition( + nodeId: "1", + nodePosition: TextNodePosition(offset: 4), + ), + ), + ), + ); + }); }); group("Windows and Linux >", () {