Added isNoOp to ve.dm.Transaction

Also added some checks in content branch conversion to make sure that converting from and to the same thing results in a no-op

Change-Id: Ie47520d666e45a77d12c7ebb9457aef7ab6b8097
This commit is contained in:
Trevor Parscal 2012-08-10 15:35:48 -07:00
parent fed1a98dd7
commit a0fa371481
2 changed files with 21 additions and 1 deletions

View file

@ -88,7 +88,7 @@ ve.dm.Surface.prototype.getFragment = function () {
* @param {ve.Range|undefined} selection * @param {ve.Range|undefined} selection
*/ */
ve.dm.Surface.prototype.change = function ( transaction, selection ) { ve.dm.Surface.prototype.change = function ( transaction, selection ) {
if ( transaction ) { if ( transaction && !transaction.isNoOp() ) {
this.bigStack = this.bigStack.slice( 0, this.bigStack.length - this.undoIndex ); this.bigStack = this.bigStack.slice( 0, this.bigStack.length - this.undoIndex );
this.undoIndex = 0; this.undoIndex = 0;
this.smallStack.push( transaction ); this.smallStack.push( transaction );

View file

@ -278,6 +278,10 @@ ve.dm.Transaction.newFromContentBranchConversion = function ( doc, range, type,
selected = selection[i]; selected = selection[i];
if ( selected.node.isContent() ) { if ( selected.node.isContent() ) {
branch = selected.node.getParent(); branch = selected.node.getParent();
// Skip branches that are already of the target type and have identical attributes
if ( branch.getType() === type && ve.compareObjects( branch.getAttributes(), attr ) ) {
continue;
}
branchOuterRange = branch.getOuterRange(); branchOuterRange = branch.getOuterRange();
// Don't convert the same branch twice // Don't convert the same branch twice
if ( branch === previousBranch ) { if ( branch === previousBranch ) {
@ -443,6 +447,22 @@ ve.dm.Transaction.newFromWrap = function ( doc, range, unwrapOuter, wrapOuter, u
/* Methods */ /* Methods */
/**
* Checks if transaction would make any actual changes if processed.
*
* There may be more sophisticated checks that can be done, like looking for things being replaced
* with identical content, but such transactions probably should not be created in the first place.
*
* @method
* @returns {Boolean} Transaction is no-op
*/
ve.dm.Transaction.prototype.isNoOp = function () {
return (
this.operations.length === 0 ||
( this.operations.length === 1 && this.operations[0].type === 'retain' )
);
};
/** /**
* Gets a list of all operations. * Gets a list of all operations.
* *