Skip to content
New issue

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

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

Already on GitHub? # to your account

[move_selected_cells] updated to Jupyter 4.2+ #860

Merged
merged 4 commits into from
Feb 5, 2017
Merged
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Move selected cells

This is a quick (and dirty) extension - move up or down several selected cell*s*. Moving cells or series of cells via simple keystrokes can be super useful.
This is a quick (and dirty) extension - move up or down several selected cell*s*. Moving cells or series of cells via simple keystrokes can be super useful.
Note: Alternatively, it is now possible to use the `keyboard_shortcut_editor` to bind the move cell up & move cell down actions to Alt-up and Alt-down (or anything else).

Initial version for Jupyter 4.0: a bit dirty because it would be better to act on DOM elements and write a correct move_cells() function.
New version, updated to Jupyter 4.2+, now takes advantage of `Jupyter.notebook.move_selection_{down, up}` new functions

It is a bit dirty because it would be better to act on DOM elements and write a correct move_cells() function.

Cautionary note: It is very probable that such functionality will be available shortly in the official Jupyter notebook. But in the meantime, it could be useful to some people.

Keyboard shortcuts: *Alt-up* and *Alt-down* (works also with single cells!)

**Cell selection**: Cells can be selected using the rubberband (required extension) or via Shift-J and Shift-K
**Cell selection**: Cells can be selected using the rubberband (if this extension is enabled) or via Shift-up/Shift-down or Shift-K/Shift-J
Original file line number Diff line number Diff line change
@@ -1,68 +1,87 @@
// Copyright (c) IPython-Contrib Team.
// Copyright (c) Jupyter-Contrib Team.
// Distributed under the terms of the Modified BSD License.

// This is a quick (and dirty) extension - move up or down several selected cells
// Dirty because it would be better to act on dom elements and write a correct move_cells() function
// Dirty because it would be better to act on dom elements and write a correct
// move_cells() function.
// Updated to Jupyter 4.2+, taking advantage of
// `Jupyter.notebook.move_selection_{down, up}` new functions
//
// Keyboard shortcuts: Alt-up and Alt-down (works with single cells also -- this is useful!)
// Cells can be selected using the rubberband or via Shift-J and Shift-K
// Cells can be selected using the rubberband (needs rubberband extension) or via Shift-up/Shift-down or Shift-K/Shift-J


define([
'base/js/namespace',
'jquery',
'require',
'base/js/events',
'nbextensions/rubberband/main'
], function(IPython, $, require, events, rubberband) {
'base/js/events'
], function(Jupyter, $, require, events, rubberband) {
"use strict";
var add_cmd_shortcuts = {
'Alt-down': {
help: 'Move selected cells down',
help_index: 'ht',
handler: function(event) {
var ncells = IPython.notebook.ncells();
var s = IPython.notebook.get_selected_indices();
//ensure cells indices are reverse sorted
var ss = s.sort(function(x, y) {return x - y}).reverse();
if (ss[0] + 1 < ncells) {
for (var k in ss) {
IPython.notebook.move_cell_down(ss[k]);
}; //The second loop is needed because move_cell deselect
for (var k in ss) {
IPython.notebook.get_cell(ss[k] + 1).select();
}
if (parseFloat(Jupyter.version.substr(0, 3)) >= 4.2) {
var add_cmd_shortcuts = {
'Alt-down': {
help: 'Move selected cells down',
help_index: 'ht',
handler: function() { Jupyter.notebook.move_selection_down() }
},
'Alt-up': {
help: 'Move selected cells up',
help_index: 'ht',
handler: function() { Jupyter.notebook.move_selection_up() }
}
}
},
'Alt-up': {
help: 'Move selected cells up',
help_index: 'ht',
handler: function(event) {
var s = IPython.notebook.get_selected_indices();
//ensure cells indices are sorted
var ss = s.sort(function(x, y) {return x - y});
if (ss[0] - 1 >= 0) {
for (var k in ss) {
IPython.notebook.move_cell_up(ss[k]);
};
for (var k in ss) {
IPython.notebook.get_cell(ss[k] - 1).select();

} else { // Jupyter version < 4.2
var add_cmd_shortcuts = {
'Alt-down': {
help: 'Move selected cells down',
help_index: 'ht',
handler: function(event) {
var ncells = Jupyter.notebook.ncells();
var s = Jupyter.notebook.get_selected_indices();
//ensure cells indices are reverse sorted
var ss = s.sort(function(x, y) {
return x - y }).reverse();
if (ss[0] + 1 < ncells) {
for (var k in ss) {
Jupyter.notebook.move_cell_down(ss[k]);
}; //The second loop is needed because move_cell deselect
for (var k in ss) {
Jupyter.notebook.get_cell(ss[k] + 1).select();
}
}
}
},
'Alt-up': {
help: 'Move selected cells up',
help_index: 'ht',
handler: function(event) {
var s = Jupyter.notebook.get_selected_indices();
//ensure cells indices are sorted
var ss = s.sort(function(x, y) {
return x - y });
if (ss[0] - 1 >= 0) {
for (var k in ss) {
Jupyter.notebook.move_cell_up(ss[k]);
};
for (var k in ss) {
Jupyter.notebook.get_cell(ss[k] - 1).select();
}
}
}
}
}
}
}

Jupyter.keyboard_manager.command_shortcuts.add_shortcuts(add_cmd_shortcuts);

IPython.keyboard_manager.command_shortcuts.add_shortcuts(add_cmd_shortcuts);

function load_ipython_extension(){
//console.log("Executing rubberband load_ipython");
rubberband.load_ipython_extension();
}
function load_ipython_extension() {
console.log("[move_selected_cells] loaded")
}

return {
load_ipython_extension: load_ipython_extension,
};

});