Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
/**
4
*
5
*
6
* @module keyboardmanager
7
* @namespace keyboardmanager
8
* @class KeyboardManager
9
*/
10
11
define([
12
'base/js/namespace',
13
'jquery',
14
'base/js/utils',
15
'base/js/keyboard',
16
], function(IPython, $, utils, keyboard) {
17
"use strict";
18
19
// Main keyboard manager for the notebook
20
var keycodes = keyboard.keycodes;
21
22
var KeyboardManager = function (options) {
23
/**
24
* A class to deal with keyboard event and shortcut
25
*
26
* @class KeyboardManager
27
* @constructor
28
* @param options {dict} Dictionary of keyword arguments :
29
* @param options.events {$(Events)} instance
30
* @param options.pager: {Pager} pager instance
31
*/
32
this.mode = 'command';
33
this.enabled = true;
34
this.pager = options.pager;
35
this.quick_help = undefined;
36
this.notebook = undefined;
37
this.last_mode = undefined;
38
this.bind_events();
39
this.env = {pager:this.pager};
40
this.actions = options.actions;
41
this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env );
42
this.command_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
43
this.command_shortcuts.add_shortcuts(this.get_default_command_shortcuts());
44
this.edit_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env);
45
this.edit_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
46
this.edit_shortcuts.add_shortcuts(this.get_default_edit_shortcuts());
47
Object.seal(this);
48
};
49
50
51
52
53
/**
54
* Return a dict of common shortcut
55
* @method get_default_common_shortcuts
56
*
57
* @example Example of returned shortcut
58
* ```
59
* 'shortcut-key': 'action-name'
60
* // a string representing the shortcut as dash separated value.
61
* // e.g. 'shift' , 'shift-enter', 'cmd-t'
62
*```
63
*/
64
KeyboardManager.prototype.get_default_common_shortcuts = function() {
65
return {
66
'shift' : 'ipython.ignore',
67
'shift-enter' : 'ipython.run-select-next',
68
'ctrl-enter' : 'ipython.execute-in-place',
69
'alt-enter' : 'ipython.execute-and-insert-after',
70
// cmd on mac, ctrl otherwise
71
'cmdtrl-s' : 'ipython.save-notebook',
72
};
73
};
74
75
KeyboardManager.prototype.get_default_edit_shortcuts = function() {
76
return {
77
'esc' : 'ipython.go-to-command-mode',
78
'ctrl-m' : 'ipython.go-to-command-mode',
79
'up' : 'ipython.move-cursor-up-or-previous-cell',
80
'down' : 'ipython.move-cursor-down-or-next-cell',
81
'ctrl-shift--' : 'ipython.split-cell-at-cursor',
82
'ctrl-shift-subtract' : 'ipython.split-cell-at-cursor'
83
};
84
};
85
86
KeyboardManager.prototype.get_default_command_shortcuts = function() {
87
return {
88
'shift-space': 'ipython.scroll-up',
89
'shift-v' : 'ipython.paste-cell-before',
90
'shift-m' : 'ipython.merge-selected-cell-with-cell-after',
91
'shift-o' : 'ipython.toggle-output-scrolling-selected-cell',
92
'enter' : 'ipython.enter-edit-mode',
93
'space' : 'ipython.scroll-down',
94
'down' : 'ipython.select-next-cell',
95
'i,i' : 'ipython.interrupt-kernel',
96
'0,0' : 'ipython.restart-kernel',
97
'd,d' : 'ipython.delete-cell',
98
'esc': 'ipython.close-pager',
99
'up' : 'ipython.select-previous-cell',
100
'k' : 'ipython.select-previous-cell',
101
'j' : 'ipython.select-next-cell',
102
'x' : 'ipython.cut-selected-cell',
103
'c' : 'ipython.copy-selected-cell',
104
'v' : 'ipython.paste-cell-after',
105
'a' : 'ipython.insert-cell-before',
106
'b' : 'ipython.insert-cell-after',
107
'y' : 'ipython.change-selected-cell-to-code-cell',
108
'm' : 'ipython.change-selected-cell-to-markdown-cell',
109
'r' : 'ipython.change-selected-cell-to-raw-cell',
110
'1' : 'ipython.change-selected-cell-to-heading-1',
111
'2' : 'ipython.change-selected-cell-to-heading-2',
112
'3' : 'ipython.change-selected-cell-to-heading-3',
113
'4' : 'ipython.change-selected-cell-to-heading-4',
114
'5' : 'ipython.change-selected-cell-to-heading-5',
115
'6' : 'ipython.change-selected-cell-to-heading-6',
116
'o' : 'ipython.toggle-output-visibility-selected-cell',
117
's' : 'ipython.save-notebook',
118
'l' : 'ipython.toggle-line-number-selected-cell',
119
'h' : 'ipython.show-keyboard-shortcut-help-dialog',
120
'z' : 'ipython.undo-last-cell-deletion',
121
'q' : 'ipython.close-pager',
122
};
123
};
124
125
KeyboardManager.prototype.bind_events = function () {
126
var that = this;
127
$(document).keydown(function (event) {
128
if(event._ipkmIgnore===true||(event.originalEvent||{})._ipkmIgnore===true){
129
return false;
130
}
131
return that.handle_keydown(event);
132
});
133
};
134
135
KeyboardManager.prototype.set_notebook = function (notebook) {
136
this.notebook = notebook;
137
this.actions.extend_env({notebook:notebook});
138
};
139
140
KeyboardManager.prototype.set_quickhelp = function (notebook) {
141
this.actions.extend_env({quick_help:notebook});
142
};
143
144
145
KeyboardManager.prototype.handle_keydown = function (event) {
146
/**
147
* returning false from this will stop event propagation
148
**/
149
150
if (event.which === keycodes.esc) {
151
// Intercept escape at highest level to avoid closing
152
// websocket connection with firefox
153
event.preventDefault();
154
}
155
156
if (!this.enabled) {
157
if (event.which === keycodes.esc) {
158
this.notebook.command_mode();
159
return false;
160
}
161
return true;
162
}
163
164
if (this.mode === 'edit') {
165
return this.edit_shortcuts.call_handler(event);
166
} else if (this.mode === 'command') {
167
return this.command_shortcuts.call_handler(event);
168
}
169
return true;
170
};
171
172
KeyboardManager.prototype.edit_mode = function () {
173
this.last_mode = this.mode;
174
this.mode = 'edit';
175
};
176
177
KeyboardManager.prototype.command_mode = function () {
178
this.last_mode = this.mode;
179
this.mode = 'command';
180
};
181
182
KeyboardManager.prototype.enable = function () {
183
this.enabled = true;
184
};
185
186
KeyboardManager.prototype.disable = function () {
187
this.enabled = false;
188
};
189
190
KeyboardManager.prototype.register_events = function (e) {
191
var that = this;
192
var handle_focus = function () {
193
that.disable();
194
};
195
var handle_blur = function () {
196
that.enable();
197
};
198
e.on('focusin', handle_focus);
199
e.on('focusout', handle_blur);
200
// TODO: Very strange. The focusout event does not seem fire for the
201
// bootstrap textboxes on FF25&26... This works around that by
202
// registering focus and blur events recursively on all inputs within
203
// registered element.
204
e.find('input').blur(handle_blur);
205
e.on('DOMNodeInserted', function (event) {
206
var target = $(event.target);
207
if (target.is('input')) {
208
target.blur(handle_blur);
209
} else {
210
target.find('input').blur(handle_blur);
211
}
212
});
213
// There are times (raw_input) where we remove the element from the DOM before
214
// focusout is called. In this case we bind to the remove event of jQueryUI,
215
// which gets triggered upon removal, iff it is focused at the time.
216
// is_focused must be used to check for the case where an element within
217
// the element being removed is focused.
218
e.on('remove', function () {
219
if (utils.is_focused(e[0])) {
220
that.enable();
221
}
222
});
223
};
224
225
226
// For backwards compatibility.
227
IPython.KeyboardManager = KeyboardManager;
228
229
return {'KeyboardManager': KeyboardManager};
230
});
231
232