appflowy-editor/test/new/command/selection_commands_test.dart

154 lines
4.8 KiB
Dart
Raw Normal View History

2023-04-21 21:03:35 +08:00
import 'package:appflowy_editor/appflowy_editor.dart';
2023-04-21 13:01:20 +08:00
import 'package:flutter_test/flutter_test.dart';
2023-04-26 23:06:16 +08:00
import '../util/util.dart';
2023-04-21 21:03:35 +08:00
2023-04-21 13:01:20 +08:00
void main() async {
group('deleteSelection', () {
test('the selection is collapsed', () async {
final document = Document.blank().addParagraphs(
3,
builder: (index) =>
Delta()..insert('$index. Welcome to AppFlowy Editor 🔥!'),
);
final editorState = EditorState(document: document);
2023-04-21 21:03:35 +08:00
final selection = Selection.collapsed(
Position(path: [1], offset: 10),
);
final before = editorState.getNodesInSelection(selection).first;
final result = await editorState.deleteSelection(selection);
2023-04-21 21:03:35 +08:00
// nothing happens
expect(result, false);
final after = editorState.getNodesInSelection(selection).first;
expect(
before.toJson(),
after.toJson(),
);
});
2023-04-21 21:03:35 +08:00
test('the selection is single', () async {
final document = Document.blank().addParagraphs(
3,
builder: (index) =>
Delta()..insert('$index. Welcome to AppFlowy Editor 🔥!'),
);
final editorState = EditorState(document: document);
2023-04-21 21:03:35 +08:00
// |Welcome|
final selection = Selection.single(
path: [1],
startOffset: 3,
endOffset: 10,
);
final result = await editorState.deleteSelection(selection);
2023-04-21 21:03:35 +08:00
// nothing happens
expect(result, true);
expect(editorState.selection, selection.collapse(atStart: true));
final after = editorState.getNodesInSelection(selection).first;
expect(after.delta?.toPlainText(), '1. to AppFlowy Editor 🔥!');
});
2023-04-21 21:03:35 +08:00
test('the selection is not single and not collapsed - 1', () async {
final document = Document.blank().addParagraphs(
3,
builder: (index) =>
Delta()..insert('$index. Welcome to AppFlowy Editor 🔥!'),
);
final editorState = EditorState(document: document);
2023-04-21 21:03:35 +08:00
// |Welcome
// ...
// Welcome|
final selection = Selection(
start: Position(
path: [0],
offset: 3,
),
end: Position(
path: [2],
offset: 10,
),
);
final result = await editorState.deleteSelection(selection);
2023-04-21 21:03:35 +08:00
// nothing happens
expect(result, true);
expect(editorState.selection, selection.collapse(atStart: true));
final length = editorState.document.root.children.length;
expect(length, 1);
expect(
editorState.document.nodeAtPath([0])?.delta?.toPlainText(),
'0. to AppFlowy Editor 🔥!',
);
});
// Before
// 0. Welcome |to AppFlowy Editor 🔥!
// 0.0. Welcome |to AppFlowy Editor 🔥!
// 0.0.0. Welcome to AppFlowy Editor 🔥!
// 0.1. Welcome to AppFlowy Editor 🔥!
// After
// 0. Welcome to AppFlowy Editor 🔥!
// 0.0.0. Welcome to AppFlowy Editor 🔥!
// 0.1. Welcome to AppFlowy Editor 🔥!
test('the selection is not single and not collapsed - 2', () async {
final document = Document.blank().addParagraph(
builder: (index) =>
Delta()..insert('$index. Welcome to AppFlowy Editor 🔥!'),
decorator: (index, node) {
node.addParagraphs(
2,
builder: (index2) => Delta()
..insert('$index.$index2. Welcome to AppFlowy Editor 🔥!'),
decorator: (index2, node2) {
if (index2 == 0) {
node2.addParagraph(
builder: (index3) => Delta()
..insert(
'$index.$index2.$index3. Welcome to AppFlowy Editor 🔥!',
),
);
}
},
);
},
);
final editorState = EditorState(document: document);
2023-04-27 14:28:17 +08:00
// 0. Welcome |to AppFlowy Editor 🔥!
// 0.0. Welcome |to AppFlowy Editor 🔥!
// 0.0.0 Welcome to AppFlowy Editor 🔥!
// 0.1 Welcome to AppFlowy Editor 🔥!
final selection = Selection(
start: Position(
path: [0],
offset: '0. Welcome '.length,
),
end: Position(
path: [0, 0],
offset: '0.0. Welcome '.length,
),
);
final result = await editorState.deleteSelection(selection);
2023-04-27 14:28:17 +08:00
// nothing happens
expect(result, true);
expect(editorState.selection, selection.collapse(atStart: true));
expect(
editorState.document.nodeAtPath([0])?.delta?.toPlainText(),
'0. Welcome to AppFlowy Editor 🔥!',
);
expect(
editorState.document.nodeAtPath([0, 0])?.delta?.toPlainText(),
'0.0.0. Welcome to AppFlowy Editor 🔥!',
);
expect(
editorState.document.nodeAtPath([0, 1])?.delta?.toPlainText(),
'0.1. Welcome to AppFlowy Editor 🔥!',
);
2023-04-21 21:03:35 +08:00
});
2023-04-21 13:01:20 +08:00
});
}