Skip to content

Commit fb029eb

Browse files
authored
Fix the toggle style button (singerdmx#1596)
* Fix the toggle style button * Update todos * Fix conflict with the exsisting solution
1 parent d588850 commit fb029eb

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44

55
## 9.0.2-dev.1
66
* Add configurations for the new dropdown `QuillToolbarSelectHeaderStyleButton`, you can use the orignal one or this
7+
* Fix the [issue](https://github.com/singerdmx/flutter-quill/issues/1119) when enter is pressed, all font settings is lost
78

89
## 9.0.2-dev
910
* **Breaking change** Remove the spacer widget, removed the controller option for each button

lib/src/widgets/quill/quill_controller.dart

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import 'dart:math' as math;
22

3-
import 'package:flutter/services.dart';
3+
import 'package:flutter/services.dart' show ClipboardData, Clipboard;
44
import 'package:flutter/widgets.dart';
55
import 'package:html2md/html2md.dart' as html2md;
66
import 'package:markdown/markdown.dart' as md;
7+
import 'package:meta/meta.dart';
78

89
import '../../../markdown_quill.dart';
910
import '../../../quill_delta.dart';
@@ -16,6 +17,7 @@ import '../../models/structs/doc_change.dart';
1617
import '../../models/structs/image_url.dart';
1718
import '../../models/structs/offset_value.dart';
1819
import '../../utils/delta.dart';
20+
import '../toolbar/buttons/toggle_style_button.dart';
1921

2022
typedef ReplaceTextCallback = bool Function(int index, int len, Object? data);
2123
typedef DeleteCallback = void Function(int cursorPosition, bool forward);
@@ -24,14 +26,13 @@ class QuillController extends ChangeNotifier {
2426
QuillController({
2527
required Document document,
2628
required TextSelection selection,
27-
bool keepStyleOnNewLine = false,
29+
this.keepStyleOnNewLine = true,
2830
this.onReplaceText,
2931
this.onDelete,
3032
this.onSelectionCompleted,
3133
this.onSelectionChanged,
3234
}) : _document = document,
33-
_selection = selection,
34-
_keepStyleOnNewLine = keepStyleOnNewLine;
35+
_selection = selection;
3536

3637
factory QuillController.basic() {
3738
return QuillController(
@@ -54,6 +55,7 @@ class QuillController extends ChangeNotifier {
5455
notifyListeners();
5556
}
5657

58+
@experimental
5759
void setContents(
5860
Delta delta, {
5961
ChangeSource changeSource = ChangeSource.local,
@@ -68,6 +70,9 @@ class QuillController extends ChangeNotifier {
6870
notifyListeners();
6971
}
7072

73+
// Thoses are the values that the user selects and not the one
74+
// from the current line
75+
7176
/// The current font family, null to use the default one
7277
String? _selectedFontFamily;
7378

@@ -88,9 +93,20 @@ class QuillController extends ChangeNotifier {
8893
_selectedFontSize = newFontSize;
8994
}
9095

96+
/// For the [QuillToolbarToggleStyleButton]
97+
final Map<Attribute, bool?> _selectedStyles = {};
98+
99+
/// For the [QuillToolbarToggleStyleButton]
100+
Map<Attribute, bool?> get selectedStyles => _selectedStyles;
101+
102+
/// For the [QuillToolbarToggleStyleButton]
103+
void selectStyle(Attribute attribute, bool value) {
104+
_selectedStyles[attribute] = value;
105+
}
106+
91107
/// Tells whether to keep or reset the [toggledStyle]
92108
/// when user adds a new line.
93-
final bool _keepStyleOnNewLine;
109+
final bool keepStyleOnNewLine;
94110

95111
/// Currently selected text within the [document].
96112
TextSelection get selection => _selection;
@@ -269,6 +285,7 @@ class QuillController extends ChangeNotifier {
269285
Object? data,
270286
TextSelection? textSelection, {
271287
bool ignoreFocus = false,
288+
bool shouldNotifyListeners = true,
272289
}) {
273290
assert(data is String || data is Embeddable);
274291

@@ -324,7 +341,9 @@ class QuillController extends ChangeNotifier {
324341
if (ignoreFocus) {
325342
ignoreFocusOnTextChange = true;
326343
}
327-
notifyListeners();
344+
if (shouldNotifyListeners) {
345+
notifyListeners();
346+
}
328347
ignoreFocusOnTextChange = false;
329348
}
330349

@@ -342,7 +361,12 @@ class QuillController extends ChangeNotifier {
342361
});
343362
}
344363

345-
void formatText(int index, int len, Attribute? attribute) {
364+
void formatText(
365+
int index,
366+
int len,
367+
Attribute? attribute, {
368+
bool shouldNotifyListeners = true,
369+
}) {
346370
if (len == 0 &&
347371
attribute!.isInline &&
348372
attribute.key != Attribute.link.key) {
@@ -361,11 +385,19 @@ class QuillController extends ChangeNotifier {
361385
if (selection != adjustedSelection) {
362386
_updateSelection(adjustedSelection, ChangeSource.local);
363387
}
364-
notifyListeners();
388+
if (shouldNotifyListeners) {
389+
notifyListeners();
390+
}
365391
}
366392

367-
void formatSelection(Attribute? attribute) {
368-
formatText(selection.start, selection.end - selection.start, attribute);
393+
void formatSelection(Attribute? attribute,
394+
{bool shouldNotifyListeners = true}) {
395+
formatText(
396+
selection.start,
397+
selection.end - selection.start,
398+
attribute,
399+
shouldNotifyListeners: shouldNotifyListeners,
400+
);
369401
}
370402

371403
void moveCursorToStart() {
@@ -447,7 +479,7 @@ class QuillController extends ChangeNotifier {
447479
_selection = selection.copyWith(
448480
baseOffset: math.min(selection.baseOffset, end),
449481
extentOffset: math.min(selection.extentOffset, end));
450-
if (_keepStyleOnNewLine) {
482+
if (keepStyleOnNewLine) {
451483
final style = getSelectionStyle();
452484
final ignoredStyles = style.attributes.values.where(
453485
(s) => !s.isInline || s.key == Attribute.link.key,

lib/src/widgets/raw_editor/raw_editor_state_text_input_client_mixin.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,17 @@ mixin RawEditorStateTextInputClientMixin on EditorState
208208
value.selection,
209209
);
210210

211-
// TODO: There is a bug here, the first character is not being formatted
212-
213211
if (widget.configurations.controller.selectedFontFamily != null) {
214-
widget.configurations.controller.formatText(
215-
diff.start,
216-
diff.deleted.length,
212+
widget.configurations.controller.formatSelection(
217213
Attribute.fromKeyValue(
218214
Attribute.font.key,
219215
widget.configurations.controller.selectedFontFamily,
220216
),
221217
);
222218
}
223219

224-
// TODO: A bug here too
225-
226220
if (widget.configurations.controller.selectedFontSize != null) {
227-
widget.configurations.controller.formatText(
228-
diff.start,
229-
diff.deleted.length,
221+
widget.configurations.controller.formatSelection(
230222
Attribute.fromKeyValue(
231223
Attribute.size.key,
232224
widget.configurations.controller.selectedFontSize == '0'
@@ -236,6 +228,13 @@ mixin RawEditorStateTextInputClientMixin on EditorState
236228
),
237229
);
238230
}
231+
// if (widget.configurations.controller.keepStyleOnNewLine) {
232+
// widget.configurations.controller.selectedStyles.forEach((key, value) {
233+
// if (value ?? false) {
234+
// widget.configurations.controller.formatSelection(key);
235+
// }
236+
// });
237+
// }
239238
}
240239
}
241240

lib/src/widgets/toolbar/buttons/toggle_style_button.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,13 @@ class QuillToolbarToggleStyleButtonState
221221
}
222222

223223
void _toggleAttribute() {
224-
controller.formatSelection(
225-
_isToggled! ? Attribute.clone(widget.attribute, null) : widget.attribute,
226-
);
224+
controller
225+
..formatSelection(
226+
(_isToggled ?? false)
227+
? Attribute.clone(widget.attribute, null)
228+
: widget.attribute,
229+
)
230+
..selectStyle(widget.attribute, _isToggled ?? false);
227231
}
228232
}
229233

@@ -249,9 +253,10 @@ Widget defaultToggleStyleButtonBuilder(
249253
: (iconTheme?.iconUnselectedColor ?? theme.iconTheme.color)
250254
: (iconTheme?.disabledIconColor ?? theme.disabledColor);
251255
return QuillToolbarIconButton(
252-
icon: Icon(icon, size: iconSize * iconButtonFactor, color: iconColor),
253-
isFilled: isEnabled ? isToggled == true : false,
254-
onPressed: onPressed,
255-
afterPressed: afterPressed,
256-
padding: iconTheme?.padding);
256+
icon: Icon(icon, size: iconSize * iconButtonFactor, color: iconColor),
257+
isFilled: isEnabled ? isToggled == true : false,
258+
onPressed: onPressed,
259+
afterPressed: afterPressed,
260+
padding: iconTheme?.padding,
261+
);
257262
}

0 commit comments

Comments
 (0)