From 575e01c9094c9e9e01ef2c3fe804ddda540b7491 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 29 Jul 2022 23:27:39 +0800 Subject: [PATCH] feat: implement text replacement in singe selection --- .../lib/operation/transaction_builder.dart | 13 ++++++++++++ .../lib/service/input_service.dart | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart index fb042fe566..64fede87b3 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart @@ -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) { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/input_service.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/input_service.dart index a28d572d30..ee570d902a 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/service/input_service.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/service/input_service.dart @@ -97,6 +97,7 @@ class _FlowyInputState extends State _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 } } + 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();