feat: deep clone nodes

This commit is contained in:
Vincent Chan 2022-08-12 17:58:16 +08:00
parent 53cb05998b
commit 0424c14c7d
3 changed files with 31 additions and 3 deletions

View File

@ -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();
}

View File

@ -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) {

View File

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