Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple 'undo' for cell deletion. #2549

Merged
merged 4 commits into from Nov 12, 2012
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions IPython/frontend/html/notebook/static/js/notebook.js
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose 90 is z a little comment below would be great :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 332a14b.

that.undelete();
that.control_key_active = false;
return false;
} else if (that.control_key_active) {
that.control_key_active = false;
return true;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions IPython/frontend/html/notebook/static/js/quickhelp.js
Expand Up @@ -33,6 +33,7 @@ var IPython = (function (IPython) {
{key: 'Ctrl-m c', help: 'copy cell'},
{key: 'Ctrl-m v', help: 'paste cell'},
{key: 'Ctrl-m d', help: 'delete cell'},
{key: 'Ctrl-m z', help: 'undo last cell deletion'},
{key: 'Ctrl-m a', help: 'insert cell above'},
{key: 'Ctrl-m b', help: 'insert cell below'},
{key: 'Ctrl-m o', help: 'toggle output'},
Expand Down