Inez has uploaded a new change for review.

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


Change subject: Cleanup in ve.ce.Surface.
......................................................................

Cleanup in ve.ce.Surface.

Mostly by extracting parts of onDocumentKeyDown method into separated methods: 
handleLeftOrRightArrowKey and handleUpOrDownArrowKey

Change-Id: If19abf2218bc667c729f7bf388074061cec3031c
---
M modules/ve/ce/ve.ce.Surface.js
1 file changed, 103 insertions(+), 91 deletions(-)


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

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 905e8e6..05768f6 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -268,101 +268,35 @@
                this.handleInsertion();
                return;
        }
-       // Detect start of selecting using shift+arrow keys.
-       if ( !this.dragging && !this.selecting && e.shiftKey && 
ve.ce.isArrowKey( e.keyCode ) ) {
-               this.selecting = true;
-               this.emit( 'selectionStart' );
-       }
-       if ( ve.ce.isLeftOrRightArrowKey( e.keyCode ) ) {
-               // On Mac OS pressing Command (metaKey) + Left/Right is same as 
pressing Home/End.
-               // As we are not able to handle it programmatically (because we 
don't know at which
-               // offsets lines starts and ends) let it happen natively.
-               if ( e.metaKey ) {
-                       return;
+       if ( ve.ce.isArrowKey( e.keyCode ) ) {
+               // Detect start of selecting using shift+arrow keys.
+               if ( !this.dragging && !this.selecting && e.shiftKey ) {
+                       this.selecting = true;
+                       this.emit( 'selectionStart' );
                }
-               // Selection is going to be displayed programmatically so 
prevent default browser behaviour
-               e.preventDefault();
-               // Stop with final poll cycle so we have correct information in 
model
-               this.surfaceObserver.stop( true );
-               selection = this.model.getSelection();
-               offset = this.getDocument().getRelativeOffset(
-                       selection.to,
-                       e.keyCode === ve.Keys.DOM_VK_LEFT ? -1 : 1, // 
direction (left or right)
-                       e.altKey === true || e.ctrlKey === true ? 'word' : 
'character' // unit
-               );
-               if ( e.shiftKey === true  ) { // expanded range
-                       range = new ve.Range( selection.from, offset );
-               } else { // collapsed range (just a cursor)
-                       range = new ve.Range( offset );
-               }
-               this.model.change( null, range );
-               this.surfaceObserver.start();
-               return;
-       }
-       if ( ve.ce.isUpOrDownArrowKey( e.keyCode ) ) {
-               if ( !$.browser.msie ) {
-                       return;
-               }
-               this.surfaceObserver.stop( true );
-               selection = this.model.getSelection();
-               rangySelection = rangy.getSelection();
-               // Perform programatic handling only for selection that is 
expanded and backwards according
-               // to model data but not according to browser data.
-               if ( selection.getLength() !== 0 &&
-                       selection.start !== selection.from &&
-                       !rangySelection.isBackwards() ) {
-                       if ( !this.hasSlugAtOffset( selection.to ) ) {
-                               // create fake element
-                               $fakeElement = $ ( '<span>' ).html( ' ' ).css( 
{ 'width' : '0px', 'display' : 'none' } );
-                               rangySelection.anchorNode.splitText( 
rangySelection.anchorOffset );
-                               
rangySelection.anchorNode.parentNode.insertBefore(
-                                       $fakeElement[0],
-                                       rangySelection.anchorNode.nextSibling
-                               );
-                               // select fake element
-                               rangyRange = rangy.createRange();
-                               rangyRange.selectNode( $fakeElement[0] );
-                               rangySelection.setSingleRange( rangyRange );
-                               setTimeout( ve.bind( function() {
-                                       $fakeElement.remove();
-                                       this.surfaceObserver.start();
-                                       this.surfaceObserver.stop( false );
-                                       if ( e.shiftKey === true  ) { // 
expanded range
-                                               range = new ve.Range( 
selection.from, this.model.getSelection().to );
-                                       } else { // collapsed range (just a 
cursor)
-                                               range = new ve.Range( 
this.model.getSelection().to );
-                                       }
-                                       this.model.change( null, range );
-                                       this.surfaceObserver.start();
-                               }, this ), 0 );
-                       }
+               if ( ve.ce.isLeftOrRightArrowKey( e.keyCode ) ) {
+                       this.handleLeftOrRightArrowKey( e );
                } else {
-                       this.surfaceObserver.start();
+                       this.handleUpOrDownArrowKey( e );
                }
-               return;
-       }
-       switch ( e.keyCode ) {
-               case ve.Keys.DOM_VK_RETURN:
+       } else if ( e.keyCode === ve.Keys.DOM_VK_RETURN ) {
+               e.preventDefault();
+               this.handleEnter( e );
+       } else if ( e.keyCode === ve.Keys.DOM_VK_BACK_SPACE ) {
+               this.handleDelete( e, true );
+               this.surfaceObserver.stop( true );
+               this.surfaceObserver.start();
+       } else if ( e.keyCode === ve.Keys.DOM_VK_DELETE ) {
+               this.handleDelete( e, false );
+               this.surfaceObserver.stop( true );
+               this.surfaceObserver.start();
+       } else {
+               // Execute key command if available
+               this.surfaceObserver.stop( true );
+               if ( this.surface.execute( new ve.Trigger( e ) ) ) {
                        e.preventDefault();
-                       this.handleEnter( e );
-                       break;
-               case ve.Keys.DOM_VK_BACK_SPACE:
-                       this.handleDelete( e, true );
-                       this.surfaceObserver.stop( true );
-                       this.surfaceObserver.start();
-                       break;
-               case ve.Keys.DOM_VK_DELETE:
-                       this.handleDelete( e, false );
-                       this.surfaceObserver.stop( true );
-                       this.surfaceObserver.start();
-                       break;
-               default:
-                       // Execute key command if available
-                       this.surfaceObserver.stop( true );
-                       if ( this.surface.execute( new ve.Trigger( e ) ) ) {
-                               e.preventDefault();
-                       }
-                       this.surfaceObserver.start();
+               }
+               this.surfaceObserver.start();
        }
 };
 
@@ -735,6 +669,84 @@
 /*! Utilities */
 
 /**
+ * @method
+ */
+ve.ce.Surface.prototype.handleLeftOrRightArrowKey = function ( e ) {
+       var selection, offset, range;
+       // On Mac OS pressing Command (metaKey) + Left/Right is same as 
pressing Home/End.
+       // As we are not able to handle it programmatically (because we don't 
know at which offsets
+       // lines starts and ends) let it happen natively.
+       if ( e.metaKey ) {
+               return;
+       }
+       // Selection is going to be displayed programmatically so prevent 
default browser behaviour
+       e.preventDefault();
+       // Stop with final poll cycle so we have correct information in model
+       this.surfaceObserver.stop( true );
+       selection = this.model.getSelection();
+       offset = this.getDocument().getRelativeOffset(
+               selection.to,
+               e.keyCode === ve.Keys.DOM_VK_LEFT ? -1 : 1, // direction (left 
or right)
+               e.altKey === true || e.ctrlKey === true ? 'word' : 'character' 
// unit
+       );
+       if ( e.shiftKey === true  ) { // expanded range
+               range = new ve.Range( selection.from, offset );
+       } else { // collapsed range (just a cursor)
+               range = new ve.Range( offset );
+       }
+       this.model.change( null, range );
+       this.surfaceObserver.start();
+};
+
+/**
+ * @method
+ */
+ve.ce.Surface.prototype.handleUpOrDownArrowKey = function ( e ) {
+       var selection, rangySelection, $fakeElement, $slug;
+       if ( !$.browser.msie ) {
+               return;
+       }
+       this.surfaceObserver.stop( true );
+       selection = this.model.getSelection();
+       rangySelection = rangy.getSelection();
+       // Perform programatic handling only for selection that is expanded and 
backwards according to
+       // model data but not according to browser data.
+       if ( !selection.isCollapsed() && selection.isBackwards() && 
!rangySelection.isBackwards() ) {
+               if ( $slug = this.documentView.getSlugAtOffset( selection.to ) 
) {
+                       rangyRange = rangy.createRange();
+                       rangyRange.selectNode( $slug[0] );
+                       rangySelection.setSingleRange( rangyRange );
+               } else {
+                       $fakeElement = $ ( '<span>' ).html( ' ' ).css( { 
'width' : '0px', 'display' : 'none' } );
+                       rangySelection.anchorNode.splitText( 
rangySelection.anchorOffset );
+                       rangySelection.anchorNode.parentNode.insertBefore(
+                               $fakeElement[0],
+                               rangySelection.anchorNode.nextSibling
+                       );
+                       rangyRange = rangy.createRange();
+                       rangyRange.selectNode( $fakeElement[0] );
+                       rangySelection.setSingleRange( rangyRange );
+               }
+               setTimeout( ve.bind( function() {
+                       if ( $fakeElement ) {
+                               $fakeElement.remove();
+                       }
+                       this.surfaceObserver.start();
+                       this.surfaceObserver.stop( false );
+                       if ( e.shiftKey === true  ) { // expanded range
+                               range = new ve.Range( selection.from, 
this.model.getSelection().to );
+                       } else { // collapsed range (just a cursor)
+                               range = new ve.Range( 
this.model.getSelection().to );
+                       }
+                       this.model.change( null, range );
+                       this.surfaceObserver.start();
+               }, this ), 0 );
+       } else {
+               this.surfaceObserver.start();
+       }
+};
+
+/**
  * Handle insertion of content.
  *
  * @method

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If19abf2218bc667c729f7bf388074061cec3031c
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