Show More
@@ -1,282 +1,282 | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2011 The IPython Development Team |
|
2 | // Copyright (C) 2011 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // Keyboard management |
|
9 | // Keyboard management | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | var IPython = (function (IPython) { |
|
12 | var IPython = (function (IPython) { | |
13 | "use strict"; |
|
13 | "use strict"; | |
14 |
|
14 | |||
15 | var key = IPython.utils.keycodes; |
|
15 | var key = IPython.utils.keycodes; | |
16 |
|
16 | |||
17 | var KeyboardManager = function () { |
|
17 | var KeyboardManager = function () { | |
18 | this.mode = 'command'; |
|
18 | this.mode = 'command'; | |
19 | this.enabled = true; |
|
19 | this.enabled = true; | |
20 | this.bind_events(); |
|
20 | this.bind_events(); | |
21 | }; |
|
21 | }; | |
22 |
|
22 | |||
23 | KeyboardManager.prototype.bind_events = function () { |
|
23 | KeyboardManager.prototype.bind_events = function () { | |
24 | var that = this; |
|
24 | var that = this; | |
25 | $(document).keydown(function (event) { |
|
25 | $(document).keydown(function (event) { | |
26 | return that.handle_keydown(event); |
|
26 | return that.handle_keydown(event); | |
27 | }); |
|
27 | }); | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | KeyboardManager.prototype.handle_keydown = function (event) { |
|
30 | KeyboardManager.prototype.handle_keydown = function (event) { | |
31 | var notebook = IPython.notebook; |
|
31 | var notebook = IPython.notebook; | |
32 |
|
32 | |||
33 | console.log('keyboard_manager', this.mode, event.keyCode); |
|
33 | console.log('keyboard_manager', this.mode, event.keyCode); | |
34 |
|
34 | |||
35 | if (event.which === key.ESC) { |
|
35 | if (event.which === key.ESC) { | |
36 | // Intercept escape at highest level to avoid closing |
|
36 | // Intercept escape at highest level to avoid closing | |
37 | // websocket connection with firefox |
|
37 | // websocket connection with firefox | |
38 | event.preventDefault(); |
|
38 | event.preventDefault(); | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | if (!this.enabled) { |
|
41 | if (!this.enabled) { | |
42 | return true; |
|
42 | return true; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | // Event handlers for both command and edit mode |
|
45 | // Event handlers for both command and edit mode | |
46 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { |
|
46 | if ((event.ctrlKey || event.metaKey) && event.keyCode==83) { | |
47 | // Save (CTRL+S) or (Command+S on Mac) |
|
47 | // Save (CTRL+S) or (Command+S on Mac) | |
48 | notebook.save_checkpoint(); |
|
48 | notebook.save_checkpoint(); | |
49 | event.preventDefault(); |
|
49 | event.preventDefault(); | |
50 | return false; |
|
50 | return false; | |
51 | } else if (event.which === key.ESC) { |
|
51 | } else if (event.which === key.ESC) { | |
52 | // Intercept escape at highest level to avoid closing |
|
52 | // Intercept escape at highest level to avoid closing | |
53 | // websocket connection with firefox |
|
53 | // websocket connection with firefox | |
54 | event.preventDefault(); |
|
54 | event.preventDefault(); | |
55 | // Don't return yet to allow edit/command modes to handle |
|
55 | // Don't return yet to allow edit/command modes to handle | |
56 | } else if (event.which === key.SHIFT) { |
|
56 | } else if (event.which === key.SHIFT) { | |
57 | // ignore shift keydown |
|
57 | // ignore shift keydown | |
58 | return true; |
|
58 | return true; | |
59 | } else if (event.which === key.ENTER && event.shiftKey) { |
|
59 | } else if (event.which === key.ENTER && event.shiftKey) { | |
60 | notebook.execute_selected_cell('shift'); |
|
60 | notebook.execute_selected_cell('shift'); | |
61 | return false; |
|
61 | return false; | |
62 | } else if (event.which === key.ENTER && event.altKey) { |
|
62 | } else if (event.which === key.ENTER && event.altKey) { | |
63 | // Execute code cell, and insert new in place |
|
63 | // Execute code cell, and insert new in place | |
64 | notebook.execute_selected_cell('alt'); |
|
64 | notebook.execute_selected_cell('alt'); | |
65 | return false; |
|
65 | return false; | |
66 | } else if (event.which === key.ENTER && event.ctrlKey) { |
|
66 | } else if (event.which === key.ENTER && event.ctrlKey) { | |
67 | notebook.execute_selected_cell('ctrl'); |
|
67 | notebook.execute_selected_cell('ctrl'); | |
68 | return false; |
|
68 | return false; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | if (this.mode === 'edit') { |
|
71 | if (this.mode === 'edit') { | |
72 | return this.handle_edit_mode(event); |
|
72 | return this.handle_edit_mode(event); | |
73 | } else if (this.mode === 'command' && !(event.ctrlKey || event.altKey || event.metaKey)) { |
|
73 | } else if (this.mode === 'command' && !(event.ctrlKey || event.altKey || event.metaKey)) { | |
74 | return this.handle_command_mode(event); |
|
74 | return this.handle_command_mode(event); | |
75 | } |
|
75 | } | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | KeyboardManager.prototype.handle_edit_mode = function (event) { |
|
78 | KeyboardManager.prototype.handle_edit_mode = function (event) { | |
79 | var notebook = IPython.notebook; |
|
79 | var notebook = IPython.notebook; | |
80 |
|
80 | |||
81 | if (event.which === key.ESC) { |
|
81 | if (event.which === key.ESC) { | |
82 | // ESC |
|
82 | // ESC | |
83 | notebook.command_mode(); |
|
83 | notebook.command_mode(); | |
84 | return false; |
|
84 | return false; | |
85 | } else if (event.which === 77 && event.ctrlKey) { |
|
85 | } else if (event.which === 77 && event.ctrlKey) { | |
86 | // Ctrl-m |
|
86 | // Ctrl-m | |
87 | notebook.command_mode(); |
|
87 | notebook.command_mode(); | |
88 | return false; |
|
88 | return false; | |
89 | } else if (event.which === key.UPARROW && !event.shiftKey) { |
|
89 | } else if (event.which === key.UPARROW && !event.shiftKey) { | |
90 | var cell = notebook.get_selected_cell(); |
|
90 | var cell = notebook.get_selected_cell(); | |
91 | if (cell && cell.at_top()) { |
|
91 | if (cell && cell.at_top()) { | |
92 | event.preventDefault(); |
|
92 | event.preventDefault(); | |
93 | notebook.command_mode() |
|
93 | notebook.command_mode() | |
94 | notebook.select_prev(); |
|
94 | notebook.select_prev(); | |
95 | notebook.edit_mode(); |
|
95 | notebook.edit_mode(); | |
96 | return false; |
|
96 | return false; | |
97 | }; |
|
97 | }; | |
98 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { |
|
98 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { | |
99 | var cell = notebook.get_selected_cell(); |
|
99 | var cell = notebook.get_selected_cell(); | |
100 | if (cell && cell.at_bottom()) { |
|
100 | if (cell && cell.at_bottom()) { | |
101 | event.preventDefault(); |
|
101 | event.preventDefault(); | |
102 | notebook.command_mode() |
|
102 | notebook.command_mode() | |
103 | notebook.select_next(); |
|
103 | notebook.select_next(); | |
104 | notebook.edit_mode(); |
|
104 | notebook.edit_mode(); | |
105 | return false; |
|
105 | return false; | |
106 | }; |
|
106 | }; | |
107 | }; |
|
107 | }; | |
108 | return true; |
|
108 | return true; | |
109 | } |
|
109 | } | |
110 |
|
110 | |||
111 | KeyboardManager.prototype.handle_command_mode = function (event) { |
|
111 | KeyboardManager.prototype.handle_command_mode = function (event) { | |
112 | var notebook = IPython.notebook; |
|
112 | var notebook = IPython.notebook; | |
113 |
|
113 | |||
114 | if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) { |
|
114 | if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) { | |
115 | // Enter edit mode = ENTER alone |
|
115 | // Enter edit mode = ENTER alone | |
116 | notebook.edit_mode(); |
|
116 | notebook.edit_mode(); | |
117 | return false; |
|
117 | return false; | |
118 | } else if (event.which === key.UPARROW && !event.shiftKey) { |
|
118 | } else if (event.which === key.UPARROW && !event.shiftKey) { | |
119 | var index = notebook.get_selected_index(); |
|
119 | var index = notebook.get_selected_index(); | |
120 | if (index !== 0 && index !== null) { |
|
120 | if (index !== 0 && index !== null) { | |
121 | notebook.select_prev(); |
|
121 | notebook.select_prev(); | |
122 | var cell = notebook.get_selected_cell(); |
|
122 | var cell = notebook.get_selected_cell(); | |
123 | cell.focus_cell(); |
|
123 | cell.focus_cell(); | |
124 | }; |
|
124 | }; | |
125 | return false; |
|
125 | return false; | |
126 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { |
|
126 | } else if (event.which === key.DOWNARROW && !event.shiftKey) { | |
127 | var index = notebook.get_selected_index(); |
|
127 | var index = notebook.get_selected_index(); | |
128 | if (index !== (notebook.ncells()-1) && index !== null) { |
|
128 | if (index !== (notebook.ncells()-1) && index !== null) { | |
129 | notebook.select_next(); |
|
129 | notebook.select_next(); | |
130 | var cell = notebook.get_selected_cell(); |
|
130 | var cell = notebook.get_selected_cell(); | |
131 | cell.focus_cell(); |
|
131 | cell.focus_cell(); | |
132 | }; |
|
132 | }; | |
133 | return false; |
|
133 | return false; | |
134 | } else if (event.which === 88) { |
|
134 | } else if (event.which === 88) { | |
135 | // Cut selected cell = x |
|
135 | // Cut selected cell = x | |
136 | notebook.cut_cell(); |
|
136 | notebook.cut_cell(); | |
137 | return false; |
|
137 | return false; | |
138 | } else if (event.which === 67) { |
|
138 | } else if (event.which === 67) { | |
139 | // Copy selected cell = c |
|
139 | // Copy selected cell = c | |
140 | notebook.copy_cell(); |
|
140 | notebook.copy_cell(); | |
141 | return false; |
|
141 | return false; | |
142 | } else if (event.which === 86) { |
|
142 | } else if (event.which === 86) { | |
143 | // Paste below selected cell = v |
|
143 | // Paste below selected cell = v | |
144 | notebook.paste_cell_below(); |
|
144 | notebook.paste_cell_below(); | |
145 | return false; |
|
145 | return false; | |
146 | } else if (event.which === 68) { |
|
146 | } else if (event.which === 68) { | |
147 | // Delete selected cell = d |
|
147 | // Delete selected cell = d | |
148 | notebook.delete_cell(); |
|
148 | notebook.delete_cell(); | |
149 | return false; |
|
149 | return false; | |
150 | } else if (event.which === 65) { |
|
150 | } else if (event.which === 65) { | |
151 | // Insert code cell above selected = a |
|
151 | // Insert code cell above selected = a | |
152 | notebook.insert_cell_above('code'); |
|
152 | notebook.insert_cell_above('code'); | |
153 | notebook.select_prev(); |
|
153 | notebook.select_prev(); | |
154 | return false; |
|
154 | return false; | |
155 | } else if (event.which === 66) { |
|
155 | } else if (event.which === 66) { | |
156 | // Insert code cell below selected = b |
|
156 | // Insert code cell below selected = b | |
157 | notebook.insert_cell_below('code'); |
|
157 | notebook.insert_cell_below('code'); | |
158 | notebook.select_next(); |
|
158 | notebook.select_next(); | |
159 | return false; |
|
159 | return false; | |
160 | } else if (event.which === 89) { |
|
160 | } else if (event.which === 89) { | |
161 | // To code = y |
|
161 | // To code = y | |
162 | notebook.to_code(); |
|
162 | notebook.to_code(); | |
163 | return false; |
|
163 | return false; | |
164 | } else if (event.which === 77) { |
|
164 | } else if (event.which === 77) { | |
165 | // To markdown = m |
|
165 | // To markdown = m | |
166 | notebook.to_markdown(); |
|
166 | notebook.to_markdown(); | |
167 | return false; |
|
167 | return false; | |
168 | } else if (event.which === 84) { |
|
168 | } else if (event.which === 84) { | |
169 | // To Raw = t |
|
169 | // To Raw = t | |
170 | notebook.to_raw(); |
|
170 | notebook.to_raw(); | |
171 | return false; |
|
171 | return false; | |
172 | } else if (event.which === 49) { |
|
172 | } else if (event.which === 49) { | |
173 | // To Heading 1 = 1 |
|
173 | // To Heading 1 = 1 | |
174 | notebook.to_heading(undefined, 1); |
|
174 | notebook.to_heading(undefined, 1); | |
175 | return false; |
|
175 | return false; | |
176 | } else if (event.which === 50) { |
|
176 | } else if (event.which === 50) { | |
177 | // To Heading 2 = 2 |
|
177 | // To Heading 2 = 2 | |
178 | notebook.to_heading(undefined, 2); |
|
178 | notebook.to_heading(undefined, 2); | |
179 | return false; |
|
179 | return false; | |
180 | } else if (event.which === 51) { |
|
180 | } else if (event.which === 51) { | |
181 | // To Heading 3 = 3 |
|
181 | // To Heading 3 = 3 | |
182 | notebook.to_heading(undefined, 3); |
|
182 | notebook.to_heading(undefined, 3); | |
183 | return false; |
|
183 | return false; | |
184 | } else if (event.which === 52) { |
|
184 | } else if (event.which === 52) { | |
185 | // To Heading 4 = 4 |
|
185 | // To Heading 4 = 4 | |
186 | notebook.to_heading(undefined, 4); |
|
186 | notebook.to_heading(undefined, 4); | |
187 | return false; |
|
187 | return false; | |
188 | } else if (event.which === 53) { |
|
188 | } else if (event.which === 53) { | |
189 | // To Heading 5 = 5 |
|
189 | // To Heading 5 = 5 | |
190 | notebook.to_heading(undefined, 5); |
|
190 | notebook.to_heading(undefined, 5); | |
191 | return false; |
|
191 | return false; | |
192 | } else if (event.which === 54) { |
|
192 | } else if (event.which === 54) { | |
193 | // To Heading 6 = 6 |
|
193 | // To Heading 6 = 6 | |
194 | notebook.to_heading(undefined, 6); |
|
194 | notebook.to_heading(undefined, 6); | |
195 | return false; |
|
195 | return false; | |
196 | } else if (event.which === 79) { |
|
196 | } else if (event.which === 79) { | |
197 | // Toggle output = o |
|
197 | // Toggle output = o | |
198 | if (event.shiftKey) { |
|
198 | if (event.shiftKey) { | |
199 | notebook.toggle_output_scroll(); |
|
199 | notebook.toggle_output_scroll(); | |
200 | } else { |
|
200 | } else { | |
201 | notebook.toggle_output(); |
|
201 | notebook.toggle_output(); | |
202 | }; |
|
202 | }; | |
203 | return false; |
|
203 | return false; | |
204 | } else if (event.which === 83) { |
|
204 | } else if (event.which === 83) { | |
205 | // Save notebook = s |
|
205 | // Save notebook = s | |
206 | notebook.save_checkpoint(); |
|
206 | notebook.save_checkpoint(); | |
207 | return false; |
|
207 | return false; | |
208 | } else if (event.which === 74) { |
|
208 | } else if (event.which === 74) { | |
209 | // Move cell down = j |
|
209 | // Move cell down = j | |
210 | notebook.move_cell_down(); |
|
210 | notebook.move_cell_down(); | |
211 | return false; |
|
211 | return false; | |
212 | } else if (event.which === 75) { |
|
212 | } else if (event.which === 75) { | |
213 | // Move cell up = k |
|
213 | // Move cell up = k | |
214 | notebook.move_cell_up(); |
|
214 | notebook.move_cell_up(); | |
215 | return false; |
|
215 | return false; | |
216 | } else if (event.which === 80) { |
|
216 | } else if (event.which === 80) { | |
217 | // Select previous = p |
|
217 | // Select previous = p | |
218 | notebook.select_prev(); |
|
218 | notebook.select_prev(); | |
219 | return false; |
|
219 | return false; | |
220 | } else if (event.which === 78) { |
|
220 | } else if (event.which === 78) { | |
221 | // Select next = n |
|
221 | // Select next = n | |
222 | notebook.select_next(); |
|
222 | notebook.select_next(); | |
223 | return false; |
|
223 | return false; | |
224 | } else if (event.which === 76) { |
|
224 | } else if (event.which === 76) { | |
225 | // Toggle line numbers = l |
|
225 | // Toggle line numbers = l | |
226 | notebook.cell_toggle_line_numbers(); |
|
226 | notebook.cell_toggle_line_numbers(); | |
227 | return false; |
|
227 | return false; | |
228 | } else if (event.which === 73) { |
|
228 | } else if (event.which === 73) { | |
229 | // Interrupt kernel = i |
|
229 | // Interrupt kernel = i | |
230 | notebook.kernel.interrupt(); |
|
230 | notebook.kernel.interrupt(); | |
231 | return false; |
|
231 | return false; | |
232 | } else if (event.which === 190) { |
|
232 | } else if (event.which === 190) { | |
233 | // Restart kernel = . # matches qt console |
|
233 | // Restart kernel = . # matches qt console | |
234 | notebook.restart_kernel(); |
|
234 | notebook.restart_kernel(); | |
235 | return false; |
|
235 | return false; | |
236 | } else if (event.which === 72) { |
|
236 | } else if (event.which === 72) { | |
237 | // Show keyboard shortcuts = h |
|
237 | // Show keyboard shortcuts = h | |
238 | IPython.quick_help.show_keyboard_shortcuts(); |
|
238 | IPython.quick_help.show_keyboard_shortcuts(); | |
239 | return false; |
|
239 | return false; | |
240 | } else if (event.which === 90) { |
|
240 | } else if (event.which === 90) { | |
241 | // Undo last cell delete = z |
|
241 | // Undo last cell delete = z | |
242 | notebook.undelete(); |
|
242 | notebook.undelete(); | |
243 | return false; |
|
243 | return false; | |
244 | } else if (event.which === 189 || event.which === 173) { |
|
244 | } else if (event.which === 189 || event.which === 173) { | |
245 | // how fun! '-' is 189 in Chrome, but 173 in FF and Opera |
|
245 | // how fun! '-' is 189 in Chrome, but 173 in FF and Opera | |
246 | // Split cell = - |
|
246 | // Split cell = - | |
247 | notebook.split_cell(); |
|
247 | notebook.split_cell(); | |
248 | return false; |
|
248 | return false; | |
249 | } else if ((event.which === 61 || event.which === 187) && event.shiftKey) { |
|
249 | } else if ((event.which === 61 || event.which === 187) && event.shiftKey) { | |
250 | notebook.merge_cell_below(); |
|
250 | notebook.merge_cell_below(); | |
251 | return false; |
|
251 | return false; | |
252 | }; |
|
252 | }; | |
253 | // If we havn't handled it, let someone else. |
|
253 | // If we havn't handled it, let someone else. | |
254 | return true; |
|
254 | return true; | |
255 | }; |
|
255 | }; | |
256 |
|
256 | |||
257 | KeyboardManager.prototype.edit_mode = function () { |
|
257 | KeyboardManager.prototype.edit_mode = function () { | |
258 | console.log('KeyboardManager', 'changing to edit mode'); |
|
258 | console.log('KeyboardManager', 'changing to edit mode'); | |
259 | this.last_mode = this.mode; |
|
259 | this.last_mode = this.mode; | |
260 | this.mode = 'edit'; |
|
260 | this.mode = 'edit'; | |
261 | } |
|
261 | } | |
262 |
|
262 | |||
263 | KeyboardManager.prototype.command_mode = function () { |
|
263 | KeyboardManager.prototype.command_mode = function () { | |
264 | console.log('KeyboardManager', 'changing to command mode'); |
|
264 | console.log('KeyboardManager', 'changing to command mode'); | |
265 | this.last_mode = this.mode; |
|
265 | this.last_mode = this.mode; | |
266 | this.mode = 'command'; |
|
266 | this.mode = 'command'; | |
267 | } |
|
267 | } | |
268 |
|
268 | |||
269 | KeyboardManager.prototype.enable = function () { |
|
269 | KeyboardManager.prototype.enable = function () { | |
270 | this.enable = true; |
|
270 | this.enabled = true; | |
271 | } |
|
271 | } | |
272 |
|
272 | |||
273 | KeyboardManager.prototype.disable = function () { |
|
273 | KeyboardManager.prototype.disable = function () { | |
274 | this.enable = false; |
|
274 | this.enabled = false; | |
275 | } |
|
275 | } | |
276 |
|
276 | |||
277 |
|
277 | |||
278 | IPython.KeyboardManager = KeyboardManager; |
|
278 | IPython.KeyboardManager = KeyboardManager; | |
279 |
|
279 | |||
280 | return IPython; |
|
280 | return IPython; | |
281 |
|
281 | |||
282 | }(IPython)); |
|
282 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now