Skip to content

Commit

Permalink
fix: shift+enter should wrap the line in list (#990)
Browse files Browse the repository at this point in the history
* fix: shift+enter should wrap the line in list

* fix: unit test

* fix: select all command
  • Loading branch information
LucasXu0 authored Dec 13, 2024
1 parent ea1bcd3 commit ca04a67
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<bool> insertNewLineInType(
EditorState editorState,
String type, {
Attributes attributes = const {},
}) async {
// check if the shift key is pressed, if so, we should return false to let the system handle it.
final isShiftPressed = HardwareKeyboard.instance.isShiftPressed;
if (isShiftPressed) {
return false;
}

final selection = editorState.selection;
if (selection == null || !selection.isCollapsed) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Future<bool> executeCharacterShortcutEvent(
for (final shortcutEvent in characterShortcutEvents) {
if (shortcutEvent.character == character &&
await shortcutEvent.handler(editorState)) {
AppFlowyEditorLog.input.debug(
'keyboard service - handled by character shortcut event: $shortcutEvent',
);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ CommandShortcutEventHandler _backspaceCommandHandler = (editorState) {
return KeyEventResult.ignored;
}

final reason = editorState.selectionUpdateReason;

if (selectionType == SelectionType.block) {
return _backspaceInBlockSelection(editorState);
} else if (selection.isCollapsed) {
return _backspaceInCollapsedSelection(editorState);
} else if (reason == SelectionUpdateReason.selectAll) {
return _backspaceInSelectAll(editorState);
} else {
return _backspaceInNotCollapsedSelection(editorState);
}
Expand Down Expand Up @@ -163,3 +167,17 @@ CommandShortcutEventHandler _backspaceInBlockSelection = (editorState) {

return KeyEventResult.handled;
};

CommandShortcutEventHandler _backspaceInSelectAll = (editorState) {
final selection = editorState.selection;
if (selection == null) {
return KeyEventResult.ignored;
}

final transaction = editorState.transaction;
final nodes = editorState.getNodesInSelection(selection);
transaction.deleteNodes(nodes);
editorState.apply(transaction);

return KeyEventResult.handled;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ CommandShortcutEventHandler _selectAllCommandHandler = (editorState) {
editorState.selection == null) {
return KeyEventResult.ignored;
}
final firstSelectable = editorState.getFirstSelectable();
final lastSelectable = editorState.getLastSelectable();
if (firstSelectable == null || lastSelectable == null) {
if (lastSelectable == null) {
return KeyEventResult.handled;
}
final start = firstSelectable.$2.start(firstSelectable.$1);
final start = Position(path: [0]);
final end = lastSelectable.$2.end(lastSelectable.$1);
editorState.updateSelectionWithReason(
Selection(start: start, end: end),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import 'package:flutter_test/flutter_test.dart';

void main() {
group('InsertNewlineInTypeCommand tests', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
});

test('bulleted list', () async {
final bulletedNode = bulletedListNode(
text: 'bulleted list 1',
Expand Down

0 comments on commit ca04a67

Please sign in to comment.