Skip to content

Commit

Permalink
feat: optimize Enter operation (AppFlowy-IO#673)
Browse files Browse the repository at this point in the history
* outdentCommand is called when the node path is greater than 1

* feat:add test

* feat:update test
  • Loading branch information
q200892907 authored Jan 15, 2024
1 parent a856888 commit b7c381e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Future<bool> insertNewLineInType(

if (selection.startIndex == 0 && delta.isEmpty) {
// clear the style
if (node != null && node.path.length > 1) {
return KeyEventResult.ignored != outdentCommand.execute(editorState);
}
return KeyEventResult.ignored !=
convertToParagraphCommand.execute(editorState);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('InsertNewlineInTypeCommand tests', () {
test('bulleted list', () async {
final bulletedNode = bulletedListNode(
text: 'bulleted list 1',
children: [
bulletedListNode(text: '1'),
bulletedListNode(text: ''),
],
);
final document = Document.blank()..insert([0], [bulletedNode]);

final editorState = EditorState(document: document);

editorState.selection = Selection.collapsed(Position(path: [0, 1]));

insertNewLineAfterBulletedList.execute(editorState);
Node? node1 = editorState.getNodeAtPath([1]);
expect(node1?.type, BulletedListBlockKeys.type);

insertNewLineAfterBulletedList.execute(editorState);
Node? node2 = editorState.getNodeAtPath([1]);

expect(node2?.type, ParagraphBlockKeys.type);
});

test('todo list', () async {
final todoListNodes = todoListNode(
checked: false,
children: [
todoListNode(checked: false),
todoListNode(checked: false),
],
);
final document = Document.blank()..insert([0], [todoListNodes]);

final editorState = EditorState(document: document);

editorState.selection = Selection.collapsed(Position(path: [0, 1]));

insertNewLineAfterTodoList.execute(editorState);
Node? node1 = editorState.getNodeAtPath([1]);
expect(node1?.type, TodoListBlockKeys.type);

insertNewLineAfterTodoList.execute(editorState);
Node? node2 = editorState.getNodeAtPath([1]);

expect(node2?.type, ParagraphBlockKeys.type);
});

test('numbered list', () async {
final numberedListNodes = numberedListNode(
children: [
numberedListNode(),
numberedListNode(),
],
);
final document = Document.blank()..insert([0], [numberedListNodes]);

final editorState = EditorState(document: document);

editorState.selection = Selection.collapsed(Position(path: [0, 1]));

insertNewLineAfterNumberedList.execute(editorState);
Node? node1 = editorState.getNodeAtPath([1]);
expect(node1?.type, NumberedListBlockKeys.type);

insertNewLineAfterNumberedList.execute(editorState);
Node? node2 = editorState.getNodeAtPath([1]);

expect(node2?.type, ParagraphBlockKeys.type);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../../infra/testable_editor.dart';

void main() async {
Expand Down Expand Up @@ -253,19 +254,19 @@ Future<void> _testListOutdent(WidgetTester tester, String style) async {
// clear the style
expect(
editor.selection,
Selection.single(path: [1, 0], startOffset: 0),
Selection.single(path: [2], startOffset: 0),
);
expect(editor.nodeAtPath([1, 0])?.type, 'paragraph');
expect(editor.nodeAtPath([2])?.type, style);

await editor.pressKey(
character: '\n',
);
expect(
editor.selection,
Selection.single(path: [1, 1], startOffset: 0),
Selection.single(path: [2], startOffset: 0),
);

expect(editor.nodeAtPath([1, 1])?.type, 'paragraph');
expect(editor.nodeAtPath([2])?.type, 'paragraph');

await editor.dispose();
}
Expand Down

0 comments on commit b7c381e

Please sign in to comment.