feat: implement deleting text in multiple lines.

This commit is contained in:
Lucas.Xu 2022-08-01 11:29:04 +08:00
parent c65f2e1b38
commit 58856ccb1e
3 changed files with 33 additions and 12 deletions

View File

@ -275,6 +275,11 @@ class Delta {
Delta([List<TextOperation>? ops]) : operations = ops ?? <TextOperation>[];
Delta addAll(List<TextOperation> textOps) {
textOps.forEach(add);
return this;
}
Delta add(TextOperation textOp) {
if (textOp.isEmpty) {
return this;

View File

@ -63,8 +63,28 @@ class TransactionBuilder {
add(TextEditOperation(path: path, delta: delta, inverted: inverted));
}
insertText(TextNode node, int index, String content) {
textEdit(node, () => Delta().retain(index).insert(content));
mergeText(TextNode firstNode, TextNode secondNode,
{int? firstOffset, int secondOffset = 0}) {
final firstLength = firstNode.delta.length;
final secondLength = secondNode.delta.length;
textEdit(
firstNode,
() => Delta()
..retain(firstOffset ?? firstLength)
..delete(firstLength - (firstOffset ?? firstLength))
..addAll(secondNode.delta.slice(secondOffset, secondLength).operations),
);
afterSelection = Selection.collapsed(
Position(
path: firstNode.path,
offset: firstOffset ?? firstLength,
),
);
}
insertText(TextNode node, int index, String content,
[Attributes? attributes]) {
textEdit(node, () => Delta().retain(index).insert(content, attributes));
afterSelection = Selection.collapsed(
Position(path: node.path, offset: index + content.length));
}

View File

@ -39,12 +39,7 @@ FlowyKeyEventHandler deleteTextHandler = (editorState, event) {
final previous = textNode.previous as TextNode;
transactionBuilder
..deleteNode(textNode)
..insertText(
previous,
previous.toRawString().length,
textNode.toRawString(),
);
// FIXME: keep the attributes.
..mergeText(previous, textNode);
break;
}
}
@ -66,17 +61,18 @@ FlowyKeyEventHandler deleteTextHandler = (editorState, event) {
}
} else {
final first = textNodes.first;
final last = textNodes.last;
var content = textNodes.last.toRawString();
content = content.substring(selection.end.offset, content.length);
// Merge the fist and the last text node content,
// and delete the all nodes expect for the first.
transactionBuilder
..deleteNodes(textNodes.sublist(1))
..replaceText(
..mergeText(
first,
selection.start.offset,
first.toRawString().length - selection.start.offset,
content,
last,
firstOffset: selection.start.offset,
secondOffset: selection.end.offset,
);
}