Merge "Base for inspectors & context menu to operate on 0 length selection."

This commit is contained in:
Trevor Parscal 2012-11-02 21:38:14 +00:00 committed by Gerrit Code Review
commit d631bac3db
4 changed files with 36 additions and 44 deletions

View file

@ -497,7 +497,7 @@ ve.ce.Surface.prototype.onKeyDown = function ( e ) {
// K
case 75:
if ( ve.ce.Surface.isShortcutKey( e ) ) {
if ( this.model.getSelection() && this.model.getSelection().getLength() ) {
if ( this.model.getSelection() ) {
e.preventDefault();
this.surface.getContext().openInspector( 'link' );
}

View file

@ -586,7 +586,6 @@ ve.dm.Document.prototype.getAnnotatedRangeFromOffset = function ( offset, annota
ve.dm.Document.prototype.getAnnotatedRangeFromSelection = function ( range, annotation ) {
var start = range.start,
end = range.end;
while ( start > 0 ) {
start--;
if ( this.offsetContainsAnnotation( start, annotation ) === false ) {
@ -616,14 +615,10 @@ ve.dm.Document.prototype.getAnnotationsFromRange = function ( range, all ) {
left,
right;
range.normalize();
// Shortcut for zero-length ranges
if ( range.getLength() === 0 ) {
return new ve.AnnotationSet();
}
// There's at least one character, get its annotations
// Look at left side of range for annotations
left = this.getAnnotationsFromOffset( range.start );
// Shortcut for single character ranges
if ( range.getLength() === 1 ) {
// Shortcut for single character and zero-length ranges
if ( range.getLength() === 0 || range.getLength() === 1 ) {
return left;
}
// Iterator over the range, looking for annotations, starting at the 2nd character

View file

@ -139,7 +139,6 @@ ve.ui.LinkInspector.prototype.prepareOpen = function () {
if ( annotation !== null ) {
annotatedRange = doc.getAnnotatedRangeFromSelection( newSelection, annotation );
// Adjust selection if it does not contain the annotated range
if ( selection.start > annotatedRange.start ||
selection.end < annotatedRange.end

View file

@ -81,54 +81,52 @@ ve.ui.Context.prototype.onChange = ve.debounce( function ( transaction, selectio
ve.ui.Context.prototype.update = function () {
var doc = this.surfaceModel.getDocument(),
sel = this.surfaceModel.getSelection(),
annotations = null,
annotations = doc.getAnnotationsFromRange( sel ).get(),
inspectors = [],
name,
i;
if ( doc.getText( sel ).length > 0 ) {
// Clearing out the toolbar & menu to rebuild.
// TODO: Consider more efficient implementation.
this.$menu.empty();
// Get annotations.
annotations = doc.getAnnotationsFromRange( sel ).get();
if ( annotations.length > 0 ) {
// Look for inspectors that match annotations.
for ( i = 0; i < annotations.length; i++ ) {
name = annotations[i].name.split('/')[0];
name = annotations[i].name.split( '/' )[0];
// Add inspector on demand.
if ( this.initInspector( name ) ) {
inspectors.push( name );
}
}
if ( inspectors.length > 0 ) {
// Toolbar
this.$toolbar = $( '<div class="ve-ui-context-toolbar"></div>' );
// Create inspector toolbar
this.toolbarView = new ve.ui.Toolbar(
this.$toolbar,
this.surface,
[{ 'name': 'inspectors', 'items' : inspectors }]
);
// Note: Menu attaches the provided $tool element to the container.
this.menuView = new ve.ui.Menu(
[ { 'name': 'tools', '$': this.$toolbar } ], // Tools
null, // Callback
this.$menu, // Container
this.$inner // Parent
);
this.set();
} else if ( !this.inspector ) {
this.unset();
}
} else {
}
// Build inspector menu.
if ( inspectors.length > 0 && !this.inspector ) {
this.$menu.empty();
// Toolbar
this.$toolbar = $( '<div class="ve-ui-context-toolbar"></div>' );
// Create inspector toolbar
this.toolbarView = new ve.ui.Toolbar(
this.$toolbar,
this.surface,
[{ 'name': 'inspectors', 'items' : inspectors }]
);
// Note: Menu attaches the provided $tool element to the container.
this.menuView = new ve.ui.Menu(
[ { 'name': 'tools', '$': this.$toolbar } ], // Tools
null, // Callback
this.$menu, // Container
this.$inner // Parent
);
this.set();
} else if ( this.selection !== sel ) {
// Cache current selection
this.selection = sel;
this.unset();
this.update();
}
};
ve.ui.Context.prototype.unset = function () {
if ( this.inspector ) {
this.closeInspector( false );
this.obscure( this.$inspectors );
this.$overlay.hide();
}
if ( this.menuView ) {
@ -284,6 +282,8 @@ ve.ui.Context.prototype.openInspector = function ( name ) {
this.resizeFrame( this.inspectors[name] );
// Save name of inspector open.
this.inspector = name;
// Cache selection, in the case of manually opened inspector.
this.selection = this.surfaceModel.getSelection();
// Set inspector
this.set();
};
@ -300,11 +300,9 @@ ve.ui.Context.prototype.closeInspector = function ( accept ) {
// Currently sizes to dimensions of specified inspector.
ve.ui.Context.prototype.resizeFrame = function ( inspector ) {
var width = inspector.$.outerWidth(),
height = inspector.$.outerHeight();
this.frameView.$frame.css( {
'width': width,
'height': height
'width': inspector.$.outerWidth(),
'height': inspector.$.outerHeight()
} );
};