From 3217059ac696bd20eb9fd8beccfb7a1391570710 Mon Sep 17 00:00:00 2001 From: Douglas Ward Date: Sun, 31 Mar 2024 16:06:22 -0600 Subject: [PATCH] toggle_style_button : calls to options.afterButtonPressed replaced by call to class function afterButtonPressed to allow default call to base button settings quill_icon_button: L26 build for isSelected updated to call afterButtonPressed = same as if not selected QuillController _updateSelection removed param=source because not used; added new param insertNewline when true set tog to style of preceding char (last entered); updated replaceText to call _updateSelection for NL document collectStyle: Selecting the start of a line, user expects the style to be the visible style of the line including inline styles --- .../lib/screens/quill/my_quill_toolbar.dart | 36 +++++++------- lib/src/models/documents/document.dart | 11 +++-- lib/src/widgets/quill/quill_controller.dart | 47 ++++++++++++------- .../toolbar/buttons/quill_icon_button.dart | 7 ++- .../toolbar/buttons/toggle_style_button.dart | 4 +- 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/example/lib/screens/quill/my_quill_toolbar.dart b/example/lib/screens/quill/my_quill_toolbar.dart index 5acfb2de..0163b355 100644 --- a/example/lib/screens/quill/my_quill_toolbar.dart +++ b/example/lib/screens/quill/my_quill_toolbar.dart @@ -223,24 +223,24 @@ class MyQuillToolbar extends StatelessWidget { '40': '40.0' }, // headerStyleType: HeaderStyleType.buttons, - // buttonOptions: QuillSimpleToolbarButtonOptions( - // base: QuillToolbarBaseButtonOptions( - // afterButtonPressed: focusNode.requestFocus, - // // iconSize: 20, - // iconTheme: QuillIconTheme( - // iconButtonSelectedData: IconButtonData( - // style: IconButton.styleFrom( - // foregroundColor: Colors.blue, - // ), - // ), - // iconButtonUnselectedData: IconButtonData( - // style: IconButton.styleFrom( - // foregroundColor: Colors.red, - // ), - // ), - // ), - // ), - // ), + buttonOptions: QuillSimpleToolbarButtonOptions( + base: QuillToolbarBaseButtonOptions( + afterButtonPressed: focusNode.requestFocus, + // iconSize: 20, + // iconTheme: QuillIconTheme( + // iconButtonSelectedData: IconButtonData( + // style: IconButton.styleFrom( + // foregroundColor: Colors.blue, + // ), + // ), + // iconButtonUnselectedData: IconButtonData( + // style: IconButton.styleFrom( + // foregroundColor: Colors.red, + // ), + // ), + // ), + ), + ), customButtons: [ QuillToolbarCustomButtonOptions( icon: const Icon(Icons.add_alarm_rounded), diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index 82c6ebd5..1a1f7f27 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -184,11 +184,12 @@ class Document { return (res.node as Line).collectStyle(res.offset, len); } if (res.offset == 0) { - rangeStyle = (res.node as Line).collectStyle(res.offset, len); - return rangeStyle.removeAll({ - for (final attr in rangeStyle.values) - if (attr.isInline) attr - }); + return rangeStyle = (res.node as Line).collectStyle(res.offset, len); +//COMMENT: Selecting the start of a line, user expects the style to be the visible style of the line including inline styles +// return rangeStyle.removeAll({ +// for (final attr in rangeStyle.values) +// if (attr.isInline) attr +// }); } rangeStyle = (res.node as Line).collectStyle(res.offset - 1, len); final linkAttribute = rangeStyle.attributes[Attribute.link.key]; diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 7f95a6cf..b91eae0b 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -327,7 +327,7 @@ class QuillController extends ChangeNotifier { if (textSelection != null) { if (delta == null || delta.isEmpty) { - _updateSelection(textSelection, ChangeSource.local); + _updateSelection(textSelection); } else { final user = Delta() ..retain(index) @@ -335,12 +335,11 @@ class QuillController extends ChangeNotifier { ..delete(len); final positionDelta = getPositionDelta(user, delta); _updateSelection( - textSelection.copyWith( - baseOffset: textSelection.baseOffset + positionDelta, - extentOffset: textSelection.extentOffset + positionDelta, - ), - ChangeSource.local, - ); + textSelection.copyWith( + baseOffset: textSelection.baseOffset + positionDelta, + extentOffset: textSelection.extentOffset + positionDelta, + ), + insertNewline: data == '\n'); } } @@ -389,7 +388,7 @@ class QuillController extends ChangeNotifier { baseOffset: change.transformPosition(selection.baseOffset), extentOffset: change.transformPosition(selection.extentOffset)); if (selection != adjustedSelection) { - _updateSelection(adjustedSelection, ChangeSource.local); + _updateSelection(adjustedSelection); } if (shouldNotifyListeners) { notifyListeners(); @@ -428,7 +427,7 @@ class QuillController extends ChangeNotifier { } void updateSelection(TextSelection textSelection, ChangeSource source) { - _updateSelection(textSelection, source); + _updateSelection(textSelection); notifyListeners(); } @@ -445,7 +444,7 @@ class QuillController extends ChangeNotifier { ), ); if (selection != textSelection) { - _updateSelection(textSelection, source); + _updateSelection(textSelection); } notifyListeners(); @@ -479,21 +478,37 @@ class QuillController extends ChangeNotifier { super.dispose(); } - void _updateSelection(TextSelection textSelection, ChangeSource source) { + /// Comments: + /// Removed param: + /// 'ChangeSource source' as not used within this function! + /// Added param: + /// insertNewline is non-null when user makes an editing change, true when newline is inserted to allow style to be maintained + void _updateSelection(TextSelection textSelection, {bool insertNewline = false}) { _selection = textSelection; final end = document.length - 1; _selection = selection.copyWith( baseOffset: math.min(selection.baseOffset, end), extentOffset: math.min(selection.extentOffset, end)); + // if (keepStyleOnNewLine) { - final style = getSelectionStyle(); - final ignoredStyles = style.attributes.values.where( - (s) => !s.isInline || s.key == Attribute.link.key, - ); - toggledStyle = style.removeAll(ignoredStyles.toSet()); + // + // Update toggledStyle: + // if insertNewline: gets style from preceding/last character entered (if any) + // else clears so style will be style of selection + // + if (insertNewline && selection.start > 0) { + final style = document.collectStyle(selection.start - 1, 0); + final ignoredStyles = style.attributes.values.where( + (s) => !s.isInline || s.key == Attribute.link.key, + ); + toggledStyle = style.removeAll(ignoredStyles.toSet()); + } else { + toggledStyle = const Style(); + } } else { toggledStyle = const Style(); } + // onSelectionChanged?.call(textSelection); } diff --git a/lib/src/widgets/toolbar/buttons/quill_icon_button.dart b/lib/src/widgets/toolbar/buttons/quill_icon_button.dart index 2bf75ebe..5cc759e0 100644 --- a/lib/src/widgets/toolbar/buttons/quill_icon_button.dart +++ b/lib/src/widgets/toolbar/buttons/quill_icon_button.dart @@ -26,7 +26,12 @@ class QuillToolbarIconButton extends StatelessWidget { if (isSelected) { return IconButton.filled( tooltip: tooltip, - onPressed: onPressed, + onPressed: onPressed != null + ? () { + onPressed?.call(); + afterPressed?.call(); + } + : null, icon: icon, style: iconTheme?.iconButtonSelectedData?.style, visualDensity: iconTheme?.iconButtonSelectedData?.visualDensity, diff --git a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart index b02e627f..f93e79a1 100644 --- a/lib/src/widgets/toolbar/buttons/toggle_style_button.dart +++ b/lib/src/widgets/toolbar/buttons/toggle_style_button.dart @@ -148,7 +148,7 @@ class QuillToolbarToggleStyleButtonState void _onPressed() { _toggleAttribute(); - options.afterButtonPressed?.call(); + afterButtonPressed?.call(); } @override @@ -175,7 +175,7 @@ class QuillToolbarToggleStyleButtonState options.fillColor, _isToggled, _toggleAttribute, - options.afterButtonPressed, + afterButtonPressed, iconSize, iconButtonFactor, iconTheme,