keyboardmanager.js
774 lines
| 24.6 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r14020 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// Keyboard management | ||||
//============================================================================ | ||||
var IPython = (function (IPython) { | ||||
"use strict"; | ||||
Brian E. Granger
|
r14036 | // Setup global keycodes and inverse keycodes. | ||
// See http://unixpapa.com/js/key.html for a complete description. The short of | ||||
// it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | ||||
// and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | ||||
// but have minor differences. | ||||
// These apply to Firefox, (Webkit and IE) | ||||
var _keycodes = { | ||||
'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | ||||
'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | ||||
's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | ||||
'1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | ||||
'7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | ||||
'[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | ||||
Brian E. Granger
|
r14037 | '\\ |': 220, '\' "': 222, | ||
Brian E. Granger
|
r14036 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | ||
'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | ||||
'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | ||||
'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | ||||
'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | ||||
'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | ||||
'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | ||||
'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | ||||
'insert': 45, 'delete': 46, 'numlock': 144, | ||||
}; | ||||
// These apply to Firefox and Opera | ||||
var _mozilla_keycodes = { | ||||
Brian E. Granger
|
r14076 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 | ||
Brian E. Granger
|
r14036 | } | ||
// This apply to Webkit and IE | ||||
var _ie_keycodes = { | ||||
Brian E. Granger
|
r14037 | '; :': 186, '= +': 187, '- _': 189, | ||
Brian E. Granger
|
r14036 | } | ||
var browser = IPython.utils.browser[0]; | ||||
Brian E. Granger
|
r14816 | var platform = IPython.utils.platform; | ||
Brian E. Granger
|
r14036 | |||
if (browser === 'Firefox' || browser === 'Opera') { | ||||
$.extend(_keycodes, _mozilla_keycodes); | ||||
} else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | ||||
$.extend(_keycodes, _ie_keycodes); | ||||
} | ||||
var keycodes = {}; | ||||
var inv_keycodes = {}; | ||||
for (var name in _keycodes) { | ||||
var names = name.split(' '); | ||||
if (names.length === 1) { | ||||
var n = names[0] | ||||
keycodes[n] = _keycodes[n] | ||||
inv_keycodes[_keycodes[n]] = n | ||||
} else { | ||||
var primary = names[0]; | ||||
var secondary = names[1]; | ||||
keycodes[primary] = _keycodes[name] | ||||
keycodes[secondary] = _keycodes[name] | ||||
inv_keycodes[_keycodes[name]] = primary | ||||
} | ||||
} | ||||
// Default keyboard shortcuts | ||||
var default_common_shortcuts = { | ||||
'shift' : { | ||||
help : '', | ||||
Brian E. Granger
|
r14093 | help_index : '', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
// ignore shift keydown | ||||
return true; | ||||
} | ||||
}, | ||||
'shift+enter' : { | ||||
Brian E. Granger
|
r14946 | help : 'run cell, select below', | ||
Brian E. Granger
|
r14093 | help_index : 'ba', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14945 | IPython.notebook.execute_cell_and_select_below(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
Brian E. Granger
|
r14093 | 'ctrl+enter' : { | ||
Brian E. Granger
|
r14946 | help : 'run cell', | ||
Brian E. Granger
|
r14093 | help_index : 'bb', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14945 | IPython.notebook.execute_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
Brian E. Granger
|
r14093 | 'alt+enter' : { | ||
help : 'run cell, insert below', | ||||
help_index : 'bc', | ||||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14093 | IPython.notebook.execute_cell_and_insert_below(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
} | ||||
} | ||||
Brian E. Granger
|
r14816 | if (platform === 'MacOS') { | ||
default_common_shortcuts['cmd+s'] = | ||||
{ | ||||
help : 'save notebook', | ||||
help_index : 'fb', | ||||
handler : function (event) { | ||||
IPython.notebook.save_checkpoint(); | ||||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}; | ||||
} else { | ||||
default_common_shortcuts['ctrl+s'] = | ||||
{ | ||||
help : 'save notebook', | ||||
help_index : 'fb', | ||||
handler : function (event) { | ||||
IPython.notebook.save_checkpoint(); | ||||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}; | ||||
} | ||||
Brian E. Granger
|
r14036 | // Edit mode defaults | ||
var default_edit_shortcuts = { | ||||
'esc' : { | ||||
help : 'command mode', | ||||
Brian E. Granger
|
r14093 | help_index : 'aa', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.command_mode(); | ||||
Brian E. Granger
|
r14073 | IPython.notebook.focus_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'ctrl+m' : { | ||||
help : 'command mode', | ||||
Brian E. Granger
|
r14093 | help_index : 'ab', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.command_mode(); | ||||
Brian E. Granger
|
r14073 | IPython.notebook.focus_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'up' : { | ||||
Brian E. Granger
|
r14099 | help : '', | ||
help_index : '', | ||||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
var cell = IPython.notebook.get_selected_cell(); | ||||
if (cell && cell.at_top()) { | ||||
event.preventDefault(); | ||||
IPython.notebook.command_mode() | ||||
IPython.notebook.select_prev(); | ||||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
}; | ||||
} | ||||
}, | ||||
'down' : { | ||||
Brian E. Granger
|
r14099 | help : '', | ||
help_index : '', | ||||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
var cell = IPython.notebook.get_selected_cell(); | ||||
if (cell && cell.at_bottom()) { | ||||
event.preventDefault(); | ||||
IPython.notebook.command_mode() | ||||
IPython.notebook.select_next(); | ||||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
}; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14057 | 'alt+-' : { | ||
help : 'split cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'ea', | ||
Brian E. Granger
|
r14057 | handler : function (event) { | ||
IPython.notebook.split_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14076 | 'alt+subtract' : { | ||
Brian E. Granger
|
r14093 | help : '', | ||
help_index : 'eb', | ||||
Brian E. Granger
|
r14076 | handler : function (event) { | ||
IPython.notebook.split_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14816 | 'tab' : { | ||
help : 'indent or complete', | ||||
help_index : 'ec', | ||||
}, | ||||
'shift+tab' : { | ||||
help : 'tooltip', | ||||
help_index : 'ed', | ||||
}, | ||||
} | ||||
if (platform === 'MacOS') { | ||||
default_edit_shortcuts['cmd+/'] = | ||||
{ | ||||
help : 'toggle comment', | ||||
help_index : 'ee' | ||||
}; | ||||
default_edit_shortcuts['cmd+]'] = | ||||
{ | ||||
help : 'indent', | ||||
help_index : 'ef' | ||||
}; | ||||
default_edit_shortcuts['cmd+['] = | ||||
{ | ||||
help : 'dedent', | ||||
help_index : 'eg' | ||||
}; | ||||
} else { | ||||
default_edit_shortcuts['ctrl+/'] = | ||||
{ | ||||
help : 'toggle comment', | ||||
help_index : 'ee' | ||||
}; | ||||
default_edit_shortcuts['ctrl+]'] = | ||||
{ | ||||
help : 'indent', | ||||
help_index : 'ef' | ||||
}; | ||||
default_edit_shortcuts['ctrl+['] = | ||||
{ | ||||
help : 'dedent', | ||||
help_index : 'eg' | ||||
}; | ||||
Brian E. Granger
|
r14036 | } | ||
// Command mode defaults | ||||
var default_command_shortcuts = { | ||||
'enter' : { | ||||
help : 'edit mode', | ||||
Brian E. Granger
|
r14093 | help_index : 'aa', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
} | ||||
}, | ||||
'up' : { | ||||
help : 'select previous cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'da', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== 0 && index !== null) { | ||||
IPython.notebook.select_prev(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
'down' : { | ||||
help : 'select next cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'db', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== (IPython.notebook.ncells()-1) && index !== null) { | ||||
IPython.notebook.select_next(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14038 | 'k' : { | ||
help : 'select previous cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'dc', | ||
Brian E. Granger
|
r14038 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== 0 && index !== null) { | ||||
IPython.notebook.select_prev(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
'j' : { | ||||
help : 'select next cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'dd', | ||
Brian E. Granger
|
r14038 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== (IPython.notebook.ncells()-1) && index !== null) { | ||||
IPython.notebook.select_next(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14036 | 'x' : { | ||
help : 'cut cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'ee', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14037 | IPython.notebook.cut_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'c' : { | ||||
help : 'copy cell', | ||||
Brian E. Granger
|
r14093 | help_index : 'ef', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14037 | IPython.notebook.copy_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
Brian E. Granger
|
r14810 | 'shift+v' : { | ||
help : 'paste cell above', | ||||
help_index : 'eg', | ||||
handler : function (event) { | ||||
IPython.notebook.paste_cell_above(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14036 | 'v' : { | ||
help : 'paste cell below', | ||||
Brian E. Granger
|
r14810 | help_index : 'eh', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14037 | IPython.notebook.paste_cell_below(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'd' : { | ||||
help : 'delete cell (press twice)', | ||||
Brian E. Granger
|
r14810 | help_index : 'ej', | ||
count: 2, | ||||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
Brian E. Granger
|
r14810 | IPython.notebook.delete_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'a' : { | ||||
help : 'insert cell above', | ||||
Brian E. Granger
|
r14093 | help_index : 'ec', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.insert_cell_above('code'); | ||||
IPython.notebook.select_prev(); | ||||
Brian E. Granger
|
r14075 | IPython.notebook.focus_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'b' : { | ||||
help : 'insert cell below', | ||||
Brian E. Granger
|
r14093 | help_index : 'ed', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.insert_cell_below('code'); | ||||
IPython.notebook.select_next(); | ||||
Brian E. Granger
|
r14075 | IPython.notebook.focus_cell(); | ||
Brian E. Granger
|
r14036 | return false; | ||
} | ||||
}, | ||||
'y' : { | ||||
help : 'to code', | ||||
Brian E. Granger
|
r14093 | help_index : 'ca', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_code(); | ||||
return false; | ||||
} | ||||
}, | ||||
'm' : { | ||||
help : 'to markdown', | ||||
Brian E. Granger
|
r14093 | help_index : 'cb', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_markdown(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14810 | 'r' : { | ||
Brian E. Granger
|
r14036 | help : 'to raw', | ||
Brian E. Granger
|
r14093 | help_index : 'cc', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_raw(); | ||||
return false; | ||||
} | ||||
}, | ||||
'1' : { | ||||
help : 'to heading 1', | ||||
Brian E. Granger
|
r14093 | help_index : 'cd', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 1); | ||||
return false; | ||||
} | ||||
}, | ||||
'2' : { | ||||
help : 'to heading 2', | ||||
Brian E. Granger
|
r14093 | help_index : 'ce', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 2); | ||||
return false; | ||||
} | ||||
}, | ||||
'3' : { | ||||
help : 'to heading 3', | ||||
Brian E. Granger
|
r14093 | help_index : 'cf', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 3); | ||||
return false; | ||||
} | ||||
}, | ||||
'4' : { | ||||
help : 'to heading 4', | ||||
Brian E. Granger
|
r14093 | help_index : 'cg', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 4); | ||||
return false; | ||||
} | ||||
}, | ||||
'5' : { | ||||
help : 'to heading 5', | ||||
Brian E. Granger
|
r14093 | help_index : 'ch', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 5); | ||||
return false; | ||||
} | ||||
}, | ||||
'6' : { | ||||
help : 'to heading 6', | ||||
Brian E. Granger
|
r14093 | help_index : 'ci', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 6); | ||||
return false; | ||||
} | ||||
}, | ||||
'o' : { | ||||
help : 'toggle output', | ||||
Brian E. Granger
|
r14093 | help_index : 'gb', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.toggle_output(); | ||||
return false; | ||||
} | ||||
}, | ||||
'shift+o' : { | ||||
Brian E. Granger
|
r14871 | help : 'toggle output scrolling', | ||
Brian E. Granger
|
r14093 | help_index : 'gc', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.toggle_output_scroll(); | ||||
return false; | ||||
} | ||||
}, | ||||
's' : { | ||||
help : 'save notebook', | ||||
Brian E. Granger
|
r14093 | help_index : 'fa', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.save_checkpoint(); | ||||
return false; | ||||
} | ||||
}, | ||||
'ctrl+j' : { | ||||
help : 'move cell down', | ||||
Brian E. Granger
|
r14093 | help_index : 'eb', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.move_cell_down(); | ||||
return false; | ||||
} | ||||
}, | ||||
'ctrl+k' : { | ||||
help : 'move cell up', | ||||
Brian E. Granger
|
r14093 | help_index : 'ea', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.move_cell_up(); | ||||
return false; | ||||
} | ||||
}, | ||||
'l' : { | ||||
help : 'toggle line numbers', | ||||
Brian E. Granger
|
r14093 | help_index : 'ga', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.cell_toggle_line_numbers(); | ||||
return false; | ||||
} | ||||
}, | ||||
'i' : { | ||||
Brian E. Granger
|
r14810 | help : 'interrupt kernel (press twice)', | ||
Brian E. Granger
|
r14093 | help_index : 'ha', | ||
Brian E. Granger
|
r14810 | count: 2, | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.kernel.interrupt(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14810 | '0' : { | ||
help : 'restart kernel (press twice)', | ||||
Brian E. Granger
|
r14093 | help_index : 'hb', | ||
Brian E. Granger
|
r14810 | count: 2, | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.restart_kernel(); | ||||
return false; | ||||
} | ||||
}, | ||||
'h' : { | ||||
help : 'keyboard shortcuts', | ||||
Brian E. Granger
|
r15194 | help_index : 'ge', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.quick_help.show_keyboard_shortcuts(); | ||||
return false; | ||||
} | ||||
}, | ||||
'z' : { | ||||
help : 'undo last delete', | ||||
Brian E. Granger
|
r14810 | help_index : 'ei', | ||
Brian E. Granger
|
r14036 | handler : function (event) { | ||
IPython.notebook.undelete_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r14816 | 'shift+m' : { | ||
help : 'merge cell below', | ||||
help_index : 'ek', | ||||
handler : function (event) { | ||||
IPython.notebook.merge_cell_below(); | ||||
return false; | ||||
} | ||||
}, | ||||
Brian E. Granger
|
r15194 | 'q' : { | ||
Brian E. Granger
|
r15195 | help : 'close pager', | ||
Brian E. Granger
|
r15194 | help_index : 'gd', | ||
handler : function (event) { | ||||
Brian E. Granger
|
r15195 | IPython.pager.collapse(); | ||
Brian E. Granger
|
r15194 | return false; | ||
} | ||||
}, | ||||
Brian E. Granger
|
r14036 | } | ||
// Shortcut manager class | ||||
Brian E. Granger
|
r14810 | var ShortcutManager = function (delay) { | ||
Brian E. Granger
|
r14036 | this._shortcuts = {} | ||
Brian E. Granger
|
r14810 | this._counts = {} | ||
Brian E. Granger
|
r15113 | this._timers = {} | ||
Brian E. Granger
|
r14810 | this.delay = delay || 800; // delay in milliseconds | ||
Brian E. Granger
|
r14036 | } | ||
Brian E. Granger
|
r14037 | ShortcutManager.prototype.help = function () { | ||
var help = []; | ||||
for (var shortcut in this._shortcuts) { | ||||
Brian E. Granger
|
r14093 | var help_string = this._shortcuts[shortcut]['help']; | ||
var help_index = this._shortcuts[shortcut]['help_index']; | ||||
if (help_string) { | ||||
Brian E. Granger
|
r14816 | if (platform === 'MacOS') { | ||
shortcut = shortcut.replace('meta', 'cmd'); | ||||
} | ||||
Brian E. Granger
|
r14093 | help.push({ | ||
shortcut: shortcut, | ||||
help: help_string, | ||||
help_index: help_index} | ||||
); | ||||
} | ||||
Brian E. Granger
|
r14037 | } | ||
Brian E. Granger
|
r14093 | help.sort(function (a, b) { | ||
if (a.help_index > b.help_index) | ||||
return 1; | ||||
if (a.help_index < b.help_index) | ||||
return -1; | ||||
return 0; | ||||
}); | ||||
Brian E. Granger
|
r14037 | return help; | ||
} | ||||
Brian E. Granger
|
r14090 | ShortcutManager.prototype.normalize_key = function (key) { | ||
Brian E. Granger
|
r14036 | return inv_keycodes[keycodes[key]]; | ||
} | ||||
Brian E. Granger
|
r14090 | ShortcutManager.prototype.normalize_shortcut = function (shortcut) { | ||
Brian E. Granger
|
r14036 | // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift | ||
Brian E. Granger
|
r14816 | shortcut = shortcut.replace('cmd', 'meta').toLowerCase(); | ||
Brian E. Granger
|
r14036 | var values = shortcut.split("+"); | ||
if (values.length === 1) { | ||||
Brian E. Granger
|
r14090 | return this.normalize_key(values[0]) | ||
Brian E. Granger
|
r14036 | } else { | ||
var modifiers = values.slice(0,-1); | ||||
Brian E. Granger
|
r14090 | var key = this.normalize_key(values[values.length-1]); | ||
Brian E. Granger
|
r14036 | modifiers.sort(); | ||
return modifiers.join('+') + '+' + key; | ||||
} | ||||
} | ||||
ShortcutManager.prototype.event_to_shortcut = function (event) { | ||||
// Convert a jQuery keyboard event to a strong based keyboard shortcut | ||||
var shortcut = ''; | ||||
var key = inv_keycodes[event.which] | ||||
if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | ||||
if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | ||||
if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | ||||
if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | ||||
shortcut += key; | ||||
return shortcut | ||||
} | ||||
ShortcutManager.prototype.clear_shortcuts = function () { | ||||
this._shortcuts = {}; | ||||
} | ||||
ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | ||||
Brian E. Granger
|
r14093 | if (typeof(data) === 'function') { | ||
Brian E. Granger
|
r14099 | data = {help: '', help_index: '', handler: data} | ||
} | ||||
Brian E. Granger
|
r14100 | data.help_index = data.help_index || ''; | ||
data.help = data.help || ''; | ||||
Brian E. Granger
|
r14810 | data.count = data.count || 1; | ||
Brian E. Granger
|
r14099 | if (data.help_index === '') { | ||
Brian E. Granger
|
r14100 | data.help_index = 'zz'; | ||
Brian E. Granger
|
r14093 | } | ||
Brian E. Granger
|
r14090 | shortcut = this.normalize_shortcut(shortcut); | ||
Brian E. Granger
|
r14810 | this._counts[shortcut] = 0; | ||
Brian E. Granger
|
r14036 | this._shortcuts[shortcut] = data; | ||
} | ||||
ShortcutManager.prototype.add_shortcuts = function (data) { | ||||
for (var shortcut in data) { | ||||
this.add_shortcut(shortcut, data[shortcut]); | ||||
} | ||||
} | ||||
ShortcutManager.prototype.remove_shortcut = function (shortcut) { | ||||
Brian E. Granger
|
r14090 | shortcut = this.normalize_shortcut(shortcut); | ||
Brian E. Granger
|
r14810 | delete this._counts[shortcut]; | ||
Brian E. Granger
|
r14036 | delete this._shortcuts[shortcut]; | ||
} | ||||
Brian E. Granger
|
r14825 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { | ||
Brian E. Granger
|
r14810 | var that = this; | ||
var c = this._counts; | ||||
Brian E. Granger
|
r15113 | var t = this._timers; | ||
var timer = null; | ||||
Brian E. Granger
|
r14825 | if (c[shortcut] === data.count-1) { | ||
c[shortcut] = 0; | ||||
Brian E. Granger
|
r15113 | var timer = t[shortcut]; | ||
Brian E. Granger
|
r15116 | if (timer) {clearTimeout(timer); delete t[shortcut];} | ||
Brian E. Granger
|
r14825 | return data.handler(event); | ||
} else { | ||||
c[shortcut] = c[shortcut] + 1; | ||||
Brian E. Granger
|
r15113 | timer = setTimeout(function () { | ||
Brian E. Granger
|
r14810 | c[shortcut] = 0; | ||
}, that.delay); | ||||
Brian E. Granger
|
r15113 | t[shortcut] = timer; | ||
Brian E. Granger
|
r14810 | } | ||
return false; | ||||
} | ||||
Brian E. Granger
|
r14036 | ShortcutManager.prototype.call_handler = function (event) { | ||
var shortcut = this.event_to_shortcut(event); | ||||
var data = this._shortcuts[shortcut]; | ||||
Brian E. Granger
|
r14810 | if (data) { | ||
Brian E. Granger
|
r14036 | var handler = data['handler']; | ||
Brian E. Granger
|
r14810 | if (handler) { | ||
if (data.count === 1) { | ||||
return handler(event); | ||||
} else if (data.count > 1) { | ||||
Brian E. Granger
|
r14825 | return this.count_handler(shortcut, event, data); | ||
Brian E. Granger
|
r14810 | } | ||
Brian E. Granger
|
r14036 | } | ||
} | ||||
return true; | ||||
} | ||||
// Main keyboard manager for the notebook | ||||
Brian E. Granger
|
r14020 | |||
var KeyboardManager = function () { | ||||
Brian E. Granger
|
r14021 | this.mode = 'command'; | ||
Brian E. Granger
|
r14023 | this.enabled = true; | ||
Brian E. Granger
|
r14020 | this.bind_events(); | ||
Brian E. Granger
|
r14036 | this.command_shortcuts = new ShortcutManager(); | ||
this.command_shortcuts.add_shortcuts(default_common_shortcuts); | ||||
this.command_shortcuts.add_shortcuts(default_command_shortcuts); | ||||
this.edit_shortcuts = new ShortcutManager(); | ||||
this.edit_shortcuts.add_shortcuts(default_common_shortcuts); | ||||
this.edit_shortcuts.add_shortcuts(default_edit_shortcuts); | ||||
Brian E. Granger
|
r14020 | }; | ||
KeyboardManager.prototype.bind_events = function () { | ||||
var that = this; | ||||
$(document).keydown(function (event) { | ||||
return that.handle_keydown(event); | ||||
}); | ||||
}; | ||||
KeyboardManager.prototype.handle_keydown = function (event) { | ||||
var notebook = IPython.notebook; | ||||
Brian E. Granger
|
r14089 | |||
Brian E. Granger
|
r14036 | if (event.which === keycodes['esc']) { | ||
Brian E. Granger
|
r14020 | // Intercept escape at highest level to avoid closing | ||
// websocket connection with firefox | ||||
event.preventDefault(); | ||||
} | ||||
Brian E. Granger
|
r14023 | if (!this.enabled) { | ||
Brian E. Granger
|
r14036 | if (event.which === keycodes['esc']) { | ||
// ESC | ||||
notebook.command_mode(); | ||||
return false; | ||||
} | ||||
Brian E. Granger
|
r14023 | return true; | ||
Brian E. Granger
|
r14020 | } | ||
if (this.mode === 'edit') { | ||||
Brian E. Granger
|
r14036 | return this.edit_shortcuts.call_handler(event); | ||
} else if (this.mode === 'command') { | ||||
return this.command_shortcuts.call_handler(event); | ||||
Brian E. Granger
|
r14020 | } | ||
return true; | ||||
} | ||||
KeyboardManager.prototype.edit_mode = function () { | ||||
this.last_mode = this.mode; | ||||
this.mode = 'edit'; | ||||
} | ||||
KeyboardManager.prototype.command_mode = function () { | ||||
this.last_mode = this.mode; | ||||
this.mode = 'command'; | ||||
} | ||||
Brian E. Granger
|
r14024 | KeyboardManager.prototype.enable = function () { | ||
Brian E. Granger
|
r14030 | this.enabled = true; | ||
Brian E. Granger
|
r14024 | } | ||
KeyboardManager.prototype.disable = function () { | ||||
Brian E. Granger
|
r14030 | this.enabled = false; | ||
Brian E. Granger
|
r14024 | } | ||
Brian E. Granger
|
r14036 | KeyboardManager.prototype.register_events = function (e) { | ||
var that = this; | ||||
e.on('focusin', function () { | ||||
that.disable(); | ||||
}); | ||||
e.on('focusout', function () { | ||||
that.enable(); | ||||
Brian E. Granger
|
r14046 | }); | ||
// There are times (raw_input) where we remove the element from the DOM before | ||||
// focusout is called. In this case we bind to the remove event of jQueryUI, | ||||
MinRK
|
r15328 | // which gets triggered upon removal, iff it is focused at the time. | ||
Brian E. Granger
|
r14046 | e.on('remove', function () { | ||
MinRK
|
r15328 | if (document.activeElement === e[0]) { | ||
that.enable(); | ||||
} | ||||
Brian E. Granger
|
r14046 | }); | ||
Brian E. Granger
|
r14036 | } | ||
Brian E. Granger
|
r14024 | |||
Brian E. Granger
|
r14036 | IPython.keycodes = keycodes; | ||
IPython.inv_keycodes = inv_keycodes; | ||||
IPython.default_common_shortcuts = default_common_shortcuts; | ||||
IPython.default_edit_shortcuts = default_edit_shortcuts; | ||||
IPython.default_command_shortcuts = default_command_shortcuts; | ||||
IPython.ShortcutManager = ShortcutManager; | ||||
Brian E. Granger
|
r14020 | IPython.KeyboardManager = KeyboardManager; | ||
return IPython; | ||||
}(IPython)); | ||||