Commit 8b7c0bfa authored by techird's avatar techird

History Module

parent 0718e9b6
<!DOCTYPE html>
<html>
<head>
<script src="../kity/dist/kitygraph.all.js"></script>
<script src="../dist/dev.php"></script>
</head>
<body>
</body>
<script>
KityMinder.registerModule("Test", function() {
var AppendCommand = kity.createClass({
base: Command,
execute: function( km, str ) {
km.text = km.text + str;
this.appendLength = str.length;
},
revert: function( km ) {
km.text = km.text.substr(0, km.text.length - this.appendLength);
}
});
var DeleteCommand = kity.createClass({
base: Command,
execute: function( km, count ) {
this.deleted = km.text.substr(km.text.length - count);
km.text = km.text.substr(0, km.text.length - count);
},
revert: function(km) {
km.text += this.deleted;
}
});
var BatchCommand = kity.createClass({
base: Command,
execute: function( km ) {
var combines = Array.prototype.slice.call(arguments, 1);
var cmd;
while(combines.length) {
cmd = combines.shift().split(':');
switch(cmd[0]) {
case 'a':
km.execCommand('append', cmd[1]);
break;
case 'd':
km.execCommand('delete', cmd[1]);
break;
}
}
}
});
return {
commands: {
'append': AppendCommand,
'delete': DeleteCommand,
'batch': BatchCommand
}
}
});
minder = new km.KityMinder();
minder.text = "";
function run(name) {
minder.execCommand.apply(minder, arguments);
console.log(name,': ', minder.text);
}
run('append', 'techird');
run('append', ' good ');
run('batch', 'a:good ', 'a:study');
run('undo');
run('redo');
run('undo');
run('redo');
run('delete', 5);
run('undo');
</script>
</html>
\ No newline at end of file
...@@ -21,5 +21,9 @@ $content = ""; ...@@ -21,5 +21,9 @@ $content = "";
header('Content-Type: text/javascript; charset=utf-8'); header('Content-Type: text/javascript; charset=utf-8');
foreach ($dependency as $index => $dep) { foreach ($dependency as $index => $dep) {
echo file_get_contents("../$dep")."\n\n"; if( $_GET['join'] != null) {
echo file_get_contents("../$dep")."\n\n";
} else {
echo "document.write('<script charset=utf-8 src=\"../$dep\"></script>');";
}
} }
\ No newline at end of file
...@@ -78,25 +78,22 @@ kity.extendClass( KityMinder, ( function () { ...@@ -78,25 +78,22 @@ kity.extendClass( KityMinder, ( function () {
var me = this; var me = this;
var _action = new _commands[ name ](); var _action = new _commands[ name ]();
var args = arguments; var cmdArgs = Array.prototype.slice.call( arguments, 1 );
args[ 0 ] = this;
var _sendingArgs = ( function () {
var _args = [];
for ( var i = 1; i < args.length; i++ ) {
_args.push( args[ i ] );
}
return _args;
} )();
var eventParams = { var eventParams = {
command: _action, command: _action,
commandName: name, commandName: name,
commandArgs: _sendingArgs commandArgs: cmdArgs
}; };
var canceled = me._fire( new MinderEvent( 'beforecommand', eventParams, true ) ); var canceled = me._fire( new MinderEvent( 'beforecommand', eventParams, true ) );
if ( !canceled ) { if ( !canceled ) {
me._fire( new MinderEvent( "precommand", eventParams, false ) ); me._fire( new MinderEvent( "precommand", eventParams, false ) );
_action.execute.apply( _action, args );
_action.execute.apply( _action, [ this ].concat( cmdArgs ) );
me._fire( new MinderEvent( "command", eventParams, false ) ); me._fire( new MinderEvent( "command", eventParams, false ) );
if ( _action.isContentChanged() ) { if ( _action.isContentChanged() ) {
......
...@@ -78,12 +78,12 @@ KityMinder.registerModule( "ExampleModule", function () { ...@@ -78,12 +78,12 @@ KityMinder.registerModule( "ExampleModule", function () {
"beforecommand": function ( e ) { "beforecommand": function ( e ) {
// e.cancel() 方法可以阻止 before 事件进入下个阶段 // e.cancel() 方法可以阻止 before 事件进入下个阶段
// e.cancelImmediately() 方法可以阻止当前回调后的回调执行,并且阻止事件进入下个阶段 // e.cancelImmediately() 方法可以阻止当前回调后的回调执行,并且阻止事件进入下个阶段
console.log( e.type + ' fired' ); // console.log( e.type + ' fired' );
}, },
"command": function ( e ) { "command": function ( e ) {
// 命令执行后的事件 // 命令执行后的事件
console.log( e.type + ' fired' ); // console.log( e.type + ' fired' );
}, },
"contentchange": function ( e ) { "contentchange": function ( e ) {
......
KityMinder.registerModule( "HistoryModule", function () { KityMinder.registerModule( "HistoryModule", function () {
var Stack = kity.createClass( "Stack", {
constructor: function ( size ) {
this.size = size || Number.MAX_VALUE;
this.length = 0;
},
push: function ( elem ) {
if ( this.length === this.size ) {
this.shift();
}
this[ this.length++ ] = elem;
},
top: function () {
return this[ this.length - 1 ];
},
pop: function () {
return this[ --this.length ];
},
empty: function () {
return this.length === 0;
},
clear: function () {
this.length = 0;
},
splice: function () {
// just to make stack array-like
}
} );
function getStack( km, type ) {
var stacks = km._hisoryStacks || ( km._hisoryStacks = {} );
return stacks[ type ] || ( stacks[ type ] = new Stack() );
}
function markExecuting( km, command ) {
km._commandExecuting = command;
}
function getExecuting( km ) {
return km._commandExecuting || null;
}
function markRedoing( km, redoing ) {
km._redoing = redoing;
}
function isRedoing( km ) {
return km._redoing;
}
function shouldIgnore( cmdName ) {
return cmdName == 'undo' || cmdName == 'redo';
}
function getCommandContext( e ) {
return {
name: e.commandName,
args: e.commandArgs,
command: e.command
};
}
var UndoCommand = kity.createClass( "UndoCommand", {
base: Command,
execute: function ( km ) {
var undoStack = getStack( km, "undo" ),
redoStack = getStack( km, "redo" ),
contextStack, context;
if ( !undoStack.empty() ) {
contextStack = undoStack.pop();
redoStack.push( contextStack );
for ( var i = contextStack.length - 1; i >= 0; i-- ) {
context = contextStack[ i ];
context.command.revert( km );
}
}
},
queryState: function ( km ) {
return getStack( km, 'undo' ).empty() ? -1 : 0;
}
} );
var RedoCommand = kity.createClass( "RedoCommand", {
base: Command,
execute: function ( km ) {
var undoStack = getStack( km, "undo" ),
redoStack = getStack( km, "redo" ),
contextStack, context;
if ( !redoStack.empty() ) {
contextStack = redoStack.pop();
undoStack.push( contextStack );
markRedoing( km, true );
for ( var i = 0; i < contextStack.length; i++ ) {
context = contextStack[ i ];
context.command.execute.apply( context.command, [ km ].concat( context.args ) );
}
markRedoing( km, false );
}
},
queryState: function ( km ) {
return getStack( km, 'redo' ).empty() ? -1 : 0;
}
} );
return { return {
"commands": { "commands": {
"undo": UndoCommand,
"redo": RedoCommand
}, },
"events": { "events": {
"beforecommand": function ( e ) {
if ( isRedoing( this ) ) {
e.cancel();
}
},
"precommand": function ( e ) {
if ( shouldIgnore( e.commandName ) ) return;
var undoStack = getStack( this, "undo" ),
redoStack = getStack( this, "redo" ),
contextStack;
if ( getExecuting( this ) === null ) {
markExecuting( this, e.command );
undoStack.push( new Stack() );
redoStack.clear();
}
contextStack = undoStack.top();
contextStack.push( getCommandContext( e ) );
},
"command": function ( e ) {
if ( shouldIgnore( e.commandName ) ) return;
if ( getExecuting( this ) === e.command ) {
markExecuting( this, null );
}
}
} }
}; };
} ); } );
\ No newline at end of file
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