Skip to content

Commit 62f7dd2

Browse files
committed
Update Navigation Keys to new Jupyter register and shortcut commands
1 parent 03109e4 commit 62f7dd2

File tree

2 files changed

+94
-65
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/navigation-hotkeys

2 files changed

+94
-65
lines changed

src/jupyter_contrib_nbextensions/nbextensions/navigation-hotkeys/main.js

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,84 @@
33
define([
44
'base/js/namespace',
55
'jquery'
6-
], function(IPython, $) {
6+
], function(Jupyter, $) {
77
"use strict";
88

99
var add_command_shortcuts = {
1010
'esc' : {
11-
help : 'edit mode',
11+
help : 'Enter edit mode',
1212
help_index : 'aa',
1313
handler : function() {
14-
IPython.notebook.edit_mode();
14+
Jupyter.notebook.edit_mode();
1515
return false;
1616
}
1717
},
1818

1919
'home' : {
20-
help : 'go to top',
20+
help : 'Go to top',
2121
help_index : 'ga',
2222
handler : function() {
23-
IPython.notebook.select(0);
24-
IPython.notebook.scroll_to_top();
23+
Jupyter.notebook.select(0);
24+
Jupyter.notebook.scroll_to_top();
2525
return false;
2626
}
2727
},
2828

2929
'end' : {
30-
help : 'go to bottom',
30+
help : 'Go to bottom',
3131
help_index : 'ga',
3232
handler : function() {
33-
var ncells = IPython.notebook.ncells();
34-
IPython.notebook.select(ncells-1);
35-
IPython.notebook.scroll_to_bottom();
33+
var ncells = Jupyter.notebook.ncells();
34+
Jupyter.notebook.select(ncells-1);
35+
Jupyter.notebook.scroll_to_bottom();
3636
return false;
3737
}
3838
},
3939

4040
'pageup' : {
41-
help : 'page up',
41+
help : 'Move page up',
4242
help_index : 'aa',
4343
handler : function() {
4444
var wh = 0.6 * $(window).height();
45-
var cell = IPython.notebook.get_selected_cell();
45+
var cell = Jupyter.notebook.get_selected_cell();
4646
var h = 0;
4747
/* loop until we have enough cells to span the size of the notebook window (= one page) */
4848
do {
4949
h += cell.element.height();
50-
IPython.notebook.select_prev();
51-
cell = IPython.notebook.get_selected_cell();
50+
Jupyter.notebook.select_prev();
51+
cell = Jupyter.notebook.get_selected_cell();
5252
} while ( h < wh );
5353
var cp = cell.element.position();
5454
var sp = $('body').scrollTop();
5555
if ( cp.top < sp) {
56-
IPython.notebook.scroll_to_cell(IPython.notebook.get_selected_index(), 0);
56+
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index(), 0);
5757
}
5858
cell.focus_cell();
5959
return false;
6060
}
6161
},
6262

6363
'pagedown' : {
64-
help : 'page down',
64+
help : 'Move page down',
6565
help_index : 'aa',
6666
handler : function() {
6767

6868
/* jump to bottom if we are already in the last cell */
69-
var ncells = IPython.notebook.ncells();
70-
if ( IPython.notebook.get_selected_index()+1 == ncells) {
71-
IPython.notebook.scroll_to_bottom();
69+
var ncells = Jupyter.notebook.ncells();
70+
if ( Jupyter.notebook.get_selected_index()+1 == ncells) {
71+
Jupyter.notebook.scroll_to_bottom();
7272
return false;
7373
}
7474

7575
var wh = 0.6*$(window).height();
76-
var cell = IPython.notebook.get_selected_cell();
76+
var cell = Jupyter.notebook.get_selected_cell();
7777
var h = 0;
7878

7979
/* loop until we have enough cells to span the size of the notebook window (= one page) */
8080
do {
8181
h += cell.element.height();
82-
IPython.notebook.select_next();
83-
cell = IPython.notebook.get_selected_cell();
82+
Jupyter.notebook.select_next();
83+
cell = Jupyter.notebook.get_selected_cell();
8484
} while ( h < wh );
8585
cell.focus_cell();
8686
return false;
@@ -90,119 +90,148 @@ define([
9090
};
9191

9292
var add_edit_shortcuts = {
93-
'alt-add' : {
94-
help : 'split cell',
93+
'alt-minus' : {
94+
help : 'Split cell at cursor',
9595
help_index : 'eb',
9696
handler : function() {
97-
IPython.notebook.split_cell();
98-
IPython.notebook.edit_mode();
97+
Jupyter.notebook.edit_mode();
98+
Jupyter.notebook.split_cell();
99+
Jupyter.notebook.edit_mode();
99100
return false;
100101
}
101102
},
102-
'alt-subtract' : {
103-
help : 'merge cell',
103+
'alt-=' : {
104+
help : 'Merge cell with previous cell',
104105
help_index : 'eb',
105106
handler : function() {
106-
var i = IPython.notebook.get_selected_index();
107+
var i = Jupyter.notebook.get_selected_index();
107108
if (i > 0) {
108-
var l = IPython.notebook.get_cell(i-1).code_mirror.lineCount();
109-
IPython.notebook.merge_cell_above();
110-
IPython.notebook.get_selected_cell().code_mirror.setCursor(l,0);
109+
var l = Jupyter.notebook.get_cell(i-1).code_mirror.lineCount();
110+
Jupyter.notebook.merge_cell_above();
111+
Jupyter.notebook.get_selected_cell().code_mirror.setCursor(l,0);
111112
}
112113
}
113114
},
114115
'shift-enter' : {
115-
help : 'run cell, select next codecell',
116+
help : 'Run cell and select next in edit mode',
116117
help_index : 'bb',
117118
handler : function() {
118-
IPython.notebook.execute_cell_and_select_below();
119-
var rendered = IPython.notebook.get_selected_cell().rendered;
120-
var ccell = IPython.notebook.get_selected_cell().cell_type;
121-
if (rendered === false || ccell === 'code') IPython.notebook.edit_mode();
119+
Jupyter.notebook.execute_cell_and_select_below();
120+
var rendered = Jupyter.notebook.get_selected_cell().rendered;
121+
var ccell = Jupyter.notebook.get_selected_cell().cell_type;
122+
if (rendered === false || ccell === 'code') Jupyter.notebook.edit_mode();
122123
return false;
123124
}
124125
},
125126
'ctrl-enter' : {
126-
help : 'run cell',
127+
help : 'Run selected cell stay in edit mode',
127128
help_index : 'bb',
128129
handler : function() {
129-
var cell = IPython.notebook.get_selected_cell();
130+
var cell = Jupyter.notebook.get_selected_cell();
130131
var mode = cell.mode;
131132
cell.execute();
132-
if (mode === "edit") IPython.notebook.edit_mode();
133+
if (mode === "edit") Jupyter.notebook.edit_mode();
133134
return false;
134135
}
135136
},
136137
'alt-n' : {
137-
help : 'toggle line numbers',
138+
help : 'Toggle line numbers',
138139
help_index : 'xy',
139140
handler : function() {
140-
var cell = IPython.notebook.get_selected_cell();
141+
var cell = Jupyter.notebook.get_selected_cell();
141142
cell.toggle_line_numbers();
142143
return false;
143144
}
144145
},
145146
'pagedown' : {
146-
help : 'page down',
147+
help : 'Page down',
147148
help_index : 'xy',
148149
handler : function() {
149150

150-
var ic = IPython.notebook.get_selected_index();
151-
var cells = IPython.notebook.get_cells();
151+
var ic = Jupyter.notebook.get_selected_index();
152+
var cells = Jupyter.notebook.get_cells();
152153
var i, h=0;
153154
for (i=0; i < ic; i ++) {
154155
h += cells[i].element.height();
155156
}
156157
var cur = cells[ic].code_mirror.getCursor();
157158
h += cells[ic].code_mirror.defaultTextHeight() * cur.line;
158-
IPython.notebook.element.animate({scrollTop:h}, 0);
159+
Jupyter.notebook.element.animate({scrollTop:h}, 0);
159160
return false;
160161
}
161162
},
162163
'pageup' : {
163-
help : 'page down',
164+
help : 'Page up',
164165
help_index : 'xy',
165166
handler : function() {
166167

167-
var ic = IPython.notebook.get_selected_index();
168-
var cells = IPython.notebook.get_cells();
168+
var ic = Jupyter.notebook.get_selected_index();
169+
var cells = Jupyter.notebook.get_cells();
169170
var i, h=0;
170171
for (i=0; i < ic; i ++) {
171172
h += cells[i].element.height();
172173
}
173174
var cur =cells[ic].code_mirror.getCursor();
174175
h += cells[ic].code_mirror.defaultTextHeight() * cur.line;
175-
IPython.notebook.element.animate({scrollTop:h}, 0);
176+
Jupyter.notebook.element.animate({scrollTop:h}, 0);
176177
return false;
177178
}
178179
},
179180
'ctrl-y' : {
180-
help : 'toggle markdown/code',
181+
help : 'Toggle markdown/code',
181182
handler : function() {
182-
var cell = IPython.notebook.get_selected_cell();
183+
var cell = Jupyter.notebook.get_selected_cell();
183184
var cur = cell.code_mirror.getCursor();
184185
if (cell.cell_type == 'code') {
185-
IPython.notebook.command_mode();
186-
IPython.notebook.to_markdown();
187-
IPython.notebook.edit_mode();
188-
cell = IPython.notebook.get_selected_cell();
186+
Jupyter.notebook.command_mode();
187+
Jupyter.notebook.to_markdown();
188+
Jupyter.notebook.edit_mode();
189+
cell = Jupyter.notebook.get_selected_cell();
189190
cell.code_mirror.setCursor(cur);
190191
} else if (cell.cell_type == 'markdown') {
191-
IPython.notebook.command_mode();
192-
IPython.notebook.to_code();
193-
IPython.notebook.edit_mode();
194-
cell = IPython.notebook.get_selected_cell();
192+
Jupyter.notebook.command_mode();
193+
Jupyter.notebook.to_code();
194+
Jupyter.notebook.edit_mode();
195+
cell = Jupyter.notebook.get_selected_cell();
195196
cell.code_mirror.setCursor(cur);
196197
}
197198
return false;
198199
}
199200
}
200201
};
201202

203+
202204
var load_ipython_extension = function() {
203-
IPython.keyboard_manager.edit_shortcuts.add_shortcuts(add_edit_shortcuts);
204-
IPython.keyboard_manager.command_shortcuts.add_shortcuts(add_command_shortcuts);
205+
var action;
206+
var prefix = 'navigation_hotkeys';
207+
var action_name;
208+
var action_name_spaces;
209+
var action_full_name;
210+
211+
for (var key in add_command_shortcuts) {
212+
// check if the property/key is defined in the object itself, not in parent
213+
if (add_command_shortcuts.hasOwnProperty(key)) {
214+
action = add_command_shortcuts[key];
215+
action_name_spaces = add_command_shortcuts[key]['help'];
216+
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
217+
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
218+
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(key, action_full_name);
219+
}
220+
};
221+
for (var key in add_edit_shortcuts) {
222+
// check if the property/key is defined in the object itself, not in parent
223+
if (add_edit_shortcuts.hasOwnProperty(key)) {
224+
action = add_edit_shortcuts[key];
225+
action_name_spaces = add_edit_shortcuts[key]['help'];
226+
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
227+
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
228+
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(key, action_full_name);
229+
}
230+
};
231+
205232
};
233+
234+
206235
var extension = {
207236
load_ipython_extension : load_ipython_extension
208237
};

src/jupyter_contrib_nbextensions/nbextensions/navigation-hotkeys/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Edit-mode hotkeys:
77

88
* `pageup` - scroll page up
99
* `pagedown` - scroll page down
10-
* `Alt`- `+` - Split cell and keep cursor position
11-
* `Alt`- `-` - Combine cell and keep cursor position
10+
* `Alt`- `-` - Split cell and keep cursor position
11+
* `Alt`- `+` - Combine cell and keep cursor position
1212
* `Alt`-`n` - Toggle line number display in current codecell
13-
* `Shift`-`Enter` - Execute cell, goto next cell and stay in edit mode if next cell is a code cell or unredered markdown cell
13+
* `Shift`-`Enter` - Execute cell, goto next cell and stay in edit mode if next cell is a code cell or unrendered markdown cell
1414
* `Ctrl`-`Enter` - Execute cell and stay in edit mode if cell is a code cell
1515
* `Ctrl`-`y` - toggle celltype between markdown and code
1616

0 commit comments

Comments
 (0)