2011-11-02 21:00:55 +00:00
|
|
|
/**
|
|
|
|
* Creates an es.SurfaceModel object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2011-11-22 22:59:05 +00:00
|
|
|
* @extends {es.EventEmitter}
|
2011-11-02 21:00:55 +00:00
|
|
|
* @param {es.DocumentModel} doc Document model to create surface for
|
|
|
|
*/
|
|
|
|
es.SurfaceModel = function( doc ) {
|
2011-11-22 22:59:05 +00:00
|
|
|
// Inheritance
|
|
|
|
es.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
2011-11-02 21:00:55 +00:00
|
|
|
this.doc = doc;
|
2011-12-06 01:52:38 +00:00
|
|
|
this.selection = null;
|
2011-11-22 22:59:05 +00:00
|
|
|
|
2011-12-09 23:52:41 +00:00
|
|
|
this.smallStack = [];
|
|
|
|
this.bigStack = [];
|
|
|
|
this.undoIndex = 0;
|
2011-12-06 01:52:38 +00:00
|
|
|
|
2011-12-07 22:28:07 +00:00
|
|
|
var _this = this;
|
2011-12-09 23:52:41 +00:00
|
|
|
setInterval( function () {
|
|
|
|
_this.breakpoint();
|
|
|
|
}, 750 );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2011-12-09 20:40:26 +00:00
|
|
|
es.SurfaceModel.prototype.purgeHistory = function() {
|
|
|
|
this.selection = null;
|
2011-12-12 22:51:32 +00:00
|
|
|
this.smallStack = [];
|
|
|
|
this.bigStack = [];
|
|
|
|
this.undoIndex = 0;
|
2011-12-09 20:40:26 +00:00
|
|
|
};
|
2011-11-22 22:59:05 +00:00
|
|
|
|
2011-12-13 00:56:29 +00:00
|
|
|
es.SurfaceModel.prototype.getHistory = function() {
|
|
|
|
if ( this.smallStack.length > 0 ) {
|
|
|
|
return this.bigStack.slice(0).concat([{stack:this.smallStack.slice(0)}]);
|
|
|
|
} else {
|
|
|
|
return this.bigStack.slice(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-22 22:59:05 +00:00
|
|
|
/**
|
|
|
|
* Gets the document model of the surface.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {es.DocumentModel} Document model of the surface
|
|
|
|
*/
|
2011-11-02 21:00:55 +00:00
|
|
|
es.SurfaceModel.prototype.getDocument = function() {
|
|
|
|
return this.doc;
|
|
|
|
};
|
2011-11-22 22:59:05 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-06 01:52:38 +00:00
|
|
|
* Gets the selection
|
2011-11-22 22:59:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-12-06 01:52:38 +00:00
|
|
|
* @returns {es.Range} Current selection
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
|
|
|
es.SurfaceModel.prototype.getSelection = function() {
|
|
|
|
return this.selection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the selection.
|
|
|
|
*
|
|
|
|
* If changing the selection at a high frequency (such as while dragging) use the combine argument
|
2011-12-06 01:52:38 +00:00
|
|
|
* to avoid them being split up into multiple history items
|
2011-11-22 22:59:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {es.Range} selection
|
2011-12-07 22:28:07 +00:00
|
|
|
* @param {Boolean} isManual Whether this selection was the result of a user action, and thus should be recorded in history...?
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
2011-12-06 01:52:38 +00:00
|
|
|
es.SurfaceModel.prototype.select = function( selection, isManual ) {
|
2011-12-01 01:01:27 +00:00
|
|
|
selection.normalize();
|
2011-12-10 10:33:00 +00:00
|
|
|
/*if (
|
2011-12-07 22:28:07 +00:00
|
|
|
( ! this.selection ) || ( ! this.selection.equals( selection ) )
|
2011-12-10 10:33:00 +00:00
|
|
|
) {*/
|
2011-12-10 00:02:47 +00:00
|
|
|
if ( isManual ) {
|
|
|
|
this.breakpoint();
|
|
|
|
}
|
2011-12-07 22:28:07 +00:00
|
|
|
// check if the last thing is a selection, if so, swap it.
|
2011-12-06 22:45:38 +00:00
|
|
|
this.selection = selection;
|
|
|
|
this.emit( 'select', this.selection.clone() );
|
2011-12-10 10:33:00 +00:00
|
|
|
//}
|
2011-12-06 01:52:38 +00:00
|
|
|
};
|
|
|
|
|
2011-11-22 22:59:05 +00:00
|
|
|
/**
|
|
|
|
* Applies a series of transactions to the content data.
|
|
|
|
*
|
|
|
|
* If committing multiple transactions which are the result of a single user action and need to be
|
2011-12-06 01:52:38 +00:00
|
|
|
* part of a single history item, use the isPartial argument for all but the last one to avoid them being
|
|
|
|
* split up into multple history items.
|
2011-11-22 22:59:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {es.TransactionModel} transactions Tranasction to apply to the document
|
2011-12-06 01:52:38 +00:00
|
|
|
* @param {boolean} isPartial whether this transaction is part of a larger logical grouping of transactions
|
|
|
|
* (such as when replacing - delete, then insert)
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
2011-12-09 23:52:41 +00:00
|
|
|
es.SurfaceModel.prototype.transact = function( transaction ) {
|
2011-12-12 22:51:32 +00:00
|
|
|
this.bigStack = this.bigStack.slice( 0, this.bigStack.length - this.undoIndex );
|
2011-12-09 23:52:41 +00:00
|
|
|
this.undoIndex = 0;
|
|
|
|
this.smallStack.push( transaction );
|
2011-11-22 22:59:05 +00:00
|
|
|
this.doc.commit( transaction );
|
|
|
|
this.emit( 'transact', transaction );
|
|
|
|
};
|
|
|
|
|
2011-12-09 23:52:41 +00:00
|
|
|
es.SurfaceModel.prototype.breakpoint = function( selection ) {
|
|
|
|
if( this.smallStack.length > 0 ) {
|
|
|
|
this.bigStack.push( {
|
|
|
|
stack: this.smallStack,
|
|
|
|
selection: selection || this.selection.clone()
|
|
|
|
} );
|
|
|
|
this.smallStack = [];
|
|
|
|
}};
|
|
|
|
|
|
|
|
es.SurfaceModel.prototype.undo = function() {
|
|
|
|
this.breakpoint();
|
|
|
|
this.undoIndex++
|
|
|
|
if ( this.bigStack[this.bigStack.length - this.undoIndex] ) {
|
|
|
|
var diff = 0;
|
|
|
|
var item = this.bigStack[this.bigStack.length - this.undoIndex];
|
|
|
|
for( var i = item.stack.length - 1; i >= 0; i-- ) {
|
|
|
|
this.doc.rollback( item.stack[i] );
|
|
|
|
diff += item.stack[i].lengthDifference;
|
2011-12-01 19:07:40 +00:00
|
|
|
}
|
2011-12-09 23:52:41 +00:00
|
|
|
var selection = item.selection;
|
|
|
|
selection.from -= diff;
|
|
|
|
selection.to -= diff;
|
|
|
|
this.select( selection );
|
2011-12-01 19:07:40 +00:00
|
|
|
}
|
2011-11-22 22:59:05 +00:00
|
|
|
};
|
|
|
|
|
2011-12-09 23:52:41 +00:00
|
|
|
es.SurfaceModel.prototype.redo = function() {
|
|
|
|
this.breakpoint();
|
|
|
|
if ( this.undoIndex > 0 ) {
|
|
|
|
if ( this.bigStack[this.bigStack.length - this.undoIndex] ) {
|
|
|
|
var diff = 0;
|
|
|
|
var item = this.bigStack[this.bigStack.length - this.undoIndex];
|
|
|
|
for( var i = 0; i < item.stack.length; i++ ) {
|
|
|
|
this.doc.commit( item.stack[i] );
|
|
|
|
diff += item.stack[i].lengthDifference;
|
|
|
|
}
|
|
|
|
var selection = item.selection;
|
|
|
|
selection.from += diff;
|
|
|
|
selection.to += diff;
|
|
|
|
this.selection = null;
|
|
|
|
this.select( selection );
|
|
|
|
}
|
|
|
|
this.undoIndex--;
|
|
|
|
}
|
2011-11-22 22:59:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
es.extendClass( es.SurfaceModel, es.EventEmitter );
|