Merge pull request #1316 from LucasXu0/escape

feat: implement new shortcut: press Escape to exit editing mode
This commit is contained in:
Lucas.Xu 2022-10-18 16:45:08 +07:00 committed by GitHub
commit 056441bb39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import 'package:appflowy_editor/src/service/shortcut_event/shortcut_event_handler.dart';
import 'package:flutter/material.dart';
ShortcutEventHandler exitEditingModeEventHandler = (editorState, event) {
editorState.service.selectionService.clearSelection();
return KeyEventResult.handled;
};

View File

@ -4,6 +4,7 @@ import 'package:appflowy_editor/src/service/internal_key_event_handlers/arrow_ke
import 'package:appflowy_editor/src/service/internal_key_event_handlers/backspace_handler.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/copy_paste_handler.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/exit_editing_mode_handler.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/page_up_down_handler.dart';
import 'package:appflowy_editor/src/service/internal_key_event_handlers/redo_undo_handler.dart';
@ -276,6 +277,11 @@ List<ShortcutEvent> builtInShortcutEvents = [
command: 'shift+parenthesis right',
handler: markdownLinkOrImageHandler,
),
ShortcutEvent(
key: 'Exit editing mode',
command: 'escape',
handler: exitEditingModeEventHandler,
),
// https://github.com/flutter/flutter/issues/104944
// Workaround: Using space editing on the web platform often results in errors,
// so adding a shortcut event to handle the space input instead of using the

View File

@ -0,0 +1,50 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
void main() async {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
group('exit_editing_mode_handler.dart', () {
testWidgets('Exit editing mode', (tester) async {
const text = 'Welcome to Appflowy 😁';
const lines = 3;
final editor = tester.editor;
for (var i = 0; i < lines; i++) {
editor.insertTextNode(text);
}
await editor.startTesting();
// collaspsed selection
await _testSelection(editor, Selection.single(path: [1], startOffset: 0));
// single selection
await _testSelection(
editor,
Selection.single(path: [1], startOffset: 0, endOffset: text.length),
);
// mutliple selection
await _testSelection(
editor,
Selection(
start: Position(path: [0], offset: 0),
end: Position(path: [2], offset: text.length),
),
);
});
// Future<void> _testSelection()
});
}
Future<void> _testSelection(
EditorWidgetTester editor, Selection selection) async {
await editor.updateSelection(selection);
expect(editor.documentSelection, selection);
await editor.pressLogicKey(LogicalKeyboardKey.escape);
expect(editor.documentSelection, null);
}