From 673dfe336e40dc6ca32aea68f4c39cf6f2e89b12 Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Mon, 9 Dec 2013 23:22:19 -0800 Subject: [PATCH] Fixing bugs and adding automatic KB shortcut help. --- .../static/notebook/js/keyboardmanager.js | 27 ++++---- IPython/html/static/notebook/js/quickhelp.js | 68 +++++++++---------- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/IPython/html/static/notebook/js/keyboardmanager.js b/IPython/html/static/notebook/js/keyboardmanager.js index 306e793cb..dba3b91e5 100644 --- a/IPython/html/static/notebook/js/keyboardmanager.js +++ b/IPython/html/static/notebook/js/keyboardmanager.js @@ -27,7 +27,7 @@ var IPython = (function (IPython) { '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, - '\\ |': 220, '- _': 109, '\' "': 222, + '\\ |': 220, '\' "': 222, '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, @@ -41,12 +41,12 @@ var IPython = (function (IPython) { // These apply to Firefox and Opera var _mozilla_keycodes = { - '; :': 59, '= +': 61 + '; :': 59, '= +': 61, '- _': 109, } // This apply to Webkit and IE var _ie_keycodes = { - '; :': 186, '= +': 187, + '; :': 186, '= +': 187, '- _': 189, } var browser = IPython.utils.browser[0]; @@ -60,20 +60,17 @@ var IPython = (function (IPython) { var keycodes = {}; var inv_keycodes = {}; for (var name in _keycodes) { - console.log(name); var names = name.split(' '); if (names.length === 1) { var n = names[0] keycodes[n] = _keycodes[n] inv_keycodes[_keycodes[n]] = n - // console.log(keycodes[n], inv_keycodes[_keycodes[n]]); } else { var primary = names[0]; var secondary = names[1]; keycodes[primary] = _keycodes[name] keycodes[secondary] = _keycodes[name] inv_keycodes[_keycodes[name]] = primary - // console.log(keycodes[primary], keycodes[secondary], inv_keycodes[_keycodes[name]]) } } @@ -210,21 +207,21 @@ var IPython = (function (IPython) { 'x' : { help : 'cut cell', handler : function (event) { - IPython.IPython.notebook.cut_cell(); + IPython.notebook.cut_cell(); return false; } }, 'c' : { help : 'copy cell', handler : function (event) { - IPython.IPython.notebook.copy_cell(); + IPython.notebook.copy_cell(); return false; } }, 'v' : { help : 'paste cell below', handler : function (event) { - IPython.IPython.notebook.paste_cell_below(); + IPython.notebook.paste_cell_below(); return false; } }, @@ -232,7 +229,6 @@ var IPython = (function (IPython) { help : 'delete cell (press twice)', handler : function (event) { var dc = IPython.delete_count; - console.log('delete_count', dc); if (dc === undefined) { IPython.delete_count = 0; } else if (dc === 0) { @@ -419,6 +415,14 @@ var IPython = (function (IPython) { this._shortcuts = {} } + ShortcutManager.prototype.help = function () { + var help = []; + for (var shortcut in this._shortcuts) { + help.push({shortcut: shortcut, help: this._shortcuts[shortcut]['help']}); + } + return help; + } + ShortcutManager.prototype.canonicalize_key = function (key) { return inv_keycodes[keycodes[key]]; } @@ -430,7 +434,7 @@ var IPython = (function (IPython) { return this.canonicalize_key(values[0]) } else { var modifiers = values.slice(0,-1); - var key = this.canonicalize_key(values[-1]); + var key = this.canonicalize_key(values[values.length-1]); modifiers.sort(); return modifiers.join('+') + '+' + key; } @@ -472,7 +476,6 @@ var IPython = (function (IPython) { var shortcut = this.event_to_shortcut(event); var data = this._shortcuts[shortcut]; if (data !== undefined) { - console.log('call_handler', shortcut, data['help']); var handler = data['handler']; if (handler !== undefined) { return handler(event); diff --git a/IPython/html/static/notebook/js/quickhelp.js b/IPython/html/static/notebook/js/quickhelp.js index 5b6f1704e..12c40aa6f 100644 --- a/IPython/html/static/notebook/js/quickhelp.js +++ b/IPython/html/static/notebook/js/quickhelp.js @@ -23,44 +23,42 @@ var IPython = (function (IPython) { $(this.shortcut_dialog).modal("toggle"); return; } - var body = $('
'); - var shortcuts = [ - {key: 'Shift-Enter', help: 'run cell'}, - {key: 'Ctrl-Enter', help: 'run cell in-place'}, - {key: 'Alt-Enter', help: 'run cell, insert below'}, - {key: 'Ctrl-m x', help: 'cut cell'}, - {key: 'Ctrl-m c', help: 'copy cell'}, - {key: 'Ctrl-m v', help: 'paste cell'}, - {key: 'Ctrl-m d', help: 'delete cell'}, - {key: 'Ctrl-m z', help: 'undo last cell deletion'}, - {key: 'Ctrl-m -', help: 'split cell'}, - {key: 'Ctrl-m a', help: 'insert cell above'}, - {key: 'Ctrl-m b', help: 'insert cell below'}, - {key: 'Ctrl-m o', help: 'toggle output'}, - {key: 'Ctrl-m O', help: 'toggle output scroll'}, - {key: 'Ctrl-m l', help: 'toggle line numbers'}, - {key: 'Ctrl-m s', help: 'save notebook'}, - {key: 'Ctrl-m j', help: 'move cell down'}, - {key: 'Ctrl-m k', help: 'move cell up'}, - {key: 'Ctrl-m y', help: 'code cell'}, - {key: 'Ctrl-m m', help: 'markdown cell'}, - {key: 'Ctrl-m t', help: 'raw cell'}, - {key: 'Ctrl-m 1-6', help: 'heading 1-6 cell'}, - {key: 'Ctrl-m p', help: 'select previous'}, - {key: 'Ctrl-m n', help: 'select next'}, - {key: 'Ctrl-m i', help: 'interrupt kernel'}, - {key: 'Ctrl-m .', help: 'restart kernel'}, - {key: 'Ctrl-m h', help: 'show keyboard shortcuts'} - ]; - for (var i=0; i').addClass('quickhelp'). - append($('').addClass('shortcut_key').html(shortcuts[i].key)). - append($('').addClass('shortcut_descr').html(' : ' + shortcuts[i].help)) - ); + var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); + var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); + var help, shortcut; + var element = $('
'); + + // Command mode + var cmd_div = $('
').addClass('clearfix').append($('

Command Mode

')); + element.append(cmd_div); + for (var i=0; i').addClass('quickhelp'). + append($('').addClass('shortcut_key').html(shortcut)). + append($('').addClass('shortcut_descr').html(' : ' + help)) + ); + } + }; + + // Edit mode + var edit_div = $('
').addClass('clearfix').append($('

Edit Mode

')); + element.append(edit_div); + for (var i=0; i').addClass('quickhelp'). + append($('').addClass('shortcut_key').html(shortcut)). + append($('').addClass('shortcut_descr').html(' : ' + help)) + ); + } }; + this.shortcut_dialog = IPython.dialog.modal({ title : "Keyboard shortcuts", - body : body, + body : element, destroy : false, buttons : { Close : {}