diff --git a/core/lib/utils/html/html_utils.dart b/core/lib/utils/html/html_utils.dart
index 1afac120f..c87a23135 100644
--- a/core/lib/utils/html/html_utils.dart
+++ b/core/lib/utils/html/html_utils.dart
@@ -48,8 +48,11 @@ class HtmlUtils {
editor.parentNode.replaceChild(newEditor, editor);''',
name: 'unregisterDropListener');
- static registerSelectionChangeListener(String viewId) => (
- script: '''
+ static ({String name, String script}) registerSelectionChangeListener(
+ String viewId,
+ ) =>
+ (
+ script: '''
let lastSelectedText = '';
const sendSelectionChangeMessage = (data) => {
@@ -149,7 +152,8 @@ class HtmlUtils {
}
});
''',
- name: 'onSelectionChange');
+ name: 'onSelectionChange',
+ );
static const collapseSelectionToEnd = (
script: '''
diff --git a/lib/features/composer/presentation/widgets/mobile/mobile_editor_widget.dart b/lib/features/composer/presentation/widgets/mobile/mobile_editor_widget.dart
index 2ece1ec3c..172736b70 100644
--- a/lib/features/composer/presentation/widgets/mobile/mobile_editor_widget.dart
+++ b/lib/features/composer/presentation/widgets/mobile/mobile_editor_widget.dart
@@ -38,6 +38,8 @@ class MobileEditorWidget extends StatefulWidget {
class _MobileEditorState extends State with TextSelectionMixin {
late String _createdViewId;
+ InAppWebViewController? _editorController;
+ ({String name, String script})? registerSelectionChange;
@override
void initState() {
@@ -45,17 +47,29 @@ class _MobileEditorState extends State with TextSelectionMix
_createdViewId = HtmlUtils.getRandString(10);
}
+ @override
+ void dispose() {
+ if (registerSelectionChange != null) {
+ _editorController?.removeJavaScriptHandler(
+ handlerName: registerSelectionChange!.name,
+ );
+ }
+ _editorController?.dispose();
+ _editorController = null;
+ super.dispose();
+ }
+
@override
void Function(TextSelectionData?)? get onSelectionChanged => widget.onTextSelectionChanged;
Future _setupSelectionListener(HtmlEditorApi editorApi) async {
- final webViewController = editorApi.webViewController;
+ _editorController = editorApi.webViewController;
- final registerSelectionChange =
+ registerSelectionChange =
HtmlUtils.registerSelectionChangeListener(_createdViewId);
- webViewController.addJavaScriptHandler(
- handlerName: registerSelectionChange.name,
+ _editorController?.addJavaScriptHandler(
+ handlerName: registerSelectionChange!.name,
callback: (args) {
if (!mounted) return;
@@ -68,8 +82,8 @@ class _MobileEditorState extends State with TextSelectionMix
},
);
- await webViewController.evaluateJavascript(
- source: registerSelectionChange.script,
+ await _editorController?.evaluateJavascript(
+ source: registerSelectionChange!.script,
);
}