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