Add a simple 'undo' for cell deletion.

I've accidentally deleted the wrong cell quite a few times and
it has been quite frustrating. This enables a very hacky backup of
the last cell deletion.

I'm sure the core devs will have some opinions on both the feature
and its implementation, I consider this very much a first pass.
David Warde-Farley 13 years ago
parent 3254572a90
commit 33c1f78d33

@ -22,6 +22,9 @@ var IPython = (function (IPython) {
this.next_prompt_number = 1;
this.kernel = null;
this.clipboard = null;
this.undelete_backup = null;
this.undelete_index = null;
this.undelete_below = false;
this.paste_enabled = false;
this.dirty = false;
this.metadata = {};
@ -257,6 +260,10 @@ var IPython = (function (IPython) {
IPython.quick_help.show_keyboard_shortcuts();
that.control_key_active = false;
return false;
} else if (event.which === 90 && that.control_key_active) {
that.undelete();
that.control_key_active = false;
return false;
} else if (that.control_key_active) {
that.control_key_active = false;
return true;
@ -536,13 +543,19 @@ var IPython = (function (IPython) {
Notebook.prototype.delete_cell = function (index) {
var i = this.index_or_selected(index);
var cell = this.get_selected_cell();
this.undelete_backup = cell.toJSON();
if (this.is_valid_cell_index(i)) {
var ce = this.get_cell_element(i);
ce.remove();
if (i === (this.ncells())) {
this.select(i-1);
this.undelete_index = i - 1;
this.undelete_below = true;
} else {
this.select(i);
this.undelete_index = i;
this.undelete_below = false;
};
this.dirty = true;
};
@ -818,6 +831,33 @@ var IPython = (function (IPython) {
};
};
// Cell undelete
Notebook.prototype.undelete = function() {
if (this.undelete_backup !== null && this.undelete_index !== null) {
var current_index = this.get_selected_index();
if (this.undelete_index < current_index) {
current_index = current_index + 1;
}
if (this.undelete_index >= this.ncells()) {
this.select(this.ncells() - 1);
}
else {
this.select(this.undelete_index);
}
var cell_data = this.undelete_backup;
var new_cell = null;
if (this.undelete_below) {
new_cell = this.insert_cell_below(cell_data.cell_type);
} else {
new_cell = this.insert_cell_above(cell_data.cell_type);
}
new_cell.fromJSON(cell_data);
this.select(current_index);
this.undelete_backup = null;
this.undelete_index = null;
}
}
// Split/merge

@ -49,7 +49,8 @@ var IPython = (function (IPython) {
{key: 'Ctrl-m n', help: 'select next'},
{key: 'Ctrl-m i', help: 'interrupt kernel'},
{key: 'Ctrl-m .', help: 'restart kernel'},
{key: 'Ctrl-m h', help: 'show keyboard shortcuts'}
{key: 'Ctrl-m h', help: 'show keyboard shortcuts'},
{key: 'Ctrl-m z', help: 'undo last cell deletion'}
];
for (var i=0; i<shortcuts.length; i++) {
dialog.append($('<div>').

Loading…
Cancel
Save