From 9a4695a4aa0e0251fca6874bb38ec5b70ad62496 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Sun, 22 Jan 2023 21:07:51 +0000 Subject: [PATCH] Initial commit --- core/modules/editor/engines/framed.js | 6 +++-- core/modules/editor/engines/simple.js | 8 +++--- core/modules/editor/factory.js | 1 + core/modules/utils/dom/dom.js | 27 +++++++++++++++++++ .../tiddlers/widgets/EditTextWidget.tid | 3 ++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/core/modules/editor/engines/framed.js b/core/modules/editor/engines/framed.js index 10ab90b39..c856ad50b 100644 --- a/core/modules/editor/engines/framed.js +++ b/core/modules/editor/engines/framed.js @@ -177,9 +177,11 @@ FramedEngine.prototype.fixHeight = function() { Focus the engine node */ FramedEngine.prototype.focus = function() { - if(this.domNode.focus && this.domNode.select) { + if(this.domNode.focus) { this.domNode.focus(); - this.domNode.select(); + } + if(this.domNode.select) { + $tw.utils.setSelectionByKeyword(this.domNode,this.widget.editFocusSelect); } }; diff --git a/core/modules/editor/engines/simple.js b/core/modules/editor/engines/simple.js index e8be19a75..507a156f7 100644 --- a/core/modules/editor/engines/simple.js +++ b/core/modules/editor/engines/simple.js @@ -119,10 +119,12 @@ SimpleEngine.prototype.fixHeight = function() { /* Focus the engine node */ -SimpleEngine.prototype.focus = function() { - if(this.domNode.focus && this.domNode.select) { +SimpleEngine.prototype.focus = function() { + if(this.domNode.focus) { this.domNode.focus(); - this.domNode.select(); + } + if(this.domNode.select) { + $tw.utils.setSelectionByKeyword(this.domNode,this.widget.editFocusSelect); } }; diff --git a/core/modules/editor/factory.js b/core/modules/editor/factory.js index 90ab66ae9..cad886bcd 100644 --- a/core/modules/editor/factory.js +++ b/core/modules/editor/factory.js @@ -180,6 +180,7 @@ function editTextWidgetFactory(toolbarEngine,nonToolbarEngine) { this.editMinHeight = this.getAttribute("minHeight",DEFAULT_MIN_TEXT_AREA_HEIGHT); this.editFocusPopup = this.getAttribute("focusPopup"); this.editFocus = this.getAttribute("focus"); + this.editFocusSelect = this.getAttribute("focusSelect","yes"); this.editTabIndex = this.getAttribute("tabindex"); this.editCancelPopups = this.getAttribute("cancelPopups","") === "yes"; this.editInputActions = this.getAttribute("inputActions"); diff --git a/core/modules/utils/dom/dom.js b/core/modules/utils/dom/dom.js index 20aa6896d..4cdc46a9c 100644 --- a/core/modules/utils/dom/dom.js +++ b/core/modules/utils/dom/dom.js @@ -28,6 +28,33 @@ exports.domMatchesSelector = function(node,selector) { return node.matches ? node.matches(selector) : node.msMatchesSelector(selector); }; +/* +Select text in a an input or textarea (setSelectionRange crashes on certain input types) +*/ +exports.setSelectionRangeSafe = function(node,start,end,direction) { + try { + node.setSelectionRange(start,end,direction); + } catch(e) { + node.select(); + } +}; + +/* +Select the text in an input or textarea by keyword: +start - place cursor at start of text +end|no - place cursor at end of text +yes - select entire text +*/ +exports.setSelectionByKeyword = function(node,selectType) { + if(selectType === "start") { + $tw.utils.setSelectionRangeSafe(node,0,0); + } else if(selectType === "end" || selectType === "no") { + $tw.utils.setSelectionRangeSafe(node,node.value.length,node.value.length); + } else { + node.select(); + } +}; + exports.removeChildren = function(node) { while(node.hasChildNodes()) { node.removeChild(node.firstChild); diff --git a/editions/tw5.com/tiddlers/widgets/EditTextWidget.tid b/editions/tw5.com/tiddlers/widgets/EditTextWidget.tid index ce8cc6f63..31fef0989 100644 --- a/editions/tw5.com/tiddlers/widgets/EditTextWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/EditTextWidget.tid @@ -1,6 +1,6 @@ caption: edit-text created: 20131024141900000 -modified: 20211104200554064 +modified: 20230122210049893 tags: Widgets title: EditTextWidget type: text/vnd.tiddlywiki @@ -24,6 +24,7 @@ The content of the `<$edit-text>` widget is ignored. |placeholder |Placeholder text to be displayed when the edit field is empty | |focusPopup |Title of a state tiddler for a popup that is displayed when the editing element has focus | |focus |Set to "yes" or "true" to automatically focus the editor after creation | +|focusSelect |<<.from-version 5.2.26>> If `focus` attribute is enabled, determines how the text is selected: "start" places the cursor at the start of the text, "end" or "no" places the cursor at the end of the text and "yes" (the default) selects the entire text | |tabindex |Sets the `tabindex` attribute of the input or textarea to the given value | |autocomplete |<<.from-version 5.1.23>> An optional string to provide a hint to the browser how to handle autocomplete for this input | |tag |Overrides the generated HTML editing element tag. For a multi-line editor use `tag=textarea`. For a single-line editor use `tag=input` |