From 011069d17c8dfdde7360976d23a7a53d26bb3634 Mon Sep 17 00:00:00 2001 From: Christian Williams Date: Thu, 11 Oct 2012 15:51:45 -0700 Subject: [PATCH] Native cursor movement The cursor will move natively when it can, allowing for word jumping with alt/ctrl keys. Special conditions exist for slugs and node boundaries that result in programatic movement. Change-Id: Ife156bf94d8192ddd322c016ca8359855b17d7fc --- modules/ve/ce/ve.ce.Surface.js | 66 ++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js index 697bf629f7..9dcbd26146 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -405,21 +405,26 @@ ve.ce.Surface.prototype.onKeyDown = function ( e ) { break; // Left arrow case 37: - if ( !e.metaKey && !e.altKey && !e.shiftKey && this.model.getSelection().getLength() === 0 ) { - offset = this.model.getSelection().start; - relativeContentOffset = this.documentView.model.getRelativeContentOffset( offset, -1 ); - relativeStructuralOffset = this.documentView.model.getRelativeStructuralOffset( offset - 1, -1, true ); - relativeStructuralOffsetNode = this.documentView.documentNode.getNodeFromOffset( relativeStructuralOffset ); - hasSlug = this.documentView.getSlugAtOffset( relativeStructuralOffset ) || false; - if ( hasSlug ) { - if ( relativeContentOffset > offset ) { - newOffset = relativeStructuralOffset; - } else { - newOffset = Math.max( relativeContentOffset, relativeStructuralOffset ); - } + offset = this.model.getSelection().start; + relativeContentOffset = this.documentView.model.getRelativeContentOffset( offset, -1 ); + relativeStructuralOffset = this.documentView.model.getRelativeStructuralOffset( offset - 1, -1, true ); + relativeStructuralOffsetNode = this.documentView.documentNode.getNodeFromOffset( relativeStructuralOffset ); + hasSlug = this.documentView.getSlugAtOffset( relativeStructuralOffset ) || false; + + if ( hasSlug ) { + if ( relativeContentOffset > offset ) { + // If relativeContentOffset returns a greater number, there's nowhere to go toward the left. Go right. + newOffset = relativeStructuralOffset; } else { - newOffset = Math.min( offset, relativeContentOffset ); + // Move cursor to whichever is nearest to the original offset. + newOffset = Math.max( relativeContentOffset, relativeStructuralOffset ); } + } else if (relativeContentOffset != offset - 1) { + // The closest content offet is further away than just one offset. Don't trust the browser. Move programatically. + newOffset = relativeContentOffset; + } + + if ( newOffset ) { this.model.change( null, new ve.Range( newOffset ) @@ -429,26 +434,25 @@ ve.ce.Surface.prototype.onKeyDown = function ( e ) { break; // Right arrow case 39: - if ( - !e.metaKey && - !e.altKey && - !e.shiftKey && - this.model.getSelection().getLength() === 0 - ) { - offset = this.model.getSelection().start; - relativeContentOffset = this.documentView.model.getRelativeContentOffset( offset, 1 ); - relativeStructuralOffset = this.documentView.model.getRelativeStructuralOffset( offset + 1, 1, true ); - relativeStructuralOffsetNode = this.documentView.documentNode.getNodeFromOffset( relativeStructuralOffset ); - hasSlug = this.documentView.getSlugAtOffset( relativeStructuralOffset ) || false; - if ( hasSlug ) { - if ( relativeContentOffset < offset ) { - newOffset = relativeStructuralOffset; - } else { - newOffset = Math.min( relativeContentOffset, relativeStructuralOffset ); - } + offset = this.model.getSelection().start; + relativeContentOffset = this.documentView.model.getRelativeContentOffset( offset, 1 ); + relativeStructuralOffset = this.documentView.model.getRelativeStructuralOffset( offset + 1, 1, true ); + relativeStructuralOffsetNode = this.documentView.documentNode.getNodeFromOffset( relativeStructuralOffset ); + hasSlug = this.documentView.getSlugAtOffset( relativeStructuralOffset ) || false; + + if ( hasSlug ) { + if ( relativeContentOffset < offset ) { + // If relativeContentOffset returns a lesser number, there's nowhere to go toward the right. Go left. + newOffset = relativeStructuralOffset; } else { - newOffset = Math.max( offset, relativeContentOffset ); + // Move cursor to whichever is nearest to the original offset. + newOffset = Math.min( relativeContentOffset, relativeStructuralOffset ); } + } else if ( relativeContentOffset != offset + 1 ) { + newOffset = relativeContentOffset; + } + + if ( newOffset ) { this.model.change( null, new ve.Range( newOffset )