mirror of
https://github.com/jupyter/notebook.git
synced 2025-03-07 13:07:22 +08:00
Add the ability to mark cells.
This commit is contained in:
parent
ff1bf59689
commit
64c93843a8
@ -423,6 +423,14 @@ define(function(require){
|
||||
env.notebook.show_command_palette();
|
||||
}
|
||||
},
|
||||
'toggle-marks': {
|
||||
help_index : 'cj',
|
||||
help: 'toggle marks',
|
||||
icon: 'fa-check',
|
||||
handler : function(env){
|
||||
env.notebook.toggle_marks(env.notebook.get_selected_cells());
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -169,6 +169,11 @@ define([
|
||||
if (!that.selected) {
|
||||
that.events.trigger('select.Cell', {'cell':that});
|
||||
}
|
||||
|
||||
// Ctrl-click should mark the c ell.
|
||||
if (event.ctrlKey) {
|
||||
that.marked = !that.marked;
|
||||
}
|
||||
});
|
||||
that.element.focusin(function (event) {
|
||||
if (!that.selected) {
|
||||
@ -280,6 +285,45 @@ define([
|
||||
}
|
||||
return was_selected_cell;
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks the cell
|
||||
* @return {Cell} this
|
||||
*/
|
||||
Cell.prototype.mark = function() {
|
||||
if (!this.marked) {
|
||||
this.element.addClass('marked');
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Unmarks the cell
|
||||
* @return {Cell} this
|
||||
*/
|
||||
Cell.prototype.unmark = function() {
|
||||
if (this.marked) {
|
||||
this.element.removeClass('marked');
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether or not the cell is marked.
|
||||
* @return {boolean}
|
||||
*/
|
||||
Object.defineProperty(Cell.prototype, 'marked', {
|
||||
get: function() {
|
||||
return this.element.hasClass('marked');
|
||||
},
|
||||
set: function(value) {
|
||||
if (value) {
|
||||
this.mark();
|
||||
} else {
|
||||
this.unmark();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* should be overritten by subclass
|
||||
|
@ -93,6 +93,7 @@ define([
|
||||
'enter' : 'ipython.enter-edit-mode',
|
||||
'space' : 'ipython.scroll-down',
|
||||
'down' : 'ipython.select-next-cell',
|
||||
'cmdtrl-space' : 'ipython.toggle-marks',
|
||||
'i,i' : 'ipython.interrupt-kernel',
|
||||
'0,0' : 'ipython.restart-kernel',
|
||||
'd,d' : 'ipython.delete-cell',
|
||||
|
@ -628,6 +628,73 @@ define(function (require) {
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Toggles the marks on the cells
|
||||
* @param {Cell[]} [cells] - optionally specify what cells should be toggled
|
||||
*/
|
||||
Notebook.prototype.toggle_marks = function(cells) {
|
||||
cells = cells || this.get_cells();
|
||||
cells.forEach(function(cell) { cell.marked = !cell.marked; });
|
||||
};
|
||||
|
||||
/**
|
||||
* Mark all of the cells
|
||||
* @param {Cell[]} [cells] - optionally specify what cells should be marked
|
||||
*/
|
||||
Notebook.prototype.mark_all = function(cells) {
|
||||
cells = cells || this.get_cells();
|
||||
cells.forEach(function(cell) { cell.mark(); });
|
||||
};
|
||||
|
||||
/**
|
||||
* Unmark all of the cells
|
||||
* @param {Cell[]} [cells] - optionally specify what cells should be unmarked
|
||||
*/
|
||||
Notebook.prototype.unmark_all = function(cells) {
|
||||
this.get_marked_cells(cells).forEach(function(cell) { cell.unmark(); });
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the cells that should be marked, exclusively
|
||||
* @param {Cell[]} cells
|
||||
*/
|
||||
Notebook.prototype.set_marked_cells = function(cells) {
|
||||
this.unmark_all();
|
||||
this.mark_all(cells);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the cells that are marked
|
||||
* @param {Cell[]} [cells] - optionally provide the cells to search through
|
||||
* @return {Cell[]} marked cells
|
||||
*/
|
||||
Notebook.prototype.get_marked_cells = function(cells) {
|
||||
cells = cells || this.get_cells();
|
||||
return cells.filter(function(cell) { return cell.marked; });
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the cells that are marked by indices
|
||||
* @param {number[]} indices
|
||||
* @param {Cell[]} [cells] - optionally provide the cells to search through
|
||||
*/
|
||||
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; }));
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the indices of the cells that are marked
|
||||
* @param {Cell[]} [cells] - optionally provide the cells to search through
|
||||
* @return {number[]} marked cell indices
|
||||
*/
|
||||
Notebook.prototype.get_marked_indices = function(cells) {
|
||||
cells = cells || this.get_cells();
|
||||
var markedCells = this.get_marked_cells(cells);
|
||||
return markedCells.map(function(cell) { return cells.indexOf(cell); });
|
||||
};
|
||||
|
||||
/**
|
||||
* Get an array of the cells in the currently selected range
|
||||
|
@ -21,6 +21,11 @@ div.cell {
|
||||
border-color: transparent;
|
||||
}
|
||||
}
|
||||
|
||||
&.marked {
|
||||
border-left-color: blue;
|
||||
border-left-width: 3px;
|
||||
}
|
||||
|
||||
width: 100%;
|
||||
padding: 5px;
|
||||
|
56
notebook/tests/notebook/marks.js
Normal file
56
notebook/tests/notebook/marks.js
Normal file
@ -0,0 +1,56 @@
|
||||
|
||||
// Test
|
||||
casper.notebook_test(function () {
|
||||
var that = this;
|
||||
|
||||
var a = 'print("a")';
|
||||
var index = this.append_cell(a);
|
||||
|
||||
var b = 'print("b")';
|
||||
index = this.append_cell(b);
|
||||
|
||||
var c = 'print("c")';
|
||||
index = this.append_cell(c);
|
||||
|
||||
this.then(function () {
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return Jupyter.notebook.get_marked_cells().length;
|
||||
}), 0, 'no cells are marked programmatically');
|
||||
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return $('.cell.marked').length;
|
||||
}), 0, 'no cells are marked visibily');
|
||||
|
||||
this.evaluate(function() {
|
||||
Jupyter.notebook.mark_all();
|
||||
});
|
||||
|
||||
var cellCount = this.evaluate(function() {
|
||||
return Jupyter.notebook.ncells();
|
||||
});
|
||||
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return Jupyter.notebook.get_marked_cells().length;
|
||||
}), cellCount, 'mark_all');
|
||||
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return $('.cell.marked').length;
|
||||
}), cellCount, 'marked cells are marked visibily');
|
||||
|
||||
this.evaluate(function() {
|
||||
Jupyter.notebook.unmark_all();
|
||||
});
|
||||
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return Jupyter.notebook.get_marked_cells().length;
|
||||
}), 0, 'unmark_all');
|
||||
|
||||
this.evaluate(function() {
|
||||
Jupyter.notebook.set_marked_indices([1]);
|
||||
});
|
||||
|
||||
this.test.assertEquals(this.evaluate(function() {
|
||||
return Jupyter.notebook.get_marked_indices()[0];
|
||||
}), 1, 'get/set_marked_indices');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user