mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
[simplistic_editor] - Fix delete key on desktop and backspace key on Web (#1458)
* Do not propagate to default text intent * Add support for backward and forward delete (desktop) * Update for nice auto-format * Fix using delete key throws if at the end of the text Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
This commit is contained in:
@@ -299,7 +299,7 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
|
|||||||
TextSelection get _selection => _value.selection;
|
TextSelection get _selection => _value.selection;
|
||||||
late final Map<Type, Action<Intent>> _actions = <Type, Action<Intent>>{
|
late final Map<Type, Action<Intent>> _actions = <Type, Action<Intent>>{
|
||||||
DeleteCharacterIntent: CallbackAction<DeleteCharacterIntent>(
|
DeleteCharacterIntent: CallbackAction<DeleteCharacterIntent>(
|
||||||
onInvoke: (intent) => _delete(),
|
onInvoke: (intent) => _delete(intent.forward),
|
||||||
),
|
),
|
||||||
ExtendSelectionByCharacterIntent:
|
ExtendSelectionByCharacterIntent:
|
||||||
CallbackAction<ExtendSelectionByCharacterIntent>(
|
CallbackAction<ExtendSelectionByCharacterIntent>(
|
||||||
@@ -315,22 +315,35 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
|
|||||||
PasteTextIntent: CallbackAction<PasteTextIntent>(
|
PasteTextIntent: CallbackAction<PasteTextIntent>(
|
||||||
onInvoke: (intent) => pasteText(intent.cause),
|
onInvoke: (intent) => pasteText(intent.cause),
|
||||||
),
|
),
|
||||||
|
DoNothingAndStopPropagationTextIntent: DoNothingAction(
|
||||||
|
consumesKey: false,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
void _delete() {
|
void _delete(bool forward) {
|
||||||
if (_value.text.isEmpty) return;
|
if (_value.text.isEmpty) return;
|
||||||
|
|
||||||
late final TextRange deletedRange;
|
late final TextRange deletedRange;
|
||||||
late final TextRange newComposing;
|
late final TextRange newComposing;
|
||||||
final int deletedLength =
|
late final String deletedText;
|
||||||
_value.text.substring(0, _selection.baseOffset).characters.last.length;
|
final int offset = _selection.baseOffset;
|
||||||
|
|
||||||
if (_selection.isCollapsed) {
|
if (_selection.isCollapsed) {
|
||||||
if (_selection.baseOffset == 0) return;
|
if (forward) {
|
||||||
deletedRange = TextRange(
|
if (_selection.baseOffset == _value.text.length) return;
|
||||||
start: _selection.baseOffset - deletedLength,
|
deletedText = _value.text.substring(offset).characters.first;
|
||||||
end: _selection.baseOffset,
|
deletedRange = TextRange(
|
||||||
);
|
start: offset,
|
||||||
|
end: offset + deletedText.length,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
if (_selection.baseOffset == 0) return;
|
||||||
|
deletedText = _value.text.substring(0, offset).characters.last;
|
||||||
|
deletedRange = TextRange(
|
||||||
|
start: offset - deletedText.length,
|
||||||
|
end: offset,
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
deletedRange = _selection;
|
deletedRange = _selection;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user