Browse Source

feat: deep clone nodes

Vincent Chan 2 năm trước cách đây
mục cha
commit
0424c14c7d

+ 28 - 0
frontend/app_flowy/packages/flowy_editor/lib/src/document/node.dart

@@ -163,6 +163,18 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
     }
     }
     return parent!._path([index, ...previous]);
     return parent!._path([index, ...previous]);
   }
   }
+
+  Node deepClone() {
+    final newNode = Node(
+        type: type, children: LinkedList<Node>(), attributes: {...attributes});
+
+    for (final node in children) {
+      final newNode = node.deepClone();
+      newNode.parent = this;
+      newNode.children.add(newNode);
+    }
+    return newNode;
+  }
 }
 }
 
 
 class TextNode extends Node {
 class TextNode extends Node {
@@ -213,5 +225,21 @@ class TextNode extends Node {
         delta: delta ?? this.delta,
         delta: delta ?? this.delta,
       );
       );
 
 
+  @override
+  TextNode deepClone() {
+    final newNode = TextNode(
+        type: type,
+        children: LinkedList<Node>(),
+        delta: delta.slice(0),
+        attributes: {...attributes});
+
+    for (final node in children) {
+      final newNode = node.deepClone();
+      newNode.parent = this;
+      newNode.children.add(newNode);
+    }
+    return newNode;
+  }
+
   String toRawString() => _delta.toRawString();
   String toRawString() => _delta.toRawString();
 }
 }

+ 2 - 2
frontend/app_flowy/packages/flowy_editor/lib/src/operation/transaction_builder.dart

@@ -36,7 +36,7 @@ class TransactionBuilder {
   /// Insert a sequence of nodes at the position of path.
   /// Insert a sequence of nodes at the position of path.
   insertNodes(Path path, List<Node> nodes) {
   insertNodes(Path path, List<Node> nodes) {
     beforeSelection = state.cursorSelection;
     beforeSelection = state.cursorSelection;
-    add(InsertOperation(path, nodes));
+    add(InsertOperation(path, nodes.map((node) => node.deepClone()).toList()));
   }
   }
 
 
   /// Update the attributes of nodes.
   /// Update the attributes of nodes.
@@ -75,7 +75,7 @@ class TransactionBuilder {
       nodes.add(node);
       nodes.add(node);
     }
     }
 
 
-    add(DeleteOperation(path, nodes));
+    add(DeleteOperation(path, nodes.map((node) => node.deepClone()).toList()));
   }
   }
 
 
   textEdit(TextNode node, Delta Function() f) {
   textEdit(TextNode node, Delta Function() f) {

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

@@ -222,7 +222,7 @@ _handleCut(EditorState editorState) {
 }
 }
 
 
 _deleteSelectedContent(EditorState editorState) {
 _deleteSelectedContent(EditorState editorState) {
-  final selection = editorState.cursorSelection;
+  final selection = editorState.cursorSelection?.normalize();
   if (selection == null) {
   if (selection == null) {
     return;
     return;
   }
   }