Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/172249

Change subject: Make caption tool available when caption selected
......................................................................

Make caption tool available when caption selected

Currently is only available on table selection. Add in node
traversal logic to check if linear selection is in a caption
so captions can be removed when the cursor is inside them.

Change-Id: Ib1d8ad574886ce83f1fee782367b2697ab696d2d
---
M src/ui/actions/ve.ui.TableAction.js
M src/ui/tools/ve.ui.TableTool.js
M src/ui/ve.ui.CommandRegistry.js
3 files changed, 43 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/49/172249/1

diff --git a/src/ui/actions/ve.ui.TableAction.js 
b/src/ui/actions/ve.ui.TableAction.js
index 56574f0..909b4fa 100644
--- a/src/ui/actions/ve.ui.TableAction.js
+++ b/src/ui/actions/ve.ui.TableAction.js
@@ -229,19 +229,42 @@
  * Toggle the existence of a caption node on the table
  */
 ve.ui.TableAction.prototype.caption = function () {
-       var fragment, captionNode,
+       var fragment, captionNode, nodes, node, tableFragment,
                surfaceModel = this.surface.getModel(),
                selection = surfaceModel.getSelection();
 
-       if ( !( selection instanceof ve.dm.TableSelection ) ) {
+       if ( selection instanceof ve.dm.TableSelection ) {
+               captionNode = selection.getTableNode().getCaptionNode();
+       } else if ( selection instanceof ve.dm.LinearSelection ) {
+               nodes = surfaceModel.getFragment().getSelectedLeafNodes();
+
+               node = nodes[0];
+               while ( node ) {
+                       if ( node instanceof ve.dm.TableCaptionNode ) {
+                               captionNode = node;
+                               break;
+                       }
+                       node = node.parent;
+               }
+               if ( !captionNode ) {
+                       return;
+               }
+               tableFragment = surfaceModel.getFragment( new 
ve.dm.TableSelection(
+                       surfaceModel.getDocument(),
+                       captionNode.parent.getOuterRange(),
+                       0, 0, 0, 0,
+                       true
+               ) );
+       } else {
                return;
        }
-
-       captionNode = selection.getTableNode().getCaptionNode();
 
        if ( captionNode ) {
                fragment = surfaceModel.getLinearFragment( 
captionNode.getOuterRange(), true );
                fragment.removeContent();
+               if ( tableFragment ) {
+                       tableFragment.select();
+               }
        } else {
                fragment = surfaceModel.getLinearFragment( new ve.Range( 
selection.tableRange.start + 1 ), true );
 
diff --git a/src/ui/tools/ve.ui.TableTool.js b/src/ui/tools/ve.ui.TableTool.js
index 4d71f9b..daeb97a 100644
--- a/src/ui/tools/ve.ui.TableTool.js
+++ b/src/ui/tools/ve.ui.TableTool.js
@@ -157,10 +157,23 @@
        // Parent method
        ve.ui.TableCaptionTool.super.prototype.onUpdateState.apply( this, 
arguments );
 
-       var selection = fragment.getSelection();
+       var i, len, nodes, hasCaptionNode,
+               selection = fragment.getSelection();
 
        if ( selection instanceof ve.dm.TableSelection ) {
-               this.setActive( !!selection.getTableNode().getCaptionNode() );
+               hasCaptionNode = !!selection.getTableNode().getCaptionNode();
+       } else {
+               nodes = fragment.getSelectedLeafNodes();
+               hasCaptionNode = !!nodes.length;
+
+               for ( i = 0, len = nodes.length; i < len; i++ ) {
+                       if ( !nodes[i].hasMatchingAncestor( 'tableCaption' ) ) {
+                               hasCaptionNode = false;
+                               break;
+                       }
+               }
+               this.setDisabled( !hasCaptionNode );
        }
+       this.setActive( hasCaptionNode );
 };
 ve.ui.toolFactory.register( ve.ui.TableCaptionTool );
diff --git a/src/ui/ve.ui.CommandRegistry.js b/src/ui/ve.ui.CommandRegistry.js
index 78f5ddc..f59f90b 100644
--- a/src/ui/ve.ui.CommandRegistry.js
+++ b/src/ui/ve.ui.CommandRegistry.js
@@ -299,5 +299,5 @@
        new ve.ui.Command( 'mergeCells', 'table', 'mergeCells', { 
supportedSelections: ['table'] } )
 );
 ve.ui.commandRegistry.register(
-       new ve.ui.Command( 'tableCaption', 'table', 'caption', { 
supportedSelections: ['table'] } )
+       new ve.ui.Command( 'tableCaption', 'table', 'caption', { 
supportedSelections: ['linear', 'table'] } )
 );

-- 
To view, visit https://gerrit.wikimedia.org/r/172249
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib1d8ad574886ce83f1fee782367b2697ab696d2d
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to