From 8ed8100e7e7091fd1987a1ee366a3ff392d9b189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Wed, 10 Dec 2025 14:25:32 +0100 Subject: [PATCH] Add JS method to collapse selection in both composer Collapsing a selection means removing it and keep the cursor at the end. It will be needed by the scribe for "Insert" action where we want to insert a text at the cursor even if the user selected text. --- core/lib/utils/html/html_utils.dart | 10 ++++++++++ .../presentation/widgets/web/web_editor_widget.dart | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/core/lib/utils/html/html_utils.dart b/core/lib/utils/html/html_utils.dart index 00dab4b69..370137532 100644 --- a/core/lib/utils/html/html_utils.dart +++ b/core/lib/utils/html/html_utils.dart @@ -111,6 +111,16 @@ class HtmlUtils { });''', name: 'onSelectionChange'); + static const collapseSelectionToEnd = ( + script: ''' + (() => { + const selection = window.getSelection(); + if (selection) { + selection.collapseToEnd() + } + })();''', + name: 'collapseSelectionToEnd'); + static recalculateEditorHeight({double? maxHeight}) => ( script: ''' const editable = document.querySelector('.note-editable'); diff --git a/lib/features/composer/presentation/widgets/web/web_editor_widget.dart b/lib/features/composer/presentation/widgets/web/web_editor_widget.dart index 9d7127dcc..aa3f3bfc8 100644 --- a/lib/features/composer/presentation/widgets/web/web_editor_widget.dart +++ b/lib/features/composer/presentation/widgets/web/web_editor_widget.dart @@ -185,6 +185,10 @@ class _WebEditorState extends State with TextSelectionMixin { name: HtmlUtils.registerSelectionChangeListener.name, script: HtmlUtils.registerSelectionChangeListener.script, ), + WebScript( + name: HtmlUtils.collapseSelectionToEnd.name, + script: HtmlUtils.collapseSelectionToEnd.script, + ), WebScript( name: HtmlUtils.recalculateEditorHeight(maxHeight: maxHeight).name, script: HtmlUtils.recalculateEditorHeight(maxHeight: maxHeight).script,