mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: deep clone nodes
This commit is contained in:
parent
53cb05998b
commit
0424c14c7d
@ -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();
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -222,7 +222,7 @@ _handleCut(EditorState editorState) {
|
||||
}
|
||||
|
||||
_deleteSelectedContent(EditorState editorState) {
|
||||
final selection = editorState.cursorSelection;
|
||||
final selection = editorState.cursorSelection?.normalize();
|
||||
if (selection == null) {
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user