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.

feat: deep clone nodes

Vincent Chan0424c14c7d99
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/document/node.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/document/node.dart
index 24c3035e90..6bfc877524 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/src/document/node.dart
+++ b/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]);
}
+
+ 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 {
@@ -213,5 +225,21 @@ class TextNode extends Node {
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();
}
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/operation/transaction_builder.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/operation/transaction_builder.dart
index f8cc80b161..dfca3cd661 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/src/operation/transaction_builder.dart
+++ b/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.
insertNodes(Path path, List<Node> nodes) {
beforeSelection = state.cursorSelection;
- add(InsertOperation(path, nodes));
+ add(InsertOperation(path, nodes.map((node) => node.deepClone()).toList()));
}
/// Update the attributes of nodes.
@@ -75,7 +75,7 @@ class TransactionBuilder {
nodes.add(node);
}
- add(DeleteOperation(path, nodes));
+ add(DeleteOperation(path, nodes.map((node) => node.deepClone()).toList()));
}
textEdit(TextNode node, Delta Function() f) {
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart
index 363b84967a..b1bf170672 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/src/service/internal_key_event_handlers/copy_paste_handler.dart
+++ b/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) {
- final selection = editorState.cursorSelection;
+ final selection = editorState.cursorSelection?.normalize();
if (selection == null) {
return;
}