Show More
@@ -1,156 +1,164 | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | //============================================================================ |
|
5 | 5 | // QuickHelp button |
|
6 | 6 | //============================================================================ |
|
7 | 7 | |
|
8 | 8 | var IPython = (function (IPython) { |
|
9 | 9 | "use strict"; |
|
10 | 10 | |
|
11 | var platform = IPython.utils.platform; | |
|
12 | ||
|
11 | 13 | var QuickHelp = function (selector) { |
|
12 | 14 | }; |
|
15 | var cmd_ctrl = 'Ctrl'; | |
|
16 | ||
|
17 | if (platform === 'MacOS') { | |
|
18 | cmd_ctrl = 'Cmd'; | |
|
19 | } | |
|
13 | 20 | |
|
14 | 21 | var cm_shortcuts = [ |
|
15 | 22 | { shortcut:"Insert", help:"toggle overwrite" }, |
|
16 | 23 | { shortcut:"Tab", help:"code completion" }, |
|
17 | 24 | { shortcut:"Shift-Tab", help:"help introspection" }, |
|
18 |
{ shortcut: |
|
|
19 |
{ shortcut: |
|
|
20 |
{ shortcut: |
|
|
21 |
{ shortcut: |
|
|
22 |
{ shortcut: |
|
|
23 |
{ shortcut: |
|
|
24 |
{ shortcut: |
|
|
25 |
{ shortcut: |
|
|
26 |
{ shortcut: |
|
|
25 | { shortcut: cmd_ctrl + "-]", help:"indent" }, | |
|
26 | { shortcut: cmd_ctrl + "-[", help:"dedent" }, | |
|
27 | { shortcut: cmd_ctrl + "-A", help:"select all" }, | |
|
28 | { shortcut: cmd_ctrl + "-D", help:"delete line" }, | |
|
29 | { shortcut: cmd_ctrl + "-Z", help:"undo" }, | |
|
30 | { shortcut: cmd_ctrl + "-Shift-Z", help:"redo" }, | |
|
31 | { shortcut: cmd_ctrl + "-Y", help:"redo" }, | |
|
32 | { shortcut: cmd_ctrl + "-Up", help:"go to cell start" }, | |
|
33 | { shortcut: cmd_ctrl + "-End", help:"go to cell start" }, | |
|
27 | 34 | { shortcut:"PageUp", help:"go to cell start" }, |
|
28 | 35 | { shortcut:"---", help:"go to cell end" }, |
|
29 |
{ shortcut: |
|
|
36 | { shortcut: cmd_ctrl + "-Down", help:"go to cell end" }, | |
|
30 | 37 | { shortcut:"PageDown", help:"go to cell end" }, |
|
31 | 38 | { shortcut:"Alt-Left", help:"go one word left" }, |
|
32 | 39 | { shortcut:"Alt-Right", help:"go one word right" }, |
|
33 |
{ shortcut: |
|
|
40 | { shortcut: cmd_ctrl + "-Left", help:"go to line start" }, | |
|
34 | 41 | { shortcut:"Home", help:"go to line start" }, |
|
35 |
{ shortcut: |
|
|
42 | { shortcut: cmd_ctrl + "-Right", help:"go to line end" }, | |
|
36 | 43 | { shortcut:"End", help:"go to line end" }, |
|
37 | 44 | { shortcut:"Alt-Backspace", help:"del word before" }, |
|
38 | 45 | |
|
46 | ||
|
39 | 47 | ] |
|
40 | 48 | |
|
41 | 49 | |
|
42 | 50 | QuickHelp.prototype.show_keyboard_shortcuts = function () { |
|
43 | 51 | // toggles display of keyboard shortcut dialog |
|
44 | 52 | var that = this; |
|
45 | 53 | if ( this.force_rebuild ) { |
|
46 | 54 | this.shortcut_dialog.remove(); |
|
47 | 55 | delete(this.shortcut_dialog); |
|
48 | 56 | this.force_rebuild = false; |
|
49 | 57 | } |
|
50 | 58 | if ( this.shortcut_dialog ){ |
|
51 | 59 | // if dialog is already shown, close it |
|
52 | 60 | $(this.shortcut_dialog).modal("toggle"); |
|
53 | 61 | return; |
|
54 | 62 | } |
|
55 | 63 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
56 | 64 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
57 | 65 | var help, shortcut; |
|
58 | 66 | var i, half, n; |
|
59 | 67 | var element = $('<div/>'); |
|
60 | 68 | |
|
61 | 69 | // The documentation |
|
62 | 70 | var doc = $('<div/>').addClass('alert'); |
|
63 | 71 | doc.append( |
|
64 | 72 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') |
|
65 | 73 | ).append( |
|
66 | 74 | 'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ |
|
67 | 75 | 'allows you to type code/text into a cell and is indicated by a green cell '+ |
|
68 | 76 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ |
|
69 | 77 | 'and is indicated by a grey cell border.' |
|
70 | 78 | ); |
|
71 | 79 | element.append(doc); |
|
72 | 80 | |
|
73 | 81 | // Command mode |
|
74 | 82 | var cmd_div = this.build_command_help(); |
|
75 | 83 | element.append(cmd_div); |
|
76 | 84 | |
|
77 | 85 | // Edit mode |
|
78 | 86 | var edit_div = this.build_edit_help(); |
|
79 | 87 | element.append(edit_div); |
|
80 | 88 | |
|
81 | 89 | // CodeMirror shortcuts |
|
82 | 90 | var cm_div = build_div('', cm_shortcuts); |
|
83 | 91 | element.append(cm_div); |
|
84 | 92 | |
|
85 | 93 | this.shortcut_dialog = IPython.dialog.modal({ |
|
86 | 94 | title : "Keyboard shortcuts", |
|
87 | 95 | body : element, |
|
88 | 96 | destroy : false, |
|
89 | 97 | buttons : { |
|
90 | 98 | Close : {} |
|
91 | 99 | } |
|
92 | 100 | }); |
|
93 | 101 | |
|
94 | 102 | $([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); |
|
95 | 103 | }; |
|
96 | 104 | |
|
97 | 105 | QuickHelp.prototype.build_command_help = function () { |
|
98 | 106 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); |
|
99 | 107 | return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts); |
|
100 | 108 | }; |
|
101 | 109 | |
|
102 | 110 | var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' }; |
|
103 | 111 | var prettify = function (s) { |
|
104 | 112 | s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key |
|
105 | 113 | var keys = s.split('-'); |
|
106 | 114 | var k, i; |
|
107 | 115 | for (i in keys) { |
|
108 | 116 | k = keys[i]; |
|
109 | 117 | if ( k.length == 1 ) { |
|
110 | 118 | keys[i] = "<code><strong>" + k + "</strong></code>"; |
|
111 | 119 | continue; // leave individual keys lower-cased |
|
112 | 120 | } |
|
113 | 121 | keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); |
|
114 | 122 | keys[i] = "<code><strong>" + keys[i] + "</strong></code>"; |
|
115 | 123 | } |
|
116 | 124 | return keys.join('-'); |
|
117 | 125 | |
|
118 | 126 | |
|
119 | 127 | }; |
|
120 | 128 | |
|
121 | 129 | QuickHelp.prototype.build_edit_help = function () { |
|
122 | 130 | var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); |
|
123 | 131 | // Edit mode |
|
124 | 132 | return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts); |
|
125 | 133 | }; |
|
126 | 134 | |
|
127 | 135 | var build_one = function (s) { |
|
128 | 136 | var help = s.help; |
|
129 | 137 | var shortcut = prettify(s.shortcut); |
|
130 | 138 | return $('<div>').addClass('quickhelp'). |
|
131 | 139 | append($('<span/>').addClass('shortcut_key').append($(shortcut))). |
|
132 | 140 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)); |
|
133 | 141 | |
|
134 | 142 | }; |
|
135 | 143 | |
|
136 | 144 | var build_div = function (title, shortcuts) { |
|
137 | 145 | var i, half, n; |
|
138 | 146 | var div = $('<div/>').append($(title)); |
|
139 | 147 | var sub_div = $('<div/>').addClass('hbox'); |
|
140 | 148 | var col1 = $('<div/>').addClass('box-flex1'); |
|
141 | 149 | var col2 = $('<div/>').addClass('box-flex1'); |
|
142 | 150 | n = shortcuts.length; |
|
143 | 151 | half = ~~(n/2); // Truncate :) |
|
144 | 152 | for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); } |
|
145 | 153 | for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); } |
|
146 | 154 | sub_div.append(col1).append(col2); |
|
147 | 155 | div.append(sub_div); |
|
148 | 156 | return div; |
|
149 | 157 | }; |
|
150 | 158 | |
|
151 | 159 | // Set module variables |
|
152 | 160 | IPython.QuickHelp = QuickHelp; |
|
153 | 161 | |
|
154 | 162 | return IPython; |
|
155 | 163 | |
|
156 | 164 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now