Inez has uploaded a new change for review.

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


Change subject: Small cleanup in CE
......................................................................

Small cleanup in CE

Better comments for:
* ve.ce.Document.getRelativeOffset,
* ve.ce.Document.getSiblingWordBoundary.
Convert ve.ce.Surface.getSelectionRect to a static method.
Moved getNodeAndOffset from ve.ce.Surface to ve.ce.Document.

Change-Id: Ic00221fa463205d04c9b52150c0dd15904493b1e
---
M modules/ve/ce/ve.ce.Document.js
M modules/ve/ce/ve.ce.Surface.js
M modules/ve/ui/ve.ui.Context.js
3 files changed, 92 insertions(+), 88 deletions(-)


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

diff --git a/modules/ve/ce/ve.ce.Document.js b/modules/ve/ce/ve.ce.Document.js
index 27e2767..394a6c4 100644
--- a/modules/ve/ce/ve.ce.Document.js
+++ b/modules/ve/ce/ve.ce.Document.js
@@ -81,6 +81,8 @@
 
 /**
  * Get the nearest word boundary.
+ * This method is in CE instead of DM because its behaviour depends on the 
browser (IE/non-IE) and
+ * that information is closer to view layer. (CE)
  *
  * @method
  * @param {number} offset Offset to start from
@@ -128,6 +130,8 @@
 
 /**
  * Get the relative word or character boundary.
+ * This method is in CE instead of DM because it uses information about slugs 
about which model
+ * does not know at all.
  *
  * @method
  * @param {number} offset Offset to start from
@@ -158,4 +162,73 @@
                        return relativeContentOffset;
                }
        }
+};
+
+/**
+ * Get a DOM node and DOM element offset for a document offset.
+ *
+ * The results of this function are meant to be used with rangy.
+ *
+ * @method
+ * @param {number} offset Linear model offset
+ * @returns {Object} Object containing a node and offset property where node 
is an HTML element and
+ * offset is the position within the element
+ * @throws {Error} Offset could not be translated to a DOM element and offset
+ */
+ve.ce.Document.prototype.getNodeAndOffset = function ( offset ) {
+       var node, startOffset, current, stack, item, $item, length,
+               $slug = this.getSlugAtOffset( offset );
+       if ( $slug ) {
+               return { node: $slug[0].childNodes[0], offset: 0 };
+       }
+       node = this.getNodeFromOffset( offset );
+       startOffset = this.getDocumentNode().getOffsetFromNode( node ) + ( ( 
node.isWrapped() ) ? 1 : 0 );
+       current = [node.$.contents(), 0];
+       stack = [current];
+       while ( stack.length > 0 ) {
+               if ( current[1] >= current[0].length ) {
+                       stack.pop();
+                       current = stack[ stack.length - 1 ];
+                       continue;
+               }
+               item = current[0][current[1]];
+               if ( item.nodeType === Node.TEXT_NODE ) {
+                       length = item.textContent.length;
+                       if ( offset >= startOffset && offset <= startOffset + 
length ) {
+                               return {
+                                       node: item,
+                                       offset: offset - startOffset
+                               };
+                       } else {
+                               startOffset += length;
+                       }
+               } else if ( item.nodeType === Node.ELEMENT_NODE ) {
+                       $item = current[0].eq( current[1] );
+                       if ( $item.hasClass('ve-ce-slug') ) {
+                               if ( offset === startOffset ) {
+                                       return {
+                                               node: $item[0],
+                                               offset: 1
+                                       };
+                               }
+                       } else if ( $item.is( '.ve-ce-branchNode, 
.ve-ce-leafNode' ) ) {
+                               length = $item.data( 'node' 
).model.getOuterLength();
+                               if ( offset >= startOffset && offset < 
startOffset + length ) {
+                                       stack.push( [$item.contents(), 0] );
+                                       current[1]++;
+                                       current = stack[stack.length-1];
+                                       continue;
+                               } else {
+                                       startOffset += length;
+                               }
+                       } else {
+                               stack.push( [$item.contents(), 0] );
+                               current[1]++;
+                               current = stack[stack.length-1];
+                               continue;
+                       }
+               }
+               current[1]++;
+       }
+       throw new Error( 'Offset could not be translated to a DOM element and 
offset: ' + offset );
 };
\ No newline at end of file
diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 262d7ae..0b4f788 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -118,6 +118,21 @@
        'g'
 );
 
+/**
+ * Get the coordinates of the selection anchor.
+ *
+ * @method
+ * @static
+ */
+ve.ce.Surface.static.getSelectionRect = function () {
+       var rangySel = rangy.getSelection();
+       return {
+               start: rangySel.getStartDocumentPos(),
+               end: rangySel.getEndDocumentPos()
+       };
+};
+
+
 /* Methods */
 
 /*! Initialization */
@@ -1110,14 +1125,14 @@
        );
 
        if ( !range.isCollapsed() ) {
-               start = this.getNodeAndOffset( range.start );
-               end = this.getNodeAndOffset( range.end );
+               start = this.documentView.getNodeAndOffset( range.start );
+               end = this.documentView.getNodeAndOffset( range.end );
                rangyRange.setStart( start.node, start.offset );
                rangyRange.setEnd( end.node, end.offset );
                rangySel.removeAllRanges();
                rangySel.addRange( rangyRange, range.start !== range.from );
        } else {
-               start = this.getNodeAndOffset( range.start );
+               start = this.documentView.getNodeAndOffset( range.start );
                rangyRange.setStart( start.node, start.offset );
                rangySel.setSingleRange( rangyRange );
        }
@@ -1192,77 +1207,6 @@
 };
 
 /**
- * Get a DOM node and DOM element offset for a document offset.
- *
- * The results of this function are meant to be used with rangy.
- *
- * @method
- * @param {number} offset Linear model offset
- * @returns {Object} Object containing a node and offset property where node 
is an HTML element and
- * offset is the position within the element
- * @throws {Error} Offset could not be translated to a DOM element and offset
- */
-ve.ce.Surface.prototype.getNodeAndOffset = function ( offset ) {
-       var node, startOffset, current, stack, item, $item, length,
-               slug = this.documentView.getSlugAtOffset( offset );
-       if ( slug ) {
-               return { node: slug[0].childNodes[0], offset: 0 };
-       }
-       node = this.documentView.getNodeFromOffset( offset );
-       startOffset = this.documentView.getDocumentNode().getOffsetFromNode( 
node ) +
-               ( ( node.isWrapped() ) ? 1 : 0 );
-       current = [node.$.contents(), 0];
-       stack = [current];
-
-       while ( stack.length > 0 ) {
-               if ( current[1] >= current[0].length ) {
-                       stack.pop();
-                       current = stack[ stack.length - 1 ];
-                       continue;
-               }
-               item = current[0][current[1]];
-               if ( item.nodeType === Node.TEXT_NODE ) {
-                       length = item.textContent.length;
-                       if ( offset >= startOffset && offset <= startOffset + 
length ) {
-                               return {
-                                       node: item,
-                                       offset: offset - startOffset
-                               };
-                       } else {
-                               startOffset += length;
-                       }
-               } else if ( item.nodeType === Node.ELEMENT_NODE ) {
-                       $item = current[0].eq( current[1] );
-                       if ( $item.hasClass('ve-ce-slug') ) {
-                               if ( offset === startOffset ) {
-                                       return {
-                                               node: $item[0],
-                                               offset: 1
-                                       };
-                               }
-                       } else if ( $item.is( '.ve-ce-branchNode, 
.ve-ce-leafNode' ) ) {
-                               length = $item.data( 'node' 
).model.getOuterLength();
-                               if ( offset >= startOffset && offset < 
startOffset + length ) {
-                                       stack.push( [$item.contents(), 0] );
-                                       current[1]++;
-                                       current = stack[stack.length-1];
-                                       continue;
-                               } else {
-                                       startOffset += length;
-                               }
-                       } else {
-                               stack.push( [$item.contents(), 0] );
-                               current[1]++;
-                               current = stack[stack.length-1];
-                               continue;
-                       }
-               }
-               current[1]++;
-       }
-       throw new Error( 'Offset could not be translated to a DOM element and 
offset: ' + offset );
-};
-
-/**
  * Check if keyboard shortcut modifier key is pressed.
  *
  * @method
@@ -1322,19 +1266,6 @@
 };
 
 /*! Getters */
-
-/**
- * Get the coordinates of the selection anchor.
- *
- * @method
- */
-ve.ce.Surface.prototype.getSelectionRect = function () {
-       var rangySel = rangy.getSelection();
-       return {
-               start: rangySel.getStartDocumentPos(),
-               end: rangySel.getEndDocumentPos()
-       };
-};
 
 /**
  * Get the surface model.
diff --git a/modules/ve/ui/ve.ui.Context.js b/modules/ve/ui/ve.ui.Context.js
index 6a03782..1da6ccd 100644
--- a/modules/ve/ui/ve.ui.Context.js
+++ b/modules/ve/ui/ve.ui.Context.js
@@ -228,7 +228,7 @@
                inspector = this.inspectors.getCurrent();
 
                // Get cursor position
-               position = this.surface.getView().getSelectionRect().end;
+               position = ve.ce.Surface.static.getSelectionRect().end;
                if ( position ) {
                        // Get additional dimensions
                        $container = inspector ? this.inspectors.$ : this.$menu;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic00221fa463205d04c9b52150c0dd15904493b1e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Inez <[email protected]>

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

Reply via email to