From 39e39cf9f25528b83cca07785d8948691e582c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=A3=8A?= <200892907@qq.com> Date: Thu, 9 Nov 2023 16:04:51 +0800 Subject: [PATCH 1/2] fix:Solve the left and right key bug. When adding an input box at the head or tail of the editor, it cannot be moved. --- .../service/keyboard_service_widget.dart | 22 +++++---------- .../shortcuts/command/arrow_left_command.dart | 27 ++++++++++--------- .../command/arrow_right_command.dart | 24 +++++++++-------- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/lib/src/editor/editor_component/service/keyboard_service_widget.dart b/lib/src/editor/editor_component/service/keyboard_service_widget.dart index e0ec0b9b0..61bc115c6 100644 --- a/lib/src/editor/editor_component/service/keyboard_service_widget.dart +++ b/lib/src/editor/editor_component/service/keyboard_service_widget.dart @@ -26,8 +26,7 @@ class KeyboardServiceWidget extends StatefulWidget { } @visibleForTesting -class KeyboardServiceWidgetState extends State - implements AppFlowyKeyboardService { +class KeyboardServiceWidgetState extends State implements AppFlowyKeyboardService { late final SelectionGestureInterceptor interceptor; late final EditorState editorState; late final TextInputService textInputService; @@ -52,8 +51,7 @@ class KeyboardServiceWidgetState extends State return true; }, ); - editorState.service.selectionService - .registerGestureInterceptor(interceptor); + editorState.service.selectionService.registerGestureInterceptor(interceptor); textInputService = NonDeltaTextInputService( onInsert: (insertion) async => await onInsert( @@ -143,8 +141,7 @@ class KeyboardServiceWidgetState extends State if (kIsWeb) { child = Shortcuts( shortcuts: { - LogicalKeySet(LogicalKeyboardKey.space): - const DoNothingAndStopPropagationIntent(), + LogicalKeySet(LogicalKeyboardKey.space): const DoNothingAndStopPropagationIntent(), }, child: child, ); @@ -182,8 +179,7 @@ class KeyboardServiceWidgetState extends State } void _onSelectionChanged() { - final doNotAttach = editorState - .selectionExtraInfo?[selectionExtraInfoDoNotAttachTextService]; + final doNotAttach = editorState.selectionExtraInfo?[selectionExtraInfoDoNotAttachTextService]; if (doNotAttach == true) { return; } @@ -228,13 +224,10 @@ class KeyboardServiceWidgetState extends State // based on the given selection. TextEditingValue? _getCurrentTextEditingValue(Selection selection) { // Get all the editable nodes in the selection. - final editableNodes = editorState - .getNodesInSelection(selection) - .where((element) => element.delta != null); + final editableNodes = editorState.getNodesInSelection(selection).where((element) => element.delta != null); // Get the composing text range. - final composingTextRange = - textInputService.composingTextRange ?? TextRange.empty; + final composingTextRange = textInputService.composingTextRange ?? TextRange.empty; if (editableNodes.isNotEmpty) { // Get the text by concatenating all the editable nodes in the selection. var text = editableNodes.fold( @@ -274,8 +267,7 @@ class KeyboardServiceWidgetState extends State return; } } - final children = - WidgetsBinding.instance.focusManager.primaryFocus?.children; + final children = WidgetsBinding.instance.focusManager.primaryFocus?.children; if (children != null && !children.contains(focusNode)) { editorState.selection = null; } diff --git a/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart b/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart index 21d8383a7..4b3d97343 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart @@ -26,6 +26,10 @@ final CommandShortcutEvent moveCursorLeftCommand = CommandShortcutEvent( ); CommandShortcutEventHandler _arrowLeftCommandHandler = (editorState) { + final selection = editorState.selection; + if (selection == null) { + return KeyEventResult.ignored; + } if (isRTL(editorState)) { editorState.moveCursorBackward(SelectionMoveRange.character); } else { @@ -44,6 +48,10 @@ final CommandShortcutEvent moveCursorToBeginCommand = CommandShortcutEvent( ); CommandShortcutEventHandler _moveCursorToBeginCommandHandler = (editorState) { + final selection = editorState.selection; + if (selection == null) { + return KeyEventResult.ignored; + } if (isRTL(editorState)) { editorState.moveCursorBackward(SelectionMoveRange.line); } else { @@ -61,8 +69,7 @@ final CommandShortcutEvent moveCursorToLeftWordCommand = CommandShortcutEvent( handler: _moveCursorToLeftWordCommandHandler, ); -CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -112,16 +119,14 @@ CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = }; // arrow left key + alt + shift -final CommandShortcutEvent moveCursorLeftWordSelectCommand = - CommandShortcutEvent( +final CommandShortcutEvent moveCursorLeftWordSelectCommand = CommandShortcutEvent( key: 'move the cursor to select the left word', command: 'ctrl+shift+arrow left', macOSCommand: 'alt+shift+arrow left', handler: _moveCursorLeftWordSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorLeftWordSelectCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorLeftWordSelectCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -153,8 +158,7 @@ final CommandShortcutEvent moveCursorLeftSelectCommand = CommandShortcutEvent( handler: _moveCursorLeftSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorLeftSelectCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorLeftSelectCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -182,8 +186,7 @@ final CommandShortcutEvent moveCursorBeginSelectCommand = CommandShortcutEvent( handler: _moveCursorBeginSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -193,9 +196,7 @@ CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = return KeyEventResult.ignored; } var end = selection.end; - final position = isRTL(editorState) - ? nodes.last.selectable?.end() - : nodes.last.selectable?.start(); + final position = isRTL(editorState) ? nodes.last.selectable?.end() : nodes.last.selectable?.start(); if (position != null) { end = position; } diff --git a/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart b/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart index 4aea0f98d..e8bb62075 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart @@ -26,6 +26,10 @@ final CommandShortcutEvent moveCursorRightCommand = CommandShortcutEvent( ); CommandShortcutEventHandler _arrowRightCommandHandler = (editorState) { + final selection = editorState.selection; + if (selection == null) { + return KeyEventResult.ignored; + } if (isRTL(editorState)) { editorState.moveCursorForward(SelectionMoveRange.character); } else { @@ -44,6 +48,10 @@ final CommandShortcutEvent moveCursorToEndCommand = CommandShortcutEvent( ); CommandShortcutEventHandler _moveCursorToEndCommandHandler = (editorState) { + final selection = editorState.selection; + if (selection == null) { + return KeyEventResult.ignored; + } if (isRTL(editorState)) { editorState.moveCursorForward(SelectionMoveRange.line); } else { @@ -61,8 +69,7 @@ final CommandShortcutEvent moveCursorToRightWordCommand = CommandShortcutEvent( handler: _moveCursorToRightWordCommandHandler, ); -CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -116,16 +123,14 @@ CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = }; // arrow right key + alt + shift -final CommandShortcutEvent moveCursorRightWordSelectCommand = - CommandShortcutEvent( +final CommandShortcutEvent moveCursorRightWordSelectCommand = CommandShortcutEvent( key: 'move the cursor to select the right word', command: 'ctrl+shift+arrow right', macOSCommand: 'alt+shift+arrow right', handler: _moveCursorRightWordSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorRightWordSelectCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorRightWordSelectCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -157,8 +162,7 @@ final CommandShortcutEvent moveCursorRightSelectCommand = CommandShortcutEvent( handler: _moveCursorRightSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorRightSelectCommandHandler = - (editorState) { +CommandShortcutEventHandler _moveCursorRightSelectCommandHandler = (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -196,9 +200,7 @@ CommandShortcutEventHandler _moveCursorEndSelectCommandHandler = (editorState) { return KeyEventResult.ignored; } var end = selection.end; - final position = isRTL(editorState) - ? nodes.last.selectable?.start() - : nodes.last.selectable?.end(); + final position = isRTL(editorState) ? nodes.last.selectable?.start() : nodes.last.selectable?.end(); if (position != null) { end = position; } From 344a3b3b4b2de76976e5db4c37a7c4589c19ecc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=A3=8A?= <200892907@qq.com> Date: Tue, 14 Nov 2023 17:31:41 +0800 Subject: [PATCH 2/2] feat:format --- .../service/keyboard_service_widget.dart | 22 +++++++++++++------ .../shortcuts/command/arrow_left_command.dart | 19 +++++++++++----- .../command/arrow_right_command.dart | 16 +++++++++----- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/lib/src/editor/editor_component/service/keyboard_service_widget.dart b/lib/src/editor/editor_component/service/keyboard_service_widget.dart index 61bc115c6..e0ec0b9b0 100644 --- a/lib/src/editor/editor_component/service/keyboard_service_widget.dart +++ b/lib/src/editor/editor_component/service/keyboard_service_widget.dart @@ -26,7 +26,8 @@ class KeyboardServiceWidget extends StatefulWidget { } @visibleForTesting -class KeyboardServiceWidgetState extends State implements AppFlowyKeyboardService { +class KeyboardServiceWidgetState extends State + implements AppFlowyKeyboardService { late final SelectionGestureInterceptor interceptor; late final EditorState editorState; late final TextInputService textInputService; @@ -51,7 +52,8 @@ class KeyboardServiceWidgetState extends State implements return true; }, ); - editorState.service.selectionService.registerGestureInterceptor(interceptor); + editorState.service.selectionService + .registerGestureInterceptor(interceptor); textInputService = NonDeltaTextInputService( onInsert: (insertion) async => await onInsert( @@ -141,7 +143,8 @@ class KeyboardServiceWidgetState extends State implements if (kIsWeb) { child = Shortcuts( shortcuts: { - LogicalKeySet(LogicalKeyboardKey.space): const DoNothingAndStopPropagationIntent(), + LogicalKeySet(LogicalKeyboardKey.space): + const DoNothingAndStopPropagationIntent(), }, child: child, ); @@ -179,7 +182,8 @@ class KeyboardServiceWidgetState extends State implements } void _onSelectionChanged() { - final doNotAttach = editorState.selectionExtraInfo?[selectionExtraInfoDoNotAttachTextService]; + final doNotAttach = editorState + .selectionExtraInfo?[selectionExtraInfoDoNotAttachTextService]; if (doNotAttach == true) { return; } @@ -224,10 +228,13 @@ class KeyboardServiceWidgetState extends State implements // based on the given selection. TextEditingValue? _getCurrentTextEditingValue(Selection selection) { // Get all the editable nodes in the selection. - final editableNodes = editorState.getNodesInSelection(selection).where((element) => element.delta != null); + final editableNodes = editorState + .getNodesInSelection(selection) + .where((element) => element.delta != null); // Get the composing text range. - final composingTextRange = textInputService.composingTextRange ?? TextRange.empty; + final composingTextRange = + textInputService.composingTextRange ?? TextRange.empty; if (editableNodes.isNotEmpty) { // Get the text by concatenating all the editable nodes in the selection. var text = editableNodes.fold( @@ -267,7 +274,8 @@ class KeyboardServiceWidgetState extends State implements return; } } - final children = WidgetsBinding.instance.focusManager.primaryFocus?.children; + final children = + WidgetsBinding.instance.focusManager.primaryFocus?.children; if (children != null && !children.contains(focusNode)) { editorState.selection = null; } diff --git a/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart b/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart index 4b3d97343..5abcf21ba 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command/arrow_left_command.dart @@ -69,7 +69,8 @@ final CommandShortcutEvent moveCursorToLeftWordCommand = CommandShortcutEvent( handler: _moveCursorToLeftWordCommandHandler, ); -CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -119,14 +120,16 @@ CommandShortcutEventHandler _moveCursorToLeftWordCommandHandler = (editorState) }; // arrow left key + alt + shift -final CommandShortcutEvent moveCursorLeftWordSelectCommand = CommandShortcutEvent( +final CommandShortcutEvent moveCursorLeftWordSelectCommand = + CommandShortcutEvent( key: 'move the cursor to select the left word', command: 'ctrl+shift+arrow left', macOSCommand: 'alt+shift+arrow left', handler: _moveCursorLeftWordSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorLeftWordSelectCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorLeftWordSelectCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -158,7 +161,8 @@ final CommandShortcutEvent moveCursorLeftSelectCommand = CommandShortcutEvent( handler: _moveCursorLeftSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorLeftSelectCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorLeftSelectCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -186,7 +190,8 @@ final CommandShortcutEvent moveCursorBeginSelectCommand = CommandShortcutEvent( handler: _moveCursorBeginSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -196,7 +201,9 @@ CommandShortcutEventHandler _moveCursorBeginSelectCommandHandler = (editorState) return KeyEventResult.ignored; } var end = selection.end; - final position = isRTL(editorState) ? nodes.last.selectable?.end() : nodes.last.selectable?.start(); + final position = isRTL(editorState) + ? nodes.last.selectable?.end() + : nodes.last.selectable?.start(); if (position != null) { end = position; } diff --git a/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart b/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart index e8bb62075..5e24e524f 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command/arrow_right_command.dart @@ -69,7 +69,8 @@ final CommandShortcutEvent moveCursorToRightWordCommand = CommandShortcutEvent( handler: _moveCursorToRightWordCommandHandler, ); -CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -123,14 +124,16 @@ CommandShortcutEventHandler _moveCursorToRightWordCommandHandler = (editorState) }; // arrow right key + alt + shift -final CommandShortcutEvent moveCursorRightWordSelectCommand = CommandShortcutEvent( +final CommandShortcutEvent moveCursorRightWordSelectCommand = + CommandShortcutEvent( key: 'move the cursor to select the right word', command: 'ctrl+shift+arrow right', macOSCommand: 'alt+shift+arrow right', handler: _moveCursorRightWordSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorRightWordSelectCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorRightWordSelectCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -162,7 +165,8 @@ final CommandShortcutEvent moveCursorRightSelectCommand = CommandShortcutEvent( handler: _moveCursorRightSelectCommandHandler, ); -CommandShortcutEventHandler _moveCursorRightSelectCommandHandler = (editorState) { +CommandShortcutEventHandler _moveCursorRightSelectCommandHandler = + (editorState) { final selection = editorState.selection; if (selection == null) { return KeyEventResult.ignored; @@ -200,7 +204,9 @@ CommandShortcutEventHandler _moveCursorEndSelectCommandHandler = (editorState) { return KeyEventResult.ignored; } var end = selection.end; - final position = isRTL(editorState) ? nodes.last.selectable?.start() : nodes.last.selectable?.end(); + final position = isRTL(editorState) + ? nodes.last.selectable?.start() + : nodes.last.selectable?.end(); if (position != null) { end = position; }