Fix the toggle style button (#1596)

* Fix the toggle style button

* Update todos

* Fix conflict with the exsisting solution
pull/1597/head
Ellet 1 year ago committed by GitHub
parent d5888509ec
commit fb029eb40e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 50
      lib/src/widgets/quill/quill_controller.dart
  3. 19
      lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart
  4. 13
      lib/src/widgets/toolbar/buttons/toggle_style_button.dart

@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## 9.0.2-dev.1 ## 9.0.2-dev.1
* Add configurations for the new dropdown `QuillToolbarSelectHeaderStyleButton`, you can use the orignal one or this * Add configurations for the new dropdown `QuillToolbarSelectHeaderStyleButton`, you can use the orignal one or this
* Fix the [issue](https://github.com/singerdmx/flutter-quill/issues/1119) when enter is pressed, all font settings is lost
## 9.0.2-dev ## 9.0.2-dev
* **Breaking change** Remove the spacer widget, removed the controller option for each button * **Breaking change** Remove the spacer widget, removed the controller option for each button

@ -1,9 +1,10 @@
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart' show ClipboardData, Clipboard;
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:html2md/html2md.dart' as html2md; import 'package:html2md/html2md.dart' as html2md;
import 'package:markdown/markdown.dart' as md; import 'package:markdown/markdown.dart' as md;
import 'package:meta/meta.dart';
import '../../../markdown_quill.dart'; import '../../../markdown_quill.dart';
import '../../../quill_delta.dart'; import '../../../quill_delta.dart';
@ -16,6 +17,7 @@ import '../../models/structs/doc_change.dart';
import '../../models/structs/image_url.dart'; import '../../models/structs/image_url.dart';
import '../../models/structs/offset_value.dart'; import '../../models/structs/offset_value.dart';
import '../../utils/delta.dart'; import '../../utils/delta.dart';
import '../toolbar/buttons/toggle_style_button.dart';
typedef ReplaceTextCallback = bool Function(int index, int len, Object? data); typedef ReplaceTextCallback = bool Function(int index, int len, Object? data);
typedef DeleteCallback = void Function(int cursorPosition, bool forward); typedef DeleteCallback = void Function(int cursorPosition, bool forward);
@ -24,14 +26,13 @@ class QuillController extends ChangeNotifier {
QuillController({ QuillController({
required Document document, required Document document,
required TextSelection selection, required TextSelection selection,
bool keepStyleOnNewLine = false, this.keepStyleOnNewLine = true,
this.onReplaceText, this.onReplaceText,
this.onDelete, this.onDelete,
this.onSelectionCompleted, this.onSelectionCompleted,
this.onSelectionChanged, this.onSelectionChanged,
}) : _document = document, }) : _document = document,
_selection = selection, _selection = selection;
_keepStyleOnNewLine = keepStyleOnNewLine;
factory QuillController.basic() { factory QuillController.basic() {
return QuillController( return QuillController(
@ -54,6 +55,7 @@ class QuillController extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
@experimental
void setContents( void setContents(
Delta delta, { Delta delta, {
ChangeSource changeSource = ChangeSource.local, ChangeSource changeSource = ChangeSource.local,
@ -68,6 +70,9 @@ class QuillController extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
// Thoses are the values that the user selects and not the one
// from the current line
/// The current font family, null to use the default one /// The current font family, null to use the default one
String? _selectedFontFamily; String? _selectedFontFamily;
@ -88,9 +93,20 @@ class QuillController extends ChangeNotifier {
_selectedFontSize = newFontSize; _selectedFontSize = newFontSize;
} }
/// For the [QuillToolbarToggleStyleButton]
final Map<Attribute, bool?> _selectedStyles = {};
/// For the [QuillToolbarToggleStyleButton]
Map<Attribute, bool?> get selectedStyles => _selectedStyles;
/// For the [QuillToolbarToggleStyleButton]
void selectStyle(Attribute attribute, bool value) {
_selectedStyles[attribute] = value;
}
/// Tells whether to keep or reset the [toggledStyle] /// Tells whether to keep or reset the [toggledStyle]
/// when user adds a new line. /// when user adds a new line.
final bool _keepStyleOnNewLine; final bool keepStyleOnNewLine;
/// Currently selected text within the [document]. /// Currently selected text within the [document].
TextSelection get selection => _selection; TextSelection get selection => _selection;
@ -269,6 +285,7 @@ class QuillController extends ChangeNotifier {
Object? data, Object? data,
TextSelection? textSelection, { TextSelection? textSelection, {
bool ignoreFocus = false, bool ignoreFocus = false,
bool shouldNotifyListeners = true,
}) { }) {
assert(data is String || data is Embeddable); assert(data is String || data is Embeddable);
@ -324,7 +341,9 @@ class QuillController extends ChangeNotifier {
if (ignoreFocus) { if (ignoreFocus) {
ignoreFocusOnTextChange = true; ignoreFocusOnTextChange = true;
} }
if (shouldNotifyListeners) {
notifyListeners(); notifyListeners();
}
ignoreFocusOnTextChange = false; ignoreFocusOnTextChange = false;
} }
@ -342,7 +361,12 @@ class QuillController extends ChangeNotifier {
}); });
} }
void formatText(int index, int len, Attribute? attribute) { void formatText(
int index,
int len,
Attribute? attribute, {
bool shouldNotifyListeners = true,
}) {
if (len == 0 && if (len == 0 &&
attribute!.isInline && attribute!.isInline &&
attribute.key != Attribute.link.key) { attribute.key != Attribute.link.key) {
@ -361,11 +385,19 @@ class QuillController extends ChangeNotifier {
if (selection != adjustedSelection) { if (selection != adjustedSelection) {
_updateSelection(adjustedSelection, ChangeSource.local); _updateSelection(adjustedSelection, ChangeSource.local);
} }
if (shouldNotifyListeners) {
notifyListeners(); notifyListeners();
} }
}
void formatSelection(Attribute? attribute) { void formatSelection(Attribute? attribute,
formatText(selection.start, selection.end - selection.start, attribute); {bool shouldNotifyListeners = true}) {
formatText(
selection.start,
selection.end - selection.start,
attribute,
shouldNotifyListeners: shouldNotifyListeners,
);
} }
void moveCursorToStart() { void moveCursorToStart() {
@ -447,7 +479,7 @@ class QuillController extends ChangeNotifier {
_selection = selection.copyWith( _selection = selection.copyWith(
baseOffset: math.min(selection.baseOffset, end), baseOffset: math.min(selection.baseOffset, end),
extentOffset: math.min(selection.extentOffset, end)); extentOffset: math.min(selection.extentOffset, end));
if (_keepStyleOnNewLine) { if (keepStyleOnNewLine) {
final style = getSelectionStyle(); final style = getSelectionStyle();
final ignoredStyles = style.attributes.values.where( final ignoredStyles = style.attributes.values.where(
(s) => !s.isInline || s.key == Attribute.link.key, (s) => !s.isInline || s.key == Attribute.link.key,

@ -208,12 +208,8 @@ mixin RawEditorStateTextInputClientMixin on EditorState
value.selection, value.selection,
); );
// TODO: There is a bug here, the first character is not being formatted
if (widget.configurations.controller.selectedFontFamily != null) { if (widget.configurations.controller.selectedFontFamily != null) {
widget.configurations.controller.formatText( widget.configurations.controller.formatSelection(
diff.start,
diff.deleted.length,
Attribute.fromKeyValue( Attribute.fromKeyValue(
Attribute.font.key, Attribute.font.key,
widget.configurations.controller.selectedFontFamily, widget.configurations.controller.selectedFontFamily,
@ -221,12 +217,8 @@ mixin RawEditorStateTextInputClientMixin on EditorState
); );
} }
// TODO: A bug here too
if (widget.configurations.controller.selectedFontSize != null) { if (widget.configurations.controller.selectedFontSize != null) {
widget.configurations.controller.formatText( widget.configurations.controller.formatSelection(
diff.start,
diff.deleted.length,
Attribute.fromKeyValue( Attribute.fromKeyValue(
Attribute.size.key, Attribute.size.key,
widget.configurations.controller.selectedFontSize == '0' widget.configurations.controller.selectedFontSize == '0'
@ -236,6 +228,13 @@ mixin RawEditorStateTextInputClientMixin on EditorState
), ),
); );
} }
// if (widget.configurations.controller.keepStyleOnNewLine) {
// widget.configurations.controller.selectedStyles.forEach((key, value) {
// if (value ?? false) {
// widget.configurations.controller.formatSelection(key);
// }
// });
// }
} }
} }

@ -221,9 +221,13 @@ class QuillToolbarToggleStyleButtonState
} }
void _toggleAttribute() { void _toggleAttribute() {
controller.formatSelection( controller
_isToggled! ? Attribute.clone(widget.attribute, null) : widget.attribute, ..formatSelection(
); (_isToggled ?? false)
? Attribute.clone(widget.attribute, null)
: widget.attribute,
)
..selectStyle(widget.attribute, _isToggled ?? false);
} }
} }
@ -253,5 +257,6 @@ Widget defaultToggleStyleButtonBuilder(
isFilled: isEnabled ? isToggled == true : false, isFilled: isEnabled ? isToggled == true : false,
onPressed: onPressed, onPressed: onPressed,
afterPressed: afterPressed, afterPressed: afterPressed,
padding: iconTheme?.padding); padding: iconTheme?.padding,
);
} }

Loading…
Cancel
Save