feat: implement text replacement in singe selection

This commit is contained in:
Lucas.Xu 2022-07-29 23:27:39 +08:00
parent 55d46edeaf
commit 575e01c909
2 changed files with 33 additions and 0 deletions

View File

@ -75,6 +75,19 @@ class TransactionBuilder {
Selection.collapsed(Position(path: node.path, offset: index));
}
replaceText(TextNode node, int index, int length, String content) {
textEdit(
node,
() => Delta().retain(index).delete(length).insert(content),
);
afterSelection = Selection.collapsed(
Position(
path: node.path,
offset: index + content.length,
),
);
}
add(Operation op) {
final Operation? last = operations.isEmpty ? null : operations.last;
if (last != null) {

View File

@ -97,6 +97,7 @@ class _FlowyInputState extends State<FlowyInput>
_applyInsert(delta);
} else if (delta is TextEditingDeltaDeletion) {
} else if (delta is TextEditingDeltaReplacement) {
_applyReplacement(delta);
} else if (delta is TextEditingDeltaNonTextUpdate) {
// We don't need to care the [TextEditingDeltaNonTextUpdate].
// Do nothing.
@ -125,6 +126,25 @@ class _FlowyInputState extends State<FlowyInput>
}
}
void _applyReplacement(TextEditingDeltaReplacement delta) {
final selectionService = _editorState.service.selectionService;
final currentSelection = selectionService.currentSelection;
if (currentSelection == null) {
return;
}
if (currentSelection.isSingle) {
final textNode =
selectionService.currentSelectedNodes.value.first as TextNode;
final length = delta.replacedRange.end - delta.replacedRange.start;
TransactionBuilder(_editorState)
..replaceText(
textNode, delta.replacedRange.start, length, delta.replacementText)
..commit();
} else {
// TODO: implement
}
}
@override
void close() {
_textInputConnection?.close();