Pass array of transactions to transact method if all those transactions are part of one "interaction"

This commit is contained in:
Inez Korczynski 2011-12-08 01:09:52 +00:00
parent fceda78696
commit 7da531b59e
3 changed files with 39 additions and 40 deletions

View file

@ -68,9 +68,7 @@ es.FormatDropdownTool.prototype.onSelect = function( item ) {
item.type,
item.attributes
);
for ( var i = 0; i < txs.length; i++ ) {
this.toolbar.surfaceView.model.transact( txs[i] );
}
this.toolbar.surfaceView.model.transact( txs );
};
es.FormatDropdownTool.prototype.updateState = function( annotations, nodes ) {

View file

@ -29,7 +29,7 @@ es.ListButtonTool.prototype.list = function( nodes, style ) {
insertAt,
removeLength,
data,
tx,
txs = [],
i,
j;
@ -82,13 +82,12 @@ es.ListButtonTool.prototype.list = function( nodes, style ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles[styles.length - 1] !== style ) {
styles.splice( styles.length - 1, 1, style );
tx = surface.model.getDocument().prepareElementAttributeChange(
txs.push( surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode( listItems[i], false ),
'set',
'styles',
styles
);
surface.model.transact( tx );
) );
}
}
@ -113,16 +112,12 @@ es.ListButtonTool.prototype.list = function( nodes, style ) {
}
data = data.concat( [ { 'type': '/list' } ] );
tx = surface.model.getDocument().prepareInsertion( insertAt, data );
surface.model.transact( tx );
tx = surface.model.getDocument().prepareRemoval(
txs.push( surface.model.getDocument().prepareInsertion( insertAt, data ) );
txs.push( surface.model.getDocument().prepareRemoval(
new es.Range( insertAt + data.length, insertAt + removeLength + data.length )
);
surface.model.transact( tx );
) );
}
surface.model.transact( txs );
surface.model.select( selection, true );
};

View file

@ -613,13 +613,15 @@ es.SurfaceView.prototype.handleInsert = function() {
}, 10 );
};
es.SurfaceView.prototype.handleDelete = function( backspace, isPartial ) {
es.SurfaceView.prototype.handleDelete = function( backspace, notExecute ) {
var selection = this.currentSelection.clone(),
sourceOffset,
targetOffset,
sourceSplitableNode,
targetSplitableNode,
tx;
tx,
txs = [];
if ( selection.from === selection.to ) {
if ( backspace ) {
sourceOffset = selection.to;
@ -649,15 +651,13 @@ es.SurfaceView.prototype.handleDelete = function( backspace, isPartial ) {
if ( sourceNode === targetNode ||
( typeof sourceSplitableNode !== 'undefined' &&
sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) {
tx = this.model.getDocument().prepareRemoval(
txs.push( this.model.getDocument().prepareRemoval(
new es.Range( targetOffset, sourceOffset )
);
this.model.transact( tx, isPartial );
) );
} else {
tx = this.model.getDocument().prepareInsertion(
txs.push( this.model.getDocument().prepareInsertion(
targetOffset, sourceNode.model.getContentData()
);
this.model.transact( tx, isPartial );
) );
var nodeToDelete = sourceNode;
es.DocumentNode.traverseUpstream( nodeToDelete, function( node ) {
@ -671,23 +671,31 @@ es.SurfaceView.prototype.handleDelete = function( backspace, isPartial ) {
var range = new es.Range();
range.from = this.documentView.getOffsetFromNode( nodeToDelete, false );
range.to = range.from + nodeToDelete.getElementLength();
tx = this.model.getDocument().prepareRemoval( range );
this.model.transact( tx, isPartial );
txs.push( this.model.getDocument().prepareRemoval( range ) );
}
if ( notExecute ) {
return txs;
} else {
this.model.transact( txs );
}
} else {
// selection removal
tx = this.model.getDocument().prepareRemoval( selection );
this.model.transact( tx, isPartial );
selection.from = selection.to = selection.start;
this.model.select( selection );
if ( notExecute ) {
return [tx];
} else {
this.model.transact( tx );
}
}
};
es.SurfaceView.prototype.handleEnter = function() {
var selection = this.currentSelection.clone(),
tx;
txs = [];
if ( selection.from !== selection.to ) {
this.handleDelete( false, true );
txs.concat( this.handleDelete( false, true ) );
}
var node = this.documentView.getNodeFromOffset( selection.to, false ),
nodeOffset = this.documentView.getOffsetFromNode( node, false );
@ -696,11 +704,11 @@ es.SurfaceView.prototype.handleEnter = function() {
nodeOffset + node.getContentLength() + 1 === selection.to &&
node === es.DocumentViewNode.getSplitableNode( node )
) {
tx = this.documentView.model.prepareInsertion(
txs.push( this.documentView.model.prepareInsertion(
nodeOffset + node.getElementLength(),
[ { 'type': 'paragraph' }, { 'type': '/paragraph' } ]
);
this.model.transact( tx );
) );
this.model.transact( txs );
selection.from = selection.to = nodeOffset + node.getElementLength() + 1;
} else {
var stack = [],
@ -726,8 +734,8 @@ es.SurfaceView.prototype.handleEnter = function() {
splitable = es.DocumentView.splitRules[ elementType ].self;
return true;
} );
tx = this.documentView.model.prepareInsertion( selection.to, stack );
this.model.transact( tx );
txs.push( this.documentView.model.prepareInsertion( selection.to, stack ) );
this.model.transact( txs );
selection.from = selection.to =
this.model.getDocument().getRelativeContentOffset( selection.to, 1 );
}
@ -756,17 +764,15 @@ es.SurfaceView.prototype.insertFromInput = function() {
this.$input.val( '' );
// Prepare and process a transaction
var tx;
var txs = [];
if ( selection.from != selection.to ) {
tx = this.model.getDocument().prepareRemoval( selection );
this.model.transact( tx, true );
selection.from = selection.to =
Math.min( selection.from, selection.to );
txs.push( this.model.getDocument().prepareRemoval( selection ) );
selection.from = selection.to = Math.min( selection.from, selection.to );
}
var data = val.split('');
es.DocumentModel.addAnnotationsToData( data, this.getInsertionAnnotations() );
tx = this.model.getDocument().prepareInsertion( selection.from, data );
this.model.transact( tx );
txs.push( this.model.getDocument().prepareInsertion( selection.from, data ) );
this.model.transact( txs );
// Move the selection
selection.from += val.length;