mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-29 00:30:44 +00:00
Optimization for toolbarView. Count data for all toolbars (currently two) once, and do it not more often than 50ms.
This commit is contained in:
parent
babf3eb9fb
commit
1f3604a959
|
@ -30,6 +30,7 @@ es.SurfaceView = function( $container, model ) {
|
|||
this.insertionAnnotations = [];
|
||||
this.updateSelectionTimeout = undefined;
|
||||
this.emitUpdateTimeout = undefined;
|
||||
this.emitCursorTimeout = undefined;
|
||||
|
||||
// Interaction states
|
||||
|
||||
|
@ -145,6 +146,42 @@ es.SurfaceView = function( $container, model ) {
|
|||
};
|
||||
|
||||
/* Methods */
|
||||
es.SurfaceView.prototype.emitCursor = function() {
|
||||
if ( this.emitCursorTimeout ) {
|
||||
clearTimeout( this.emitCursorTimeout );
|
||||
}
|
||||
var _this = this;
|
||||
this.emitCursorTimeout = setTimeout( function() {
|
||||
var annotations,
|
||||
nodes = [],
|
||||
model = _this.documentView.model;
|
||||
|
||||
if( _this.currentSelection.from === _this.currentSelection.to ) {
|
||||
var insertionAnnotations = _this.getInsertionAnnotations();
|
||||
annotations = {
|
||||
'full': insertionAnnotations,
|
||||
'partial': [],
|
||||
'all': insertionAnnotations
|
||||
};
|
||||
nodes.push( model.getNodeFromOffset( _this.currentSelection.from ) );
|
||||
} else {
|
||||
annotations = model.getAnnotationsFromRange( _this.currentSelection );
|
||||
var startNode = model.getNodeFromOffset( _this.currentSelection.start ),
|
||||
endNode = model.getNodeFromOffset( _this.currentSelection.end );
|
||||
if ( startNode === endNode ) {
|
||||
nodes.push( startNode );
|
||||
} else {
|
||||
model.traverseLeafNodes( function( node ) {
|
||||
nodes.push( node );
|
||||
if( node === endNode ) {
|
||||
return false;
|
||||
}
|
||||
}, startNode );
|
||||
}
|
||||
}
|
||||
_this.emit( 'cursor', annotations, nodes );
|
||||
}, 50 );
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.getInsertionAnnotations = function() {
|
||||
return this.insertionAnnotations;
|
||||
|
@ -152,7 +189,7 @@ es.SurfaceView.prototype.getInsertionAnnotations = function() {
|
|||
|
||||
es.SurfaceView.prototype.addInsertionAnnotation = function( annotation ) {
|
||||
this.insertionAnnotations.push( annotation );
|
||||
this.emit( 'cursor' );
|
||||
this.emitCursor();
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.loadInsertionAnnotations = function( annotation ) {
|
||||
|
@ -165,7 +202,7 @@ es.SurfaceView.prototype.loadInsertionAnnotations = function( annotation ) {
|
|||
i--;
|
||||
}
|
||||
}
|
||||
this.emit( 'cursor' );
|
||||
this.emitCursor();
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.removeInsertionAnnotation = function( annotation ) {
|
||||
|
@ -173,12 +210,12 @@ es.SurfaceView.prototype.removeInsertionAnnotation = function( annotation ) {
|
|||
if ( index !== -1 ) {
|
||||
this.insertionAnnotations.splice( index, 1 );
|
||||
}
|
||||
this.emit( 'cursor' );
|
||||
this.emitCursor();
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.clearInsertionAnnotations = function() {
|
||||
this.insertionAnnotations = [];
|
||||
this.emit( 'cursor' );
|
||||
this.emitCursor();
|
||||
};
|
||||
|
||||
es.SurfaceView.prototype.getModel = function() {
|
||||
|
|
|
@ -40,11 +40,11 @@ es.ToolbarView = function( $container, surfaceView, config ) {
|
|||
}
|
||||
}
|
||||
} );
|
||||
this.surfaceView.model.on( 'select', function() {
|
||||
_this.updateState();
|
||||
} );
|
||||
this.surfaceView.on( 'cursor', function() {
|
||||
_this.updateState();
|
||||
|
||||
this.surfaceView.on( 'cursor', function( annotations, nodes ) {
|
||||
for( var i = 0; i < _this.tools.length; i++ ) {
|
||||
_this.tools[i].updateState( annotations, nodes );
|
||||
}
|
||||
} );
|
||||
|
||||
this.config = config || [
|
||||
|
@ -56,40 +56,6 @@ es.ToolbarView = function( $container, surfaceView, config ) {
|
|||
this.setup();
|
||||
};
|
||||
|
||||
es.ToolbarView.prototype.updateState = function() {
|
||||
var selection = this.surfaceView.currentSelection,
|
||||
annotations,
|
||||
nodes = [];
|
||||
|
||||
if( selection.from === selection.to ) {
|
||||
var insertionAnnotations = this.surfaceView.getInsertionAnnotations();
|
||||
annotations = {
|
||||
'full': insertionAnnotations,
|
||||
'partial': [],
|
||||
'all': insertionAnnotations
|
||||
};
|
||||
nodes.push( this.surfaceView.documentView.model.getNodeFromOffset( selection.from ) );
|
||||
} else {
|
||||
annotations = this.surfaceView.documentView.model.getAnnotationsFromRange( selection );
|
||||
var startNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.start ),
|
||||
endNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.end );
|
||||
if ( startNode === endNode ) {
|
||||
nodes.push( startNode );
|
||||
} else {
|
||||
this.surfaceView.documentView.model.traverseLeafNodes( function( node ) {
|
||||
nodes.push( node );
|
||||
if( node === endNode ) {
|
||||
return false;
|
||||
}
|
||||
}, startNode );
|
||||
}
|
||||
}
|
||||
|
||||
for( var i = 0; i < this.tools.length; i++ ) {
|
||||
this.tools[i].updateState( annotations, nodes );
|
||||
}
|
||||
};
|
||||
|
||||
es.ToolbarView.prototype.setup = function() {
|
||||
for ( var i = 0; i < this.config.length; i++ ) {
|
||||
var $group = $( '<div>' )
|
||||
|
|
Loading…
Reference in a new issue