Merge pull request #2277 from bfroehle/notebook_arrow_keys

nb: up/down arrow keys move to begin/end of line at top/bottom of cell
This commit is contained in:
Brian E. Granger 2012-08-10 18:08:58 -07:00
commit 638478fa23
2 changed files with 35 additions and 4 deletions

View File

@ -234,7 +234,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.at_top = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === 0) {
if (cursor.line === 0 && cursor.ch === 0) {
return true;
} else {
return false;
@ -244,7 +244,7 @@ var IPython = (function (IPython) {
CodeCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === (this.code_mirror.lineCount()-1)) {
if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
return true;
} else {
return false;

View File

@ -12,6 +12,7 @@
var IPython = (function (IPython) {
// TextCell base class
var key = IPython.utils.keycodes;
var TextCell = function () {
this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
@ -269,6 +270,36 @@ var IPython = (function (IPython) {
};
RawCell.prototype.handle_codemirror_keyevent = function (editor, event) {
// This method gets called in CodeMirror's onKeyDown/onKeyPress
// handlers and is used to provide custom key handling. Its return
// value is used to determine if CodeMirror should ignore the event:
// true = ignore, false = don't ignore.
var that = this;
if (event.which === key.UPARROW && event.type === 'keydown') {
// If we are not at the top, let CM handle the up arrow and
// prevent the global keydown handler from handling it.
if (!that.at_top()) {
event.stop();
return false;
} else {
return true;
};
} else if (event.which === key.DOWNARROW && event.type === 'keydown') {
// If we are not at the bottom, let CM handle the down arrow and
// prevent the global keydown handler from handling it.
if (!that.at_bottom()) {
event.stop();
return false;
} else {
return true;
};
};
return false;
};
RawCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
this.code_mirror.refresh();
@ -278,7 +309,7 @@ var IPython = (function (IPython) {
RawCell.prototype.at_top = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === 0) {
if (cursor.line === 0 && cursor.ch === 0) {
return true;
} else {
return false;
@ -288,7 +319,7 @@ var IPython = (function (IPython) {
RawCell.prototype.at_bottom = function () {
var cursor = this.code_mirror.getCursor();
if (cursor.line === (this.code_mirror.lineCount()-1)) {
if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
return true;
} else {
return false;