Fixing execution related things.

* Extra enter on FF is fixed by hooking into CodeMirror's
  onKeyEvent hook. We now have CodeMirror ignore shift-enter
  completely as we handle it ourselves.
* The cell execution logic in notebook.js has been refactored and
  the Run All/Selected buttons have been hooked up.
This commit is contained in:
Brian E. Granger 2011-07-22 16:04:54 -07:00
parent 5fb6787570
commit 27b5357678
2 changed files with 65 additions and 37 deletions

View File

@ -25,7 +25,8 @@ var IPython = (function (IPython) {
this.code_mirror = CodeMirror(input_area.get(0), {
indentUnit : 4,
enterMode : 'flat',
tabMode: 'shift'
tabMode: 'shift',
onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
});
input.append(input_area);
var output = $('<div></div>').addClass('output vbox');
@ -35,6 +36,18 @@ var IPython = (function (IPython) {
};
CodeCell.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.
if (event.keyCode === 13 && event.shiftKey) {
// Always ignore shift-enter in CodeMirror as we handle it.
return true;
} else {
return false;
};
};
CodeCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
this.code_mirror.focus();

View File

@ -57,42 +57,9 @@ var IPython = (function (IPython) {
that.select_next();
};
} else if (event.which === 13 && event.shiftKey) {
// The focus is not quite working here.
var cell = that.selected_cell();
var cell_index = that.find_cell_index(cell);
// TODO: the logic here needs to be moved into appropriate
// methods of Notebook.
if (cell instanceof IPython.CodeCell) {
event.preventDefault();
cell.clear_output();
var code = cell.get_code();
if (that.notebook_load_re.test(code)) {
var code_parts = code.split(' ');
if (code_parts.length === 3) {
that.load_notebook(code_parts[2]);
};
} else if (that.notebook_save_re.test(code)) {
var code_parts = code.split(' ');
if (code_parts.length === 3) {
that.save_notebook(code_parts[2]);
} else {
that.save_notebook()
};
} else {
var msg_id = that.kernel.execute(cell.get_code());
that.msg_cell_map[msg_id] = cell.cell_id;
};
} else if (cell instanceof IPython.TextCell) {
event.preventDefault();
cell.render();
}
if (cell_index === (that.ncells()-1)) {
that.insert_code_cell_after();
// If we are adding a new cell at the end, scroll down to show it.
that.scroll_to_bottom();
} else {
that.select(cell_index+1);
};
console.log('Entering execute');
that.execute_selected_cell(true);
console.log('Leaving execute');
};
});
@ -489,6 +456,54 @@ var IPython = (function (IPython) {
};
Notebook.prototype.execute_selected_cell = function (add_new) {
if (add_new === undefined) {add_new = true;};
var that = this;
var cell = that.selected_cell();
var cell_index = that.find_cell_index(cell);
// TODO: the logic here needs to be moved into appropriate
// methods of Notebook.
if (cell instanceof IPython.CodeCell) {
cell.clear_output();
var code = cell.get_code();
if (that.notebook_load_re.test(code)) {
var code_parts = code.split(' ');
if (code_parts.length === 3) {
that.load_notebook(code_parts[2]);
};
} else if (that.notebook_save_re.test(code)) {
var code_parts = code.split(' ');
if (code_parts.length === 3) {
that.save_notebook(code_parts[2]);
} else {
that.save_notebook()
};
} else {
var msg_id = that.kernel.execute(cell.get_code());
that.msg_cell_map[msg_id] = cell.cell_id;
};
} else if (cell instanceof IPython.TextCell) {
cell.render();
}
if ((cell_index === (that.ncells()-1)) && add_new) {
that.insert_code_cell_after();
// If we are adding a new cell at the end, scroll down to show it.
that.scroll_to_bottom();
} else {
that.select(cell_index+1);
};
};
Notebook.prototype.execute_all_cells = function () {
var ncells = this.ncells();
for (var i=0; i<ncells; i++) {
this.select(i);
this.execute_selected_cell(false);
};
this.scroll_to_bottom();
};
// Persistance and loading