From cd27269568380049425b9a0a2baab51311fca769 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 Jan 2015 11:25:56 +0000 Subject: Do not change selection if selection exists. Fixes #1430. --- lib/dom_utils.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index aee2f972..2cf94a14 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -177,9 +177,10 @@ DomUtils = simulateSelect: (element) -> element.focus() - # When focusing a textbox, put the selection caret at the end of the textbox's contents. - # For some HTML5 input types (eg. date) we can't position the caret, so we wrap this with a try. - try element.setSelectionRange(element.value.length, element.value.length) + unless @isSelected element + # When focusing a textbox, put the selection caret at the end of the textbox's contents. + # For some HTML5 input types (eg. date) we can't position the caret, so we wrap this with a try. + try element.setSelectionRange(element.value.length, element.value.length) simulateClick: (element, modifiers) -> modifiers ||= {} -- cgit v1.2.3 From e700982a20737bdc1cf0c4babaa2c199a53a1300 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 Jan 2015 11:35:30 +0000 Subject: Move isSelected test to before possible blur/click. --- lib/dom_utils.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index af563fd1..21a00ae4 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -176,13 +176,14 @@ DomUtils = element.selectionStart? and element.selectionEnd? and element.selectionStart != element.selectionEnd simulateSelect: (element) -> + isSelected = @isSelected element # If element == document.activeElement, then we won't get a new focus event. So, we pretend (to any # active modes which care, e.g. PostFindMode) that element has been clicked. if element == document.activeElement and DomUtils.isEditable document.activeElement handlerStack.bubbleEvent "click", target: element element.focus() - unless @isSelected element + unless isSelected # When focusing a textbox, put the selection caret at the end of the textbox's contents. # For some HTML5 input types (eg. date) we can't position the caret, so we wrap this with a try. try element.setSelectionRange(element.value.length, element.value.length) -- cgit v1.2.3 From 45dd6235a31fecfd3235db9881ddfad8e5a2835a Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 20 Jan 2015 06:10:31 +0000 Subject: Minor refactoring of isSelected(). There's no point in focusing element if it's already active. Also, we leave the caret in place, not just a range selection. isSelected makes the wrong decision if the user has placed the caret at the start of element. We cannot distinguish that case from the user having made no selection. Nevertheless, this is substantially better than the existing UX, which *always* moves the caret to the end. --- lib/dom_utils.coffee | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 21a00ae4..1d1b67ba 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -173,20 +173,23 @@ DomUtils = node = document.getSelection()?.anchorNode node and @isDOMDescendant element, node else - element.selectionStart? and element.selectionEnd? and element.selectionStart != element.selectionEnd + # Note. This makes the wrong decision if the user has placed the caret at the start of element. We + # cannot distinguish that case from the user having made no selection. + element.selectionStart? and element.selectionEnd? and element.selectionEnd != 0 simulateSelect: (element) -> - isSelected = @isSelected element - # If element == document.activeElement, then we won't get a new focus event. So, we pretend (to any - # active modes which care, e.g. PostFindMode) that element has been clicked. + # If element is already active, then we don't move the selection. However, we also won't get a new focus + # event. So, instead we pretend (to any active modes which care, e.g. PostFindMode) that element has been + # clicked. if element == document.activeElement and DomUtils.isEditable document.activeElement handlerStack.bubbleEvent "click", target: element - - element.focus() - unless isSelected - # When focusing a textbox, put the selection caret at the end of the textbox's contents. - # For some HTML5 input types (eg. date) we can't position the caret, so we wrap this with a try. - try element.setSelectionRange(element.value.length, element.value.length) + else + element.focus() + unless @isSelected element + # When focusing a textbox (without an existing selection), put the selection caret at the end of the + # textbox's contents. For some HTML5 input types (eg. date) we can't position the caret, so we wrap + # this with a try. + try element.setSelectionRange(element.value.length, element.value.length) simulateClick: (element, modifiers) -> modifiers ||= {} -- cgit v1.2.3