Commit ab550838 authored by campaign's avatar campaign

修改history

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