fix: the index error when deleting text and then using the up or down arrow keys (#7009)

This commit is contained in:
Morn 2024-12-19 14:40:11 +08:00 committed by GitHub
parent e73fd56152
commit c2743472bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 8 deletions

View File

@ -1,6 +1,5 @@
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/code_block/code_block_language_selector.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:easy_localization/easy_localization.dart';
@ -45,8 +44,10 @@ void main() {
await onKey(tester, LogicalKeyboardKey.enter);
final editorState = tester.editor.getCurrentEditorState();
final language =
editorState.getNodeAtPath([0])!.attributes['language'].toString();
String language = editorState
.getNodeAtPath([0])!
.attributes[CodeBlockKeys.language]
.toString();
expect(
language.toLowerCase(),
defaultCodeBlockSupportedLanguages.first.toLowerCase(),
@ -58,14 +59,29 @@ void main() {
await onKey(tester, LogicalKeyboardKey.arrowUp);
await onKey(tester, LogicalKeyboardKey.enter);
language = editorState
.getNodeAtPath([0])!
.attributes[CodeBlockKeys.language]
.toString();
expect(
editorState
.getNodeAtPath([0])!
.attributes['language']
.toString()
.toLowerCase(),
language.toLowerCase(),
defaultCodeBlockSupportedLanguages.last.toLowerCase(),
);
await tester.hoverOnWidget(find.byType(CodeBlockComponentWidget));
await tester.tapButtonWithName(language);
tester.testTextInput.enterText("rust");
await onKey(tester, LogicalKeyboardKey.delete);
await onKey(tester, LogicalKeyboardKey.delete);
await onKey(tester, LogicalKeyboardKey.arrowDown);
tester.testTextInput.enterText("st");
await onKey(tester, LogicalKeyboardKey.arrowDown);
await onKey(tester, LogicalKeyboardKey.enter);
language = editorState
.getNodeAtPath([0])!
.attributes[CodeBlockKeys.language]
.toString();
expect(language.toLowerCase(), 'rust');
});
}

View File

@ -29,6 +29,7 @@ class SelectableItemListMenu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScrollablePositionedList.builder(
physics: const ClampingScrollPhysics(),
shrinkWrap: shrinkWrap,
itemCount: items.length,
itemScrollController: controller,

View File

@ -222,6 +222,9 @@ class _LanguageSelectionPopoverState extends State<_LanguageSelectionPopover> {
void onArrowKey(AxisDirection direction) {
if (filteredLanguages.isEmpty) return;
final isUp = direction == AxisDirection.up;
if (selectedIndex < 0) {
selectedIndex = isUp ? 0 : -1;
}
final length = filteredLanguages.length;
setState(() {
if (isUp) {
@ -232,6 +235,7 @@ class _LanguageSelectionPopoverState extends State<_LanguageSelectionPopover> {
});
languageListController.scrollTo(
index: selectedIndex,
alignment: 0.5,
duration: const Duration(milliseconds: 300),
);
}