@@ -14,20 +14,16 @@ class ToolbarResult {
14
14
15
15
class Toolbar {
16
16
final TextEditingController controller;
17
- final FocusNode focusNode;
18
- final ValueChanged <bool > isEditorFocused;
19
-
20
- Toolbar (
21
- {required this .controller,
22
- required this .focusNode,
23
- required this .isEditorFocused});
24
-
25
- // check if have selection text
26
- bool checkHasSelection () {
27
- return (controller.selection.baseOffset -
28
- controller.selection.extentOffset) !=
29
- 0 ;
30
- }
17
+ final VoidCallback ? bringEditorToFocus;
18
+
19
+ Toolbar ({required this .controller, this .bringEditorToFocus});
20
+
21
+ /// Returns true if controller contains selection text
22
+ ///
23
+ /// else returns false
24
+ bool get hasSelection =>
25
+ (controller.selection.baseOffset - controller.selection.extentOffset) !=
26
+ 0 ;
31
27
32
28
// get selection text pffset
33
29
TextSelection getSelection (TextSelection selection) {
@@ -40,27 +36,27 @@ class Toolbar {
40
36
}
41
37
42
38
// toolbar action
43
- void action (
44
- String left,
45
- String right, {
46
- TextSelection ? textSelection,
47
- }) {
48
- if (! focusNode.hasFocus) {
49
- print ('Editor is not in focus' );
50
-
51
- isEditorFocused (true );
52
- focusNode.requestFocus ();
53
- }
39
+ void action (String left, String right, { TextSelection ? textSelection}) {
40
+ // Keep this as it is
41
+ // Dont remove or place in the end
42
+ bringEditorToFocus ? . call ();
43
+
44
+ // if (!focusNode.hasFocus) {
45
+ // print('Editor is not in focus');
46
+
47
+ // isEditorFocused(true);
48
+ // focusNode.requestFocus();
49
+ // }
54
50
55
51
// default parameter
56
- final currentTextValue = controller.value.text;
57
- var selection = textSelection ?? controller.selection;
52
+ final String currentTextValue = controller.value.text;
53
+ TextSelection selection = textSelection ?? controller.selection;
58
54
selection = getSelection (selection);
59
55
60
- final middle = selection.textInside (currentTextValue);
61
- var selectionText = '$left $middle $right ' ;
62
- var baseOffset = left.length + middle.length;
63
- var extentOffset = selection.extentOffset + left.length + right.length;
56
+ final String middle = selection.textInside (currentTextValue);
57
+ String selectionText = '$left $middle $right ' ;
58
+ int baseOffset = left.length + middle.length;
59
+ int extentOffset = selection.extentOffset + left.length + right.length;
64
60
65
61
// check if middle text have char \n
66
62
if (middle.split ("\n " ).length > 1 ) {
@@ -80,7 +76,7 @@ class Toolbar {
80
76
extentOffset = selection.extentOffset - (left.length + right.length);
81
77
}
82
78
83
- final newTextValue = selection.textBefore (currentTextValue) +
79
+ final String newTextValue = selection.textBefore (currentTextValue) +
84
80
selectionText +
85
81
selection.textAfter (currentTextValue);
86
82
@@ -106,12 +102,12 @@ class Toolbar {
106
102
String right,
107
103
int selection,
108
104
) {
109
- final splitData = middle.split ("\n " );
110
- var index = 0 ;
111
- var resetLength = 0 ;
112
- var addLength = 0 ;
105
+ final List < String > splitData = middle.split ("\n " );
106
+ int index = 0 ;
107
+ int resetLength = 0 ;
108
+ int addLength = 0 ;
113
109
114
- final selectionText = splitData.map ((text) {
110
+ final String selectionText = splitData.map ((text) {
115
111
index++ ;
116
112
addLength += left.length + right.length;
117
113
@@ -127,12 +123,12 @@ class Toolbar {
127
123
addLength -= left.length + right.length;
128
124
}
129
125
130
- final newText = text.trim ().isEmpty ? text : "$left $text $right " ;
126
+ final String newText = text.trim ().isEmpty ? text : "$left $text $right " ;
131
127
return index == splitData.length ? newText : "$newText \n " ;
132
128
}).join ();
133
129
134
- final baseOffset = addLength + (middle.length - (resetLength * 2 ));
135
- final extentOffset = selection + addLength - (resetLength * 2 );
130
+ final int baseOffset = addLength + (middle.length - (resetLength * 2 ));
131
+ final int extentOffset = selection + addLength - (resetLength * 2 );
136
132
137
133
return ToolbarResult (
138
134
baseOffset: baseOffset,
@@ -142,19 +138,21 @@ class Toolbar {
142
138
}
143
139
144
140
void selectSingleLine () {
145
- var currentPosition = controller.selection;
141
+ TextSelection currentPosition = controller.selection;
146
142
if (! currentPosition.isValid) {
147
143
currentPosition = currentPosition.copyWith (
148
144
baseOffset: 0 ,
149
145
extentOffset: 0 ,
150
146
);
151
147
}
152
- var textBefore = currentPosition.textBefore (controller.text);
153
- var textAfter = currentPosition.textAfter (controller.text);
148
+ String textBefore = currentPosition.textBefore (controller.text);
149
+ String textAfter = currentPosition.textAfter (controller.text);
154
150
155
151
textBefore = textBefore.split ("\n " ).last;
156
152
textAfter = textAfter.split ("\n " )[0 ];
157
- final firstTextPosition = controller.text.indexOf (textBefore + textAfter);
153
+
154
+ final int firstTextPosition =
155
+ controller.text.indexOf (textBefore + textAfter);
158
156
controller.value = controller.value.copyWith (
159
157
selection: TextSelection (
160
158
baseOffset: firstTextPosition,
0 commit comments