Mooeypoo has uploaded a new change for review.

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


Change subject: Add a getDirectionFromRange Method to ve.ce.Document
......................................................................

Add a getDirectionFromRange Method to ve.ce.Document

getDirectionFromRange returns the direction property of the ce nodes under
that range. That method is mostly useful to recognize the overall block
direction of a selection or fragment.

The method is currently used in the following locations:
* ve.ui.Toolbar onContextChange - as a means to recognize the current context's
  block direction for the icon directionality.
* ve.ui.MWExtensionInspector - if the selection is text and not an existing node
  the input directionality adjusts to the context direction.

Bug: 57421
Change-Id: Ifc01b8e5dc0a2fe39d221e59e452c5cfad709a2d
---
M modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js
M modules/ve/ce/ve.ce.Document.js
M modules/ve/ui/ve.ui.Toolbar.js
3 files changed, 47 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/25/101125/1

diff --git a/modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js 
b/modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js
index 84a5ead..093001b 100644
--- a/modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js
+++ b/modules/ve-mw/ui/inspectors/ve.ui.MWExtensionInspector.js
@@ -59,6 +59,9 @@
  * @inheritdoc
  */
 ve.ui.MWExtensionInspector.prototype.setup = function ( data ) {
+       var dir,
+               fragment = this.surface.getModel().getFragment( null, true );
+
        // Parent method
        ve.ui.Inspector.prototype.setup.call( this, data );
 
@@ -66,12 +69,16 @@
        this.node = this.surface.getView().getFocusedNode();
        this.input.setValue( this.node ? this.node.getModel().getAttribute( 
'mw' ).body.extsrc : '' );
 
+       // By default, the direction of the input element should be the same
+       // as the direction of the content it applies to
        if ( this.node ) {
-               // Direction of the input textarea should correspond to the
-               // direction of the surrounding content of the node itself
-               // rather than the GUI direction:
-               this.input.setRTL( this.node.$element.css( 'direction' ) === 
'rtl' );
+               // The node is being edited
+               dir = this.node.$element.css( 'direction' );
+       } else {
+               // New insertion, base direction on the fragment range
+               dir = 
this.surface.getView().documentView.getDirectionFromRange( fragment.getRange() 
);
        }
+       this.input.setRTL( dir === 'rtl' );
 };
 
 /**
diff --git a/modules/ve/ce/ve.ce.Document.js b/modules/ve/ce/ve.ce.Document.js
index 5a374a6..d9b6ad1 100644
--- a/modules/ve/ce/ve.ce.Document.js
+++ b/modules/ve/ce/ve.ce.Document.js
@@ -268,3 +268,33 @@
                return new ve.Range( contentOrSlugOffset );
        }
 };
+
+
+/**
+ * Get the directionality of some range.
+ *
+ * @method
+ * @param {ve.Range} range Selection range
+ * @returns {String} 'rtl' or 'ltr' as response
+ */
+ve.ce.Document.prototype.getDirectionFromRange = function ( range ) {
+       var node, effectiveNodes, selectedNodes;
+
+       selectedNodes = this.selectNodes( range, 'covered' );
+       if ( selectedNodes.length > 1 ) {
+               // selection of multiple nodes
+               effectiveNodes = this.selectNodes( range, 'siblings' );
+
+               // Get the common parent's outer range
+               range = effectiveNodes[0].parentOuterRange;
+       } else {
+               // selection of a single node
+
+               // Get the node's outer range
+               range = selectedNodes[0].node.getOuterRange();
+       }
+
+       node = this.getNodeFromOffset( range.start );
+
+       return node.$element.css( 'direction' );
+};
diff --git a/modules/ve/ui/ve.ui.Toolbar.js b/modules/ve/ui/ve.ui.Toolbar.js
index b61d524..13bc0d0 100644
--- a/modules/ve/ui/ve.ui.Toolbar.js
+++ b/modules/ve/ui/ve.ui.Toolbar.js
@@ -172,27 +172,17 @@
                        nodes.push( leafNodes[i].node );
                }
        }
-       // Update context direction for button icons UI:
+       // Update context direction for button icons UI
 
-       // block direction (direction of the current node)
-       currentNodes.fragNodes = fragment.getCoveredNodes();
-       if ( currentNodes.fragNodes.length > 1 ) {
-               // selection of multiple nodes
-               currentNodes.dm.block = 
fragment.getSiblingNodes()[0].node.parent;
-       } else {
-               // selection of a single node
-               currentNodes.dm.block = currentNodes.fragNodes[0].node;
-       }
-       // get the direction of the block:
-       currentNodes.ce.block = doc.getNodeFromOffset( 
currentNodes.dm.block.getRange().start );
-       dirBlock = currentNodes.ce.block.$element.css( 'direction' );
-       // by default, inline and block are the same, unless there's an 
inline-specific direction
-       dirInline = dirBlock;
-       // 'inline' direction is set by language annotation:
+       // by default, inline and block directions are the same
+       dirInline = dirBlock = 
this.surface.getView().documentView.getDirectionFromRange( fragment.getRange() 
);
+
+       // 'inline' direction is different only if we are inside a language 
annotation
        fragmentAnnotation = fragment.getAnnotations();
        if ( fragmentAnnotation.hasAnnotationWithName( 'meta/language' ) ) {
                dirInline = fragmentAnnotation.getAnnotationsByName( 
'meta/language' ).get( 0 ).getAttribute( 'dir' );
        }
+
        if ( dirInline !== this.contextDirection.inline ) {
                // remove previous class:
                this.$element.removeClass( 've-ui-dir-inline-rtl 
ve-ui-dir-inline-ltr' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifc01b8e5dc0a2fe39d221e59e452c5cfad709a2d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

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

Reply via email to