From 6d99d40539fd413a74fea836f9a1244168d43788 Mon Sep 17 00:00:00 2001 From: crasowas <110958409+crasowas@users.noreply.github.com> Date: Fri, 24 May 2024 00:29:20 +0800 Subject: [PATCH] fix: toolbar style change will be invalid when inputting more than 2 characters at a time (#1890) --- lib/src/widgets/quill/quill_controller.dart | 32 ++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 234d4ee3..b6369e2e 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -261,9 +261,7 @@ class QuillController extends ChangeNotifier { var shouldRetainDelta = toggledStyle.isNotEmpty && delta.isNotEmpty && delta.length <= 2 && - delta.last.isInsert && - // pasted text should not use toggledStyle - (data is! String || data.length < 2); + delta.last.isInsert; if (shouldRetainDelta && toggledStyle.isNotEmpty && delta.length == 2 && @@ -518,13 +516,27 @@ class QuillController extends ChangeNotifier { // See https://github.com/flutter/flutter/issues/11427 final plainText = await Clipboard.getData(Clipboard.kTextPlain); if (plainText != null) { - replaceTextWithEmbeds( - selection.start, - selection.end - selection.start, - plainText.text!, - TextSelection.collapsed( - offset: selection.start + plainText.text!.length), - ); + final lines = plainText.text!.split('\n'); + for (var i = 0; i < lines.length; ++i) { + final line = lines[i]; + if (line.isNotEmpty) { + replaceTextWithEmbeds( + selection.start, + selection.end - selection.start, + line, + TextSelection.collapsed(offset: selection.start + line.length), + ); + } + if (i != lines.length - 1) { + document.insert(selection.extentOffset, '\n'); + _updateSelection( + TextSelection.collapsed( + offset: selection.extentOffset + 1, + ), + insertNewline: true, + ); + } + } updateEditor?.call(); return true; }