2012-05-03 20:21:13 +00:00
|
|
|
/**
|
|
|
|
* DataModel surface.
|
2012-05-03 23:02:21 +00:00
|
|
|
*
|
2012-05-03 20:21:13 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-05-03 23:02:21 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
2012-05-03 20:21:13 +00:00
|
|
|
* @param {ve.dm.Document} doc Document model to create surface for
|
|
|
|
*/
|
|
|
|
ve.dm.Surface = function( doc ) {
|
2012-05-03 23:02:21 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.EventEmitter.call( this );
|
2012-05-03 20:21:13 +00:00
|
|
|
// Properties
|
|
|
|
this.documentModel = doc;
|
2012-06-08 23:21:47 +00:00
|
|
|
this.selection = null;
|
2012-06-07 03:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
this.smallStack = [];
|
|
|
|
this.bigStack = [];
|
|
|
|
this.undoIndex = 0;
|
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
setInterval( function () {
|
|
|
|
_this.breakpoint();
|
|
|
|
}, 750 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.dm.Surface.prototype.purgeHistory = function() {
|
2012-05-03 23:02:21 +00:00
|
|
|
this.selection = null;
|
2012-06-07 03:28:06 +00:00
|
|
|
this.smallStack = [];
|
|
|
|
this.bigStack = [];
|
|
|
|
this.undoIndex = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.Surface.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 );
|
|
|
|
}
|
2012-05-03 20:21:13 +00:00
|
|
|
};
|
2012-05-03 23:02:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the document model of the surface.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.DocumentNode} Document model of the surface
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.getDocument = function() {
|
2012-05-04 22:20:14 +00:00
|
|
|
return this.documentModel;
|
2012-05-03 23:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.Range} Current selection
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.getSelection = function() {
|
|
|
|
return this.selection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Range} selection
|
|
|
|
*/
|
2012-05-04 22:29:47 +00:00
|
|
|
ve.dm.Surface.prototype.setSelection = function( selection ) {
|
2012-05-03 23:02:21 +00:00
|
|
|
selection.normalize();
|
2012-05-04 22:29:47 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
( !this.selection ) ||
|
|
|
|
( !this.selection.equals ( selection ) )
|
|
|
|
)
|
|
|
|
{
|
2012-05-03 23:02:21 +00:00
|
|
|
this.selection = selection;
|
2012-05-04 22:29:47 +00:00
|
|
|
}
|
2012-06-07 00:05:32 +00:00
|
|
|
this.emit ('select', this.selection.clone() );
|
2012-05-03 23:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a series of transactions to the content data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Transaction} transactions Tranasction to apply to the document
|
|
|
|
*/
|
|
|
|
ve.dm.Surface.prototype.transact = function( transaction ) {
|
2012-06-07 03:28:06 +00:00
|
|
|
this.bigStack = this.bigStack.slice( 0, this.bigStack.length - this.undoIndex );
|
|
|
|
this.undoIndex = 0;
|
|
|
|
this.smallStack.push( transaction );
|
2012-06-04 21:29:27 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( this.getDocument(), transaction );
|
2012-05-03 23:02:21 +00:00
|
|
|
this.emit( 'transact', transaction );
|
|
|
|
};
|
|
|
|
|
2012-06-04 22:06:37 +00:00
|
|
|
/**
|
|
|
|
* Applies an annotation to the current selection
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String} annotation action: toggle, clear, set
|
|
|
|
* @param {Object} annotation object to apply.
|
|
|
|
*/
|
2012-06-04 21:29:27 +00:00
|
|
|
ve.dm.Surface.prototype.annotate = function( method, annotation ) {
|
|
|
|
var selection = this.getSelection();
|
|
|
|
if ( this.selection.getLength() ) {
|
|
|
|
var tx = ve.dm.Transaction.newFromAnnotation(
|
|
|
|
this.getDocument(), selection, method, annotation
|
|
|
|
);
|
|
|
|
this.transact( tx );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-07 03:28:06 +00:00
|
|
|
ve.dm.Surface.prototype.breakpoint = function( selection ) {
|
|
|
|
if( this.smallStack.length > 0 ) {
|
|
|
|
this.bigStack.push( {
|
|
|
|
stack: this.smallStack,
|
|
|
|
selection: selection || this.selection.clone()
|
|
|
|
} );
|
|
|
|
this.smallStack = [];
|
2012-06-08 23:21:47 +00:00
|
|
|
this.emit ( 'history' );
|
2012-06-07 03:28:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.Surface.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.documentModel.rollback( item.stack[i] );
|
|
|
|
diff += item.stack[i].lengthDifference;
|
|
|
|
}
|
|
|
|
var selection = item.selection;
|
2012-06-08 23:21:47 +00:00
|
|
|
selection.end -= diff;
|
|
|
|
this.emit ( 'history' );
|
|
|
|
return selection;
|
2012-06-07 03:28:06 +00:00
|
|
|
}
|
2012-06-08 23:21:47 +00:00
|
|
|
return null;
|
2012-06-07 03:28:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.Surface.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++ ) {
|
2012-06-08 23:21:47 +00:00
|
|
|
this.documentModel.commit( item.stack[i] );
|
2012-06-07 03:28:06 +00:00
|
|
|
diff += item.stack[i].lengthDifference;
|
|
|
|
}
|
|
|
|
var selection = item.selection;
|
2012-06-08 23:21:47 +00:00
|
|
|
selection.end += diff;
|
2012-06-07 03:28:06 +00:00
|
|
|
}
|
|
|
|
this.undoIndex--;
|
2012-06-08 23:21:47 +00:00
|
|
|
this.emit ( 'history' );
|
|
|
|
return selection;
|
2012-06-07 03:28:06 +00:00
|
|
|
}
|
2012-06-08 23:21:47 +00:00
|
|
|
return null;
|
2012-06-07 03:28:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-03 23:02:21 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.Surface, ve.EventEmitter );
|