diff --git a/zeppelin-web/src/app/notebook/paragraph/paragraph.controller.js b/zeppelin-web/src/app/notebook/paragraph/paragraph.controller.js index 5e8e2fe89c8..2925c476cb7 100644 --- a/zeppelin-web/src/app/notebook/paragraph/paragraph.controller.js +++ b/zeppelin-web/src/app/notebook/paragraph/paragraph.controller.js @@ -665,6 +665,19 @@ angular.module('zeppelinWebApp').controller('ParagraphCtrl', function($scope, $r $scope.editor.commands.bindKey('ctrl-.', 'startAutocomplete'); $scope.editor.commands.bindKey('ctrl-space', null); + var keyBindingEditorFocusAction = function(scrollValue) { + var numRows = $scope.editor.getSession().getLength(); + var currentRow = $scope.editor.getCursorPosition().row; + if (currentRow === 0 && scrollValue <= 0) { + // move focus to previous paragraph + $scope.$emit('moveFocusToPreviousParagraph', $scope.paragraph.id); + } else if (currentRow === numRows - 1 && scrollValue >= 0) { + $scope.$emit('moveFocusToNextParagraph', $scope.paragraph.id); + } else { + $scope.scrollToCursor($scope.paragraph.id, scrollValue); + } + }; + // handle cursor moves $scope.editor.keyBinding.origOnCommandKey = $scope.editor.keyBinding.onCommandKey; $scope.editor.keyBinding.onCommandKey = function(e, hashId, keyCode) { @@ -678,27 +691,26 @@ angular.module('zeppelinWebApp').controller('ParagraphCtrl', function($scope, $r angular.element('#' + $scope.paragraph.id + '_editor > textarea').css('top', cursorPos.top); } - var numRows; - var currentRow; + var ROW_UP = -1; + var ROW_DOWN = 1; - if (keyCode === 38 || (keyCode === 80 && e.ctrlKey && !e.altKey)) { // UP - numRows = $scope.editor.getSession().getLength(); - currentRow = $scope.editor.getCursorPosition().row; - if (currentRow === 0) { - // move focus to previous paragraph - $scope.$emit('moveFocusToPreviousParagraph', $scope.paragraph.id); - } else { - $scope.scrollToCursor($scope.paragraph.id, -1); - } - } else if (keyCode === 40 || (keyCode === 78 && e.ctrlKey && !e.altKey)) { // DOWN - numRows = $scope.editor.getSession().getLength(); - currentRow = $scope.editor.getCursorPosition().row; - if (currentRow === numRows - 1) { - // move focus to next paragraph - $scope.$emit('moveFocusToNextParagraph', $scope.paragraph.id); - } else { - $scope.scrollToCursor($scope.paragraph.id, 1); - } + switch (keyCode) { + case 38: + keyBindingEditorFocusAction(ROW_UP); + break; + case 80: + if (e.ctrlKey && !e.altKey) { + keyBindingEditorFocusAction(ROW_UP); + } + break; + case 40: + keyBindingEditorFocusAction(ROW_DOWN); + break; + case 78: + if (e.ctrlKey && !e.altKey) { + keyBindingEditorFocusAction(ROW_DOWN); + } + break; } } this.origOnCommandKey(e, hashId, keyCode);