From 71d4c427c940e35b2d838840dbb0de1e58df75bb Mon Sep 17 00:00:00 2001 From: Paul Ivanov Date: Thu, 6 Mar 2014 16:25:35 -0800 Subject: [PATCH] refactor to improve cell switching in edit mode This code was repeated in both CodeCell and TextCell, both of which are extensions of Cell, so this just unifies the logic in Cell. TextCell had logic here to check if the cell was rendered or not, but I don't believe it is possible to end up triggering such a code path. (Should that be required, I can always just add back these methods to TextCell, performing the .rendered==True check, and calling the Cell prior to this, code mirror at_top would only return true on if the cursor was at the first character of the top line. Now, pressing up arrow on any character on the top line will take you to the cell above. The same applies for the bottom line. Pressing down arrow would only go to the next cell if the cursor was at a location *after* the last character (something that is only possible to achieve in vim mode if the last line is empty, for example). Now, down arrow on any character of the last line will go to the next cell. --- IPython/html/static/notebook/js/cell.js | 28 +++++++++++++++++ IPython/html/static/notebook/js/codecell.js | 20 ------------ IPython/html/static/notebook/js/textcell.js | 34 --------------------- 3 files changed, 28 insertions(+), 54 deletions(-) diff --git a/IPython/html/static/notebook/js/cell.js b/IPython/html/static/notebook/js/cell.js index ffe72cc89..67f4eb4ae 100644 --- a/IPython/html/static/notebook/js/cell.js +++ b/IPython/html/static/notebook/js/cell.js @@ -229,6 +229,34 @@ var IPython = (function (IPython) { } }; + /** + * @method at_top + * @return {Boolean} + */ + Cell.prototype.at_top = function () { + var cm = this.code_mirror + var cursor = cm.getCursor(); + if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { + console.log('at top'); + return true; + } else { + return false; + } + }; + + /** + * @method at_bottom + * @return {Boolean} + * */ + Cell.prototype.at_bottom = function () { + var cm = this.code_mirror + var cursor = cm.getCursor(); + if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { + return true; + } else { + return false; + } + }; /** * enter the command mode for the cell * @method command_mode diff --git a/IPython/html/static/notebook/js/codecell.js b/IPython/html/static/notebook/js/codecell.js index 3ef98ff09..44541ec39 100644 --- a/IPython/html/static/notebook/js/codecell.js +++ b/IPython/html/static/notebook/js/codecell.js @@ -501,26 +501,6 @@ var IPython = (function (IPython) { }; - CodeCell.prototype.at_top = function () { - var cursor = this.code_mirror.getCursor(); - if (cursor.line === 0 && cursor.ch === 0) { - return true; - } else { - return false; - } - }; - - - CodeCell.prototype.at_bottom = function () { - var cursor = this.code_mirror.getCursor(); - if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { - return true; - } else { - return false; - } - }; - - CodeCell.prototype.clear_output = function (wait) { this.output_area.clear_output(wait); this.set_input_prompt(); diff --git a/IPython/html/static/notebook/js/textcell.js b/IPython/html/static/notebook/js/textcell.js index 038dee4e6..db69d2da6 100644 --- a/IPython/html/static/notebook/js/textcell.js +++ b/IPython/html/static/notebook/js/textcell.js @@ -242,40 +242,6 @@ var IPython = (function (IPython) { this.element.find('div.text_cell_render').html(text); }; - /** - * @method at_top - * @return {Boolean} - */ - TextCell.prototype.at_top = function () { - if (this.rendered) { - return true; - } else { - var cursor = this.code_mirror.getCursor(); - if (cursor.line === 0 && cm.findPosV(cm.getCursor(), -1, 'line', cm.charCoords(cm.getCursor(),'div').left).hitSide) { - console.log('at top'); - return true; - } else { - return false; - } - } - }; - - /** - * @method at_bottom - * @return {Boolean} - * */ - TextCell.prototype.at_bottom = function () { - if (this.rendered) { - return true; - } else { - var cursor = this.code_mirror.getCursor(); - if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { - return true; - } else { - return false; - } - } - }; /** * Create Text cell from JSON