Make dm.Surface's 'select' event more useful

It was previously emitted before the selection was updated and with the
old selection as a parameter. Instead, emit it afterwards, and make sure
it's emitted even if the selection changes because it was translated
for a transaction.

Also correct its event documentation, which seems to have been copied
from a UI class somewhere.

Change-Id: I521eff0095959572587c0ecffd24dbf322e12d82
This commit is contained in:
Roan Kattouw 2013-10-08 10:41:14 +02:00
parent 904c5a48ad
commit ede989445e

View file

@ -47,7 +47,7 @@ OO.mixinClass( ve.dm.Surface, OO.EventEmitter );
/**
* @event select
* @param {ve.ui.MenuItemWidget} item Menu item
* @param {ve.Range} selection
*/
/**
@ -315,7 +315,7 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
}
var i, len, left, right, leftAnnotations, rightAnnotations, insertionAnnotations,
selectedNodes = {},
selectionChange = false,
oldSelection = this.selection,
contextChange = false,
dataModelData = this.documentModel.data;
@ -342,10 +342,6 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
}
if ( selection ) {
// Detect if selection range changed
if ( !this.selection || !this.selection.equals( selection ) ) {
selectionChange = true;
}
// Detect if selected nodes changed
selectedNodes.start = this.documentModel.getNodeFromOffset( selection.start );
if ( selection.getLength() ) {
@ -358,12 +354,14 @@ ve.dm.Surface.prototype.change = function ( transactions, selection ) {
contextChange = true;
}
this.selectedNodes = selectedNodes;
if ( selectionChange ) {
this.emit( 'select', this.selection.clone() );
}
this.selection = selection;
}
// Emit select event if selection range changed
if ( !oldSelection || !oldSelection.equals( this.selection ) ) {
this.emit( 'select', this.selection.clone() );
}
// Only emit a transact event if transactions were actually processed
if ( transactions ) {
this.emit( 'transact', transactions );