fix: could not delete the image when the selection is multiple

This commit is contained in:
Lucas.Xu 2022-08-26 16:00:20 +08:00
parent b52f618b1a
commit 01328442a0
3 changed files with 81 additions and 22 deletions

View File

@ -101,7 +101,11 @@ class _ImageNodeWidgetState extends State<ImageNodeWidget> with Selectable {
@override @override
Selection getSelectionInRange(Offset start, Offset end) { Selection getSelectionInRange(Offset start, Offset end) {
return Selection(start: this.start(), end: this.end()); if (start <= end) {
return Selection(start: this.start(), end: this.end());
} else {
return Selection(start: this.end(), end: this.start());
}
} }
@override @override

View File

@ -80,6 +80,9 @@ KeyEventResult _handleBackspace(EditorState editorState, RawKeyEvent event) {
} }
if (transactionBuilder.operations.isNotEmpty) { if (transactionBuilder.operations.isNotEmpty) {
if (nonTextNodes.isNotEmpty) {
transactionBuilder.afterSelection = Selection.collapsed(selection.start);
}
transactionBuilder.commit(); transactionBuilder.commit();
} }

View File

@ -215,30 +215,82 @@ void main() async {
}); });
}); });
testWidgets('Deletes the first image', (tester) async { testWidgets('Deletes the first image, and selection is backward',
mockNetworkImagesFor(() async { (tester) async {
const text = 'Welcome to Appflowy 😁'; await _deleteFirstImage(tester, true);
const src = 'https://s1.ax1x.com/2022/08/26/v2sSbR.jpg'; });
final editor = tester.editor
..insertImageNode(src)
..insertTextNode(text)
..insertTextNode(text);
await editor.startTesting();
expect(editor.documentLength, 3); testWidgets('Deletes the first image, and selection is not backward',
expect(find.byType(ImageNodeWidget), findsOneWidget); (tester) async {
await _deleteFirstImage(tester, false);
});
await editor.updateSelection( testWidgets('Deletes the last image and selection is backward',
Selection( (tester) async {
start: Position(path: [0], offset: 0), await _deleteLastImage(tester, true);
end: Position(path: [0], offset: 1), });
),
);
await editor.pressLogicKey(LogicalKeyboardKey.backspace); testWidgets('Deletes the last image and selection is not backward',
expect(editor.documentLength, 2); (tester) async {
expect(find.byType(ImageNodeWidget), findsNothing); await _deleteLastImage(tester, false);
}); });
}
Future<void> _deleteFirstImage(WidgetTester tester, bool isBackward) async {
mockNetworkImagesFor(() async {
const text = 'Welcome to Appflowy 😁';
const src = 'https://s1.ax1x.com/2022/08/26/v2sSbR.jpg';
final editor = tester.editor
..insertImageNode(src)
..insertTextNode(text)
..insertTextNode(text);
await editor.startTesting();
expect(editor.documentLength, 3);
expect(find.byType(ImageNodeWidget), findsOneWidget);
final start = Position(path: [0], offset: 0);
final end = Position(path: [1], offset: 1);
await editor.updateSelection(
Selection(
start: isBackward ? start : end,
end: isBackward ? end : start,
),
);
await editor.pressLogicKey(LogicalKeyboardKey.backspace);
expect(editor.documentLength, 2);
expect(find.byType(ImageNodeWidget), findsNothing);
expect(editor.documentSelection, Selection.collapsed(start));
});
}
Future<void> _deleteLastImage(WidgetTester tester, bool isBackward) async {
mockNetworkImagesFor(() async {
const text = 'Welcome to Appflowy 😁';
const src = 'https://s1.ax1x.com/2022/08/26/v2sSbR.jpg';
final editor = tester.editor
..insertTextNode(text)
..insertTextNode(text)
..insertImageNode(src);
await editor.startTesting();
expect(editor.documentLength, 3);
expect(find.byType(ImageNodeWidget), findsOneWidget);
final start = Position(path: [1], offset: 0);
final end = Position(path: [2], offset: 1);
await editor.updateSelection(
Selection(
start: isBackward ? start : end,
end: isBackward ? end : start,
),
);
await editor.pressLogicKey(LogicalKeyboardKey.backspace);
expect(editor.documentLength, 2);
expect(find.byType(ImageNodeWidget), findsNothing);
expect(editor.documentSelection, Selection.collapsed(start));
}); });
} }