feat: listen on delete_backward changes (#898)

This commit is contained in:
Lucas 2024-09-25 15:59:23 +08:00 • committed by GitHub
commit 737681dd97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 10 deletions

View file

@ -23,16 +23,7 @@ jobs:
desktop: desktop:
strategy: strategy:
matrix: matrix:
os: os: [macos-latest, ubuntu-latest, windows-latest]
[
macos-14,
macos-13,
ubuntu-24.04,
ubuntu-22.04,
ubuntu-20.04,
windows-2022,
windows-2019,
]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}

View file

@ -6,6 +6,9 @@ import 'package:appflowy_editor/src/editor/editor_component/service/ime/text_inp
import 'package:appflowy_editor/src/editor/util/platform_extension.dart'; import 'package:appflowy_editor/src/editor/util/platform_extension.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
// string from flutter callback
const String _deleteBackwardSelectorName = 'deleteBackward:';
class NonDeltaTextInputService extends TextInputService with TextInputClient { class NonDeltaTextInputService extends TextInputService with TextInputClient {
NonDeltaTextInputService({ NonDeltaTextInputService({
required super.onInsert, required super.onInsert,
@ -168,6 +171,35 @@ class NonDeltaTextInputService extends TextInputService with TextInputClient {
@override @override
void performSelector(String selectorName) { void performSelector(String selectorName) {
AppFlowyEditorLog.editor.debug('performSelector: $selectorName'); AppFlowyEditorLog.editor.debug('performSelector: $selectorName');
final currentTextEditingValue = this.currentTextEditingValue?.unformat();
if (currentTextEditingValue == null) {
return;
}
if (selectorName == _deleteBackwardSelectorName) {
final oldText = currentTextEditingValue.text;
final selection = currentTextEditingValue.selection;
final deleteRange = selection.isCollapsed
? TextRange(
start: selection.start - 1,
end: selection.end,
)
: selection;
final deleteSelection = TextSelection(
baseOffset: selection.baseOffset - 1,
extentOffset: selection.extentOffset - 1,
);
// valid the result
onDelete(
TextEditingDeltaDeletion(
oldText: oldText,
deletedRange: deleteRange,
selection: deleteSelection,
composing: TextRange.empty,
),
);
}
} }
@override @override
@ -229,6 +261,14 @@ extension on TextEditingValue {
composing: composing, composing: composing,
); );
} }
TextEditingValue unformat() {
return TextEditingValue(
text: text << _len,
selection: selection << _len,
composing: composing << _len,
);
}
} }
extension on TextEditingDelta { extension on TextEditingDelta {

View file

@ -221,6 +221,10 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
if (editorState.selectionUpdateReason == SelectionUpdateReason.uiEvent) { if (editorState.selectionUpdateReason == SelectionUpdateReason.uiEvent) {
focusNode.requestFocus(); focusNode.requestFocus();
AppFlowyEditorLog.editor.debug('keyboard service - request focus'); AppFlowyEditorLog.editor.debug('keyboard service - request focus');
} else {
AppFlowyEditorLog.editor.debug(
'keyboard service - selection changed: $selection',
);
} }
} }