Commit 3ea5ea8c authored by techird's avatar techird

merge

parent 79fc4a40
......@@ -10,7 +10,7 @@
</body>
<script>
minder = new KM.createMinder( document.body, {
modules: [ "ExampleModule", "RenderModule", "KeyboardModule" ]
modules: [ "KeyboardModule" ]
});
minder.importData({
......
......@@ -3,7 +3,10 @@ var MinderEvent = kity.createClass( 'MindEvent', {
params = params || {};
if ( params.getType && params.getType() == 'ShapeEvent' ) {
this.kityEvent = params;
this.originEvent = params.originEvent;
this.getPosition = params.getPosition.bind( params );
} else if ( params.target && params.preventDefault ) {
this.originEvent = params;
} else {
kity.Utils.extend( this, params );
}
......
......@@ -19,7 +19,7 @@ kity.extendClass( Minder, {
}
},
execCommand: function ( name ) {
var TargetCommand, command, cmdArgs, eventParams, stoped, isTopCommand;
var TargetCommand, command, cmdArgs, eventParams, stoped, isTopCommand, result;
TargetCommand = this._getCommand( name );
if ( !TargetCommand ) {
......@@ -48,7 +48,7 @@ kity.extendClass( Minder, {
if ( !stoped ) {
this._fire( new MinderEvent( "precommand", eventParams, false ) );
command.execute.apply( command, [ this ].concat( cmdArgs ) );
result = command.execute.apply( command, [ this ].concat( cmdArgs ) );
this._fire( new MinderEvent( "command", eventParams, false ) );
// 顶级命令才触发事件
......@@ -67,6 +67,8 @@ kity.extendClass( Minder, {
if ( isTopCommand ) {
this._executingCommand = null;
}
return result || null;
},
queryCommandState: function ( name ) {
......
......@@ -15,6 +15,10 @@ kity.extendClass( Minder, {
return this;
},
isNodeSelected: function ( node ) {
return !!~this.getSelectedNodes().indexOf( node );
},
selectSingle: function ( node ) {
return this.clearSelect().select( node );
},
......
......@@ -24,7 +24,7 @@ KityMinder.registerModule( "HistoryModule", function () {
this.length = 0;
},
splice: function () {
// just to make stack array-like
// to make stack array-like
}
} );
......
KityMinder.registerModule( "KeyboardModule", function () {
function buildPositionNetwork( root ) {
var pointIndexes = [],
x, y;
root.traverse( function ( node ) {
pointIndexes.push( {
x: node.getData( 'x' ),
y: node.getData( 'y' ),
node: node
} );
} );
for ( var i = 0; i < pointIndexes.length; i++ ) {
findClosestPointsFor( pointIndexes, i );
}
}
function calcQuad( p ) {
return p.x > 0 ?
( p.y > 0 ? 1 : 2 ) :
( p.y < 0 ? 3 : 4 );
}
function findClosestPointsFor( pointIndexes, iFind ) {
var find = pointIndexes[ iFind ];
var matrix = new kity.Matrix().translate( -find.x, -find.y ).rotate( -45 );
var most = {}, quad;
var current;
for ( var i = 0; i < pointIndexes.length; i++ ) {
if ( i == iFind ) continue;
current = matrix.transformPoint( pointIndexes[ i ].x, pointIndexes[ i ].y );
quad = calcQuad( current );
if ( !most[ quad ] || current.length() < most[ quad ].point.length ) {
most[ quad ] = {
point: current,
node: pointIndexes[ i ].node
};
}
}
find.node.setData( 'nearestNodes', {
right: most[ 1 ] || null,
top: most[ 2 ] || null,
left: most[ 3 ] || null,
down: most[ 4 ] || null
} );
}
var KBCreateAndEditCommand = kity.createClass( {
base: Command,
execute: function ( km, type, referNode ) {
var node = km.execCommand( 'create' + type + 'node', referNode );
km.execCommand( 'edittext', node );
this.setContentChanged( true );
}
} );
var KBNavigateCommand = kity.createClass( {
base: Command,
execute: function ( km, direction, referNode ) {
var nextNode = referNode.getData( 'nearestNodes' )[ direction ];
if ( nextNode ) {
km.toggleSelect( [ referNode, nextNode ] );
this.execCommand( 'rendernode', [ referNode, nextNode ] );
}
}
} );
return {
// private usage
"commands": {
'kbCreateAndEdit': KBCreateAndEditCommand,
'kbNavigate': KBNavigateCommand
},
"events": {
contentchange: function () {
buildPositionNetwork( this.getRoot() );
},
keydown: function ( e ) {
console.log( e );
var sNodes = this.getSelectedNodes(),
isSingleSelected = sNodes.length === 1,
isRootSelected = this.isNodeSelected( this.getRoot() );
console.log( e.originEvent.keyCode );
e.originEvent.preventDefault();
switch ( e.keyCode ) {
case 13:
// Enter
if ( isSingleSelected ) {
if ( isRootSelected ) {
this.execCommand( 'kbCreateAndEdit', 'child', sNodes[ 0 ] );
} else {
this.execCommand( 'kbCreateAndEdit', 'sibling', sNodes[ 0 ] );
}
}
break;
case 9:
// Tab
if ( isSingleSelected ) {
this.execCommand( 'kbCreateAndEdit', 'child', sNodes[ 0 ] );
}
break;
case 8:
case 46:
// Backspace or Delete
case 37:
case 38:
case 39:
case 40:
if ( isSingleSelected ) {
this.execCommand( 'kbNavigate', {
37: 'left',
38: 'top',
39: 'right',
40: 'down'
}[ e.keyCode ], sNodes[ 0 ] );
}
break;
}
}
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment