Skip to content

Commit 06a159a

Browse files
authored
[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 <[email protected]>
1 parent a695bfc commit 06a159a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

simplistic_editor/lib/basic_text_input_client.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
299299
TextSelection get _selection => _value.selection;
300300
late final Map<Type, Action<Intent>> _actions = <Type, Action<Intent>>{
301301
DeleteCharacterIntent: CallbackAction<DeleteCharacterIntent>(
302-
onInvoke: (intent) => _delete(),
302+
onInvoke: (intent) => _delete(intent.forward),
303303
),
304304
ExtendSelectionByCharacterIntent:
305305
CallbackAction<ExtendSelectionByCharacterIntent>(
@@ -315,22 +315,35 @@ class BasicTextInputClientState extends State<BasicTextInputClient>
315315
PasteTextIntent: CallbackAction<PasteTextIntent>(
316316
onInvoke: (intent) => pasteText(intent.cause),
317317
),
318+
DoNothingAndStopPropagationTextIntent: DoNothingAction(
319+
consumesKey: false,
320+
),
318321
};
319322

320-
void _delete() {
323+
void _delete(bool forward) {
321324
if (_value.text.isEmpty) return;
322325

323326
late final TextRange deletedRange;
324327
late final TextRange newComposing;
325-
final int deletedLength =
326-
_value.text.substring(0, _selection.baseOffset).characters.last.length;
328+
late final String deletedText;
329+
final int offset = _selection.baseOffset;
327330

328331
if (_selection.isCollapsed) {
329-
if (_selection.baseOffset == 0) return;
330-
deletedRange = TextRange(
331-
start: _selection.baseOffset - deletedLength,
332-
end: _selection.baseOffset,
333-
);
332+
if (forward) {
333+
if (_selection.baseOffset == _value.text.length) return;
334+
deletedText = _value.text.substring(offset).characters.first;
335+
deletedRange = TextRange(
336+
start: offset,
337+
end: offset + deletedText.length,
338+
);
339+
} else {
340+
if (_selection.baseOffset == 0) return;
341+
deletedText = _value.text.substring(0, offset).characters.last;
342+
deletedRange = TextRange(
343+
start: offset - deletedText.length,
344+
end: offset,
345+
);
346+
}
334347
} else {
335348
deletedRange = _selection;
336349
}

0 commit comments

Comments
 (0)