2011-11-02 21:00:55 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.dm.Surface object.
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.EventEmitter}
|
|
|
|
* @param {ve.dm.DocumentNode} doc Document model to create surface for
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface = function( doc ) {
|
2011-11-22 22:59:05 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.EventEmitter.call( this );
|
2011-11-22 22:59:05 +00:00
|
|
|
|
|
|
|
// 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 */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.purgeHistory = function() {
|
2011-12-09 20:40:26 +00:00
|
|
|
this.selection = null;
|
2011-12-12 22:51:32 +00:00
|
|
|
this.smallStack = [];
|
|
|
|
this.bigStack = [];
|
2012-02-24 00:49:28 +00:00
|
|
|
this.undoIndex = 0;
|
2011-12-09 20:40:26 +00:00
|
|
|
};
|
2011-11-22 22:59:05 +00:00
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.getHistory = function() {
|
2011-12-13 00:56:29 +00:00
|
|
|
if ( this.smallStack.length > 0 ) {
|
2012-02-24 00:49:28 +00:00
|
|
|
return this.bigStack.slice( 0 ).concat( [{ 'stack': this.smallStack.slice(0) }] );
|
2011-12-13 00:56:29 +00:00
|
|
|
} else {
|
2012-02-07 00:42:16 +00:00
|
|
|
return this.bigStack.slice( 0 );
|
2011-12-13 00:56:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-22 22:59:05 +00:00
|
|
|
/**
|
|
|
|
* Gets the document model of the surface.
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.dm.DocumentNode} Document model of the surface
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.getDocument = function() {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.doc;
|
|
|
|
};
|
2011-11-22 22:59:05 +00:00
|
|
|
|
|
|
|
/**
|
2012-02-24 00:49:28 +00:00
|
|
|
* Gets the selection
|
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Range} Current selection
|
2011-11-22 22:59:05 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.getSelection = function() {
|
2011-11-22 22:59:05 +00:00
|
|
|
return this.selection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the selection.
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* 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
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Range} selection
|
2012-02-07 00:42:16 +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
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.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.
|
2012-02-24 00:49:28 +00:00
|
|
|
this.selection = selection;
|
2011-12-06 22:45:38 +00:00
|
|
|
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.
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* If committing multiple transactions which are the result of a single user action and need to be
|
2012-02-07 00:42:16 +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.
|
2012-02-24 00:49:28 +00:00
|
|
|
*
|
2011-11-22 22:59:05 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.Transaction} transactions Tranasction to apply to the document
|
2012-02-07 00:42:16 +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
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.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 );
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.breakpoint = function( selection ) {
|
2011-12-09 23:52:41 +00:00
|
|
|
if( this.smallStack.length > 0 ) {
|
|
|
|
this.bigStack.push( {
|
|
|
|
stack: this.smallStack,
|
|
|
|
selection: selection || this.selection.clone()
|
|
|
|
} );
|
|
|
|
this.smallStack = [];
|
2012-04-02 22:28:26 +00:00
|
|
|
}
|
|
|
|
};
|
2011-12-09 23:52:41 +00:00
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.undo = function() {
|
2011-12-09 23:52:41 +00:00
|
|
|
this.breakpoint();
|
2012-02-24 00:49:28 +00:00
|
|
|
this.undoIndex++;
|
2011-12-09 23:52:41 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Surface.prototype.redo = function() {
|
2011-12-09 23:52:41 +00:00
|
|
|
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 */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.dm.Surface, ve.EventEmitter );
|