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;
|
|
|
|
this.history = [];
|
|
|
|
this.historyIndex = 0;
|
|
|
|
this.currentLengthDifference = 0;
|
2011-11-22 22:59:05 +00:00
|
|
|
|
2011-12-06 01:52:38 +00:00
|
|
|
// TODO magic number move to configuration
|
2011-11-22 22:59:05 +00:00
|
|
|
// Configuration
|
|
|
|
this.lengthDifferenceLimit = 24;
|
2011-12-06 01:52:38 +00:00
|
|
|
|
|
|
|
// DEBUG don't commit
|
2011-12-07 22:28:07 +00:00
|
|
|
var _this = this;
|
|
|
|
this.addListener( 'transact', function() { console.log( _this.history ); } );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
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-06 22:45:38 +00:00
|
|
|
if (
|
2011-12-07 22:28:07 +00:00
|
|
|
( ! this.selection ) || ( ! this.selection.equals( selection ) )
|
2011-12-06 22:45:38 +00:00
|
|
|
) {
|
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;
|
|
|
|
if ( isManual ) {
|
2011-12-07 22:28:07 +00:00
|
|
|
this.historyPush( selection );
|
2011-12-06 22:45:38 +00:00
|
|
|
}
|
|
|
|
this.emit( 'select', this.selection.clone() );
|
2011-12-06 01:52:34 +00:00
|
|
|
}
|
2011-12-06 01:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a selection (which is really just a marker for when we stop undo/redo) to the history.
|
|
|
|
* For the history, selections are just markers, so we don't want to record many of them in a row.
|
|
|
|
*
|
|
|
|
* @method
|
2011-12-07 22:28:07 +00:00
|
|
|
* @param {es.Range|es.Transaction} historyItem
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO docs
|
2011-12-06 01:52:38 +00:00
|
|
|
*/
|
2011-12-07 22:28:07 +00:00
|
|
|
es.SurfaceModel.prototype.historyPush = function ( historyItem ) {
|
|
|
|
// truncate anything past our current history position
|
|
|
|
this.history.splice( this.historyIndex );
|
|
|
|
|
|
|
|
// push the next item. Could be combined with above splice given sufficient cleverness
|
|
|
|
this.history.push( historyItem );
|
|
|
|
|
|
|
|
// get ready to insert at the end
|
|
|
|
this.historyIndex = this.history.length;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2011-12-06 01:52:38 +00:00
|
|
|
es.SurfaceModel.prototype.pushSelection = function( selection ) {
|
|
|
|
if ( this.history[ this.history.length - 1 ] instanceof es.Range ) {
|
|
|
|
this.history[ this.history.length - 1 ] = selection;
|
|
|
|
} else {
|
|
|
|
this.history.push( selection );
|
2011-11-23 00:36:46 +00:00
|
|
|
}
|
2011-11-22 22:59:05 +00:00
|
|
|
};
|
2011-12-07 22:28:07 +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-06 01:52:38 +00:00
|
|
|
es.SurfaceModel.prototype.transact = function( transaction, isPartial ) {
|
2011-12-06 22:14:48 +00:00
|
|
|
// console.log( 'tx:' + $.map( transaction.getOperations(), function(tx) { return tx.type; } ).join(",")
|
|
|
|
// + ' isPartial:' + isPartial );
|
2011-11-22 22:59:05 +00:00
|
|
|
this.doc.commit( transaction );
|
2011-12-06 01:52:38 +00:00
|
|
|
|
|
|
|
// if we have changed the kind of operation (delete -> insert or insert -> delete or annotations )
|
|
|
|
// then push a new selection onto the history, to mark where the undo/redo should end.
|
|
|
|
var d = transaction.getLengthDifference();
|
|
|
|
if (
|
|
|
|
!isPartial &&
|
|
|
|
(
|
|
|
|
( d === 0 ) ||
|
|
|
|
( this.currentLengthDifference < 0 && d > 0 ) ||
|
|
|
|
( this.currentLengthDifference > 0 && d < 0 ) ||
|
|
|
|
( Math.abs( this.currentLengthDifference ) > this.lengthDifferenceLimit )
|
|
|
|
)
|
|
|
|
) {
|
2011-12-07 22:28:07 +00:00
|
|
|
this.currentLengthDifference = 0;
|
|
|
|
this.historyPush( this.selection );
|
2011-12-06 01:52:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 22:28:07 +00:00
|
|
|
this.currentLengthDifference += d;
|
|
|
|
this.historyPush( transaction );
|
2011-11-22 22:59:05 +00:00
|
|
|
this.emit( 'transact', transaction );
|
|
|
|
};
|
|
|
|
|
2011-12-06 01:52:38 +00:00
|
|
|
|
2011-11-22 22:59:05 +00:00
|
|
|
/**
|
2011-12-06 01:52:38 +00:00
|
|
|
* Reverses one or more history items.
|
2011-11-22 22:59:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2011-12-07 22:28:07 +00:00
|
|
|
* @param {Integer} n Number of history items to roll back
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
2011-12-07 22:28:07 +00:00
|
|
|
es.SurfaceModel.prototype.undo = function( n ) {
|
2011-12-06 01:52:38 +00:00
|
|
|
|
2011-12-07 22:28:07 +00:00
|
|
|
console.log( this.history );
|
2011-12-06 01:52:38 +00:00
|
|
|
console.log( 'about to undo...' );
|
2011-12-07 22:28:07 +00:00
|
|
|
console.log( "historyIndex: " + this.historyIndex );
|
2011-12-06 01:52:38 +00:00
|
|
|
|
|
|
|
lengthDifference = 0;
|
2011-12-07 22:28:07 +00:00
|
|
|
var finalSelection = null;
|
|
|
|
|
|
|
|
while ( n ) {
|
|
|
|
n--;
|
|
|
|
|
|
|
|
if ( this.history.length ) {
|
|
|
|
for (var i = this.history.length - 1; i >= 0; i-- ) {
|
|
|
|
this.historyIndex = i;
|
|
|
|
if ( this.history[i] instanceof es.Range ) {
|
|
|
|
finalSelection = this.history[i];
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
this.doc.rollback( this.history[i] );
|
|
|
|
}
|
2011-12-01 19:07:40 +00:00
|
|
|
}
|
2011-12-06 01:52:38 +00:00
|
|
|
this.emit( 'undo', this.currentState );
|
2011-12-01 19:07:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-06 01:52:38 +00:00
|
|
|
|
|
|
|
console.log( 'after undo...' );
|
2011-12-07 22:28:07 +00:00
|
|
|
console.log( "historyIndex: " + this.historyIndex );
|
2011-12-06 01:52:38 +00:00
|
|
|
|
2011-12-07 22:28:07 +00:00
|
|
|
this.select( finalSelection );
|
2011-11-22 22:59:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repeats one or more selections and transactions.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} steps Number of steps to repeat
|
|
|
|
*/
|
2011-11-30 22:06:19 +00:00
|
|
|
es.SurfaceModel.prototype.redo = function( steps ) {
|
2011-11-22 22:59:05 +00:00
|
|
|
// TODO: Implement me!
|
|
|
|
this.emit( 'redo'/*, transaction/selection*/ );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
es.extendClass( es.SurfaceModel, es.EventEmitter );
|