From 4694189a6c6af65c6a04cab0928c2d901c7f8e9e Mon Sep 17 00:00:00 2001 From: Andy Trand Date: Tue, 7 Dec 2021 12:52:50 +0200 Subject: [PATCH 1/3] add delete handler (#501) --- lib/src/widgets/controller.dart | 13 ++++++++++++- .../raw_editor_state_keyboard_mixin.dart | 16 ++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index d47dce72..bb092646 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -11,6 +11,7 @@ import '../models/quill_delta.dart'; import '../utils/diff_delta.dart'; typedef ReplaceTextCallback = bool Function(int index, int len, Object? data); +typedef DeleteCallback = void Function(int cursorPosition, bool forward); class QuillController extends ChangeNotifier { QuillController({ @@ -18,6 +19,7 @@ class QuillController extends ChangeNotifier { required TextSelection selection, bool keepStyleOnNewLine = false, this.onReplaceText, + this.onDelete, }) : _selection = selection, _keepStyleOnNewLine = keepStyleOnNewLine; @@ -39,10 +41,13 @@ class QuillController extends ChangeNotifier { TextSelection get selection => _selection; TextSelection _selection; - /// Manual [replaceText] handler + /// Custom [replaceText] handler /// Return false to ignore the event ReplaceTextCallback? onReplaceText; + /// Custom delete handler + DeleteCallback? onDelete; + /// Store any styles attribute that got toggled by the tap of a button /// and that has not been applied yet. /// It gets reset after each format action within the [document]. @@ -186,6 +191,12 @@ class QuillController extends ChangeNotifier { 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) { if (len == 0 && attribute!.isInline && diff --git a/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart b/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart index db860248..72fd5dc2 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state_keyboard_mixin.dart @@ -153,12 +153,16 @@ mixin RawEditorStateKeyboardMixin on EditorState { final newSelection = TextSelection.collapsed(offset: cursorPosition); final newText = textBefore + textAfter; final size = plainText.length - newText.length; - widget.controller.replaceText( - cursorPosition, - size, - '', - newSelection, - ); + if (size == 0) { + widget.controller.handleDelete(cursorPosition, forward); + } else { + widget.controller.replaceText( + cursorPosition, + size, + '', + newSelection, + ); + } } TextSelection _jumpToBeginOrEndOfWord( From 931f7e65137ac977a5a884c8d587f93c4c14a695 Mon Sep 17 00:00:00 2001 From: Andy Trand Date: Tue, 7 Dec 2021 12:53:13 +0200 Subject: [PATCH 2/3] remove newline on concat (#502) --- lib/src/models/quill_delta.dart | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/src/models/quill_delta.dart b/lib/src/models/quill_delta.dart index e4a11055..3cab1910 100644 --- a/lib/src/models/quill_delta.dart +++ b/lib/src/models/quill_delta.dart @@ -608,9 +608,28 @@ class Delta { } } + /// Removes trailing '\n' + void _trimNewLine() { + if (isNotEmpty) { + final lastOp = _operations.last; + final lastOpData = lastOp.data; + + if (lastOpData is String && lastOpData.endsWith('\n')) { + _operations.removeLast(); + if (lastOpData.length > 1) { + insert(lastOpData.substring(0, lastOpData.length - 1), + lastOp.attributes); + } + } + } + } + /// Concatenates [other] with this delta and returns the result. - Delta concat(Delta other) { + Delta concat(Delta other, {bool trimNewLine = false}) { final result = Delta.from(this); + if (trimNewLine) { + result._trimNewLine(); + } if (other.isNotEmpty) { // In case first operation of other can be merged with last operation in // our list. From e265b54b1bcfcd696f81307161b7edf7b6386778 Mon Sep 17 00:00:00 2001 From: X Code Date: Tue, 7 Dec 2021 03:18:13 -0800 Subject: [PATCH 3/3] Upgrade to 2.1.0. --- CHANGELOG.md | 3 +++ pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 391ce2f1..0704a47e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [2.1.0] +* Add delete handler + ## [2.0.23] * Support custom replaceText handler. diff --git a/pubspec.yaml b/pubspec.yaml index 281475e1..0ed5e01b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_quill description: A rich text editor supporting mobile and web (Demo App @ bulletjournal.us) -version: 2.0.23 +version: 2.1.0 #author: bulletjournal homepage: https://bulletjournal.us/home/index.html repository: https://github.com/singerdmx/flutter-quill