From 7c7f61fe90b45ebb35f99a308c289c417fbdcdb1 Mon Sep 17 00:00:00 2001 From: kevinDespoulains <46108869+kevinDespoulains@users.noreply.github.com> Date: Tue, 31 Aug 2021 18:10:08 +0200 Subject: [PATCH] Option added to controller to keep inline style on new lines (#372) Co-authored-by: Kevin Despoulains --- lib/src/widgets/controller.dart | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/src/widgets/controller.dart b/lib/src/widgets/controller.dart index f26765b0..7d2e0714 100644 --- a/lib/src/widgets/controller.dart +++ b/lib/src/widgets/controller.dart @@ -14,7 +14,9 @@ class QuillController extends ChangeNotifier { QuillController({ required this.document, required TextSelection selection, - }) : _selection = selection; + bool keepStyleOnNewLine = false, + }) : _selection = selection, + _keepStyleOnNewLine = keepStyleOnNewLine; factory QuillController.basic() { return QuillController( @@ -26,6 +28,10 @@ class QuillController extends ChangeNotifier { /// Document managed by this controller. final Document document; + /// Tells whether to keep or reset the [toggledStyle] + /// when user adds a new line. + final bool _keepStyleOnNewLine; + /// Currently selected text within the [document]. TextSelection get selection => _selection; TextSelection _selection; @@ -135,7 +141,14 @@ class QuillController extends ChangeNotifier { } } - toggledStyle = Style(); + if (_keepStyleOnNewLine) { + final style = getSelectionStyle(); + final notInlineStyle = style.attributes.values.where((s) => !s.isInline); + toggledStyle = style.removeAll(notInlineStyle.toSet()); + } else { + toggledStyle = Style(); + } + if (textSelection != null) { if (delta == null || delta.isEmpty) { _updateSelection(textSelection, ChangeSource.LOCAL);