Skip to content

Commit

Permalink
feat: support for shift+home/end keys (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
MayurSMahajan authored Jul 13, 2023
1 parent 87fd7ed commit 6830014
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);

Expand All @@ -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,
);

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);

Expand All @@ -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,
);

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
});
});
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
});
});
}
Loading

0 comments on commit 6830014

Please sign in to comment.