Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
/**
4
*
5
*
6
* @module keyboard
7
* @namespace keyboard
8
* @class ShortcutManager
9
*/
10
11
define([
12
'base/js/namespace',
13
'jquery',
14
'base/js/utils',
15
'underscore',
16
], function(IPython, $, utils, _) {
17
"use strict";
18
19
20
/**
21
* Setup global keycodes and inverse keycodes.
22
*
23
* See http://unixpapa.com/js/key.html for a complete description. The short of
24
* it is that there are different keycode sets. Firefox uses the "Mozilla keycodes"
25
* and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same
26
* but have minor differences.
27
**/
28
29
// These apply to Firefox, (Webkit and IE)
30
// This does work **only** on US keyboard.
31
var _keycodes = {
32
'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73,
33
'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82,
34
's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
35
'1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54,
36
'7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48,
37
'[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191,
38
'\\ |': 220, '\' "': 222,
39
'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100,
40
'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105,
41
'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111,
42
'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118,
43
'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126,
44
'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18,
45
'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34,
46
'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
47
'insert': 45, 'delete': 46, 'numlock': 144,
48
};
49
50
// These apply to Firefox and Opera
51
var _mozilla_keycodes = {
52
'; :': 59, '= +': 61, '- _': 173, 'meta': 224
53
};
54
55
// This apply to Webkit and IE
56
var _ie_keycodes = {
57
'; :': 186, '= +': 187, '- _': 189
58
};
59
60
var browser = utils.browser[0];
61
var platform = utils.platform;
62
63
if (browser === 'Firefox' || browser === 'Opera' || browser === 'Netscape') {
64
$.extend(_keycodes, _mozilla_keycodes);
65
} else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') {
66
$.extend(_keycodes, _ie_keycodes);
67
}
68
69
var keycodes = {};
70
var inv_keycodes = {};
71
for (var name in _keycodes) {
72
var names = name.split(' ');
73
if (names.length === 1) {
74
var n = names[0];
75
keycodes[n] = _keycodes[n];
76
inv_keycodes[_keycodes[n]] = n;
77
} else {
78
var primary = names[0];
79
var secondary = names[1];
80
keycodes[primary] = _keycodes[name];
81
keycodes[secondary] = _keycodes[name];
82
inv_keycodes[_keycodes[name]] = primary;
83
}
84
}
85
86
var normalize_key = function (key) {
87
return inv_keycodes[keycodes[key]];
88
};
89
90
var normalize_shortcut = function (shortcut) {
91
/**
92
* @function _normalize_shortcut
93
* @private
94
* return a dict containing the normalized shortcut and the number of time it should be pressed:
95
*
96
* Put a shortcut into normalized form:
97
* 1. Make lowercase
98
* 2. Replace cmd by meta
99
* 3. Sort '-' separated modifiers into the order alt-ctrl-meta-shift
100
* 4. Normalize keys
101
**/
102
if (platform === 'MacOS') {
103
shortcut = shortcut.toLowerCase().replace('cmdtrl-', 'cmd-');
104
} else {
105
shortcut = shortcut.toLowerCase().replace('cmdtrl-', 'ctrl-');
106
}
107
108
shortcut = shortcut.toLowerCase().replace('cmd', 'meta');
109
shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
110
shortcut = shortcut.replace(/,$/, 'comma'); // catch shortcuts using '-' key
111
if(shortcut.indexOf(',') !== -1){
112
var sht = shortcut.split(',');
113
sht = _.map(sht, normalize_shortcut);
114
return shortcut;
115
}
116
shortcut = shortcut.replace(/comma/g, ','); // catch shortcuts using '-' key
117
var values = shortcut.split("-");
118
if (values.length === 1) {
119
return normalize_key(values[0]);
120
} else {
121
var modifiers = values.slice(0,-1);
122
var key = normalize_key(values[values.length-1]);
123
modifiers.sort();
124
return modifiers.join('-') + '-' + key;
125
}
126
};
127
128
var shortcut_to_event = function (shortcut, type) {
129
/**
130
* Convert a shortcut (shift-r) to a jQuery Event object
131
**/
132
type = type || 'keydown';
133
shortcut = normalize_shortcut(shortcut);
134
shortcut = shortcut.replace(/-$/, '_'); // catch shortcuts using '-' key
135
var values = shortcut.split("-");
136
var modifiers = values.slice(0,-1);
137
var key = values[values.length-1];
138
var opts = {which: keycodes[key]};
139
if (modifiers.indexOf('alt') !== -1) {opts.altKey = true;}
140
if (modifiers.indexOf('ctrl') !== -1) {opts.ctrlKey = true;}
141
if (modifiers.indexOf('meta') !== -1) {opts.metaKey = true;}
142
if (modifiers.indexOf('shift') !== -1) {opts.shiftKey = true;}
143
return $.Event(type, opts);
144
};
145
146
var only_modifier_event = function(event){
147
/**
148
* Return `true` if the event only contains modifiers keys.
149
* false otherwise
150
**/
151
var key = inv_keycodes[event.which];
152
return ((event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) &&
153
(key === 'alt'|| key === 'ctrl'|| key === 'meta'|| key === 'shift'));
154
155
};
156
157
var event_to_shortcut = function (event) {
158
/**
159
* Convert a jQuery Event object to a normalized shortcut string (shift-r)
160
**/
161
var shortcut = '';
162
var key = inv_keycodes[event.which];
163
if (event.altKey && key !== 'alt') {shortcut += 'alt-';}
164
if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl-';}
165
if (event.metaKey && key !== 'meta') {shortcut += 'meta-';}
166
if (event.shiftKey && key !== 'shift') {shortcut += 'shift-';}
167
shortcut += key;
168
return shortcut;
169
};
170
171
// Shortcut manager class
172
173
var ShortcutManager = function (delay, events, actions, env) {
174
/**
175
* A class to deal with keyboard event and shortcut
176
*
177
* @class ShortcutManager
178
* @constructor
179
*/
180
this._shortcuts = {};
181
this.delay = delay || 800; // delay in milliseconds
182
this.events = events;
183
this.actions = actions;
184
this.actions.extend_env(env);
185
this._queue = [];
186
this._cleartimeout = null;
187
Object.seal(this);
188
};
189
190
ShortcutManager.prototype.clearsoon = function(){
191
/**
192
* Clear the pending shortcut soon, and cancel previous clearing
193
* that might be registered.
194
**/
195
var that = this;
196
clearTimeout(this._cleartimeout);
197
this._cleartimeout = setTimeout(function(){that.clearqueue();}, this.delay);
198
};
199
200
201
ShortcutManager.prototype.clearqueue = function(){
202
/**
203
* clear the pending shortcut sequence now.
204
**/
205
this._queue = [];
206
clearTimeout(this._cleartimeout);
207
};
208
209
210
var flatten_shorttree = function(tree){
211
/**
212
* Flatten a tree of shortcut sequences.
213
* use full to iterate over all the key/values of available shortcuts.
214
**/
215
var dct = {};
216
for(var key in tree){
217
var value = tree[key];
218
if(typeof(value) === 'string'){
219
dct[key] = value;
220
} else {
221
var ftree=flatten_shorttree(value);
222
for(var subkey in ftree){
223
dct[key+','+subkey] = ftree[subkey];
224
}
225
}
226
}
227
return dct;
228
};
229
230
ShortcutManager.prototype.help = function () {
231
var help = [];
232
var ftree = flatten_shorttree(this._shortcuts);
233
for (var shortcut in ftree) {
234
var action = this.actions.get(ftree[shortcut]);
235
var help_string = action.help||'== no help ==';
236
var help_index = action.help_index;
237
if (help_string) {
238
var shortstring = (action.shortstring||shortcut);
239
help.push({
240
shortcut: shortstring,
241
help: help_string,
242
help_index: help_index}
243
);
244
}
245
}
246
help.sort(function (a, b) {
247
if (a.help_index === b.help_index) {
248
return 0;
249
}
250
if (a.help_index === undefined || a.help_index > b.help_index){
251
return 1;
252
}
253
return -1;
254
});
255
return help;
256
};
257
258
ShortcutManager.prototype.clear_shortcuts = function () {
259
this._shortcuts = {};
260
};
261
262
ShortcutManager.prototype.get_shortcut = function (shortcut){
263
/**
264
* return a node of the shortcut tree which an action name (string) if leaf,
265
* and an object with `object.subtree===true`
266
**/
267
if(typeof(shortcut) === 'string'){
268
shortcut = shortcut.split(',');
269
}
270
271
return this._get_leaf(shortcut, this._shortcuts);
272
};
273
274
275
ShortcutManager.prototype._get_leaf = function(shortcut_array, tree){
276
/**
277
* @private
278
* find a leaf/node in a subtree of the keyboard shortcut
279
*
280
**/
281
if(shortcut_array.length === 1){
282
return tree[shortcut_array[0]];
283
} else if( typeof(tree[shortcut_array[0]]) !== 'string'){
284
return this._get_leaf(shortcut_array.slice(1), tree[shortcut_array[0]]);
285
}
286
return null;
287
};
288
289
ShortcutManager.prototype.set_shortcut = function( shortcut, action_name){
290
if( typeof(action_name) !== 'string'){ throw('action is not a string', action_name);}
291
if( typeof(shortcut) === 'string'){
292
shortcut = shortcut.split(',');
293
}
294
return this._set_leaf(shortcut, action_name, this._shortcuts);
295
};
296
297
ShortcutManager.prototype._is_leaf = function(shortcut_array, tree){
298
if(shortcut_array.length === 1){
299
return(typeof(tree[shortcut_array[0]]) === 'string');
300
} else {
301
var subtree = tree[shortcut_array[0]];
302
return this._is_leaf(shortcut_array.slice(1), subtree );
303
}
304
};
305
306
ShortcutManager.prototype._remove_leaf = function(shortcut_array, tree, allow_node){
307
if(shortcut_array.length === 1){
308
var current_node = tree[shortcut_array[0]];
309
if(typeof(current_node) === 'string'){
310
delete tree[shortcut_array[0]];
311
} else {
312
throw('try to delete non-leaf');
313
}
314
} else {
315
this._remove_leaf(shortcut_array.slice(1), tree[shortcut_array[0]], allow_node);
316
if(_.keys(tree[shortcut_array[0]]).length === 0){
317
delete tree[shortcut_array[0]];
318
}
319
}
320
};
321
322
ShortcutManager.prototype._set_leaf = function(shortcut_array, action_name, tree){
323
var current_node = tree[shortcut_array[0]];
324
if(shortcut_array.length === 1){
325
if(current_node !== undefined && typeof(current_node) !== 'string'){
326
console.warn('[warning], you are overriting a long shortcut with a shorter one');
327
}
328
tree[shortcut_array[0]] = action_name;
329
return true;
330
} else {
331
if(typeof(current_node) === 'string'){
332
console.warn('you are trying to set a shortcut that will be shadowed'+
333
'by a more specific one. Aborting for :', action_name, 'the follwing '+
334
'will take precedence', current_node);
335
return false;
336
} else {
337
tree[shortcut_array[0]] = tree[shortcut_array[0]]||{};
338
}
339
this._set_leaf(shortcut_array.slice(1), action_name, tree[shortcut_array[0]]);
340
return true;
341
}
342
};
343
344
ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
345
/**
346
* Add a action to be handled by shortcut manager.
347
*
348
* - `shortcut` should be a `Shortcut Sequence` of the for `Ctrl-Alt-C,Meta-X`...
349
* - `data` could be an `action name`, an `action` or a `function`.
350
* if a `function` is passed it will be converted to an anonymous `action`.
351
*
352
**/
353
var action_name = this.actions.get_name(data);
354
if (! action_name){
355
throw('does nto know how to deal with ', data);
356
}
357
358
shortcut = normalize_shortcut(shortcut);
359
this.set_shortcut(shortcut, action_name);
360
361
if (!suppress_help_update) {
362
// update the keyboard shortcuts notebook help
363
this.events.trigger('rebuild.QuickHelp');
364
}
365
};
366
367
ShortcutManager.prototype.add_shortcuts = function (data) {
368
/**
369
* Convenient methods to call `add_shortcut(key, value)` on several items
370
*
371
* data : Dict of the form {key:value, ...}
372
**/
373
for (var shortcut in data) {
374
this.add_shortcut(shortcut, data[shortcut], true);
375
}
376
// update the keyboard shortcuts notebook help
377
this.events.trigger('rebuild.QuickHelp');
378
};
379
380
ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
381
/**
382
* Remove the binding of shortcut `sortcut` with its action.
383
* throw an error if trying to remove a non-exiting shortcut
384
**/
385
shortcut = normalize_shortcut(shortcut);
386
if( typeof(shortcut) === 'string'){
387
shortcut = shortcut.split(',');
388
}
389
this._remove_leaf(shortcut, this._shortcuts);
390
if (!suppress_help_update) {
391
// update the keyboard shortcuts notebook help
392
this.events.trigger('rebuild.QuickHelp');
393
}
394
};
395
396
397
398
ShortcutManager.prototype.call_handler = function (event) {
399
/**
400
* Call the corresponding shortcut handler for a keyboard event
401
* @method call_handler
402
* @return {Boolean} `true|false`, `false` if no handler was found, otherwise the value return by the handler.
403
* @param event {event}
404
*
405
* given an event, call the corresponding shortcut.
406
* return false is event wan handled, true otherwise
407
* in any case returning false stop event propagation
408
**/
409
410
411
this.clearsoon();
412
if(only_modifier_event(event)){
413
return true;
414
}
415
var shortcut = event_to_shortcut(event);
416
this._queue.push(shortcut);
417
var action_name = this.get_shortcut(this._queue);
418
419
if (typeof(action_name) === 'undefined'|| action_name === null){
420
this.clearqueue();
421
return true;
422
}
423
424
if (this.actions.exists(action_name)) {
425
event.preventDefault();
426
this.clearqueue();
427
return this.actions.call(action_name, event);
428
}
429
430
return false;
431
};
432
433
434
ShortcutManager.prototype.handles = function (event) {
435
var shortcut = event_to_shortcut(event);
436
var action_name = this.get_shortcut(this._queue.concat(shortcut));
437
return (typeof(action_name) !== 'undefined');
438
};
439
440
var keyboard = {
441
keycodes : keycodes,
442
inv_keycodes : inv_keycodes,
443
ShortcutManager : ShortcutManager,
444
normalize_key : normalize_key,
445
normalize_shortcut : normalize_shortcut,
446
shortcut_to_event : shortcut_to_event,
447
event_to_shortcut : event_to_shortcut,
448
};
449
450
// For backwards compatibility.
451
IPython.keyboard = keyboard;
452
453
return keyboard;
454
});
455
456