Browse Source

TextInput: Add ctrl + backspace/delete

Call onChange() when un/redoing with ctrl Z/Y
Leonardo Jeanteur 3 years ago
parent
commit
d646420e6e
1 changed files with 31 additions and 14 deletions
  1. 31 14
      h2d/TextInput.hx

+ 31 - 14
h2d/TextInput.hx

@@ -160,21 +160,12 @@ class TextInput extends Text {
 
 
 		switch( e.keyCode ) {
 		switch( e.keyCode ) {
 		case K.LEFT if (K.isDown(K.CTRL)):
 		case K.LEFT if (K.isDown(K.CTRL)):
-			if (cursorIndex > 0) {
-				var charset = hxd.Charset.getDefault();
-				while (cursorIndex > 0 && charset.isSpace(StringTools.fastCodeAt(text, cursorIndex - 1))) cursorIndex--;
-				while (cursorIndex > 0 && !charset.isSpace(StringTools.fastCodeAt(text, cursorIndex - 1))) cursorIndex--;
-			}
+			cursorIndex = getWordStart();
 		case K.LEFT:
 		case K.LEFT:
 			if( cursorIndex > 0 )
 			if( cursorIndex > 0 )
 				cursorIndex--;
 				cursorIndex--;
 		case K.RIGHT if (K.isDown(K.CTRL)):
 		case K.RIGHT if (K.isDown(K.CTRL)):
-			var len = text.length;
-			if (cursorIndex < text.length) {
-				var charset = hxd.Charset.getDefault();
-				while (cursorIndex < len && charset.isSpace(StringTools.fastCodeAt(text, cursorIndex))) cursorIndex++;
-				while (cursorIndex < len && !charset.isSpace(StringTools.fastCodeAt(text, cursorIndex))) cursorIndex++;
-			}
+			cursorIndex = getWordEnd();
 		case K.RIGHT:
 		case K.RIGHT:
 			if( cursorIndex < text.length )
 			if( cursorIndex < text.length )
 				cursorIndex++;
 				cursorIndex++;
@@ -190,14 +181,16 @@ class TextInput extends Text {
 		case K.DELETE:
 		case K.DELETE:
 			if( cursorIndex < text.length && canEdit ) {
 			if( cursorIndex < text.length && canEdit ) {
 				beforeChange();
 				beforeChange();
-				text = text.substr(0, cursorIndex) + text.substr(cursorIndex + 1);
+				var end = K.isDown(K.CTRL) ? getWordEnd() : cursorIndex + 1;
+				text = text.substr(0, cursorIndex) + text.substr(end);
 				onChange();
 				onChange();
 			}
 			}
 		case K.BACKSPACE:
 		case K.BACKSPACE:
 			if( cursorIndex > 0 && canEdit ) {
 			if( cursorIndex > 0 && canEdit ) {
 				beforeChange();
 				beforeChange();
-				cursorIndex--;
-				text = text.substr(0, cursorIndex) + text.substr(cursorIndex + 1);
+				var end = cursorIndex;
+				cursorIndex = K.isDown(K.CTRL) ? getWordStart() : cursorIndex - 1;
+				text = text.substr(0, cursorIndex) + text.substr(end);
 				onChange();
 				onChange();
 			}
 			}
 		case K.ESCAPE, K.ENTER, K.NUMPAD_ENTER:
 		case K.ESCAPE, K.ENTER, K.NUMPAD_ENTER:
@@ -208,12 +201,14 @@ class TextInput extends Text {
 			if( undo.length > 0 && canEdit ) {
 			if( undo.length > 0 && canEdit ) {
 				redo.push(curHistoryState());
 				redo.push(curHistoryState());
 				setState(undo.pop());
 				setState(undo.pop());
+				onChange();
 			}
 			}
 			return;
 			return;
 		case K.Y if( K.isDown(K.CTRL) ):
 		case K.Y if( K.isDown(K.CTRL) ):
 			if( redo.length > 0 && canEdit ) {
 			if( redo.length > 0 && canEdit ) {
 				undo.push(curHistoryState());
 				undo.push(curHistoryState());
 				setState(redo.pop());
 				setState(redo.pop());
+				onChange();
 			}
 			}
 			return;
 			return;
 		case K.A if (K.isDown(K.CTRL)):
 		case K.A if (K.isDown(K.CTRL)):
@@ -299,6 +294,28 @@ class TextInput extends Text {
 		return true;
 		return true;
 	}
 	}
 
 
+	function getWordEnd() {
+		var len = text.length;
+		if (cursorIndex >= len) {
+			return cursorIndex;
+		}
+		var charset = hxd.Charset.getDefault();
+		var ret = cursorIndex;
+		while (ret < len && charset.isSpace(StringTools.fastCodeAt(text, ret))) ret++;
+		while (ret < len && !charset.isSpace(StringTools.fastCodeAt(text, ret))) ret++;
+		return ret;
+	}
+	function getWordStart() {
+		if (cursorIndex <= 0) {
+			return cursorIndex;
+		}
+		var charset = hxd.Charset.getDefault();
+		var ret = cursorIndex;
+		while (ret > 0 && charset.isSpace(StringTools.fastCodeAt(text, ret - 1))) ret--;
+		while (ret > 0 && !charset.isSpace(StringTools.fastCodeAt(text, ret - 1))) ret--;
+		return ret;
+	}
+
 	function setState(h:TextHistoryElement) {
 	function setState(h:TextHistoryElement) {
 		text = h.t;
 		text = h.t;
 		cursorIndex = h.c;
 		cursorIndex = h.c;