Переглянути джерело

feat: paste multi lines text

Vincent Chan 2 роки тому
батько
коміт
d283211671

+ 4 - 3
frontend/app_flowy/packages/flowy_editor/lib/document/node.dart

@@ -176,10 +176,11 @@ class TextNode extends Node {
 
   TextNode({
     required super.type,
-    required super.children,
-    required super.attributes,
     required Delta delta,
-  }) : _delta = delta;
+    LinkedList<Node>? children,
+    Attributes? attributes,
+  })  : _delta = delta,
+        super(children: children ?? LinkedList(), attributes: attributes ?? {});
 
   TextNode.empty()
       : _delta = Delta([TextInsert(' ')]),

+ 1 - 4
frontend/app_flowy/packages/flowy_editor/lib/infra/html_converter.dart

@@ -1,5 +1,3 @@
-import 'dart:collection';
-
 import 'package:flowy_editor/document/node.dart';
 import 'package:flowy_editor/document/text_delta.dart';
 import 'package:html/parser.dart' show parse;
@@ -20,8 +18,7 @@ class HTMLConverter {
     }
 
     if (delta.operations.isNotEmpty) {
-      result.add(TextNode(
-          type: "text", children: LinkedList(), attributes: {}, delta: delta));
+      result.add(TextNode(type: "text", delta: delta));
     }
 
     return result;

+ 27 - 1
frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/copy_paste_handler.dart

@@ -34,7 +34,33 @@ _handlePaste(EditorState editorState) async {
     _pasteHTML(editorState, data.html!);
     return;
   }
-  debugPrint('paste ${data.text ?? ''}');
+  if (data.text != null) {
+    _handlePastePlainText(editorState, data.text!);
+    return;
+  }
+}
+
+_handlePastePlainText(EditorState editorState, String plainText) {
+  final selection = editorState.cursorSelection;
+  if (selection == null) {
+    return;
+  }
+
+  final path = [...selection.end.path];
+  if (path.isEmpty) {
+    return;
+  }
+  path[path.length - 1]++;
+
+  final lines =
+      plainText.split("\n").map((e) => e.replaceAll(RegExp(r'\r'), ""));
+  final nodes = lines
+      .map((e) => TextNode(type: "text", delta: Delta().insert(e)))
+      .toList();
+
+  final tb = TransactionBuilder(editorState);
+  tb.insertNodes(path, nodes);
+  tb.commit();
 }
 
 _handleCut() {