penguin/AstroJS

Javascript framework for my blog

commit db922421be9a0b99128eacafd15634bbbf5bb1ff

author斟酌 鵬兄 <tgckpg@gmail.com>
date2016-03-17T11:59:07Z
subjectHandle the DELETE / BACKSPACE on INSERT
commit db922421be9a0b99128eacafd15634bbbf5bb1ff
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2016-03-17T11:59:07Z

    Handle the DELETE / BACKSPACE on INSERT
---
 botanjs/src/Components/Vim/Actions/INSERT.js | 12 ++++++++++--
 botanjs/src/Components/Vim/Controls.js       |  3 ++-
 botanjs/src/Components/Vim/Cursor.js         | 24 +++++++++++++++++++-----
 botanjs/src/Components/Vim/LineBuffer.js     |  5 +++++
 4 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/botanjs/src/Components/Vim/Actions/INSERT.js b/botanjs/src/Components/Vim/Actions/INSERT.js
index 708dd44..7523173 100644
--- a/botanjs/src/Components/Vim/Actions/INSERT.js
+++ b/botanjs/src/Components/Vim/Actions/INSERT.js
@@ -14,6 +14,8 @@
 		{
 			case "Tab":
 				return "\t";
+			case "Enter":
+				return "\n";
 			default:
 				return c;
 		}
@@ -106,6 +108,11 @@
 			this.__startPosition = ContentPosition( this.__cursor.feeder );
 		}
 
+		if( c == "\n" )
+		{
+			// todo
+		}
+
 		this.__insertLength += c.length;
 	};
 
@@ -117,9 +124,10 @@
 		switch( e.keyCode )
 		{
 			case 8: // Backspace
-				if( cur.X == 0 ) return;
+				var oY = feeder.panY + cur.Y;
+				if( cur.X == 0 && feeder.panY == 0 && cur.Y == 0 ) return;
 
-				cur.moveX( -1 );
+				cur.moveX( -1, true, true );
 
 				var f = ContentPosition( feeder );
 
diff --git a/botanjs/src/Components/Vim/Controls.js b/botanjs/src/Components/Vim/Controls.js
index 794202c..31bd7ca 100644
--- a/botanjs/src/Components/Vim/Controls.js
+++ b/botanjs/src/Components/Vim/Controls.js
@@ -54,6 +54,7 @@
 		{
 			// Cursor movements
 			case BACKSPACE: // Backspace, go back 1 char, regardless of line
+				cfeeder.cursor.moveX( -1, true );
 				break;
 			case H: // Left
 				cfeeder.cursor.moveX( -1 );
@@ -70,7 +71,7 @@
 
 			// Insert
 			case A: // Append
-				cfeeder.cursor.moveX( 1 );
+				cfeeder.cursor.moveX( 1, true, true );
 				cfeeder.cursor.openAction( "INSERT" );
 				break;
 			case I: // Insert
diff --git a/botanjs/src/Components/Vim/Cursor.js b/botanjs/src/Components/Vim/Cursor.js
index abb5f1b..eb84ad1 100644
--- a/botanjs/src/Components/Vim/Cursor.js
+++ b/botanjs/src/Components/Vim/Cursor.js
@@ -82,7 +82,7 @@
 
 	// Can only be 1, -1
 	// 0 will be treated as undefined
-	Cursor.prototype.moveX = function( d )
+	Cursor.prototype.moveX = function( d, penentrate, phantomSpace )
 	{
 		var x = this.pX;
 
@@ -93,15 +93,29 @@
 
 		var buffs = this.feeder.lineBuffers;
 
+		if( penentrate && x < 0 && ( 0 < this.feeder.panY || 0 < this.Y ) )
+		{
+			this.moveY( -1 );
+			this.lineEnd( phantomSpace );
+			return;
+		}
+
 		/** @type {Components.Vim.LineBuffer} */
 		var line = GetLine( buffs, this.Y );
 		var content = line.visualLines.join( "\n" );
+		var cLen = content.length;
 
 		var c = content[ x ];
 
-		if( c == undefined )
+		// Include empty lines befor cursor end
+		if( ( phantomSpace && cLen - 1 <= x ) || ( cLen == 1 && c == undefined ) )
+		{
+			x = d > 0 ? cLen - 1 : 0;
+		}
+		// ( 2 < cLen ) Exclude empty lines at cursor end
+		else if( ( 2 < cLen && x == cLen - 1 && c == " " ) || c == undefined )
 		{
-			x = d > 0 ? content.length - 1 : 0;
+			x = d > 0 ? cLen - 2 : 0;
 		}
 		else if( c == "\n" )
 		{
@@ -124,9 +138,9 @@
 		this.updatePosition();
 	};
 
-	Cursor.prototype.lineEnd = function()
+	Cursor.prototype.lineEnd = function( phantomSpace )
 	{
-		this.moveX( Number.MAX_VALUE );
+		this.moveX( Number.MAX_VALUE, false, phantomSpace );
 	};
 
 	Cursor.prototype.updatePosition = function()
diff --git a/botanjs/src/Components/Vim/LineBuffer.js b/botanjs/src/Components/Vim/LineBuffer.js
index 2eccb0d..238cfb3 100644
--- a/botanjs/src/Components/Vim/LineBuffer.js
+++ b/botanjs/src/Components/Vim/LineBuffer.js
@@ -97,6 +97,11 @@
 
 	LineBuffer.prototype.toString = function()
 	{
+		if( this.content.length < this.cols )
+		{
+			return this.content + " ";
+		}
+
 		return this.content || " ";
 	};