feat: implement delete multiple text node and merge the text.

This commit is contained in:
Lucas.Xu 2022-07-31 16:14:12 +08:00
parent 89a0a5599e
commit b577489c2f
2 changed files with 17 additions and 18 deletions

View File

@ -48,6 +48,10 @@ class TransactionBuilder {
add(DeleteOperation(path: node.path, removedValue: node)); add(DeleteOperation(path: node.path, removedValue: node));
} }
deleteNodes(List<Node> nodes) {
nodes.forEach(deleteNode);
}
textEdit(TextNode node, Delta Function() f) { textEdit(TextNode node, Delta Function() f) {
beforeSelection = state.cursorSelection; beforeSelection = state.cursorSelection;
final path = node.path; final path = node.path;

View File

@ -57,24 +57,19 @@ FlowyKeyEventHandler deleteTextHandler = (editorState, event) {
); );
} }
} else { } else {
for (var i = 0; i < textNodes.length; i++) { final first = textNodes.first;
final textNode = textNodes[i]; var content = textNodes.last.toRawString();
if (i == 0) { content = content.substring(selection.end.offset, content.length);
transactionBuilder.deleteText( // Merge the fist and the last text node content,
textNode, // and delete the all nodes expect for the first.
selection.start.offset, transactionBuilder
textNode.toRawString().length - selection.start.offset, ..deleteNodes(textNodes.sublist(1))
); ..replaceText(
} else if (i == textNodes.length - 1) { first,
transactionBuilder.deleteText( selection.start.offset,
textNode, first.toRawString().length - selection.start.offset,
0, content,
selection.end.offset, );
);
} else {
transactionBuilder.deleteNode(textNode);
}
}
} }
transactionBuilder.commit(); transactionBuilder.commit();