Ensure editor is focused before calling insert HTML

enough_html_editor uses execCommand to insert HTML. However
execCommand only works if the contenteditable element is focused. So
inserting or replacing Scribe result do not work if the user did not
click before in the composer.

Here we manually focus the contenteditable element to fix this before
inserting content.
This commit is contained in:
Théo Poizat
2026-01-14 14:23:05 +01:00
committed by Dat H. Pham
parent 84176cb113
commit 752f42dc7d
2 changed files with 20 additions and 0 deletions
@@ -19,6 +19,14 @@ class RichTextMobileTabletController extends GetxController {
Future<bool> get isEditorFocused async => await htmlEditorApi?.hasFocus() ?? false;
Future<void> focus() async {
try {
await htmlEditorApi?.webViewController.evaluateJavascript(source: "document.getElementById('editor').focus();");
} catch (e) {
logWarning('RichTextMobileTabletController::focus:Exception: $e');
}
}
void insertImage(InlineImage inlineImage) async {
final isFocused = await isEditorFocused;
log('RichTextMobileTabletController::insertImage: isEditorFocused = $isFocused');
@@ -148,6 +148,14 @@ extension HandleAiScribeInComposerExtension on ComposerController {
}
}
Future<void> ensureMobileEditorFocused() async {
try {
await richTextMobileTabletController?.focus();
} catch (e) {
logError('$runtimeType::ensureMobileEditorFocused:Exception = $e');
}
}
void clearTextInEditor() {
try {
if (PlatformInfo.isWeb) {
@@ -207,6 +215,10 @@ extension HandleAiScribeInComposerExtension on ComposerController {
AiScribeSuggestionActions action,
String suggestionText,
) async {
if (PlatformInfo.isMobile) {
await ensureMobileEditorFocused();
}
switch (action) {
case AiScribeSuggestionActions.replace:
await onReplaceTextCallback(suggestionText);