mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: implement text replacement in singe selection
This commit is contained in:
parent
55d46edeaf
commit
575e01c909
@ -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) {
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user