From 032124c00284fa461ffcca1245407010e2285a49 Mon Sep 17 00:00:00 2001 From: Morn Date: Wed, 9 Apr 2025 11:19:17 +0800 Subject: [PATCH] fix: overwriting a selected text removes more than expected --- .../document/document_selection_test.dart | 37 +++++++++++++++++++ .../plugins/emoji/emoji_actions_command.dart | 6 +-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_selection_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_selection_test.dart index bd0fd18c50..de1cb880a5 100644 --- a/frontend/appflowy_flutter/integration_test/desktop/document/document_selection_test.dart +++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_selection_test.dart @@ -1,5 +1,6 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -47,5 +48,41 @@ void main() { expect(editorState.selection!.start.offset, 0); }); + + testWidgets('select and delete text', (tester) async { + await tester.initializeAppFlowy(); + await tester.tapAnonymousSignInButton(); + + /// create a new document + await tester.createNewPageWithNameUnderParent(); + + /// input text + final editor = tester.editor; + final editorState = editor.getCurrentEditorState(); + + const inputText = 'Test for text selection and deletion'; + final texts = inputText.split(' '); + await editor.tapLineOfEditorAt(0); + await tester.ime.insertText(inputText); + + /// selecte and delete + int index = 0; + while (texts.isNotEmpty) { + final text = texts.removeAt(0); + await tester.editor.updateSelection( + Selection( + start: Position(path: [0], offset: index), + end: Position(path: [0], offset: index + text.length), + ), + ); + await tester.simulateKeyEvent(LogicalKeyboardKey.delete); + index++; + } + + /// excpete the text value is correct + final node = editorState.getNodeAtPath([0])!; + final nodeText = node.delta?.toPlainText() ?? ''; + expect(nodeText, ' ' * (index - 1)); + }); }); } diff --git a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_actions_command.dart b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_actions_command.dart index ffe25be8bd..9d386b36be 100644 --- a/frontend/appflowy_flutter/lib/plugins/emoji/emoji_actions_command.dart +++ b/frontend/appflowy_flutter/lib/plugins/emoji/emoji_actions_command.dart @@ -38,10 +38,6 @@ Future emojiCommandHandler( return false; } - if (!selection.isCollapsed) { - await editorState.deleteSelection(selection); - } - final node = editorState.getNodeAtPath(selection.end.path); final delta = node?.delta; if (node == null || @@ -58,6 +54,8 @@ Future emojiCommandHandler( if (previousCharacter != _emojiCharacter) return false; if (!context.mounted) return false; + if (!selection.isCollapsed) return false; + await editorState.insertTextAtPosition( character, position: selection.start,