penguin/AstroJS

Javascript framework for my blog

commit 5f5e0604fa8d7ddba1ef5a0bcf27941819398e35

author斟酌 鵬兄 <tgckpg@gmail.com>
date2016-03-22T11:31:19Z
subjectWrapping keyboardevent with InputEvent
commit 5f5e0604fa8d7ddba1ef5a0bcf27941819398e35
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2016-03-22T11:31:19Z

    Wrapping keyboardevent with InputEvent
---
 botanjs/src/Components/Vim/Actions/VISUAL.js       |  8 +--
 botanjs/src/Components/Vim/Controls.js             | 78 ++++++++++++++--------
 botanjs/src/Components/Vim/VimArea.js              |  3 +-
 .../externs/Components.Vim.Controls.InputEvent.js  | 13 ++++
 botanjs/src/externs/Components.Vim.Controls.js     |  5 --
 5 files changed, 69 insertions(+), 38 deletions(-)

diff --git a/botanjs/src/Components/Vim/Actions/VISUAL.js b/botanjs/src/Components/Vim/Actions/VISUAL.js
index dad2fbb..ab9671b 100644
--- a/botanjs/src/Components/Vim/Actions/VISUAL.js
+++ b/botanjs/src/Components/Vim/Actions/VISUAL.js
@@ -5,8 +5,6 @@
 	var debug = __import( "System.Debug" );
 
 	var Mesg = __import( "Components.Vim.Message" );
-	/** @type {Components.Vim.Controls} */
-	var Controls = __import( "Components.Vim.Controls" );
 
 	/** @type {Components.Vim.Cursor.IAction} */
 	var YANK = ns[ NS_INVOKE ]( "YANK" );
@@ -41,15 +39,15 @@
 	{
 		e.preventDefault();
 
-		if( Controls.ModKeys( e ) ) return;
+		if( e.ModKeys ) return;
 
 		var Action = null;
 		switch( true )
 		{
-			case Controls.KMap( e, "y" ):
+			case e.kMap( "y" ):
 				Action = new YANK( this.__cursor );
 				break;
-			case Controls.KMap( e, "d" ):
+			case e.kMap( "d" ):
 				Action = new DELETE( this.__cursor );
 				break;
 		}
diff --git a/botanjs/src/Components/Vim/Controls.js b/botanjs/src/Components/Vim/Controls.js
index 918cc41..ce1e947 100644
--- a/botanjs/src/Components/Vim/Controls.js
+++ b/botanjs/src/Components/Vim/Controls.js
@@ -7,6 +7,7 @@
 
 	var SHIFT = 1 << 9;
 	var CTRL = 1 << 10;
+	var ALT = 1 << 11;
 
 	var KEY_SHIFT = 16;
 	var KEY_CTRL = 17;
@@ -139,7 +140,7 @@
 				if( compReg.i == keys.length )
 				{
 					compReg.handler();
-					compReg = null;
+					this.__compReg = null;
 					this.__cMovement = false;
 				}
 
@@ -153,13 +154,13 @@
 		return false;
 	};
 
-	Controls.prototype.__actionCommand = function( e, kCode )
+	Controls.prototype.__actionCommand = function( e )
 	{
 		var ActionHandled = true;
 		var ccur = this.__ccur;
 
 		// Action Command
-		switch( kCode )
+		switch( e.keyCode )
 		{
 			case SHIFT + A: // Append at the line end
 				ccur.lineEnd();
@@ -226,12 +227,13 @@
 		}
 	};
 
-	Controls.prototype.__cursorCommand = function( e, kCode )
+	Controls.prototype.__cursorCommand = function( e )
 	{
+		var kCode = e.keyCode;
+
 		if( this.__cMovement && this.__comp )
 		{
-			var k = e.keyCode;
-			if(!( k == KEY_SHIFT || k == KEY_CTRL  || k == KEY_ALT ))
+			if( !e.ModKeys )
 			{
 				this.__comp( kCode );
 				return true;
@@ -285,19 +287,20 @@
 		return cursorHandled;
 	};
 
+	/**
+	 * sender @param  {Components.Vim.VimArea}
+	 * e @param {Components.Vim.Controls.InputEvent}
+	 * */
 	Controls.prototype.handler = function( sender, e )
 	{
 		// Neve capture these keys
-		if( e.altKey
+		if( e.ModKeys
 			// F2 - F12
 			|| ( F1 < e.keyCode && e.keyCode < 124 )
 		) return;
 
-		// Esc OR Ctrl + c
-		var Escape = e.keyCode == ESC || ( e.ctrlKey && e.keyCode == C );
-
 		// Clear composite command
-		if( Escape && this.__compReg )
+		if( e.Escape && this.__compReg )
 		{
 			this.__compReg = null;
 			this.__cMovement = false;
@@ -308,14 +311,12 @@
 		var cfeeder = this.__cfeeder;
 		var ccur = this.__ccur;
 
-		var kCode = e.keyCode
-			+ ( e.shiftKey || e.getModifierState( "CapsLock" ) ? SHIFT : 0 )
-			+ ( e.ctrlKey ? CTRL : 0 );
+		var kCode = e.keyCode;
 
 		// Action commands are handled by the actions themselves
 		if( ccur.action )
 		{
-			if( Escape )
+			if( e.Escape )
 			{
 				e.preventDefault();
 				ccur.closeAction();
@@ -333,26 +334,49 @@
 			return;
 		}
 
-		e.preventDefault();
+		if( this.__cursorCommand( e ) )
+		{
+			e.preventDefault();
+			return;
+		}
 
-		if( this.__cursorCommand( e, kCode ) ) return;
-		if( this.__actionCommand( e, kCode ) ) return;
+		if( this.__actionCommand( e ) )
+		{
+			e.preventDefault();
+			return;
+		}
 	};
 
-	__static_method( Controls, "ModKeys", function( e )
+	var InputEvent = function( e )
 	{
-		var c = e.keyCode;
-		return c == KEY_SHIFT || c == KEY_CTRL || c == KEY_ALT;
-	} );
+		this.__e = e;
 
-	__static_method( Controls, "KMap", function( e, map )
-	{
-		var kCode = e.keyCode
+		var c = this.__e.keyCode;
+
+		this.__escape = c == ESC || ( e.ctrlKey && c == C );
+		this.__kCode = c
 			+ ( e.shiftKey || e.getModifierState( "CapsLock" ) ? SHIFT : 0 )
 			+ ( e.ctrlKey ? CTRL : 0 );
 
-		return kCode == Map( map );
-	} );
+		this.__modKeys = c == KEY_SHIFT || c == KEY_CTRL || c == KEY_ALT;
+		this.__key = e.key;
+	};
+
+ 	__readOnly( InputEvent.prototype, "key", function() { return this.__key; } );
+ 	__readOnly( InputEvent.prototype, "keyCode", function() { return this.__kCode; } );
+	__readOnly( InputEvent.prototype, "ModKeys", function() { return this.__modKeys; } );
+	__readOnly( InputEvent.prototype, "Escape", function() { return this.__escape; } );
+
+	InputEvent.prototype.kMap = function( map )
+	{
+		return this.__kCode == Map( map );
+	};
+
+	InputEvent.prototype.preventDefault = function()
+	{
+		if( this.__e ) this.__e.preventDefault();
+	};
 
 	ns[ NS_EXPORT ]( EX_CLASS, "Controls", Controls );
+	ns[ NS_EXPORT ]( EX_CLASS, "InputEvent", InputEvent );
 })();
diff --git a/botanjs/src/Components/Vim/VimArea.js b/botanjs/src/Components/Vim/VimArea.js
index d02bdf4..368cecb 100644
--- a/botanjs/src/Components/Vim/VimArea.js
+++ b/botanjs/src/Components/Vim/VimArea.js
@@ -19,6 +19,7 @@
 	var StatusBar = ns[ NS_INVOKE ]( "StatusBar" );
 
 	var VimControls = ns[ NS_INVOKE ]( "Controls" );
+	var InputEvent = ns[ NS_INVOKE ]( "InputEvent" );
 	var mesg = ns[ NS_INVOKE ]( "Message" );
 
 	var Insts = [];
@@ -31,7 +32,7 @@
 			if ( e.keyCode ) code = e.keyCode;
 			else if ( e.which ) code = e.which;
 
-			handler( sender, e );
+			handler( sender, new InputEvent( e ) );
 		};
 	};
 
diff --git a/botanjs/src/externs/Components.Vim.Controls.InputEvent.js b/botanjs/src/externs/Components.Vim.Controls.InputEvent.js
new file mode 100644
index 0000000..ffd085e
--- /dev/null
+++ b/botanjs/src/externs/Components.Vim.Controls.InputEvent.js
@@ -0,0 +1,13 @@
+/** @constructor */
+Components.Vim.Controls.InputEvent = function(){};
+
+/** @type String */
+Components.Vim.Controls.InputEvent.key;
+/** @type Boolean */
+Components.Vim.Controls.InputEvent.ModKeys;
+/** @type Boolean */
+Components.Vim.Controls.InputEvent.Escape;
+/** @type Number */
+Components.Vim.Controls.InputEvent.keyCode;
+/** @type Function */
+Components.Vim.Controls.InputEvent.kMap;
diff --git a/botanjs/src/externs/Components.Vim.Controls.js b/botanjs/src/externs/Components.Vim.Controls.js
index 6fec2e7..d87960f 100644
--- a/botanjs/src/externs/Components.Vim.Controls.js
+++ b/botanjs/src/externs/Components.Vim.Controls.js
@@ -1,7 +1,2 @@
 /** @constructor */
 Components.Vim.Controls = function(){};
-
-/** @type Function */
-Components.Vim.Controls.ModKeys;
-/** @type Function */
-Components.Vim.Controls.KMap;