Skip to content

Commit

Permalink
feat: support keep editor focus on mobile (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Dec 22, 2023
1 parent 739cd59 commit 92e4260
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/src/core/transform/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Transaction {
/// The before selection is to be recovered if needed.
Selection? beforeSelection;

Map? selectionExtraInfo;

// mark needs to be composed
bool markNeedsComposing = false;

Expand Down
8 changes: 5 additions & 3 deletions lib/src/editor/command/text_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ extension TextTransforms on EditorState {
/// If the [Selection] is not passed in, use the current selection.
Future<void> formatDelta(
Selection? selection,
Attributes attributes, [
Attributes attributes, {
bool withUpdateSelection = true,
]) async {
Map? selectionExtraInfo,
}) async {
selection ??= this.selection;
selection = selection?.normalized;

Expand Down Expand Up @@ -167,7 +168,8 @@ extension TextTransforms on EditorState {
endIndex - startIndex,
attributes,
)
..afterSelection = transaction.beforeSelection;
..afterSelection = transaction.beforeSelection
..selectionExtraInfo = selectionExtraInfo;
}

return apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,10 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>

// clear the selection when the focus is lost.
if (!focusNode.hasFocus) {
if (PlatformExtension.isDesktopOrWeb) {
if (keepEditorFocusNotifier.shouldKeepFocus) {
return;
}
if (keepEditorFocusNotifier.shouldKeepFocus) {
return;
}

final children =
WidgetsBinding.instance.focusManager.primaryFocus?.children;
if (children != null && !children.contains(focusNode)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/editor/find_replace_menu/search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class SearchService {
editorState.formatDelta(
selection,
{AppFlowyRichTextKeys.findBackgroundColor: color},
false,
withUpdateSelection: false,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/mobile_selection_service.dart';
import 'package:flutter/material.dart';

const disableFloatingToolbar = 'disableFloatingToolbar';

class MobileFloatingToolbarItem {
const MobileFloatingToolbarItem({
required this.builder,
Expand Down Expand Up @@ -100,7 +102,8 @@ class _MobileFloatingToolbarState extends State<MobileFloatingToolbar>
if (_isToolbarVisible) {
_clear();
} else if (prevSelection == selection &&
editorState.selectionUpdateReason == SelectionUpdateReason.uiEvent) {
editorState.selectionUpdateReason == SelectionUpdateReason.uiEvent &&
editorState.selectionExtraInfo?[disableFloatingToolbar] != true) {
_showAfterDelay(const Duration(milliseconds: 400));
}
prevSelection = selection;
Expand All @@ -113,8 +116,11 @@ class _MobileFloatingToolbarState extends State<MobileFloatingToolbar>
].contains(dragMode)) {
return;
}
// uses debounce to avoid the computing the rects too frequently.
_showAfterDelay(const Duration(milliseconds: 400));

if (editorState.selectionExtraInfo?[disableFloatingToolbar] != true) {
// uses debounce to avoid the computing the rects too frequently.
_showAfterDelay(const Duration(milliseconds: 400));
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/editor/toolbar/utils/format_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void formatHighlightColor(
editorState.formatDelta(
selection,
{AppFlowyRichTextKeys.highlightColor: color},
withUpdateSelection,
withUpdateSelection: withUpdateSelection,
);
}

Expand All @@ -22,6 +22,6 @@ void formatFontColor(
editorState.formatDelta(
selection,
{AppFlowyRichTextKeys.textColor: color},
withUpdateSelection,
withUpdateSelection: withUpdateSelection,
);
}
3 changes: 3 additions & 0 deletions lib/src/editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ class EditorState {

if (withUpdateSelection) {
_selectionUpdateReason = SelectionUpdateReason.transaction;
if (transaction.selectionExtraInfo != null) {
selectionExtraInfo = transaction.selectionExtraInfo;
}
selection = transaction.afterSelection;
_selectionUpdateReason = SelectionUpdateReason.uiEvent;
}
Expand Down

0 comments on commit 92e4260

Please sign in to comment.