diff --git a/modules/ve/ce/ve.ce.Document.js b/modules/ve/ce/ve.ce.Document.js index c7ea4db12f..27e276761a 100644 --- a/modules/ve/ce/ve.ce.Document.js +++ b/modules/ve/ce/ve.ce.Document.js @@ -95,7 +95,7 @@ ve.ce.Document.prototype.getSiblingWordBoundary = function ( offset, direction ) inc = direction > 0 ? 1 : -1, oneChar, prevType, nextType; if ( !data[i] || data[i].type !== undefined ) { - return this.model.getRelativeContentOffset( offset, direction ); + return this.getRelativeOffset( offset, direction, 'character' ); } else { config = $.browser.msie ? config.ie : config.default; config = direction > 0 ? config.right : config.left; @@ -136,10 +136,26 @@ ve.ce.Document.prototype.getSiblingWordBoundary = function ( offset, direction ) * @returns {number} Relative offset */ ve.ce.Document.prototype.getRelativeOffset = function ( offset, direction, unit ) { + var bias, relativeContentOffset, relativeStructuralOffset; if ( unit === 'word' ) { // word return this.getSiblingWordBoundary( offset, direction ); } else { // character - // TODO: add support for slugs - return this.model.getRelativeContentOffset( offset, direction ); + bias = direction > 0 ? 1 : -1; + relativeContentOffset = this.model.getRelativeContentOffset( offset, direction ); + relativeStructuralOffset = this.model.getRelativeStructuralOffset( offset + bias, direction, true ); + // Check if we've moved into a slug + if ( !!this.getSlugAtOffset( relativeStructuralOffset ) ) { + // Check if the relative content offset is in the opposite direction we are trying to go + if ( ( relativeContentOffset - offset < 0 ? -1 : 1 ) !== bias ) { + // There's nothing past the slug we are already in, stay in it + return relativeStructuralOffset; + } + // There's a slug neaby, go into it if it's closer + return direction > 0 ? + Math.min( relativeContentOffset, relativeStructuralOffset ) : + Math.max( relativeContentOffset, relativeStructuralOffset ); + } else { + return relativeContentOffset; + } } }; \ 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 b1c013d658..262d7aec4c 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -1091,62 +1091,6 @@ ve.ce.Surface.prototype.handleDelete = function ( e, backspace ) { this.surfaceObserver.start(); }; -/** - * Adjust the cursor position in a given distance. - * - * This method only affects the selection target, preserving selections that are not collapsed and - * the direction of the selection. - * - * @method - * @param {number} adjustment Distance to adjust the cursor, can be positive or negative - * @returns {boolean} Cursor was moved - */ -ve.ce.Surface.prototype.adjustCursor = function ( adjustment ) { - // Bypass for zero-adjustment - if ( !adjustment ) { - return false; - } - var adjustedTargetOffset, - bias = adjustment > 0 ? 1 : -1, - selection = this.model.getSelection(), - targetOffset = selection.to, - documentModel = this.model.getDocument(), - relativeContentOffset = documentModel.getRelativeContentOffset( targetOffset, adjustment ), - relativeStructuralOffset = documentModel.getRelativeStructuralOffset( - targetOffset + bias, adjustment, true - ); - // Check if we've moved into a slug - if ( this.hasSlugAtOffset( relativeStructuralOffset ) ) { - // Check if the relative content offset is in the opposite direction we are trying to go - if ( ( relativeContentOffset - targetOffset < 0 ? -1 : 1 ) !== bias ) { - // There's nothing past the slug we are already in, stay in it - adjustedTargetOffset = relativeStructuralOffset; - } else { - // There's a slug neaby, go into it if it's closer - adjustedTargetOffset = adjustment < 0 ? - Math.max( relativeContentOffset, relativeStructuralOffset ) : - Math.min( relativeContentOffset, relativeStructuralOffset ); - } - } - // Check if we've moved a different distance than we asked for - else if ( relativeContentOffset !== targetOffset + adjustment ) { - // We can't trust the browser, move programatically - adjustedTargetOffset = relativeContentOffset; - } - // If the target changed, update the model - if ( adjustedTargetOffset ) { - this.model.change( - null, - new ve.Range( - selection.isCollapsed() ? - adjustedTargetOffset : selection.from, adjustedTargetOffset - ) - ); - return true; - } - return false; -}; - /** * Show selection on a range. *