Browse Source

cut text selection when pressing char key

bstouls 8 years ago
parent
commit
fc86e3b93e
1 changed files with 12 additions and 4 deletions
  1. 12 4
      h2d/TextInput.hx

+ 12 - 4
h2d/TextInput.hx

@@ -92,10 +92,7 @@ class TextInput extends Text {
 			case K.BACKSPACE, K.DELETE if( selectionRange != null ):
 			case K.BACKSPACE, K.DELETE if( selectionRange != null ):
 				if( !canEdit ) return;
 				if( !canEdit ) return;
 				beforeChange();
 				beforeChange();
-				cursorIndex = selectionRange.start;
-				var end = cursorIndex + selectionRange.length;
-				text = text.substr(0, cursorIndex) + text.substr(end);
-				selectionRange = null;
+				cutSelection();
 				onChange();
 				onChange();
 			case K.DELETE:
 			case K.DELETE:
 				if( cursorIndex < text.length && canEdit ) {
 				if( cursorIndex < text.length && canEdit ) {
@@ -129,6 +126,8 @@ class TextInput extends Text {
 			default:
 			default:
 				if( e.charCode != 0 && canEdit ) {
 				if( e.charCode != 0 && canEdit ) {
 					beforeChange();
 					beforeChange();
+					if( selectionRange != null )
+						cutSelection();
 					text = text.substr(0, cursorIndex) + String.fromCharCode(e.charCode) + text.substr(cursorIndex);
 					text = text.substr(0, cursorIndex) + String.fromCharCode(e.charCode) + text.substr(cursorIndex);
 					cursorIndex++;
 					cursorIndex++;
 					onChange();
 					onChange();
@@ -178,6 +177,15 @@ class TextInput extends Text {
 		addChildAt(interactive, 0);
 		addChildAt(interactive, 0);
 	}
 	}
 
 
+	function cutSelection() {
+		if(selectionRange == null) return false;
+		cursorIndex = selectionRange.start;
+		var end = cursorIndex + selectionRange.length;
+		text = text.substr(0, cursorIndex) + text.substr(end);
+		selectionRange = null;
+		return true;
+	}
+
 	function setState(h:TextHistoryElement) {
 	function setState(h:TextHistoryElement) {
 		text = h.t;
 		text = h.t;
 		cursorIndex = h.c;
 		cursorIndex = h.c;