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.
This commit is contained in:
Théo Poizat
2025-12-10 14:25:32 +01:00
committed by Dat H. Pham
parent af5f316e31
commit 8ed8100e7e
2 changed files with 14 additions and 0 deletions
+10
View File
@@ -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');
@@ -185,6 +185,10 @@ class _WebEditorState extends State<WebEditorWidget> 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,