Add support for slugs in ve.ce.Document.prototype.getRelativeOffset

Also removed ve.ce.Surface.prototype.adjustCursor because it is not being used anymore

Change-Id: Ib7dd8f4f6eaa688995d33247600875a45fc5a602
This commit is contained in:
Inez Korczyński 2013-03-25 16:43:36 -07:00
parent e3fd32923f
commit 2108030cfd
2 changed files with 19 additions and 59 deletions

View file

@ -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;
}
}
};

View file

@ -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.
*