feat: select all

This commit is contained in:
Vincent Chan 2022-08-11 16:40:07 +08:00
parent 4794203e74
commit c42f9001c4
3 changed files with 28 additions and 1 deletions

View File

@ -1,5 +1,4 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flowy_editor/src/service/keyboard_service.dart';
import 'package:flowy_editor/src/infra/html_converter.dart';
import 'package:flowy_editor/src/document/node_iterator.dart';
import 'package:flutter/material.dart';

View File

@ -7,6 +7,7 @@ import 'package:flowy_editor/src/service/internal_key_event_handlers/redo_undo_h
import 'package:flowy_editor/src/service/internal_key_event_handlers/slash_handler.dart';
import 'package:flowy_editor/src/service/internal_key_event_handlers/update_text_style_by_command_x_handler.dart';
import 'package:flowy_editor/src/service/internal_key_event_handlers/whitespace_handler.dart';
import 'package:flowy_editor/src/service/internal_key_event_handlers/select_all_handler.dart';
import 'package:flowy_editor/src/service/keyboard_service.dart';
List<FlowyKeyEventHandler> defaultKeyEventHandlers = [
@ -19,4 +20,5 @@ List<FlowyKeyEventHandler> defaultKeyEventHandlers = [
enterWithoutShiftInTextNodesHandler,
updateTextStyleByCommandXHandler,
whiteSpaceHandler,
selectAllHandler,
];

View File

@ -0,0 +1,26 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
KeyEventResult _selectAll(EditorState editorState) {
if (editorState.document.root.children.isEmpty) {
return KeyEventResult.handled;
}
final firstNode = editorState.document.root.children.first;
final lastNode = editorState.document.root.children.last;
var offset = 0;
if (lastNode is TextNode) {
offset = lastNode.delta.length;
}
editorState.updateCursorSelection(Selection(
start: Position(path: firstNode.path, offset: 0),
end: Position(path: lastNode.path, offset: offset)));
return KeyEventResult.handled;
}
FlowyKeyEventHandler selectAllHandler = (editorState, event) {
if (event.isMetaPressed && event.logicalKey == LogicalKeyboardKey.keyA) {
return _selectAll(editorState);
}
return KeyEventResult.ignored;
};