From a686b5d33489b19f4ab06c67f96c9d1172ed3afb Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 19 Dec 2025 13:16:11 +0700 Subject: [PATCH] feat(ai-scribe): Adding error handling for async operations --- ...andle_ai_scribe_in_composer_extension.dart | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) 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 cb58a33dc..8ed485b32 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 @@ -30,51 +30,63 @@ extension HandleAiScribeInComposerExtension on ComposerController { } void insertTextInEditor(String text) async { - final htmlContent = StringConvert.convertTextContentToHtmlContent(text); + try { + final htmlContent = StringConvert.convertTextContentToHtmlContent(text); - if (PlatformInfo.isWeb) { - await richTextWebController?.editorController.evaluateJavascriptWeb( - HtmlUtils.deleteSelectionContent.name, - hasReturnValue: false, - ); + if (PlatformInfo.isWeb) { + await richTextWebController?.editorController.evaluateJavascriptWeb( + HtmlUtils.deleteSelectionContent.name, + hasReturnValue: false, + ); - richTextWebController?.editorController.insertHtml(htmlContent); - } else { - richTextMobileTabletController?.htmlEditorApi?.insertHtml(htmlContent); + richTextWebController?.editorController.insertHtml(htmlContent); + } else { + richTextMobileTabletController?.htmlEditorApi?.insertHtml(htmlContent); + } + } catch (e) { + logError('$runtimeType::insertTextInEditor:Exception = $e'); } } Future collapseSelection() async { - if (PlatformInfo.isWeb) { - await richTextWebController?.editorController.evaluateJavascriptWeb( - HtmlUtils.collapseSelectionToEnd.name, - hasReturnValue: false, - ); - } else { - await richTextMobileTabletController?.htmlEditorApi?.webViewController - .evaluateJavascript( - source: HtmlUtils.collapseSelectionToEnd.script, - ); + try { + if (PlatformInfo.isWeb) { + await richTextWebController?.editorController.evaluateJavascriptWeb( + HtmlUtils.collapseSelectionToEnd.name, + hasReturnValue: false, + ); + } else { + await richTextMobileTabletController?.htmlEditorApi?.webViewController + .evaluateJavascript( + source: HtmlUtils.collapseSelectionToEnd.script, + ); + } + } catch (e) { + logError('$runtimeType::collapseSelection:Exception = $e'); } } void clearTextInEditor() { - if (PlatformInfo.isWeb) { - richTextWebController?.editorController.setText(''); - } else { - richTextMobileTabletController?.htmlEditorApi?.setText(''); + try { + if (PlatformInfo.isWeb) { + richTextWebController?.editorController.setText(''); + } else { + richTextMobileTabletController?.htmlEditorApi?.setText(''); + } + } catch (e) { + logError('$runtimeType::clearTextInEditor:Exception = $e'); } } // Ensure we only insert at cursor position by collapsing selection before inserting - void onInsertTextCallback(String text) async { + Future onInsertTextCallback(String text) async { await collapseSelection(); insertTextInEditor(text); } // If there is a selection, it will replace the selection, else it will replace everything - void onReplaceTextCallback(String text) async { + void onReplaceTextCallback(String text) { final selection = editorTextSelection.value?.selectedText; if (selection == null || selection.isEmpty) { clearTextInEditor();