diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 11c8951b..b0c476e9 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -4,6 +4,7 @@ import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:html2md/html2md.dart' as html2md; import 'package:markdown/markdown.dart' as md; +import 'package:meta/meta.dart'; import '../../../markdown_quill.dart'; import '../../../quill_delta.dart'; @@ -16,6 +17,7 @@ import '../../models/structs/doc_change.dart'; import '../../models/structs/image_url.dart'; import '../../models/structs/offset_value.dart'; import '../../utils/delta.dart'; +import '../toolbar/buttons/toggle_style_button.dart'; typedef ReplaceTextCallback = bool Function(int index, int len, Object? data); typedef DeleteCallback = void Function(int cursorPosition, bool forward); @@ -54,6 +56,7 @@ class QuillController extends ChangeNotifier { notifyListeners(); } + @experimental void setContents( Delta delta, { ChangeSource changeSource = ChangeSource.local, @@ -84,6 +87,17 @@ class QuillController extends ChangeNotifier { /// The current font size, null to use the default one String? get selectedFontSize => _selectedFontSize; + /// For the [QuillToolbarToggleStyleButton] + final Map _selectedStyles = {}; + + /// For the [QuillToolbarToggleStyleButton] + Map get selectedStyles => _selectedStyles; + + /// For the [QuillToolbarToggleStyleButton] + void selectStyle(Attribute attribute, bool value) { + _selectedStyles[attribute] = value; + } + void selectFontSize(String? newFontSize) { _selectedFontSize = newFontSize; } diff --git a/lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart b/lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart index aeb2135e..267f6d57 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart @@ -211,9 +211,7 @@ mixin RawEditorStateTextInputClientMixin on EditorState // TODO: There is a bug here, the first character is not being formatted if (widget.configurations.controller.selectedFontFamily != null) { - widget.configurations.controller.formatText( - diff.start, - diff.deleted.length, + widget.configurations.controller.formatSelection( Attribute.fromKeyValue( Attribute.font.key, widget.configurations.controller.selectedFontFamily, @@ -224,9 +222,7 @@ mixin RawEditorStateTextInputClientMixin on EditorState // TODO: A bug here too if (widget.configurations.controller.selectedFontSize != null) { - widget.configurations.controller.formatText( - diff.start, - diff.deleted.length, + widget.configurations.controller.formatSelection( Attribute.fromKeyValue( Attribute.size.key, widget.configurations.controller.selectedFontSize == '0' @@ -236,6 +232,12 @@ mixin RawEditorStateTextInputClientMixin on EditorState ), ); } + + widget.configurations.controller.selectedStyles.forEach((key, value) { + if (value ?? false) { + widget.configurations.controller.formatSelection(key); + } + }); } } diff --git a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart index e608d161..a44d5561 100644 --- a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart +++ b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart @@ -221,9 +221,13 @@ class QuillToolbarToggleStyleButtonState } void _toggleAttribute() { - controller.formatSelection( - _isToggled! ? Attribute.clone(widget.attribute, null) : widget.attribute, - ); + controller + ..formatSelection( + (_isToggled ?? false) + ? Attribute.clone(widget.attribute, null) + : widget.attribute, + ) + ..selectStyle(widget.attribute, _isToggled == true ? true : false); } } @@ -249,9 +253,10 @@ Widget defaultToggleStyleButtonBuilder( : (iconTheme?.iconUnselectedColor ?? theme.iconTheme.color) : (iconTheme?.disabledIconColor ?? theme.disabledColor); return QuillToolbarIconButton( - icon: Icon(icon, size: iconSize * iconButtonFactor, color: iconColor), - isFilled: isEnabled ? isToggled == true : false, - onPressed: onPressed, - afterPressed: afterPressed, - padding: iconTheme?.padding); + icon: Icon(icon, size: iconSize * iconButtonFactor, color: iconColor), + isFilled: isEnabled ? isToggled == true : false, + onPressed: onPressed, + afterPressed: afterPressed, + padding: iconTheme?.padding, + ); }