1
1
import 'dart:math' as math;
2
2
3
- import 'package:flutter/services.dart' ;
3
+ import 'package:flutter/services.dart' show ClipboardData, Clipboard ;
4
4
import 'package:flutter/widgets.dart' ;
5
5
import 'package:html2md/html2md.dart' as html2md;
6
6
import 'package:markdown/markdown.dart' as md;
7
+ import 'package:meta/meta.dart' ;
7
8
8
9
import '../../../markdown_quill.dart' ;
9
10
import '../../../quill_delta.dart' ;
@@ -16,6 +17,7 @@ import '../../models/structs/doc_change.dart';
16
17
import '../../models/structs/image_url.dart' ;
17
18
import '../../models/structs/offset_value.dart' ;
18
19
import '../../utils/delta.dart' ;
20
+ import '../toolbar/buttons/toggle_style_button.dart' ;
19
21
20
22
typedef ReplaceTextCallback = bool Function (int index, int len, Object ? data);
21
23
typedef DeleteCallback = void Function (int cursorPosition, bool forward);
@@ -24,14 +26,13 @@ class QuillController extends ChangeNotifier {
24
26
QuillController ({
25
27
required Document document,
26
28
required TextSelection selection,
27
- bool keepStyleOnNewLine = false ,
29
+ this . keepStyleOnNewLine = true ,
28
30
this .onReplaceText,
29
31
this .onDelete,
30
32
this .onSelectionCompleted,
31
33
this .onSelectionChanged,
32
34
}) : _document = document,
33
- _selection = selection,
34
- _keepStyleOnNewLine = keepStyleOnNewLine;
35
+ _selection = selection;
35
36
36
37
factory QuillController .basic () {
37
38
return QuillController (
@@ -54,6 +55,7 @@ class QuillController extends ChangeNotifier {
54
55
notifyListeners ();
55
56
}
56
57
58
+ @experimental
57
59
void setContents (
58
60
Delta delta, {
59
61
ChangeSource changeSource = ChangeSource .local,
@@ -68,6 +70,9 @@ class QuillController extends ChangeNotifier {
68
70
notifyListeners ();
69
71
}
70
72
73
+ // Thoses are the values that the user selects and not the one
74
+ // from the current line
75
+
71
76
/// The current font family, null to use the default one
72
77
String ? _selectedFontFamily;
73
78
@@ -88,9 +93,20 @@ class QuillController extends ChangeNotifier {
88
93
_selectedFontSize = newFontSize;
89
94
}
90
95
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
+
91
107
/// Tells whether to keep or reset the [toggledStyle]
92
108
/// when user adds a new line.
93
- final bool _keepStyleOnNewLine ;
109
+ final bool keepStyleOnNewLine ;
94
110
95
111
/// Currently selected text within the [document] .
96
112
TextSelection get selection => _selection;
@@ -269,6 +285,7 @@ class QuillController extends ChangeNotifier {
269
285
Object ? data,
270
286
TextSelection ? textSelection, {
271
287
bool ignoreFocus = false ,
288
+ bool shouldNotifyListeners = true ,
272
289
}) {
273
290
assert (data is String || data is Embeddable );
274
291
@@ -324,7 +341,9 @@ class QuillController extends ChangeNotifier {
324
341
if (ignoreFocus) {
325
342
ignoreFocusOnTextChange = true ;
326
343
}
327
- notifyListeners ();
344
+ if (shouldNotifyListeners) {
345
+ notifyListeners ();
346
+ }
328
347
ignoreFocusOnTextChange = false ;
329
348
}
330
349
@@ -342,7 +361,12 @@ class QuillController extends ChangeNotifier {
342
361
});
343
362
}
344
363
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
+ }) {
346
370
if (len == 0 &&
347
371
attribute! .isInline &&
348
372
attribute.key != Attribute .link.key) {
@@ -361,11 +385,19 @@ class QuillController extends ChangeNotifier {
361
385
if (selection != adjustedSelection) {
362
386
_updateSelection (adjustedSelection, ChangeSource .local);
363
387
}
364
- notifyListeners ();
388
+ if (shouldNotifyListeners) {
389
+ notifyListeners ();
390
+ }
365
391
}
366
392
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
+ );
369
401
}
370
402
371
403
void moveCursorToStart () {
@@ -447,7 +479,7 @@ class QuillController extends ChangeNotifier {
447
479
_selection = selection.copyWith (
448
480
baseOffset: math.min (selection.baseOffset, end),
449
481
extentOffset: math.min (selection.extentOffset, end));
450
- if (_keepStyleOnNewLine ) {
482
+ if (keepStyleOnNewLine ) {
451
483
final style = getSelectionStyle ();
452
484
final ignoredStyles = style.attributes.values.where (
453
485
(s) => ! s.isInline || s.key == Attribute .link.key,
0 commit comments