瀏覽代碼

added onFocusSelectAll

ssaket 6 年之前
父節點
當前提交
cc9af8bb45
共有 2 個文件被更改,包括 36 次插入15 次删除
  1. 1 1
      dist/preview release/what's new.md
  2. 35 14
      gui/src/2D/controls/inputText.ts

+ 1 - 1
dist/preview release/what's new.md

@@ -29,7 +29,7 @@
 - Added support for performing operations like select all, text highlight, delete selected in `inputText` ([Saket Saurabh](https://github.com/ssaket))
 - Added `inputText.onTextCopyObservable`, `inputText.onTextCutObservable` and `inputText.onTextPasteObservable` to inputText ([Saket Saurabh](https://github.com/ssaket))
 - Added `AdvancedDynamicTexture.onClipboardObservable` to observe for clipboard events in AdvancedDynamicTexture([Saket Saurabh](https://github.com/ssaket))
-
+- Added `inputText.onFocusSelectAll` to allow complete selection of text on focus event.([Saket Saurabh](https://github.com/ssaket))
 
 ### Core Engine
 

+ 35 - 14
gui/src/2D/controls/inputText.ts

@@ -34,6 +34,7 @@ export class InputText extends Control implements IFocusableControl {
     private _highlightedText = "";
     private _startHighlightIndex = 0;
     private _endHighlightIndex = 0;
+    private _onFocusSelectAll = false;
     private _onClipboardObserver: Nullable<Observer<ClipboardInfo>>;
     private _onPointerDblTapObserver: Nullable<Observer<PointerInfo>>;
 
@@ -80,7 +81,7 @@ export class InputText extends Control implements IFocusableControl {
         }
     }
 
-    /** Gets and sets the text highlighter transparency; default: 0.4 */
+    /** Gets or sets the text highlighter transparency; default: 0.4 */
     public get highligherOpacity(): number {
         return this._highligherOpacity;
     }
@@ -92,8 +93,21 @@ export class InputText extends Control implements IFocusableControl {
         this._highligherOpacity = value;
         this._markAsDirty();
     }
+    /** Gets or sets a boolean indicating whether to select complete text by default on input focus */
+    public get onFocusSelectAll(): boolean {
+        return this._onFocusSelectAll;
+    }
+
+    public set onFocusSelectAll(value: boolean) {
+        if (this._onFocusSelectAll === value) {
+            return;
+        }
+
+        this._onFocusSelectAll = value;
+        this._markAsDirty();
+    }
 
-    /** Gets and sets the text hightlight color */
+    /** Gets or sets the text hightlight color */
     public get textHighlightColor(): string {
         return this._textHighlightColor;
     }
@@ -368,6 +382,10 @@ export class InputText extends Control implements IFocusableControl {
             });
         }
 
+        if (this._onFocusSelectAll) {
+            this._selectAllText();
+        }
+
     }
 
     protected _getTypeName(): string {
@@ -395,19 +413,8 @@ export class InputText extends Control implements IFocusableControl {
 
         //select all
         if (evt && (evt.ctrlKey || evt.metaKey) && keyCode === 65) {
-
-            this._blinkIsEven = false;
-            this._isTextHighlightOn = true;
+            this._selectAllText();
             evt.preventDefault();
-
-            //if already highlighted pass
-            if (this._highlightedText) {
-                return;
-            }
-
-            this._startHighlightIndex = 0;
-            this._endHighlightIndex = this._text.length;
-            this._cursorOffset = 0;
             return;
         }
         // Specific cases
@@ -560,6 +567,20 @@ export class InputText extends Control implements IFocusableControl {
         this._isTextHighlightOn = true;
         this._blinkIsEven = false;
     }
+    /** @hidden */
+    private _selectAllText() {
+        this._blinkIsEven = false;
+        this._isTextHighlightOn = true;
+
+        //if already highlighted pass
+        if (this._highlightedText) {
+            return;
+        }
+
+        this._startHighlightIndex = 0;
+        this._endHighlightIndex = this._text.length;
+        this._cursorOffset = 0;
+    }
 
     /**
      * Handles the keyboard event