Skip to content

Commit 51fb117

Browse files
committed
Add toggle settings and backwards compatibility for all keys
1 parent 9bf76fe commit 51fb117

File tree

3 files changed

+120
-47
lines changed

3 files changed

+120
-47
lines changed

src/jupyter_contrib_nbextensions/nbextensions/navigation-hotkeys/hotkeys.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ Link: readme.md
55
Icon: icon.png
66
Main: main.js
77
Compatibility: 4.x, 5.x
8+
Parameters:
9+
- name: toggle_enable_edit_shortcuts
10+
description: Enable all edit shortcuts
11+
input_type: checkbox
12+
default: true
13+
- name: toggle_enable_command_shortcuts
14+
description: Enable all command shortcuts
15+
input_type: checkbox
16+
default: true
17+
- name: toggle_enable_esc_shortcut
18+
description: Enable Esc to enter Edit mode (so Esc toggles Edit/Command mode)
19+
input_type: checkbox
20+
default: true
21+
- name: toggle_enable_enter_shortcuts
22+
description: Enable Enter keys to remain in Edit mode when currently in Edit mode
23+
input_type: checkbox
24+
default: true
25+

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

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ define([
66
], function(Jupyter, $) {
77
"use strict";
88

9+
// define default values for config parameters
10+
var params = {
11+
toggle_enable_edit_shortcuts : true,
12+
toggle_enable_command_shortcuts : true,
13+
toggle_enable_esc_shortcut : true,
14+
toggle_enable_enter_shortcuts : true,
15+
};
16+
17+
// updates default params with any specified in the server's config
18+
var update_params = function() {
19+
var config = Jupyter.notebook.config;
20+
for (var key in params){
21+
if (config.data.hasOwnProperty(key) ){
22+
params[key] = config.data[key];
23+
}
24+
}
25+
};
26+
927
var add_command_shortcuts = {
1028
'home' : {
1129
help : 'Go to top',
@@ -81,7 +99,7 @@ define([
8199
};
82100

83101
var add_edit_shortcuts = {
84-
'alt-=' : {
102+
'alt-subtract' : {
85103
help : 'Merge cell with previous cell',
86104
help_index : 'eb',
87105
handler : function() {
@@ -93,28 +111,6 @@ define([
93111
}
94112
}
95113
},
96-
'shift-enter' : {
97-
help : 'Run cell and select next in edit mode',
98-
help_index : 'bb',
99-
handler : function() {
100-
Jupyter.notebook.execute_cell_and_select_below();
101-
var rendered = Jupyter.notebook.get_selected_cell().rendered;
102-
var ccell = Jupyter.notebook.get_selected_cell().cell_type;
103-
if (rendered === false || ccell === 'code') Jupyter.notebook.edit_mode();
104-
return false;
105-
}
106-
},
107-
'ctrl-enter' : {
108-
help : 'Run selected cell stay in edit mode',
109-
help_index : 'bb',
110-
handler : function() {
111-
var cell = Jupyter.notebook.get_selected_cell();
112-
var mode = cell.mode;
113-
cell.execute();
114-
if (mode === "edit") Jupyter.notebook.edit_mode();
115-
return false;
116-
}
117-
},
118114
'alt-n' : {
119115
help : 'Toggle line numbers',
120116
help_index : 'xy',
@@ -181,37 +177,92 @@ define([
181177
}
182178
};
183179

180+
var add_edit_enter_shortcuts = {
181+
'shift-enter' : {
182+
help : 'Run cell and select next in edit mode',
183+
help_index : 'bb',
184+
handler : function() {
185+
Jupyter.notebook.execute_cell_and_select_below();
186+
var rendered = Jupyter.notebook.get_selected_cell().rendered;
187+
var ccell = Jupyter.notebook.get_selected_cell().cell_type;
188+
if (rendered === false || ccell === 'code') Jupyter.notebook.edit_mode();
189+
return false;
190+
}
191+
},
192+
'ctrl-enter' : {
193+
help : 'Run selected cell stay in edit mode',
194+
help_index : 'bb',
195+
handler : function() {
196+
var cell = Jupyter.notebook.get_selected_cell();
197+
var mode = cell.mode;
198+
cell.execute();
199+
if (mode === "edit") Jupyter.notebook.edit_mode();
200+
return false;
201+
}
202+
}
203+
};
184204

185-
var load_ipython_extension = function() {
205+
var initialize = function() {
206+
// Update default parameters
207+
update_params();
208+
186209
var action;
187210
var prefix = 'navigation_hotkeys';
188211
var action_name;
189212
var action_name_spaces;
190213
var action_full_name;
191214

192-
for (var key in add_command_shortcuts) {
193-
// check if the property/key is defined in the object itself, not in parent
194-
if (add_command_shortcuts.hasOwnProperty(key)) {
195-
action = add_command_shortcuts[key];
196-
action_name_spaces = add_command_shortcuts[key]['help'];
197-
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
198-
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
199-
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(key, action_full_name);
215+
if (params.toggle_enable_command_shortcuts) {
216+
for (var key in add_command_shortcuts) {
217+
// check if the property/key is defined in the object itself, not in parent
218+
if (add_command_shortcuts.hasOwnProperty(key)) {
219+
action = add_command_shortcuts[key];
220+
action_name_spaces = add_command_shortcuts[key]['help'];
221+
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
222+
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
223+
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(
224+
key, action_full_name);
225+
}
226+
};
227+
if (params.toggle_enable_esc_shortcut) {
228+
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(
229+
'Esc','jupyter-notebook:enter-edit-mode');
200230
}
201231
};
202-
for (var key in add_edit_shortcuts) {
203-
// check if the property/key is defined in the object itself, not in parent
204-
if (add_edit_shortcuts.hasOwnProperty(key)) {
205-
action = add_edit_shortcuts[key];
206-
action_name_spaces = add_edit_shortcuts[key]['help'];
207-
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
208-
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
209-
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(key, action_full_name);
210-
}
232+
233+
if (params.toggle_enable_edit_shortcuts) {
234+
for (var key in add_edit_shortcuts) {
235+
// check if the property/key is defined in the object itself, not in parent
236+
if (add_edit_shortcuts.hasOwnProperty(key)) {
237+
action = add_edit_shortcuts[key];
238+
action_name_spaces = add_edit_shortcuts[key]['help'];
239+
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
240+
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
241+
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(key, action_full_name);
242+
}
243+
};
244+
if (params.toggle_enable_enter_shortcuts) {
245+
for (var key in add_edit_enter_shortcuts) {
246+
// check if the property/key is defined in the object itself, not in parent
247+
if (add_edit_enter_shortcuts.hasOwnProperty(key)) {
248+
action = add_edit_enter_shortcuts[key];
249+
action_name_spaces = add_edit_enter_shortcuts[key]['help'];
250+
action_name = action_name_spaces.replace(/ /g,"-").toLowerCase();
251+
action_full_name = Jupyter.keyboard_manager.actions.register(action, action_name, prefix);
252+
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(
253+
key, action_full_name);
254+
}
255+
};
256+
};
257+
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(
258+
'alt-add','jupyter-notebook:split-cell-at-cursor');
211259
};
212260

213261
};
214262

263+
var load_ipython_extension = function() {
264+
return Jupyter.notebook.config.loaded.then(initialize);
265+
};
215266

216267
var extension = {
217268
load_ipython_extension : load_ipython_extension
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
Navigation Hotkeys
22
==================
33

4-
Adds new key combinations for easier notebook navigation.
4+
Adds new key combinations for easier notebook navigation. (*t*) means that key or category you can toggle on/off in the settings.
55

6-
Edit-mode hotkeys:
6+
Edit-mode hotkeys (*t*):
77

88
* `pageup` - scroll page up
99
* `pagedown` - scroll page down
10-
* `Alt`- `+` - Combine cell and keep cursor position
10+
* `Alt`- `Add` - Split cell and keep cursor position (+ on the keypad)
11+
* `Alt`- `Subtract` - Combine cell and keep cursor position (- on the keypad)
1112
* `Alt`-`n` - Toggle line number display in current codecell
12-
* `Shift`-`Enter` - Execute cell, goto next cell and stay in edit mode if next cell is a code cell or unrendered markdown cell
13-
* `Ctrl`-`Enter` - Execute cell and stay in edit mode if cell is a code cell
1413
* `Ctrl`-`y` - toggle celltype between markdown and code
14+
* `Shift`-`Enter` - (*t*) Execute cell, goto next cell and stay in edit mode if next cell is a code cell or unrendered markdown cell
15+
* `Ctrl`-`Enter` - (*t*) Execute cell and stay in edit mode if cell is a code cell
1516

16-
Command-mode hotkeys:
17+
Command-mode hotkeys (*t*):
1718

19+
* `esc` - (*t*) toggle to edit mode
1820
* `home` - Go to top of notebook
1921
* `end` - Go to bottom of notebook
2022
* `pageup` - scroll page up
2123
* `pagedown` - scroll page down
24+
25+

0 commit comments

Comments
 (0)