Esanders has uploaded a new change for review.

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

Change subject: Provide a method to get all the rectangles for a given selection
......................................................................

Provide a method to get all the rectangles for a given selection

Change-Id: I46d30ef788cfe93defbc793ca678db886994622e
---
M src/ce/ve.ce.FocusableNode.js
M src/ce/ve.ce.Surface.js
2 files changed, 47 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/91/160591/1

diff --git a/src/ce/ve.ce.FocusableNode.js b/src/ce/ve.ce.FocusableNode.js
index d4dd8b9..7457eea 100644
--- a/src/ce/ve.ce.FocusableNode.js
+++ b/src/ce/ve.ce.FocusableNode.js
@@ -501,6 +501,18 @@
 };
 
 /**
+ * Get list of rectangles outlining the shape of the node relative to the 
surface
+ *
+ * @return {Object[]} List of rectangle objects
+ */
+ve.ce.FocusableNode.prototype.getRects = function () {
+       if ( !this.highlighted ) {
+               this.calculateHighlights();
+       }
+       return this.rects;
+};
+
+/**
  * Get the bounding rectangle of the focusable node highight relative to the 
surface
  *
  * @return {Object|null} Top, left, bottom & right positions of the focusable 
node relative to the surface
diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 301b027..c697cfc 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -317,20 +317,21 @@
 };
 
 /**
- * Get the start and end rectangles of the selection range relative to the 
surface.
+ * Get the rectangles of the selection range relative to the surface.
  *
  * @method
  * @param {ve.Range} [range] Optional range to get the selection for, defaults 
to current selection
- * @returns {Object|null} Start and end selection rectangles
+ * @returns {Object[]|null} Selection rectangles
  */
-ve.ce.Surface.prototype.getSelectionStartAndEndRects = function ( range ) {
-       var startAndEndRects, nativeRange, surfaceRect, focusNodeRect, rtl, x, 
collapsedRect, focusedNode;
+ve.ce.Surface.prototype.getSelectionRects = function ( range ) {
+       var i, l, rects, nativeRange, surfaceRect, focusNodeRect, rtl, x, 
focusedNode,
+               relativeRects = [];
 
        range = range || this.getModel().getSelection();
        focusedNode = this.getFocusedNode( range );
 
        if ( focusedNode ) {
-               return focusedNode.getStartAndEndRects();
+               return focusedNode.getRects();
        }
 
        nativeRange = this.getNativeRange( range );
@@ -342,7 +343,7 @@
        // * in Firefox on page load when the address bar is still focused
        // * in empty paragraphs
        try {
-               startAndEndRects = ve.getStartAndEndRects( 
nativeRange.getClientRects() );
+               rects = nativeRange.getClientRects();
        } catch ( e ) {
                // When possible, pretend the cursor is the left/right border 
of the node
                // (depending on directionality) as a fallback.
@@ -356,31 +357,48 @@
                        }
                        rtl = this.getModel().getDocument().getDir() === 'rtl';
                        x = rtl ? focusNodeRect.right : focusNodeRect.left;
-                       collapsedRect = {
+                       rects = [ {
                                top: focusNodeRect.top,
                                bottom: focusNodeRect.bottom,
                                left: x,
                                right: x,
                                width: 0,
                                height: focusNodeRect.height
-                       };
-                       startAndEndRects = {
-                               start: collapsedRect,
-                               end: collapsedRect
-                       };
+                       } ];
                } else {
                        return null;
                }
        }
 
        surfaceRect = this.getSurface().getBoundingClientRect();
-       if ( !startAndEndRects || !surfaceRect ) {
+       if ( !rects || !surfaceRect ) {
                return null;
        }
-       return {
-               start: ve.translateRect( startAndEndRects.start, 
-surfaceRect.left, -surfaceRect.top ),
-               end: ve.translateRect( startAndEndRects.end, -surfaceRect.left, 
-surfaceRect.top )
-       };
+
+       for ( i = 0, l = rects.length; i < l; i++ ) {
+               relativeRects.push( ve.translateRect( rects[i], 
-surfaceRect.left, -surfaceRect.top ) );
+       }
+       return relativeRects;
+};
+
+/**
+ * Get the start and end rectangles of the selection range relative to the 
surface.
+ *
+ * @method
+ * @param {ve.Range} [range] Optional range to get the selection for, defaults 
to current selection
+ * @returns {Object|null} Start and end selection rectangles
+ */
+ve.ce.Surface.prototype.getSelectionStartAndEndRects = function ( range ) {
+       var focusedNode;
+
+       range = range || this.getModel().getSelection();
+       focusedNode = this.getFocusedNode( range );
+
+       if ( focusedNode ) {
+               return focusedNode.getStartAndEndRects();
+       }
+
+       return ve.getStartAndEndRects( this.getSelectionRects() );
 };
 
 /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I46d30ef788cfe93defbc793ca678db886994622e
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

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

Reply via email to