From 752f42dc7d5456f79d1dc4facd0825c98039c368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Wed, 14 Jan 2026 14:23:05 +0100 Subject: [PATCH] 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. --- .../rich_text_mobile_tablet_controller.dart | 8 ++++++++ .../handle_ai_scribe_in_composer_extension.dart | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart b/lib/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart index 2b771be04..0008c233b 100644 --- a/lib/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart +++ b/lib/features/composer/presentation/controller/rich_text_mobile_tablet_controller.dart @@ -19,6 +19,14 @@ class RichTextMobileTabletController extends GetxController { Future get isEditorFocused async => await htmlEditorApi?.hasFocus() ?? false; + Future 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'); diff --git a/lib/features/composer/presentation/extensions/ai_scribe/handle_ai_scribe_in_composer_extension.dart b/lib/features/composer/presentation/extensions/ai_scribe/handle_ai_scribe_in_composer_extension.dart index 7b3e5c094..27ea9a451 100644 --- a/lib/features/composer/presentation/extensions/ai_scribe/handle_ai_scribe_in_composer_extension.dart +++ b/lib/features/composer/presentation/extensions/ai_scribe/handle_ai_scribe_in_composer_extension.dart @@ -148,6 +148,14 @@ extension HandleAiScribeInComposerExtension on ComposerController { } } + Future 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);