Commit ab550838 authored by campaign's avatar campaign

修改history

parent ca60958e
......@@ -20,7 +20,8 @@ $dependency = Array(
'src/module/layout.green.js',
'src/core/minder.select.js',
'src/module/keyboard.js',
'src/module/mouse.js'
'src/module/mouse.js',
'src/module/history.js'
// 'src/module/editor.js',
// 'src/module/editor.range.js',
// 'src/module/editor.receiver.js',
......
......@@ -6,7 +6,7 @@ describe("utils", function () {
'test':[1,2],
'test1':1
};
var obj2 = utils.clone(obj1);
var obj2 = utils.clonePlainObject(obj1);
it('相等',function(){
expect(utils.compareObject(obj1,obj2)).toBeTruthy();
......
......@@ -27,6 +27,15 @@ var Minder = KityMinder.Minder = kity.createClass( "KityMinder", {
getOptions: function ( key ) {
return this._options[ key ];
},
setDefaultOptions:function(key,val){
var obj = {};
if (utils.isString(key)) {
obj[key] = val
} else {
obj = key;
}
utils.extend(this._options, obj, true);
},
_initMinder: function () {
this._rc = new kity.Group();
......@@ -40,9 +49,6 @@ var Minder = KityMinder.Minder = kity.createClass( "KityMinder", {
this.renderTo( this._options.renderTo );
}
},
clearPaper:function(){
this._rc.clear();
},
renderTo: function ( target ) {
this._paper.renderTo( this._renderTarget = target );
this._bindEvents();
......
......@@ -21,7 +21,8 @@ kity.extendClass( Minder, {
this._modules[ name ] = moduleDeals;
if ( moduleDeals.init ) {
moduleDeals.init.call( me, Utils.extend( moduleDeals.defaultOptions || {}, this._options ) );
this.setDefaultOptions( moduleDeals.defaultOptions || {});
moduleDeals.init.call( me, this._options );
}
//command加入命令池子
......
......@@ -3,7 +3,9 @@ kity.extendClass( Minder, {
getRoot: function () {
return this._root;
},
setRoot:function(root){
this._root = root;
},
handelNodeInsert: function ( node ) {
var rc = this._rc;
node.traverse( function ( current ) {
......
......@@ -188,8 +188,19 @@ var MinderNode = KityMinder.MinderNode = kity.createClass( "MinderNode", {
},
clone:function(){
return utils.clone(this)
},
function cloneNode(parent,isClonedNode){
var _tmp = new KM.MinderNode(isClonedNode.getData());
_tmp.parent = parent;
for(var i= 0,ci;ci=isClonedNode.children[i++];){
cloneNode(_tmp,ci);
}
return _tmp;
}
return function(){
return cloneNode(null,this);
}
}(),
equals:function(node){
if(node.children.length != this.children.length){
return false;
......
......@@ -109,7 +109,7 @@ var utils = Utils = KityMinder.Utils = {
argsToArray: function ( args,index ) {
return Array.prototype.slice.call( args, index || 0 );
},
clone:function (source, target) {
clonePlainObject:function (source, target) {
var tmp;
target = target || {};
for (var i in source) {
......
KityMinder.registerModule( "HistoryModule", function () {
var Scene = kity.createClass('Scene',{
constructor:function(root){
this.data = root.clone();
},
getData:function(){
return this.data;
},
cloneData:function(){
return this.getData().clone();
},
equals:function(scene){
return this.getData().equals(scene.getData())
}
})
var UndoManager = kity.createClass('UndoManager',{
constructor : function(){
});
var HistoryManager = kity.createClass('UndoManager',{
constructor : function(km){
this.list = [];
this.index = 0;
this.hasUndo = false;
this.hasRedo = false;
this.km = km;
},
undo:function(){
if (this.hasUndo) {
if (!this.list[this.index - 1] && this.list.length == 1) {
this.reset();
return;
}
while (this.list[this.index].equals(this.list[this.index - 1])) {
this.index--;
if (this.index == 0) {
return this.restore(0);
}
})
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 );
}
this.restore(--this.index);
}
},
queryState: function ( km ) {
return getStack( km, 'undo' ).empty() ? -1 : 0;
redo:function(){
if (this.hasRedo) {
while (this.list[this.index].equals(this.list[this.index + 1])) {
this.index++;
if (this.index == this.list.length - 1) {
return this.restore(this.index);
}
}
this.restore(++this.index);
}
},
restore:function(){
var scene = this.list[this.index];
this.km.setRoot(scene.cloneData());
this.km.initStyle();
this.update();
},
getScene:function(){
return new Scene(this.km.getRoot())
},
saveScene:function(){
var currentScene = this.getScene();
var lastScene = this.list[this.index];
if(lastScene && lastScene.equals(currentScene)){
return
}
this.list = this.list.slice(0, this.index + 1);
this.list.push(currentScene);
//如果大于最大数量了,就把最前的剔除
if (this.list.length > this.km.getOptions('maxUndoCount')) {
this.list.shift();
}
} );
this.index = this.list.length - 1;
//跟新undo/redo状态
this.update();
},
update : function () {
this.hasRedo = !!this.list[this.index + 1];
this.hasUndo = !!this.list[this.index - 1];
},
reset:function(){
this.list = [];
this.index = 0;
this.hasUndo = false;
this.hasRedo = false;
}
});
this.historyManager = new HistoryManager(this);
var RedoCommand = kity.createClass( "RedoCommand", {
return {
defaultOptions:{
maxUndoCount : 20
},
"commands": {
"undo": kity.createClass( "UndoCommand", {
base: Command,
execute: function ( km ) {
var undoStack = getStack( km, "undo" ),
redoStack = getStack( km, "redo" ),
contextStack, context;
km.historyManager.undo()
},
if ( !redoStack.empty() ) {
contextStack = redoStack.pop();
undoStack.push( contextStack );
queryState: function ( km ) {
km.historyManager.hasUndo ? 0 : -1;
},
isNeedUndo : true
} ),
"redo": kity.createClass( "RedoCommand", {
base: Command,
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 );
}
execute: function ( km ) {
km.historyManager.redo()
},
queryState: function ( km ) {
return getStack( km, 'redo' ).empty() ? -1 : 0;
}
} );
return {
"commands": {
"undo": UndoCommand,
"redo": RedoCommand
return km.historyManager.hasRedo ? 0 : -1;
},
isNeedUndo : true
} )
},
"events": {
"saveScene": function ( e ) {
this.historyManager.saveScene();
}
}
};
......
......@@ -19,9 +19,6 @@ KityMinder.registerModule( "LayoutModule", function () {
clearLayout: function () {
this.setData( 'layout', {} );
this.getRenderContainer().clear();
},
updateLayout: function ( km ) {
km.updateLayout( this );
}
} );
var switchLayout = function ( km, style ) {
......
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