@@ -35,6 +35,10 @@ extension HandleAiScribeInComposerExtension on ComposerController {
3535 }
3636
3737 Future <void > insertTextInEditor (String text) async {
38+ if (PlatformInfo .isMobile) {
39+ await ensureMobileEditorFocused ();
40+ }
41+
3842 try {
3943 final htmlContent = StringConvert .convertTextContentToHtmlContent (text);
4044
@@ -46,7 +50,7 @@ extension HandleAiScribeInComposerExtension on ComposerController {
4650
4751 richTextWebController? .editorController.insertHtml (htmlContent);
4852 } else {
49- richTextMobileTabletController? .htmlEditorApi? .insertHtml (htmlContent);
53+ await richTextMobileTabletController? .htmlEditorApi? .insertHtml (htmlContent);
5054 }
5155 } catch (e) {
5256 logWarning ('$runtimeType ::insertTextInEditor:Exception = $e ' );
@@ -71,45 +75,45 @@ extension HandleAiScribeInComposerExtension on ComposerController {
7175 }
7276 }
7377
74- Future <bool > saveSelection () async {
78+ Future <String > saveSelection () async {
7579 try {
7680 if (PlatformInfo .isWeb) {
7781 final result = await richTextWebController? .editorController.evaluateJavascriptWeb (
7882 HtmlUtils .saveSelection.name,
7983 hasReturnValue: true ,
80- ) ?? false ;
84+ );
8185 return result;
8286 } else {
8387 final result = await richTextMobileTabletController? .htmlEditorApi? .webViewController
8488 .evaluateJavascript (
8589 source: HtmlUtils .saveSelection.script,
86- ) ?? false ;
90+ );
8791 return result;
8892 }
8993 } catch (e) {
9094 logError ('$runtimeType ::saveSelection:Exception = $e ' );
91- return false ;
95+ return "" ;
9296 }
9397 }
9498
95- Future <bool > restoreSelection () async {
99+ Future <String > restoreSelection () async {
96100 try {
97101 if (PlatformInfo .isWeb) {
98102 final result = await richTextWebController? .editorController.evaluateJavascriptWeb (
99103 HtmlUtils .restoreSelection.name,
100104 hasReturnValue: true ,
101- ) ?? false ;
105+ );
102106 return result;
103107 } else {
104108 final result = await richTextMobileTabletController? .htmlEditorApi? .webViewController
105109 .evaluateJavascript (
106110 source: HtmlUtils .restoreSelection.script,
107- ) ?? false ;
111+ );
108112 return result;
109113 }
110114 } catch (e) {
111115 logError ('$runtimeType ::restoreSelection:Exception = $e ' );
112- return false ;
116+ return "" ;
113117 }
114118 }
115119
@@ -142,10 +146,8 @@ extension HandleAiScribeInComposerExtension on ComposerController {
142146 }
143147
144148 Future <void > saveAndUnfocusForModal () async {
145- final saved = await saveSelection ();
146- if (saved) {
147- await unfocusEditor ();
148- }
149+ await saveSelection ();
150+ await unfocusEditor ();
149151 }
150152
151153 Future <void > ensureMobileEditorFocused () async {
@@ -156,12 +158,12 @@ extension HandleAiScribeInComposerExtension on ComposerController {
156158 }
157159 }
158160
159- void clearTextInEditor () {
161+ Future < void > clearTextInEditor () async {
160162 try {
161163 if (PlatformInfo .isWeb) {
162164 richTextWebController? .editorController.setText ('' );
163165 } else {
164- richTextMobileTabletController? .htmlEditorApi? .setText ('' );
166+ await richTextMobileTabletController? .htmlEditorApi? .setText ('' );
165167 }
166168 } catch (e) {
167169 logWarning ('$runtimeType ::clearTextInEditor:Exception = $e ' );
@@ -178,12 +180,10 @@ extension HandleAiScribeInComposerExtension on ComposerController {
178180 Future <void > onReplaceTextCallback (String text) async {
179181 final selection = editorTextSelection.value? .selectedText;
180182
181- if (PlatformInfo .isMobile) {
182- await restoreSelection ();
183- }
183+ final isSelectionRestored = PlatformInfo .isMobile ? await restoreSelection () : "" ;
184184
185- if (selection == null || selection.isEmpty) {
186- clearTextInEditor ();
185+ if (( selection == null || selection.isEmpty) && isSelectionRestored .isEmpty) {
186+ await clearTextInEditor ();
187187 }
188188
189189 await insertTextInEditor (text);
@@ -215,10 +215,6 @@ extension HandleAiScribeInComposerExtension on ComposerController {
215215 AiScribeSuggestionActions action,
216216 String suggestionText,
217217 ) async {
218- if (PlatformInfo .isMobile) {
219- await ensureMobileEditorFocused ();
220- }
221-
222218 switch (action) {
223219 case AiScribeSuggestionActions .replace:
224220 await onReplaceTextCallback (suggestionText);
0 commit comments