Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2011 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// Keyboard management
10
//============================================================================
11
12
var IPython = (function (IPython) {
13
"use strict";
14
15
var browser = IPython.utils.browser[0];
16
var platform = IPython.utils.platform;
17
18
// Default keyboard shortcuts
19
20
var default_common_shortcuts = {
21
'shift' : {
22
help : '',
23
help_index : '',
24
handler : function (event) {
25
// ignore shift keydown
26
return true;
27
}
28
},
29
'shift-enter' : {
30
help : 'run cell, select below',
31
help_index : 'ba',
32
handler : function (event) {
33
IPython.notebook.execute_cell_and_select_below();
34
return false;
35
}
36
},
37
'ctrl-enter' : {
38
help : 'run cell',
39
help_index : 'bb',
40
handler : function (event) {
41
IPython.notebook.execute_cell();
42
return false;
43
}
44
},
45
'alt-enter' : {
46
help : 'run cell, insert below',
47
help_index : 'bc',
48
handler : function (event) {
49
IPython.notebook.execute_cell_and_insert_below();
50
return false;
51
}
52
}
53
};
54
55
if (platform === 'MacOS') {
56
default_common_shortcuts['cmd-s'] =
57
{
58
help : 'save notebook',
59
help_index : 'fb',
60
handler : function (event) {
61
IPython.notebook.save_checkpoint();
62
event.preventDefault();
63
return false;
64
}
65
};
66
} else {
67
default_common_shortcuts['ctrl-s'] =
68
{
69
help : 'save notebook',
70
help_index : 'fb',
71
handler : function (event) {
72
IPython.notebook.save_checkpoint();
73
event.preventDefault();
74
return false;
75
}
76
};
77
}
78
79
// Edit mode defaults
80
81
var default_edit_shortcuts = {
82
'esc' : {
83
help : 'command mode',
84
help_index : 'aa',
85
handler : function (event) {
86
IPython.notebook.command_mode();
87
return false;
88
}
89
},
90
'ctrl-m' : {
91
help : 'command mode',
92
help_index : 'ab',
93
handler : function (event) {
94
IPython.notebook.command_mode();
95
return false;
96
}
97
},
98
'up' : {
99
help : '',
100
help_index : '',
101
handler : function (event) {
102
var index = IPython.notebook.get_selected_index();
103
var cell = IPython.notebook.get_cell(index);
104
if (cell && cell.at_top() && index !== 0) {
105
event.preventDefault();
106
IPython.notebook.command_mode();
107
IPython.notebook.select_prev();
108
IPython.notebook.edit_mode();
109
var cm = IPython.notebook.get_selected_cell().code_mirror;
110
cm.setCursor(cm.lastLine(), 0);
111
return false;
112
} else if (cell) {
113
var cm = cell.code_mirror;
114
cm.execCommand('goLineUp');
115
return false;
116
}
117
}
118
},
119
'down' : {
120
help : '',
121
help_index : '',
122
handler : function (event) {
123
var index = IPython.notebook.get_selected_index();
124
var cell = IPython.notebook.get_cell(index);
125
if (cell.at_bottom() && index !== (IPython.notebook.ncells()-1)) {
126
event.preventDefault();
127
IPython.notebook.command_mode();
128
IPython.notebook.select_next();
129
IPython.notebook.edit_mode();
130
var cm = IPython.notebook.get_selected_cell().code_mirror;
131
cm.setCursor(0, 0);
132
return false;
133
} else {
134
var cm = cell.code_mirror;
135
cm.execCommand('goLineDown');
136
return false;
137
}
138
}
139
},
140
'ctrl-shift--' : {
141
help : 'split cell',
142
help_index : 'ea',
143
handler : function (event) {
144
IPython.notebook.split_cell();
145
return false;
146
}
147
},
148
'ctrl-shift-subtract' : {
149
help : '',
150
help_index : 'eb',
151
handler : function (event) {
152
IPython.notebook.split_cell();
153
return false;
154
}
155
},
156
};
157
158
// Command mode defaults
159
160
var default_command_shortcuts = {
161
'enter' : {
162
help : 'edit mode',
163
help_index : 'aa',
164
handler : function (event) {
165
IPython.notebook.edit_mode();
166
return false;
167
}
168
},
169
'up' : {
170
help : 'select previous cell',
171
help_index : 'da',
172
handler : function (event) {
173
var index = IPython.notebook.get_selected_index();
174
if (index !== 0 && index !== null) {
175
IPython.notebook.select_prev();
176
IPython.notebook.focus_cell();
177
}
178
return false;
179
}
180
},
181
'down' : {
182
help : 'select next cell',
183
help_index : 'db',
184
handler : function (event) {
185
var index = IPython.notebook.get_selected_index();
186
if (index !== (IPython.notebook.ncells()-1) && index !== null) {
187
IPython.notebook.select_next();
188
IPython.notebook.focus_cell();
189
}
190
return false;
191
}
192
},
193
'k' : {
194
help : 'select previous cell',
195
help_index : 'dc',
196
handler : function (event) {
197
var index = IPython.notebook.get_selected_index();
198
if (index !== 0 && index !== null) {
199
IPython.notebook.select_prev();
200
IPython.notebook.focus_cell();
201
}
202
return false;
203
}
204
},
205
'j' : {
206
help : 'select next cell',
207
help_index : 'dd',
208
handler : function (event) {
209
var index = IPython.notebook.get_selected_index();
210
if (index !== (IPython.notebook.ncells()-1) && index !== null) {
211
IPython.notebook.select_next();
212
IPython.notebook.focus_cell();
213
}
214
return false;
215
}
216
},
217
'x' : {
218
help : 'cut cell',
219
help_index : 'ee',
220
handler : function (event) {
221
IPython.notebook.cut_cell();
222
return false;
223
}
224
},
225
'c' : {
226
help : 'copy cell',
227
help_index : 'ef',
228
handler : function (event) {
229
IPython.notebook.copy_cell();
230
return false;
231
}
232
},
233
'shift-v' : {
234
help : 'paste cell above',
235
help_index : 'eg',
236
handler : function (event) {
237
IPython.notebook.paste_cell_above();
238
return false;
239
}
240
},
241
'v' : {
242
help : 'paste cell below',
243
help_index : 'eh',
244
handler : function (event) {
245
IPython.notebook.paste_cell_below();
246
return false;
247
}
248
},
249
'd' : {
250
help : 'delete cell (press twice)',
251
help_index : 'ej',
252
count: 2,
253
handler : function (event) {
254
IPython.notebook.delete_cell();
255
return false;
256
}
257
},
258
'a' : {
259
help : 'insert cell above',
260
help_index : 'ec',
261
handler : function (event) {
262
IPython.notebook.insert_cell_above('code');
263
IPython.notebook.select_prev();
264
IPython.notebook.focus_cell();
265
return false;
266
}
267
},
268
'b' : {
269
help : 'insert cell below',
270
help_index : 'ed',
271
handler : function (event) {
272
IPython.notebook.insert_cell_below('code');
273
IPython.notebook.select_next();
274
IPython.notebook.focus_cell();
275
return false;
276
}
277
},
278
'y' : {
279
help : 'to code',
280
help_index : 'ca',
281
handler : function (event) {
282
IPython.notebook.to_code();
283
return false;
284
}
285
},
286
'm' : {
287
help : 'to markdown',
288
help_index : 'cb',
289
handler : function (event) {
290
IPython.notebook.to_markdown();
291
return false;
292
}
293
},
294
'r' : {
295
help : 'to raw',
296
help_index : 'cc',
297
handler : function (event) {
298
IPython.notebook.to_raw();
299
return false;
300
}
301
},
302
'1' : {
303
help : 'to heading 1',
304
help_index : 'cd',
305
handler : function (event) {
306
IPython.notebook.to_heading(undefined, 1);
307
return false;
308
}
309
},
310
'2' : {
311
help : 'to heading 2',
312
help_index : 'ce',
313
handler : function (event) {
314
IPython.notebook.to_heading(undefined, 2);
315
return false;
316
}
317
},
318
'3' : {
319
help : 'to heading 3',
320
help_index : 'cf',
321
handler : function (event) {
322
IPython.notebook.to_heading(undefined, 3);
323
return false;
324
}
325
},
326
'4' : {
327
help : 'to heading 4',
328
help_index : 'cg',
329
handler : function (event) {
330
IPython.notebook.to_heading(undefined, 4);
331
return false;
332
}
333
},
334
'5' : {
335
help : 'to heading 5',
336
help_index : 'ch',
337
handler : function (event) {
338
IPython.notebook.to_heading(undefined, 5);
339
return false;
340
}
341
},
342
'6' : {
343
help : 'to heading 6',
344
help_index : 'ci',
345
handler : function (event) {
346
IPython.notebook.to_heading(undefined, 6);
347
return false;
348
}
349
},
350
'o' : {
351
help : 'toggle output',
352
help_index : 'gb',
353
handler : function (event) {
354
IPython.notebook.toggle_output();
355
return false;
356
}
357
},
358
'shift-o' : {
359
help : 'toggle output scrolling',
360
help_index : 'gc',
361
handler : function (event) {
362
IPython.notebook.toggle_output_scroll();
363
return false;
364
}
365
},
366
's' : {
367
help : 'save notebook',
368
help_index : 'fa',
369
handler : function (event) {
370
IPython.notebook.save_checkpoint();
371
return false;
372
}
373
},
374
'ctrl-j' : {
375
help : 'move cell down',
376
help_index : 'eb',
377
handler : function (event) {
378
IPython.notebook.move_cell_down();
379
return false;
380
}
381
},
382
'ctrl-k' : {
383
help : 'move cell up',
384
help_index : 'ea',
385
handler : function (event) {
386
IPython.notebook.move_cell_up();
387
return false;
388
}
389
},
390
'l' : {
391
help : 'toggle line numbers',
392
help_index : 'ga',
393
handler : function (event) {
394
IPython.notebook.cell_toggle_line_numbers();
395
return false;
396
}
397
},
398
'i' : {
399
help : 'interrupt kernel (press twice)',
400
help_index : 'ha',
401
count: 2,
402
handler : function (event) {
403
IPython.notebook.kernel.interrupt();
404
return false;
405
}
406
},
407
'0' : {
408
help : 'restart kernel (press twice)',
409
help_index : 'hb',
410
count: 2,
411
handler : function (event) {
412
IPython.notebook.restart_kernel();
413
return false;
414
}
415
},
416
'h' : {
417
help : 'keyboard shortcuts',
418
help_index : 'ge',
419
handler : function (event) {
420
IPython.quick_help.show_keyboard_shortcuts();
421
return false;
422
}
423
},
424
'z' : {
425
help : 'undo last delete',
426
help_index : 'ei',
427
handler : function (event) {
428
IPython.notebook.undelete_cell();
429
return false;
430
}
431
},
432
'shift-m' : {
433
help : 'merge cell below',
434
help_index : 'ek',
435
handler : function (event) {
436
IPython.notebook.merge_cell_below();
437
return false;
438
}
439
},
440
'q' : {
441
help : 'close pager',
442
help_index : 'gd',
443
handler : function (event) {
444
IPython.pager.collapse();
445
return false;
446
}
447
},
448
};
449
450
451
// Main keyboard manager for the notebook
452
453
var ShortcutManager = IPython.keyboard.ShortcutManager;
454
var keycodes = IPython.keyboard.keycodes;
455
456
var KeyboardManager = function () {
457
this.mode = 'command';
458
this.enabled = true;
459
this.bind_events();
460
this.command_shortcuts = new ShortcutManager();
461
this.command_shortcuts.add_shortcuts(default_common_shortcuts);
462
this.command_shortcuts.add_shortcuts(default_command_shortcuts);
463
this.edit_shortcuts = new ShortcutManager();
464
this.edit_shortcuts.add_shortcuts(default_common_shortcuts);
465
this.edit_shortcuts.add_shortcuts(default_edit_shortcuts);
466
};
467
468
KeyboardManager.prototype.bind_events = function () {
469
var that = this;
470
$(document).keydown(function (event) {
471
return that.handle_keydown(event);
472
});
473
};
474
475
KeyboardManager.prototype.handle_keydown = function (event) {
476
var notebook = IPython.notebook;
477
478
if (event.which === keycodes.esc) {
479
// Intercept escape at highest level to avoid closing
480
// websocket connection with firefox
481
event.preventDefault();
482
}
483
484
if (!this.enabled) {
485
if (event.which === keycodes.esc) {
486
// ESC
487
notebook.command_mode();
488
return false;
489
}
490
return true;
491
}
492
493
if (this.mode === 'edit') {
494
return this.edit_shortcuts.call_handler(event);
495
} else if (this.mode === 'command') {
496
return this.command_shortcuts.call_handler(event);
497
}
498
return true;
499
};
500
501
KeyboardManager.prototype.edit_mode = function () {
502
this.last_mode = this.mode;
503
this.mode = 'edit';
504
};
505
506
KeyboardManager.prototype.command_mode = function () {
507
this.last_mode = this.mode;
508
this.mode = 'command';
509
};
510
511
KeyboardManager.prototype.enable = function () {
512
this.enabled = true;
513
};
514
515
KeyboardManager.prototype.disable = function () {
516
this.enabled = false;
517
};
518
519
KeyboardManager.prototype.register_events = function (e) {
520
var that = this;
521
var handle_focus = function () {
522
that.disable();
523
};
524
var handle_blur = function () {
525
that.enable();
526
};
527
e.on('focusin', handle_focus);
528
e.on('focusout', handle_blur);
529
// TODO: Very strange. The focusout event does not seem fire for the
530
// bootstrap textboxes on FF25&26... This works around that by
531
// registering focus and blur events recursively on all inputs within
532
// registered element.
533
e.find('input').blur(handle_blur);
534
e.on('DOMNodeInserted', function (event) {
535
var target = $(event.target);
536
if (target.is('input')) {
537
target.blur(handle_blur);
538
} else {
539
target.find('input').blur(handle_blur);
540
}
541
});
542
// There are times (raw_input) where we remove the element from the DOM before
543
// focusout is called. In this case we bind to the remove event of jQueryUI,
544
// which gets triggered upon removal, iff it is focused at the time.
545
// is_focused must be used to check for the case where an element within
546
// the element being removed is focused.
547
e.on('remove', function () {
548
if (IPython.utils.is_focused(e[0])) {
549
that.enable();
550
}
551
});
552
};
553
554
555
IPython.default_common_shortcuts = default_common_shortcuts;
556
IPython.default_edit_shortcuts = default_edit_shortcuts;
557
IPython.default_command_shortcuts = default_command_shortcuts;
558
IPython.KeyboardManager = KeyboardManager;
559
560
return IPython;
561
562
}(IPython));
563
564