Alex Monk has uploaded a new change for review.

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

Change subject: [WIP] Fix pulling of annotations when typing at the beginning 
of the document
......................................................................

[WIP] Fix pulling of annotations when typing at the beginning of the document

By allowing callers to disable part of
ve.dm.ElementLinearData.prototype.getRelativeOffset's initial direction
behaviour, which at least fixes the issue...
Specifically, this parameter controls whether or not getRelativeOffset
can force positive direction when the offset is 0 or less (even when we
specifically want negative direction), or force negative direction when the
offset is at/over the length of the data (even when we specifically want
positive direction - this doesn't change the bug, just makes the change more
logically consistent).
We may want to just get rid of the behaviour of forcing direction if the offset
is out of certain bounds completely, but that sounds like it might break other
things in obscure ways. See also I3090c258
The new parameter has to be sent through a few functions, and isn't documented
(yet... if we decide we actually want this change...)

Bug: 68597
Change-Id: I84870cc70a24c3639cee41769d2f826a26a19eba
---
M modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
M modules/ve/dm/ve.dm.Surface.js
2 files changed, 13 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/78/150378/1

diff --git a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js 
b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
index de32774..9e6c8cd 100644
--- a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
+++ b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
@@ -507,7 +507,7 @@
  * @returns {number} Relative valid offset or -1 if there are no valid offsets 
in data
  * @throws {Error} offset was inside a handlesOwnChildren node
  */
-ve.dm.ElementLinearData.prototype.getRelativeOffset = function ( offset, 
distance, callback ) {
+ve.dm.ElementLinearData.prototype.getRelativeOffset = function ( offset, 
distance, callback, bounce ) {
        var i, direction,
                dataOffset, isOpen,
                args = Array.prototype.slice.call( arguments, 3 ),
@@ -524,10 +524,13 @@
                        distance = 1;
                }
        }
+       if ( bounce === undefined ) {
+               bounce = true;
+       }
        // Initial values
        direction = (
-               offset <= 0 ? 1 : (
-                       offset >= this.getLength() ? -1 : (
+               offset <= 0 && bounce ? 1 : (
+                       offset >= this.getLength() && bounce ? -1 : (
                                distance > 0 ? 1 : -1
                        )
                )
@@ -601,8 +604,8 @@
  * @param {number} distance Number of content offsets to move
  * @returns {number} Relative content offset or -1 if there are no valid 
offsets in data
  */
-ve.dm.ElementLinearData.prototype.getRelativeContentOffset = function ( 
offset, distance ) {
-       return this.getRelativeOffset( offset, distance, 
this.constructor.prototype.isContentOffset );
+ve.dm.ElementLinearData.prototype.getRelativeContentOffset = function ( 
offset, distance, bounce ) {
+       return this.getRelativeOffset( offset, distance, 
this.constructor.prototype.isContentOffset, bounce );
 };
 
 /**
@@ -619,16 +622,16 @@
  * @param {number} [direction] Direction to prefer matching offset in, -1 for 
left and 1 for right
  * @returns {number} Nearest content offset or -1 if there are no valid 
offsets in data
  */
-ve.dm.ElementLinearData.prototype.getNearestContentOffset = function ( offset, 
direction ) {
+ve.dm.ElementLinearData.prototype.getNearestContentOffset = function ( offset, 
direction, bounce ) {
        if ( this.isContentOffset( offset ) ) {
                return offset;
        }
        if ( direction === undefined ) {
-               var left = this.getRelativeContentOffset( offset, -1 ),
-                       right = this.getRelativeContentOffset( offset, 1 );
+               var left = this.getRelativeContentOffset( offset, -1, bounce ),
+                       right = this.getRelativeContentOffset( offset, 1, 
bounce );
                return offset - left < right - offset ? left : right;
        } else {
-               return this.getRelativeContentOffset( offset, direction > 0 ? 1 
: -1 );
+               return this.getRelativeContentOffset( offset, direction > 0 ? 1 
: -1, bounce );
        }
 };
 
diff --git a/modules/ve/dm/ve.dm.Surface.js b/modules/ve/dm/ve.dm.Surface.js
index 0f979d8..d91da68 100644
--- a/modules/ve/dm/ve.dm.Surface.js
+++ b/modules/ve/dm/ve.dm.Surface.js
@@ -564,7 +564,7 @@
                // Figure out which annotations to use for insertions
                if ( this.selection.isCollapsed() ) {
                        // Get annotations from the left of the cursor
-                       left = dataModelData.getNearestContentOffset( Math.max( 
0, this.selection.start - 1 ), -1 );
+                       left = dataModelData.getNearestContentOffset( Math.max( 
0, this.selection.start - 1 ), -1, false );
                        right = dataModelData.getNearestContentOffset( 
Math.max( 0, this.selection.start ) );
                } else {
                        // Get annotations from the first character of the 
selection

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I84870cc70a24c3639cee41769d2f826a26a19eba
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Alex Monk <kren...@wikimedia.org>

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

Reply via email to