From 8b17fcdc54d5013a079b149f2f85a5c005ca2d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Mon, 8 Dec 2025 09:52:58 +0100 Subject: [PATCH] fixup! Add text selection mixin --- core/lib/utils/html/html_utils.dart | 75 ++++++++----------- .../widgets/mixins/text_selection_mixin.dart | 4 +- 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/core/lib/utils/html/html_utils.dart b/core/lib/utils/html/html_utils.dart index 656b2a994..00dab4b69 100644 --- a/core/lib/utils/html/html_utils.dart +++ b/core/lib/utils/html/html_utils.dart @@ -52,6 +52,24 @@ class HtmlUtils { script: ''' let lastSelectedText = ''; + const sendSelectionChangeMessage = (data) => { + // When iframe + if (window.parent) { + window.parent.postMessage( + JSON.stringify({ + ...data, + name: 'onSelectionChange', + }), + "*" + ) + } + + // When WebView + if (window.flutter_inappwebview) { + window.flutter_inappwebview.callHandler('onSelectionChange', data); + } + } + document.addEventListener('selectionchange', function() { const selection = window.getSelection(); const selectedText = selection ? selection.toString().trim() : ''; @@ -62,33 +80,14 @@ class HtmlUtils { lastSelectedText = selectedText; if (selectedText.length > 0 && selection.rangeCount > 0) { - const range = selection.getRangeAt(0); - const rects = range.getClientRects(); + try { + const range = selection.getRangeAt(0); + const rects = range.getClientRects(); - if (rects.length > 0) { - const rect = rects[rects.length - 1]; + if (rects.length > 0) { + const rect = rects[rects.length - 1]; - // When iframe - if (window.parent) { - window.parent.postMessage( - JSON.stringify({ - name: 'onSelectionChange', - hasSelection: true, - selectedText: selectedText, - coordinates: { - x: rect.right, - y: rect.bottom, - width: rect.width, - height: rect.height - } - }), - "*" - ) - } - - // When WebView - if (window.flutter_inappwebview) { - window.flutter_inappwebview.callHandler('onSelectionChange', { + sendSelectionChangeMessage({ hasSelection: true, selectedText: selectedText, coordinates: { @@ -97,27 +96,17 @@ class HtmlUtils { width: rect.width, height: rect.height } - }); + }) } + } catch { + sendSelectionChangeMessage({ + hasSelection: false + }) } } else { - // When iframe - if (window.parent) { - window.parent.postMessage( - JSON.stringify({ - name: 'onSelectionChange', - hasSelection: false - }), - "*" - ) - } - - // When WebView - if (window.flutter_inappwebview) { - window.flutter_inappwebview.callHandler('onSelectionChange', { - hasSelection: false - }); - } + sendSelectionChangeMessage({ + hasSelection: false + }) } });''', name: 'onSelectionChange'); diff --git a/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart b/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart index da97d1d77..24fa2bb92 100644 --- a/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart +++ b/lib/features/composer/presentation/widgets/mixins/text_selection_mixin.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +typedef OnTextSelectionChanged = Function(TextSelectionData?); + class TextSelectionData { final bool hasSelection; final String? selectedText; @@ -68,7 +70,7 @@ class TextSelectionCoordinates { } mixin TextSelectionMixin on State { - void Function(TextSelectionData?)? get onSelectionChanged => null; + OnTextSelectionChanged? get onSelectionChanged => null; void handleSelectionChange(Map data) { final selectionData = TextSelectionData.fromMap(data);