Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix: use drop path rather than node (#6611)

Mathias Mogensen79116e7c7ffa
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart
index cadfd36bac..0f16938d71 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart
@@ -91,19 +91,13 @@ class EditorDropHandler extends StatelessWidget {
return;
}
- final node = editorState.getNodeAtPath(dropPath);
-
- if (node == null) {
- return;
- }
-
for (final file in details.files) {
final fileName = file.name.toLowerCase();
if (file.mimeType?.startsWith('image/') ??
false || imgExtensionRegex.hasMatch(fileName)) {
- await editorState.dropImages(node, [file], viewId, isLocalMode);
+ await editorState.dropImages(dropPath, [file], viewId, isLocalMode);
} else {
- await editorState.dropFiles(node, [file], viewId, isLocalMode);
+ await editorState.dropFiles(dropPath, [file], viewId, isLocalMode);
}
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart
index 1ca77a96d8..dc05e852c9 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart
@@ -5,7 +5,7 @@ import 'package:cross_file/cross_file.dart';
extension PasteFromFile on EditorState {
Future<void> dropFiles(
- Node dropNode,
+ List<int> dropPath,
List<XFile> files,
String documentId,
bool isLocalMode,
@@ -27,7 +27,7 @@ extension PasteFromFile on EditorState {
final t = transaction
..insertNode(
- dropNode.path,
+ dropPath,
fileNode(
url: path,
type: type,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart
index 4e932ee2f2..ce62fa60f6 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart
@@ -22,7 +22,7 @@ import 'package:universal_platform/universal_platform.dart';
extension PasteFromImage on EditorState {
Future<void> dropImages(
- Node dropNode,
+ List<int> dropPath,
List<XFile> files,
String documentId,
bool isLocalMode,
@@ -50,7 +50,7 @@ extension PasteFromImage on EditorState {
final t = transaction
..insertNode(
- dropNode.path,
+ dropPath,
customImageNode(url: path, type: type),
);
await apply(t);
diff --git a/frontend/appflowy_flutter/test/unit_test/editor/editor_drop_test.dart b/frontend/appflowy_flutter/test/unit_test/editor/editor_drop_test.dart
new file mode 100644
index 0000000000..9d798c3768
--- /dev/null
+++ b/frontend/appflowy_flutter/test/unit_test/editor/editor_drop_test.dart
@@ -0,0 +1,91 @@
+import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:cross_file/cross_file.dart';
+import 'package:flutter_test/flutter_test.dart';
+import '../../util.dart';
+
+void main() {
+ setUpAll(() async {
+ await AppFlowyUnitTest.ensureInitialized();
+ });
+
+ group('drop images and files in EditorState', () {
+ test('dropImages on same path as paragraph node ', () async {
+ final editorState = EditorState(
+ document: Document.blank(withInitialText: true),
+ );
+
+ expect(editorState.getNodeAtPath([0])!.type, ParagraphBlockKeys.type);
+
+ const dropPath = <int>[0];
+
+ const imagePath = 'assets/test/images/sample.jpeg';
+ final imageFile = XFile(imagePath);
+ await editorState.dropImages(dropPath, [imageFile], 'documentId', true);
+
+ final node = editorState.getNodeAtPath(dropPath);
+ expect(node, isNotNull);
+ expect(node!.type, CustomImageBlockKeys.type);
+ expect(editorState.getNodeAtPath([1])!.type, ParagraphBlockKeys.type);
+ });
+
+ test('dropImages should insert image node on empty path', () async {
+ final editorState = EditorState(
+ document: Document.blank(withInitialText: true),
+ );
+
+ expect(editorState.getNodeAtPath([0])!.type, ParagraphBlockKeys.type);
+
+ const dropPath = <int>[1];
+
+ const imagePath = 'assets/test/images/sample.jpeg';
+ final imageFile = XFile(imagePath);
+ await editorState.dropImages(dropPath, [imageFile], 'documentId', true);
+
+ final node = editorState.getNodeAtPath(dropPath);
+ expect(node, isNotNull);
+ expect(node!.type, CustomImageBlockKeys.type);
+ expect(editorState.getNodeAtPath([0])!.type, ParagraphBlockKeys.type);
+ expect(editorState.getNodeAtPath([2]), null);
+ });
+
+ test('dropFiles on same path as paragraph node ', () async {
+ final editorState = EditorState(
+ document: Document.blank(withInitialText: true),
+ );
+
+ expect(editorState.getNodeAtPath([0])!.type, ParagraphBlockKeys.type);
+
+ const dropPath = <int>[0];
+
+ const filePath = 'assets/test/images/sample.jpeg';
+ final file = XFile(filePath);
+ await editorState.dropFiles(dropPath, [file], 'documentId', true);
+
+ final node = editorState.getNodeAtPath(dropPath);
+ expect(node, isNotNull);
+ expect(node!.type, FileBlockKeys.type);
+ expect(editorState.getNodeAtPath([1])!.type, ParagraphBlockKeys.type);
+ });
+
+ test('dropFiles should insert file node on empty path', () async {
+ final editorState = EditorState(
+ document: Document.blank(withInitialText: true),
+ );
+ const dropPath = <int>[1];
+
+ const filePath = 'assets/test/images/sample.jpeg';
+ final file = XFile(filePath);
+ await editorState.dropFiles(dropPath, [file], 'documentId', true);
+
+ final node = editorState.getNodeAtPath(dropPath);
+ expect(node, isNotNull);
+ expect(node!.type, FileBlockKeys.type);
+ expect(editorState.getNodeAtPath([0])!.type, ParagraphBlockKeys.type);
+ expect(editorState.getNodeAtPath([2]), null);
+ });
+ });
+}