Fixing bugs that have shown up since updating CM to 2.2.

This commit is contained in:
Brian Granger 2012-01-18 11:42:31 -08:00
parent 13ca0054f7
commit deb800daff
3 changed files with 35 additions and 12 deletions

View File

@ -86,7 +86,7 @@ var IPython = (function (IPython) {
if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
// Always ignore shift-enter in CodeMirror as we handle it.
return true;
}else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) {
} else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) {
// triger aon keypress (!) otherwise inconsistent event.which depending on plateform
// browser and keyboard layout !
// Pressing '(' , request tooltip, don't forget to reappend it
@ -499,6 +499,7 @@ var IPython = (function (IPython) {
select.focus();
};
CodeCell.prototype.toggle_line_numbers = function () {
if (this.code_mirror.getOption('lineNumbers') == false) {
this.code_mirror.setOption('lineNumbers', true);
@ -508,15 +509,14 @@ var IPython = (function (IPython) {
this.code_mirror.refresh();
};
CodeCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
// Todo: this dance is needed because as of CodeMirror 2.12, focus is
// not causing the cursor to blink if the editor is empty initially.
// While this seems to fix the issue, this should be fixed
// in CodeMirror proper.
var s = this.code_mirror.getValue();
// In some cases (inserting a new cell) we need a refresh before and
// after the focus. Not sure why this is the case.
this.code_mirror.refresh();
this.code_mirror.focus();
if (s === '') this.code_mirror.setValue('');
this.code_mirror.refresh();
};

View File

@ -516,7 +516,11 @@ var IPython = (function (IPython) {
source_cell instanceof IPython.MarkdownCell) {
this.insert_code_cell_below(i);
var target_cell = this.cells()[i+1];
target_cell.set_code(source_cell.get_source());
var text = source_cell.get_source();
if (text === source_cell.placeholder) {
text = '';
}
target_cell.set_code(text);
source_element.remove();
target_cell.select();
};
@ -544,9 +548,11 @@ var IPython = (function (IPython) {
}
if (target_cell !== null) {
if (text === "") {text = target_cell.placeholder;};
// The edit must come before the set_source.
target_cell.edit();
target_cell.set_source(text);
source_element.remove();
target_cell.edit();
target_cell.select();
}
this.dirty = true;
};

View File

@ -34,7 +34,8 @@ var IPython = (function (IPython) {
mode: this.code_mirror_mode,
theme: 'default',
value: this.placeholder,
readOnly: this.read_only
readOnly: this.read_only,
onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
});
// The tabindex=-1 makes this div focusable.
var render_area = $('<div/>').addClass('text_cell_render').
@ -51,13 +52,27 @@ var IPython = (function (IPython) {
if (event.which === 13) {
if (that.rendered) {
that.edit();
event.preventDefault();
return false;
}
}
});
};
TextCell.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 || event.ctrlKey)) {
// Always ignore shift-enter in CodeMirror as we handle it.
return true;
}
return false;
};
TextCell.prototype.select = function () {
IPython.Cell.prototype.select.apply(this);
var output = this.element.find("div.text_cell_render");
@ -73,14 +88,16 @@ var IPython = (function (IPython) {
TextCell.prototype.edit = function () {
var that = this;
if ( this.read_only ) return;
if (this.rendered === true) {
var text_cell = this.element;
var output = text_cell.find("div.text_cell_render");
output.hide();
text_cell.find('div.text_cell_input').show();
that.code_mirror.refresh();
this.code_mirror.focus();
this.code_mirror.refresh();
that.code_mirror.refresh();
this.rendered = false;
if (this.get_source() === this.placeholder) {
this.set_source('');