From 5d86bc437900f3050aece776ae3c7292b5da460c Mon Sep 17 00:00:00 2001 From: Rob Moen Date: Wed, 31 Oct 2012 15:17:20 -0700 Subject: [PATCH] Base for inspectors & context menu to operate on 0 length selection. * Allow inspector to open with 0 length selection. * Allow context menu to open with 0 length selection. * Fixed bug in doc.getAnnotationsFromRange on zero length selection: Method now returns annotations from start vs empty annotation set. Change-Id: I3937c5c2824c7396d0c3ee11c13ffecdbed6052a --- modules/ve/ce/ve.ce.Surface.js | 2 +- modules/ve/dm/ve.dm.Document.js | 11 +--- .../ve/ui/inspectors/ve.ui.LinkInspector.js | 1 - modules/ve/ui/ve.ui.Context.js | 66 +++++++++---------- 4 files changed, 36 insertions(+), 44 deletions(-) diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js index 5a39280572..328b02c683 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -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' ); } diff --git a/modules/ve/dm/ve.dm.Document.js b/modules/ve/dm/ve.dm.Document.js index 0205930aaa..ee8834f7ce 100644 --- a/modules/ve/dm/ve.dm.Document.js +++ b/modules/ve/dm/ve.dm.Document.js @@ -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 diff --git a/modules/ve/ui/inspectors/ve.ui.LinkInspector.js b/modules/ve/ui/inspectors/ve.ui.LinkInspector.js index 6aaf3d2b0e..f10843d3bf 100644 --- a/modules/ve/ui/inspectors/ve.ui.LinkInspector.js +++ b/modules/ve/ui/inspectors/ve.ui.LinkInspector.js @@ -137,7 +137,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 diff --git a/modules/ve/ui/ve.ui.Context.js b/modules/ve/ui/ve.ui.Context.js index 796b02dd18..70f2d955e8 100644 --- a/modules/ve/ui/ve.ui.Context.js +++ b/modules/ve/ui/ve.ui.Context.js @@ -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 = $( '
' ); - // 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 = $( '
' ); + // 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 ) { @@ -283,6 +281,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(); }; @@ -299,11 +299,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() } ); };