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([List<TextOperation>? ops]) : operations = ops ?? <TextOperation>[];
Delta addAll(List<TextOperation> textOps) {
textOps.forEach(add);
return this;
}
Delta add(TextOperation textOp) { Delta add(TextOperation textOp) {
if (textOp.isEmpty) { if (textOp.isEmpty) {
return this; return this;

View File

@ -63,8 +63,28 @@ class TransactionBuilder {
add(TextEditOperation(path: path, delta: delta, inverted: inverted)); add(TextEditOperation(path: path, delta: delta, inverted: inverted));
} }
insertText(TextNode node, int index, String content) { mergeText(TextNode firstNode, TextNode secondNode,
textEdit(node, () => Delta().retain(index).insert(content)); {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( afterSelection = Selection.collapsed(
Position(path: node.path, offset: index + content.length)); Position(path: node.path, offset: index + content.length));
} }

View File

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