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