From 0c2cc3e695820921115febd7360bb1115a64f8ec Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Mon, 12 Oct 2015 14:57:16 -0700 Subject: [PATCH] Address @ellisonbg's comments --- notebook/static/notebook/js/actions.js | 4 +-- .../static/notebook/js/keyboardmanager.js | 2 +- notebook/static/notebook/js/notebook.js | 35 ++++++++++++++----- notebook/static/notebook/less/cell.less | 1 + notebook/tests/notebook/marks.js | 4 +-- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index efece6a69..a169bcf80 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -423,12 +423,12 @@ define(function(require){ env.notebook.show_command_palette(); } }, - 'toggle-marks': { + 'toggle-cell-marked': { help_index : 'cj', help: 'toggle marks', icon: 'fa-check', handler : function(env){ - env.notebook.toggle_marks(env.notebook.get_selected_cells()); + env.notebook.toggle_cells_marked(env.notebook.get_selected_cells()); } }, }; diff --git a/notebook/static/notebook/js/keyboardmanager.js b/notebook/static/notebook/js/keyboardmanager.js index 519939cb6..407a7a11a 100644 --- a/notebook/static/notebook/js/keyboardmanager.js +++ b/notebook/static/notebook/js/keyboardmanager.js @@ -93,7 +93,7 @@ define([ 'enter' : 'ipython.enter-edit-mode', 'space' : 'ipython.scroll-down', 'down' : 'ipython.select-next-cell', - 'ctrl-space' : 'ipython.toggle-marks', + 'ctrl-space' : 'ipython.toggle-cell-marked', 'i,i' : 'ipython.interrupt-kernel', '0,0' : 'ipython.restart-kernel', 'd,d' : 'ipython.delete-cell', diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index 5859cce2d..9e060666d 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -401,7 +401,7 @@ define(function (require) { var st = sme.scrollTop(); var t = sme.offset().top; var ct = cells[index].element.offset().top; - var scroll_value = st + ct - (t + .01 * percent * h); + var scroll_value = st + ct - (t + 0.01 * percent * h); this.scroll_manager.element.animate({scrollTop:scroll_value}, time); return scroll_value; }; @@ -633,7 +633,7 @@ define(function (require) { * Toggles the marks on the cells * @param {Cell[]} [cells] - optionally specify what cells should be toggled */ - Notebook.prototype.toggle_marks = function(cells) { + Notebook.prototype.toggle_cells_marked = function(cells) { cells = cells || this.get_cells(); cells.forEach(function(cell) { cell.marked = !cell.marked; }); }; @@ -642,7 +642,7 @@ define(function (require) { * Mark all of the cells * @param {Cell[]} [cells] - optionally specify what cells should be marked */ - Notebook.prototype.mark_all = function(cells) { + Notebook.prototype.mark_all_cells = function(cells) { cells = cells || this.get_cells(); cells.forEach(function(cell) { cell.mark(); }); }; @@ -651,7 +651,7 @@ define(function (require) { * Unmark all of the cells * @param {Cell[]} [cells] - optionally specify what cells should be unmarked */ - Notebook.prototype.unmark_all = function(cells) { + Notebook.prototype.unmark_all_cells = function(cells) { this.get_marked_cells(cells).forEach(function(cell) { cell.unmark(); }); }; @@ -660,8 +660,8 @@ define(function (require) { * @param {Cell[]} cells */ Notebook.prototype.set_marked_cells = function(cells) { - this.unmark_all(); - this.mark_all(cells); + this.unmark_all_cells(); + this.mark_all_cells(cells); }; /** @@ -681,8 +681,8 @@ define(function (require) { */ Notebook.prototype.set_marked_indices = function(indices, cells) { cells = cells || this.get_cells(); - this.unmark_all(cells); - this.mark_all(cells.filter(function(cell, index) { return indices.indexOf(index) !== -1; })); + this.unmark_all_cells(cells); + this.mark_all_cells(cells.filter(function(cell, index) { return indices.indexOf(index) !== -1; })); }; /** @@ -695,6 +695,25 @@ define(function (require) { var markedCells = this.get_marked_cells(cells); return markedCells.map(function(cell) { return cells.indexOf(cell); }); }; + + /** + * Checks if the marked cells are contiguous + * @param {Cell[]} [cells] - optionally provide the cells to search through + * @return {boolean} + */ + Notebook.prototype.are_marks_contiguous = function(cells) { + // Get a numerically sorted list of the marked indices. + var markedIndices = this.get_marked_indices(cells).sort( + function(a,b) { return a-b; }); + + // Check for contiguousness + for (var i = 0; i < markedIndices.length - 1; i++) { + if (markedIndices[i+1] - markedIndices[i] !== 1) { + return false; + } + } + return true; + }; /** * Get an array of the cells in the currently selected range diff --git a/notebook/static/notebook/less/cell.less b/notebook/static/notebook/less/cell.less index 8e48cd322..ba10744ae 100644 --- a/notebook/static/notebook/less/cell.less +++ b/notebook/static/notebook/less/cell.less @@ -25,6 +25,7 @@ div.cell { &.marked { border-left-color: blue; border-left-width: 3px; + margin-left: -2px; } width: 100%; diff --git a/notebook/tests/notebook/marks.js b/notebook/tests/notebook/marks.js index 32d2a5f2d..bb7dc4dc6 100644 --- a/notebook/tests/notebook/marks.js +++ b/notebook/tests/notebook/marks.js @@ -22,7 +22,7 @@ casper.notebook_test(function () { }), 0, 'no cells are marked visibily'); this.evaluate(function() { - Jupyter.notebook.mark_all(); + Jupyter.notebook.mark_all_cells(); }); var cellCount = this.evaluate(function() { @@ -38,7 +38,7 @@ casper.notebook_test(function () { }), cellCount, 'marked cells are marked visibily'); this.evaluate(function() { - Jupyter.notebook.unmark_all(); + Jupyter.notebook.unmark_all_cells(); }); this.test.assertEquals(this.evaluate(function() {