Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
fix: do not bring back purposeful null selection (#6464)
Mathias Mogensen6b585ef9a73d
diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dartindex 61984eae5c..fdde8bbeb8 100644--- a/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart+++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_app_lifecycle_test.dart@@ -28,5 +28,45 @@ void main() {expect(tester.editor.getCurrentEditorState().selection, selection);},);++ testWidgets(+ 'Null selection is retained after pausing AppFlowy',+ (tester) async {+ await tester.initializeAppFlowy();+ await tester.tapAnonymousSignInButton();++ final selection = Selection.single(path: [4], startOffset: 0);+ await tester.editor.updateSelection(selection);+ await tester.editor.updateSelection(null);++ binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);+ expect(tester.editor.getCurrentEditorState().selection, null);++ binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ await tester.pumpAndSettle();++ expect(tester.editor.getCurrentEditorState().selection, null);+ },+ );++ testWidgets(+ 'Non-collapsed selection is retained after pausing AppFlowy',+ (tester) async {+ await tester.initializeAppFlowy();+ await tester.tapAnonymousSignInButton();++ final selection = Selection(+ start: Position(path: [3]),+ end: Position(path: [3], offset: 8),+ );+ await tester.editor.updateSelection(selection);++ binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);+ binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ await tester.pumpAndSettle();++ expect(tester.editor.getCurrentEditorState().selection, selection);+ },+ );});}diff --git a/frontend/appflowy_flutter/integration_test/shared/document_test_operations.dart b/frontend/appflowy_flutter/integration_test/shared/document_test_operations.dartindex dac4fad44f..6522837765 100644--- a/frontend/appflowy_flutter/integration_test/shared/document_test_operations.dart+++ b/frontend/appflowy_flutter/integration_test/shared/document_test_operations.dart@@ -1,6 +1,9 @@import 'dart:async';import 'dart:ui';+import 'package:flutter/material.dart';+import 'package:flutter/services.dart';+import 'package:appflowy/generated/locale_keys.g.dart';import 'package:appflowy/plugins/base/emoji/emoji_picker.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/actions/block_action_add_button.dart';@@ -16,8 +19,6 @@ import 'package:appflowy/shared/icon_emoji_picker/emoji_skin_tone.dart';import 'package:appflowy/shared/icon_emoji_picker/flowy_icon_emoji_picker.dart';import 'package:appflowy_editor/appflowy_editor.dart';import 'package:easy_localization/easy_localization.dart';-import 'package:flutter/material.dart';-import 'package:flutter/services.dart';import 'package:flutter_emoji_mart/flutter_emoji_mart.dart';import 'package:flutter_test/flutter_test.dart';@@ -215,7 +216,7 @@ class EditorOperations {}/// Update the editor's selection- Future<void> updateSelection(Selection selection) async {+ Future<void> updateSelection(Selection? selection) async {final editorState = getCurrentEditorState();unawaited(editorState.updateSelectionWithReason(diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dartindex 0dd62b24da..fe35b1e959 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart@@ -124,6 +124,9 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>AFFocusManager? focusManager;+ AppLifecycleState? lifecycleState = WidgetsBinding.instance.lifecycleState;+ List<Selection?> previousSelections = [];+@overridevoid initState() {super.initState();@@ -185,21 +188,31 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>});}- Selection? previousSelection;-void onSelectionChanged() {- if (widget.editorState.isDisposed || widget.editorState.selection == null) {+ if (widget.editorState.isDisposed) {return;}- previousSelection = widget.editorState.selection;+ previousSelections.add(widget.editorState.selection);++ if (previousSelections.length > 2) {+ previousSelections.removeAt(0);+ }}@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {super.didChangeAppLifecycleState(state);- if (state == AppLifecycleState.resumed && !widget.editorState.isDisposed) {- widget.editorState.selection = previousSelection;+ lifecycleState = state;++ if (widget.editorState.isDisposed) {+ return;+ }++ if (previousSelections.length == 2 &&+ state == AppLifecycleState.resumed &&+ widget.editorState.selection == null) {+ widget.editorState.selection = previousSelections.first;}}@@ -218,7 +231,6 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>@overridevoid reassemble() {super.reassemble();-slashMenuItems = _customSlashMenuItems();}