add delete handler (#501)

pull/505/head^2
Andy Trand 3 years ago committed by GitHub
parent 35c9f6bb0e
commit 4694189a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      lib/src/widgets/controller.dart
  2. 16
      lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart

@ -11,6 +11,7 @@ import '../models/quill_delta.dart';
import '../utils/diff_delta.dart'; import '../utils/diff_delta.dart';
typedef ReplaceTextCallback = bool Function(int index, int len, Object? data); typedef ReplaceTextCallback = bool Function(int index, int len, Object? data);
typedef DeleteCallback = void Function(int cursorPosition, bool forward);
class QuillController extends ChangeNotifier { class QuillController extends ChangeNotifier {
QuillController({ QuillController({
@ -18,6 +19,7 @@ class QuillController extends ChangeNotifier {
required TextSelection selection, required TextSelection selection,
bool keepStyleOnNewLine = false, bool keepStyleOnNewLine = false,
this.onReplaceText, this.onReplaceText,
this.onDelete,
}) : _selection = selection, }) : _selection = selection,
_keepStyleOnNewLine = keepStyleOnNewLine; _keepStyleOnNewLine = keepStyleOnNewLine;
@ -39,10 +41,13 @@ class QuillController extends ChangeNotifier {
TextSelection get selection => _selection; TextSelection get selection => _selection;
TextSelection _selection; TextSelection _selection;
/// Manual [replaceText] handler /// Custom [replaceText] handler
/// Return false to ignore the event /// Return false to ignore the event
ReplaceTextCallback? onReplaceText; ReplaceTextCallback? onReplaceText;
/// Custom delete handler
DeleteCallback? onDelete;
/// Store any styles attribute that got toggled by the tap of a button /// Store any styles attribute that got toggled by the tap of a button
/// and that has not been applied yet. /// and that has not been applied yet.
/// It gets reset after each format action within the [document]. /// It gets reset after each format action within the [document].
@ -186,6 +191,12 @@ class QuillController extends ChangeNotifier {
ignoreFocusOnTextChange = false; ignoreFocusOnTextChange = false;
} }
/// Called in two cases:
/// forward == false && textBefore.isEmpty
/// forward == true && textAfter.isEmpty
void handleDelete(int cursorPosition, bool forward) =>
onDelete?.call(cursorPosition, forward);
void formatText(int index, int len, Attribute? attribute) { void formatText(int index, int len, Attribute? attribute) {
if (len == 0 && if (len == 0 &&
attribute!.isInline && attribute!.isInline &&

@ -153,12 +153,16 @@ mixin RawEditorStateKeyboardMixin on EditorState {
final newSelection = TextSelection.collapsed(offset: cursorPosition); final newSelection = TextSelection.collapsed(offset: cursorPosition);
final newText = textBefore + textAfter; final newText = textBefore + textAfter;
final size = plainText.length - newText.length; final size = plainText.length - newText.length;
widget.controller.replaceText( if (size == 0) {
cursorPosition, widget.controller.handleDelete(cursorPosition, forward);
size, } else {
'', widget.controller.replaceText(
newSelection, cursorPosition,
); size,
'',
newSelection,
);
}
} }
TextSelection _jumpToBeginOrEndOfWord( TextSelection _jumpToBeginOrEndOfWord(

Loading…
Cancel
Save