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