Skip to content

Commit

Permalink
feat: alt + arrow key to move cursor one word
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin committed Mar 22, 2023
1 parent 16b64cf commit 012449d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,62 @@ ShortcutEventHandler cursorLeftWordSelect = (editorState, event) {
return KeyEventResult.handled;
};

ShortcutEventHandler cursorLeftWordMove = (editorState, event) {
final nodes = editorState.service.selectionService.currentSelectedNodes;
final selection =
editorState.service.selectionService.currentSelection.value?.normalized;

if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}

if (selection.isCollapsed) {
final leftPosition = selection.start
.goLeft(editorState, selectionRange: _SelectionRange.word);
if (leftPosition != null) {
editorState.service.selectionService.updateSelection(
Selection.collapsed(leftPosition),
);

return KeyEventResult.handled;
}

editorState.service.selectionService.updateSelection(
Selection.collapsed(selection.start),
);
}

return KeyEventResult.handled;
};

ShortcutEventHandler cursorRightWordMove = (editorState, event) {
final nodes = editorState.service.selectionService.currentSelectedNodes;
final selection =
editorState.service.selectionService.currentSelection.value?.normalized;

if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}

if (selection.isCollapsed) {
final rightPosition = selection.start
.goRight(editorState, selectionRange: _SelectionRange.word);
if (rightPosition != null) {
editorState.service.selectionService.updateSelection(
Selection.collapsed(rightPosition),
);

return KeyEventResult.handled;
}

editorState.service.selectionService.updateSelection(
Selection.collapsed(selection.end),
);
}

return KeyEventResult.handled;
};

ShortcutEventHandler cursorRightWordSelect = (editorState, event) {
final nodes = editorState.service.selectionService.currentSelectedNodes;
final selection = editorState.service.selectionService.currentSelection.value;
Expand Down Expand Up @@ -363,13 +419,15 @@ extension on Position {
if (node == null) {
return null;
}

if (offset == 0) {
final previousEnd = node.previous?.selectable?.end();
if (previousEnd != null) {
return previousEnd;
}
return null;
}

switch (selectionRange) {
case _SelectionRange.character:
if (node is TextNode) {
Expand Down
14 changes: 14 additions & 0 deletions lib/src/service/shortcut_event/built_in_shortcut_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ List<ShortcutEvent> builtInShortcutEvents = [
linuxCommand: 'shift+alt+arrow left',
handler: cursorLeftWordSelect,
),
ShortcutEvent(
key: 'Move cursor left one word',
command: 'alt+arrow left',
windowsCommand: 'alt+arrow left',
linuxCommand: 'alt+arrow left',
handler: cursorLeftWordMove,
),
ShortcutEvent(
key: 'Move cursor right one word',
command: 'alt+arrow right',
windowsCommand: 'alt+arrow right',
linuxCommand: 'alt+arrow right',
handler: cursorRightWordMove,
),
ShortcutEvent(
key: 'Cursor right word select',
command: 'shift+alt+arrow right',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,63 @@ void main() async {
});
});

testWidgets(
'Presses alt + arrow left/right key, move the cursor one word left/right',
(tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor
..insertTextNode(text)
..insertTextNode(text);
await editor.startTesting();

await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);

await editor.pressLogicKey(
LogicalKeyboardKey.arrowRight,
isAltPressed: true,
);

expect(
editor.documentSelection,
Selection.single(
path: [0],
startOffset: 7,
),
);

await editor.pressLogicKey(
LogicalKeyboardKey.arrowRight,
);

await editor.pressLogicKey(
LogicalKeyboardKey.arrowRight,
isAltPressed: true,
);

expect(
editor.documentSelection,
Selection.single(
path: [0],
startOffset: 10,
),
);

await editor.pressLogicKey(
LogicalKeyboardKey.arrowLeft,
isAltPressed: true,
);

expect(
editor.documentSelection,
Selection.single(
path: [0],
startOffset: 8,
),
);
});

testWidgets(
'Presses arrow left/right key since selection is not collapsed and backward',
(tester) async {
Expand Down

0 comments on commit 012449d

Please sign in to comment.