Skip to content

Commit

Permalink
feat: delete sentence shortcut
Browse files Browse the repository at this point in the history
Closes: #4
  • Loading branch information
Xazin committed Mar 25, 2023
1 parent 16b64cf commit f17b6cb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ ShortcutEventHandler cursorBeginSelect = (editorState, event) {
if (position != null) {
end = position;
}

editorState.service.selectionService.updateSelection(
selection.copyWith(start: start, end: end),
);
Expand Down Expand Up @@ -349,6 +350,45 @@ ShortcutEventHandler cursorLeftWordDelete = (editorState, event) {
return KeyEventResult.handled;
};

ShortcutEventHandler cursorLeftSentenceDelete = (editorState, event) {
final nodes = editorState.service.selectionService.currentSelectedNodes;
final selection = editorState.service.selectionService.currentSelection.value;
if (nodes.isEmpty || selection == null) {
return KeyEventResult.ignored;
}

if (nodes.length == 1 && nodes.first is TextNode) {
final textNode = nodes.first as TextNode;
if (textNode.toPlainText().isEmpty) {
return KeyEventResult.ignored;
}
}

final deleteTransaction = editorState.transaction;
deleteTransaction.deleteNodes(
editorState.service.selectionService.getNodesInSelection(selection),
);
editorState.apply(deleteTransaction, withUpdateCursor: false);

final cursorPosition =
selection.start.copyWith(offset: 0).goLeft(editorState);
if (cursorPosition != null) {
final next = cursorPosition.path.next;
final transaction = editorState.transaction
..insertNode(
next,
TextNode.empty(),
)
..afterSelection = Selection.collapsed(
Position(path: next, offset: 0),
);

editorState.apply(transaction);
}

return KeyEventResult.handled;
};

enum _SelectionRange {
character,
word,
Expand Down
9 changes: 8 additions & 1 deletion lib/src/service/shortcut_event/built_in_shortcut_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ List<ShortcutEvent> builtInShortcutEvents = [
),
ShortcutEvent(
key: 'Cursor word delete',
command: 'meta+backspace',
command: 'alt+backspace',
windowsCommand: 'ctrl+backspace',
linuxCommand: 'ctrl+backspace',
handler: cursorLeftWordDelete,
),
ShortcutEvent(
key: 'Cursor sentence delete',
command: 'meta+backspace',
windowsCommand: 'alt+backspace',
linuxCommand: 'alt+backspace',
handler: cursorLeftSentenceDelete,
),
ShortcutEvent(
key: 'Cursor left select',
command: 'shift+arrow left',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void main() async {
(tester) async {
await _deleteTextByBackspace(tester, true);
});

testWidgets(
'Presses backspace key in non-empty document and selection is forward',
(tester) async {
Expand All @@ -85,6 +86,7 @@ void main() async {
(tester) async {
await _deleteTextByDelete(tester, true);
});

testWidgets(
'Presses delete key in non-empty document and selection is forward',
(tester) async {
Expand Down Expand Up @@ -135,14 +137,17 @@ void main() async {
(tester) async {
await _deleteStyledTextByBackspace(tester, BuiltInAttributeKey.checkbox);
});

testWidgets('Presses backspace key in styled text (bulletedList)',
(tester) async {
await _deleteStyledTextByBackspace(
tester, BuiltInAttributeKey.bulletedList);
});

testWidgets('Presses backspace key in styled text (heading)', (tester) async {
await _deleteStyledTextByBackspace(tester, BuiltInAttributeKey.heading);
});

testWidgets('Presses backspace key in styled text (quote)', (tester) async {
await _deleteStyledTextByBackspace(tester, BuiltInAttributeKey.quote);
});
Expand All @@ -161,13 +166,16 @@ void main() async {
testWidgets('Presses delete key in styled text (checkbox)', (tester) async {
await _deleteStyledTextByDelete(tester, BuiltInAttributeKey.checkbox);
});

testWidgets('Presses delete key in styled text (bulletedList)',
(tester) async {
await _deleteStyledTextByDelete(tester, BuiltInAttributeKey.bulletedList);
});

testWidgets('Presses delete key in styled text (heading)', (tester) async {
await _deleteStyledTextByDelete(tester, BuiltInAttributeKey.heading);
});

testWidgets('Presses delete key in styled text (quote)', (tester) async {
await _deleteStyledTextByDelete(tester, BuiltInAttributeKey.quote);
});
Expand Down Expand Up @@ -251,7 +259,7 @@ void main() async {
await editor.insertText(textNode, '#', 0);
await editor.pressLogicKey(LogicalKeyboardKey.space);
expect(
(editor.nodeAtPath([0]) as TextNode).attributes.heading,
textNode.attributes.heading,
BuiltInAttributeKey.h1,
);

Expand All @@ -264,7 +272,7 @@ void main() async {
await editor.insertText(textNode, '#', 0);
await editor.pressLogicKey(LogicalKeyboardKey.space);
expect(
(editor.nodeAtPath([0]) as TextNode).attributes.heading,
textNode.attributes.heading,
BuiltInAttributeKey.h1,
);
});
Expand Down Expand Up @@ -297,24 +305,31 @@ void main() async {
Selection.single(path: [0, 0, 0], startOffset: 0),
);
await editor.pressLogicKey(LogicalKeyboardKey.backspace);

expect(editor.nodeAtPath([0, 0, 0])?.subtype, null);

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

expect(editor.nodeAtPath([0, 1]) != null, true);

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

expect(editor.nodeAtPath([1]) != null, true);

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

// * Welcome to Appflowy 😁
// * Welcome to Appflowy 😁Welcome to Appflowy 😁
await editor.pressLogicKey(LogicalKeyboardKey.backspace);

expect(
editor.documentSelection,
Selection.single(path: [0, 0], startOffset: text.length),
Expand Down Expand Up @@ -357,18 +372,22 @@ void main() async {
Selection.single(path: [0, 1], startOffset: 0),
);
await editor.pressLogicKey(LogicalKeyboardKey.backspace);

expect(
editor.nodeAtPath([0, 1])!.subtype != BuiltInAttributeKey.bulletedList,
true,
);

expect(
editor.nodeAtPath([0, 1, 0])!.subtype,
BuiltInAttributeKey.bulletedList,
);

expect(
editor.nodeAtPath([0, 1, 1])!.subtype,
BuiltInAttributeKey.bulletedList,
);

expect(find.byType(FlowyRichText), findsNWidgets(5));

// Before
Expand All @@ -383,18 +402,22 @@ void main() async {
// * Welcome to Appflowy 😁
// * Welcome to Appflowy 😁
await editor.pressLogicKey(LogicalKeyboardKey.backspace);

expect(
editor.nodeAtPath([0, 0])!.subtype == BuiltInAttributeKey.bulletedList,
true,
);

expect(
(editor.nodeAtPath([0, 0]) as TextNode).toPlainText() == text * 2,
true,
);

expect(
editor.nodeAtPath([0, 1])!.subtype == BuiltInAttributeKey.bulletedList,
true,
);

expect(
editor.nodeAtPath([0, 2])!.subtype == BuiltInAttributeKey.bulletedList,
true,
Expand Down
Loading

0 comments on commit f17b6cb

Please sign in to comment.