jenkins-bot has submitted this change and it was merged.

Change subject: Refactor out finding of CE focusedNodes
......................................................................


Refactor out finding of CE focusedNodes

getFocusedNode now takes an arbitrary range.

Change-Id: Ie59239ec5fa3493dfc8f6a6b200a9a5405a96c66
---
M src/ce/ve.ce.Surface.js
1 file changed, 50 insertions(+), 35 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 897284f..887a978 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -1515,9 +1515,7 @@
  * @param {ve.Range|null} selection
  */
 ve.ce.Surface.prototype.onModelSelect = function ( selection ) {
-       var start, end, nativeRange,
-               next = null,
-               previous = this.focusedNode;
+       var nativeRange, focusedNode;
 
        this.contentBranchNodeChanged = false;
 
@@ -1525,31 +1523,17 @@
                return;
        }
 
-       // Detect when only a single inline element is selected
-       if ( !selection.isCollapsed() ) {
-               start = this.documentView.getDocumentNode().getNodeFromOffset( 
selection.start + 1 );
-               if ( start.isFocusable() ) {
-                       end = 
this.documentView.getDocumentNode().getNodeFromOffset( selection.end - 1 );
-                       if ( start === end ) {
-                               next = start;
-                       }
-               }
-       } else {
-               // Check we haven't been programmatically placed inside a 
focusable node with a collapsed selection
-               start = this.documentView.getDocumentNode().getNodeFromOffset( 
selection.start );
-               if ( start.isFocusable() ) {
-                       next = start;
-               }
-       }
+       focusedNode = this.findFocusedNode( selection );
+
        // If focus has changed, update nodes and this.focusedNode
-       if ( previous !== next ) {
-               if ( previous ) {
-                       previous.setFocused( false );
+       if ( focusedNode !== this.focusedNode ) {
+               if ( this.focusedNode ) {
+                       this.focusedNode.setFocused( false );
                        this.focusedNode = null;
                }
-               if ( next ) {
-                       next.setFocused( true );
-                       this.focusedNode = next;
+               if ( focusedNode ) {
+                       focusedNode.setFocused( true );
+                       this.focusedNode = focusedNode;
 
                        // As FF won't fire a copy event with nothing selected, 
make
                        // a dummy selection of one space in the pasteTarget.
@@ -1577,6 +1561,47 @@
 
        // Update the selection state in the SurfaceObserver
        this.surfaceObserver.pollOnceNoEmit();
+};
+
+/**
+ * Get the focused node (optionally at a specified range), or null if one is 
not present
+ *
+ * @param {ve.Range} [range] Optional range to check for focused node, 
defaults to current selection
+ * @return {ve.ce.Node|null} Focused node
+ */
+ve.ce.Surface.prototype.getFocusedNode = function ( range ) {
+       if ( !range || range.equalsSelection( this.getModel().getSelection() ) 
) {
+               return this.focusedNode;
+       }
+       return this.findFocusedNode( range );
+};
+
+/**
+ * Find the focusedNode at a specified range
+ *
+ * @param {ve.Range} range Range to search at for a focusable node
+ * @return {ve.ce.Node|null} Focused node
+ */
+ve.ce.Surface.prototype.findFocusedNode = function ( range ) {
+       var startNode, endNode,
+               documentNode = this.documentView.getDocumentNode();
+       // Detect when only a single focusable element is selected
+       if ( !range.isCollapsed() ) {
+               startNode = documentNode.getNodeFromOffset( range.start + 1 );
+               if ( startNode && startNode.isFocusable() ) {
+                       endNode = documentNode.getNodeFromOffset( range.end - 1 
);
+                       if ( startNode === endNode ) {
+                               return startNode;
+                       }
+               }
+       } else {
+               // Check if the range is inside a focusable node with a 
collapsed selection
+               startNode = documentNode.getNodeFromOffset( range.start );
+               if ( startNode && startNode.isFocusable() ) {
+                       return startNode;
+               }
+       }
+       return null;
 };
 
 /**
@@ -2572,16 +2597,6 @@
  */
 ve.ce.Surface.prototype.getDocument = function () {
        return this.documentView;
-};
-
-/**
- * Get the currently focused node.
- *
- * @method
- * @returns {ve.ce.Node|undefined} Focused node
- */
-ve.ce.Surface.prototype.getFocusedNode = function () {
-       return this.focusedNode;
 };
 
 /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie59239ec5fa3493dfc8f6a6b200a9a5405a96c66
Gerrit-PatchSet: 13
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to