Skip to content

Don't break user keyboard shortcut. #667

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notebook/static/base/js/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ define([
**/
var action_name = this.actions.get_name(data);
if (! action_name){
throw new Error('does not know how to deal with', data);
throw new Error('does not know how to deal with ' + data);
}
shortcut = normalize_shortcut(shortcut);
this.set_shortcut(shortcut, action_name);
Expand Down
34 changes: 28 additions & 6 deletions notebook/static/notebook/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,35 @@ define(function(require){
env.notebook.extend_marked(1);
}
},
'cut-cell' : {
'cut-selected-cell' : {
icon: 'fa-cut',
help_index : 'ee',
handler : function (env) {
var index = env.notebook.get_selected_index();
env.notebook.cut_cell();
env.notebook.cut_selected_cell();
env.notebook.select(index);
}
},
'copy-cell' : {
'cut-marked-cells' : {
help_index : 'ee',
handler : function (env) {
env.notebook.cut_marked_cells();
}
},

'copy-selected-cell' : {
help: 'copy selected cells to clipboard',
icon: 'fa-copy',
help_index : 'ef',
handler : function (env) {
env.notebook.copy_cell();
env.notebook.copy_selected_cell();
}
},
'copy-marked-cells' : {
help: 'copy marked cells to clipboard',
help_index : 'ef',
handler : function (env) {
env.notebook.copy_marked_cells();
}
},
'paste-cell-above' : {
Expand Down Expand Up @@ -352,11 +367,18 @@ define(function(require){
env.quick_help.show_keyboard_shortcuts();
}
},
'delete-cell': {
'delete-selected-cell': {
help: 'delete selected cell',
help_index : 'ej',
handler : function (env) {
env.notebook.delete_selected_cell();
}
},
'delete-marked-cells': {
help: 'delete selected cell',
help_index : 'ej',
handler : function (env) {
env.notebook.delete_cell();
env.notebook.delete_marked_cells();
}
},
'undo-cell-deletion' : {
Expand Down
6 changes: 3 additions & 3 deletions notebook/static/notebook/js/keyboardmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ define([
'down' : 'jupyter-notebook:select-next-cell',
'i,i' : 'jupyter-notebook:interrupt-kernel',
'0,0' : 'jupyter-notebook:confirm-restart-kernel',
'd,d' : 'jupyter-notebook:delete-cell',
'd,d' : 'jupyter-notebook:delete-selected-cell',
'esc': 'jupyter-notebook:close-pager-or-unmark-all-cells',
'up' : 'jupyter-notebook:select-previous-cell',
'k' : 'jupyter-notebook:select-previous-cell',
Expand All @@ -105,8 +105,8 @@ define([
'shift-up': 'jupyter-notebook:extend-marked-cells-above',
'shift-down': 'jupyter-notebook:extend-marked-cells-below',
'ctrl-space': 'jupyter-notebook:toggle-cell-marked',
'x' : 'jupyter-notebook:cut-cell',
'c' : 'jupyter-notebook:copy-cell',
'x' : 'jupyter-notebook:cut-selected-cell',
'c' : 'jupyter-notebook:copy-selected-cell',
'v' : 'jupyter-notebook:paste-cell-below',
'a' : 'jupyter-notebook:insert-cell-above',
'b' : 'jupyter-notebook:insert-cell-below',
Expand Down
4 changes: 2 additions & 2 deletions notebook/static/notebook/js/maintoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ define([
['jupyter-notebook:insert-cell-below'],
'insert_above_below'],
[
['jupyter-notebook:cut-cell',
'jupyter-notebook:copy-cell',
['jupyter-notebook:cut-selected-cell',
'jupyter-notebook:copy-selected-cell',
'jupyter-notebook:paste-cell-below'
] ,
'cut_copy_paste'],
Expand Down
6 changes: 3 additions & 3 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ define([
'#restart_clear_output': 'jupyter-notebook:confirm-restart-kernel-and-clear-output',
'#restart_run_all': 'jupyter-notebook:confirm-restart-kernel-and-run-all-cells',
'#int_kernel': 'jupyter-notebook:interrupt-kernel',
'#cut_cell': 'jupyter-notebook:cut-cell',
'#copy_cell': 'jupyter-notebook:copy-cell',
'#delete_cell': 'jupyter-notebook:delete-cell',
'#cut_cell': 'jupyter-notebook:cut-selected-cell',
'#copy_cell': 'jupyter-notebook:copy-selected-cell',
'#delete_cell': 'jupyter-notebook:delete-selected-cell',
'#undelete_cell': 'jupyter-notebook:undo-cell-deletion',
'#split_cell': 'jupyter-notebook:split-cell-at-cursor',
'#merge_cell_above': 'jupyter-notebook:merge-cell-with-previous-cell',
Expand Down
83 changes: 70 additions & 13 deletions notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,19 @@ define(function (require) {
};


Notebook.prototype.delete_marked_cells = function(indices) {
indices = this.get_marked_indices();
if(indices.length === 0){
return;
}
this._delete_cells(indices);
};

Notebook.prototype.delete_selected_cell = function(indices) {
indices = [this.get_selected_index()];
this._delete_cells(indices)
};

/**
* Delete cells from the notebook
*
Expand All @@ -956,11 +969,15 @@ define(function (require) {
*/
Notebook.prototype.delete_cells = function(indices) {
if (indices === undefined) {
indices = this.get_marked_indices();

if (indices.length === 0) {
indices = [this.get_selected_index()];
}
indices = [this.get_selected_index()];
}
this._delete_cells(indices);
};


Notebook.prototype._delete_cells = function(indices) {
if(indices === undefined){
throw new Error('need indices to delete cells');
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious, why'd you split delete_cells from _delete_cells?

Copy link
Member Author

Choose a reason for hiding this comment

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

The private one should always raise if no indices are given. The public one can do heuristics, that reduce the number of potential bugs.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

}

this.undelete_backup = [];
Expand Down Expand Up @@ -1030,7 +1047,7 @@ define(function (require) {
if (index === undefined) {
return this.delete_cells();
} else {
return this.delete_cells([index]);
return this._delete_cells([index]);
}
};

Expand Down Expand Up @@ -1381,21 +1398,47 @@ define(function (require) {
}
};


/**
* Cut a cell.
*/
Notebook.prototype.cut_cell = function () {
this.copy_cell();
this.delete_cell();
Notebook.prototype.cut_marked_cells = function () {
this.copy_marked_cells();
this.delete_marked_cells();
};

Notebook.prototype.cut_selected_cell = function () {
this.copy_selected_cell();
this.delete_selected_cell();
};

Notebook.prototype.cut_cell = Notebook.prototype.cut_selected_cell;


/**
* Select
**/

Notebook.prototype.copy_selected_cell = function () {
var cells = [this.get_selected_cell()];
return this._copy_cells(cells);
};

Notebook.prototype.copy_marked_cells = function () {
var cells = this.get_marked_cells();
if(cells.length === 0){
return;
}
return this._copy_cells(cells);
};


/**
* Copy cells.
*/
Notebook.prototype.copy_cell = function () {
var cells = this.get_marked_cells();
Notebook.prototype._copy_cells = function (cells) {
if (cells.length === 0) {
cells = [this.get_selected_cell()];
throw new Error('need to get a cell list')
}

this.clipboard = [];
Expand All @@ -1410,6 +1453,20 @@ define(function (require) {
this.enable_paste();
};

Notebook.prototype.copy_marked_selected_cell = function (cells) {
var cells = this.get_marked_indices();
if (cells.length === 0) {
cells = [this.get_selected_cell()];
}
return this._copy_cells(cells);
};



// TODO: deprecate
Notebook.prototype.copy_cell = Notebook.prototype.copy_marked_selected_cell;


/**
* Replace the selected cell with the cells in the clipboard.
*/
Expand Down Expand Up @@ -1536,7 +1593,7 @@ define(function (require) {
}

// Delete the other cells
this.delete_cells(indices);
this._delete_cells(indices);

this.select(this.find_cell_index(target));
};
Expand Down
3 changes: 3 additions & 0 deletions notebook/static/notebook/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ define([
var action;
if(typeof(el) === 'string'){
action = that.actions.get(el);
if(action === undefined){
throw new Error('Action linked to ' + el + ' does not exists');
}
action_name = el;

}
Expand Down