Show More
@@ -1,231 +1,232 b'' | |||
|
1 | 1 | // Copyright (c) IPython Development Team. |
|
2 | 2 | // Distributed under the terms of the Modified BSD License. |
|
3 | 3 | /** |
|
4 | 4 | * |
|
5 | 5 | * |
|
6 | 6 | * @module keyboardmanager |
|
7 | 7 | * @namespace keyboardmanager |
|
8 | 8 | * @class KeyboardManager |
|
9 | 9 | */ |
|
10 | 10 | |
|
11 | 11 | define([ |
|
12 | 12 | 'base/js/namespace', |
|
13 | 13 | 'jquery', |
|
14 | 14 | 'base/js/utils', |
|
15 | 15 | 'base/js/keyboard', |
|
16 | 16 | ], function(IPython, $, utils, keyboard) { |
|
17 | 17 | "use strict"; |
|
18 | 18 | |
|
19 | 19 | // Main keyboard manager for the notebook |
|
20 | 20 | var keycodes = keyboard.keycodes; |
|
21 | 21 | |
|
22 | 22 | var KeyboardManager = function (options) { |
|
23 | 23 | /** |
|
24 | 24 | * A class to deal with keyboard event and shortcut |
|
25 | 25 | * |
|
26 | 26 | * @class KeyboardManager |
|
27 | 27 | * @constructor |
|
28 | 28 | * @param options {dict} Dictionary of keyword arguments : |
|
29 | 29 | * @param options.events {$(Events)} instance |
|
30 | 30 | * @param options.pager: {Pager} pager instance |
|
31 | 31 | */ |
|
32 | 32 | this.mode = 'command'; |
|
33 | 33 | this.enabled = true; |
|
34 | 34 | this.pager = options.pager; |
|
35 | 35 | this.quick_help = undefined; |
|
36 | 36 | this.notebook = undefined; |
|
37 | 37 | this.last_mode = undefined; |
|
38 | 38 | this.bind_events(); |
|
39 | 39 | this.env = {pager:this.pager}; |
|
40 | 40 | this.actions = options.actions; |
|
41 | 41 | this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env ); |
|
42 | 42 | this.command_shortcuts.add_shortcuts(this.get_default_common_shortcuts()); |
|
43 | 43 | this.command_shortcuts.add_shortcuts(this.get_default_command_shortcuts()); |
|
44 | 44 | this.edit_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env); |
|
45 | 45 | this.edit_shortcuts.add_shortcuts(this.get_default_common_shortcuts()); |
|
46 | 46 | this.edit_shortcuts.add_shortcuts(this.get_default_edit_shortcuts()); |
|
47 | 47 | Object.seal(this); |
|
48 | 48 | }; |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | /** |
|
54 | 54 | * Return a dict of common shortcut |
|
55 | 55 | * @method get_default_common_shortcuts |
|
56 | 56 | * |
|
57 | 57 | * @example Example of returned shortcut |
|
58 | 58 | * ``` |
|
59 | 59 | * 'shortcut-key': 'action-name' |
|
60 | 60 | * // a string representing the shortcut as dash separated value. |
|
61 | 61 | * // e.g. 'shift' , 'shift-enter', 'cmd-t' |
|
62 | 62 | *``` |
|
63 | 63 | */ |
|
64 | 64 | KeyboardManager.prototype.get_default_common_shortcuts = function() { |
|
65 | 65 | return { |
|
66 | 66 | 'shift' : 'ipython.ignore', |
|
67 | 67 | 'shift-enter' : 'ipython.run-select-next', |
|
68 | 68 | 'ctrl-enter' : 'ipython.execute-in-place', |
|
69 | 69 | 'alt-enter' : 'ipython.execute-and-insert-after', |
|
70 | 70 | // cmd on mac, ctrl otherwise |
|
71 | 71 | 'cmdtrl-s' : 'ipython.save-notebook', |
|
72 | 72 | }; |
|
73 | 73 | }; |
|
74 | 74 | |
|
75 | 75 | KeyboardManager.prototype.get_default_edit_shortcuts = function() { |
|
76 | 76 | return { |
|
77 | 77 | 'esc' : 'ipython.go-to-command-mode', |
|
78 | 78 | 'ctrl-m' : 'ipython.go-to-command-mode', |
|
79 | 79 | 'up' : 'ipython.move-cursor-up-or-previous-cell', |
|
80 | 80 | 'down' : 'ipython.move-cursor-down-or-next-cell', |
|
81 | 81 | 'ctrl-shift--' : 'ipython.split-cell-at-cursor', |
|
82 | 82 | 'ctrl-shift-subtract' : 'ipython.split-cell-at-cursor' |
|
83 | 83 | }; |
|
84 | 84 | }; |
|
85 | 85 | |
|
86 | 86 | KeyboardManager.prototype.get_default_command_shortcuts = function() { |
|
87 | 87 | return { |
|
88 | 88 | 'shift-space': 'ipython.scroll-up', |
|
89 | 89 | 'shift-v' : 'ipython.paste-cell-before', |
|
90 | 90 | 'shift-m' : 'ipython.merge-selected-cell-with-cell-after', |
|
91 | 91 | 'shift-o' : 'ipython.toggle-output-scrolling-selected-cell', |
|
92 | 92 | 'ctrl-j' : 'ipython.move-selected-cell-down', |
|
93 | 93 | 'ctrl-k' : 'ipython.move-selected-cell-up', |
|
94 | 94 | 'enter' : 'ipython.enter-edit-mode', |
|
95 | 95 | 'space' : 'ipython.scroll-down', |
|
96 | 96 | 'down' : 'ipython.select-next-cell', |
|
97 | 97 | 'i,i' : 'ipython.interrupt-kernel', |
|
98 | 98 | '0,0' : 'ipython.restart-kernel', |
|
99 | 99 | 'd,d' : 'ipython.delete-cell', |
|
100 | 'esc': 'ipython.close-pager', | |
|
100 | 101 | 'up' : 'ipython.select-previous-cell', |
|
101 | 102 | 'k' : 'ipython.select-previous-cell', |
|
102 | 103 | 'j' : 'ipython.select-next-cell', |
|
103 | 104 | 'x' : 'ipython.cut-selected-cell', |
|
104 | 105 | 'c' : 'ipython.copy-selected-cell', |
|
105 | 106 | 'v' : 'ipython.paste-cell-after', |
|
106 | 107 | 'a' : 'ipython.insert-cell-before', |
|
107 | 108 | 'b' : 'ipython.insert-cell-after', |
|
108 | 109 | 'y' : 'ipython.change-selected-cell-to-code-cell', |
|
109 | 110 | 'm' : 'ipython.change-selected-cell-to-markdown-cell', |
|
110 | 111 | 'r' : 'ipython.change-selected-cell-to-raw-cell', |
|
111 | 112 | '1' : 'ipython.change-selected-cell-to-heading-1', |
|
112 | 113 | '2' : 'ipython.change-selected-cell-to-heading-2', |
|
113 | 114 | '3' : 'ipython.change-selected-cell-to-heading-3', |
|
114 | 115 | '4' : 'ipython.change-selected-cell-to-heading-4', |
|
115 | 116 | '5' : 'ipython.change-selected-cell-to-heading-5', |
|
116 | 117 | '6' : 'ipython.change-selected-cell-to-heading-6', |
|
117 | 118 | 'o' : 'ipython.toggle-output-visibility-selected-cell', |
|
118 | 119 | 's' : 'ipython.save-notebook', |
|
119 | 120 | 'l' : 'ipython.toggle-line-number-selected-cell', |
|
120 | 121 | 'h' : 'ipython.show-keyboard-shortcut-help-dialog', |
|
121 | 122 | 'z' : 'ipython.undo-last-cell-deletion', |
|
122 | 123 | 'q' : 'ipython.close-pager', |
|
123 | 124 | }; |
|
124 | 125 | }; |
|
125 | 126 | |
|
126 | 127 | KeyboardManager.prototype.bind_events = function () { |
|
127 | 128 | var that = this; |
|
128 | 129 | $(document).keydown(function (event) { |
|
129 | 130 | if(event._ipkmIgnore===true||(event.originalEvent||{})._ipkmIgnore===true){ |
|
130 | 131 | return false; |
|
131 | 132 | } |
|
132 | 133 | return that.handle_keydown(event); |
|
133 | 134 | }); |
|
134 | 135 | }; |
|
135 | 136 | |
|
136 | 137 | KeyboardManager.prototype.set_notebook = function (notebook) { |
|
137 | 138 | this.notebook = notebook; |
|
138 | 139 | this.actions.extend_env({notebook:notebook}); |
|
139 | 140 | }; |
|
140 | 141 | |
|
141 | 142 | KeyboardManager.prototype.set_quickhelp = function (notebook) { |
|
142 | 143 | this.actions.extend_env({quick_help:notebook}); |
|
143 | 144 | }; |
|
144 | 145 | |
|
145 | 146 | |
|
146 | 147 | KeyboardManager.prototype.handle_keydown = function (event) { |
|
147 | 148 | /** |
|
148 | 149 | * returning false from this will stop event propagation |
|
149 | 150 | **/ |
|
150 | 151 | |
|
151 | 152 | if (event.which === keycodes.esc) { |
|
152 | 153 | // Intercept escape at highest level to avoid closing |
|
153 | 154 | // websocket connection with firefox |
|
154 | 155 | event.preventDefault(); |
|
155 | 156 | } |
|
156 | 157 | |
|
157 | 158 | if (!this.enabled) { |
|
158 | 159 | if (event.which === keycodes.esc) { |
|
159 | 160 | this.notebook.command_mode(); |
|
160 | 161 | return false; |
|
161 | 162 | } |
|
162 | 163 | return true; |
|
163 | 164 | } |
|
164 | 165 | |
|
165 | 166 | if (this.mode === 'edit') { |
|
166 | 167 | return this.edit_shortcuts.call_handler(event); |
|
167 | 168 | } else if (this.mode === 'command') { |
|
168 | 169 | return this.command_shortcuts.call_handler(event); |
|
169 | 170 | } |
|
170 | 171 | return true; |
|
171 | 172 | }; |
|
172 | 173 | |
|
173 | 174 | KeyboardManager.prototype.edit_mode = function () { |
|
174 | 175 | this.last_mode = this.mode; |
|
175 | 176 | this.mode = 'edit'; |
|
176 | 177 | }; |
|
177 | 178 | |
|
178 | 179 | KeyboardManager.prototype.command_mode = function () { |
|
179 | 180 | this.last_mode = this.mode; |
|
180 | 181 | this.mode = 'command'; |
|
181 | 182 | }; |
|
182 | 183 | |
|
183 | 184 | KeyboardManager.prototype.enable = function () { |
|
184 | 185 | this.enabled = true; |
|
185 | 186 | }; |
|
186 | 187 | |
|
187 | 188 | KeyboardManager.prototype.disable = function () { |
|
188 | 189 | this.enabled = false; |
|
189 | 190 | }; |
|
190 | 191 | |
|
191 | 192 | KeyboardManager.prototype.register_events = function (e) { |
|
192 | 193 | var that = this; |
|
193 | 194 | var handle_focus = function () { |
|
194 | 195 | that.disable(); |
|
195 | 196 | }; |
|
196 | 197 | var handle_blur = function () { |
|
197 | 198 | that.enable(); |
|
198 | 199 | }; |
|
199 | 200 | e.on('focusin', handle_focus); |
|
200 | 201 | e.on('focusout', handle_blur); |
|
201 | 202 | // TODO: Very strange. The focusout event does not seem fire for the |
|
202 | 203 | // bootstrap textboxes on FF25&26... This works around that by |
|
203 | 204 | // registering focus and blur events recursively on all inputs within |
|
204 | 205 | // registered element. |
|
205 | 206 | e.find('input').blur(handle_blur); |
|
206 | 207 | e.on('DOMNodeInserted', function (event) { |
|
207 | 208 | var target = $(event.target); |
|
208 | 209 | if (target.is('input')) { |
|
209 | 210 | target.blur(handle_blur); |
|
210 | 211 | } else { |
|
211 | 212 | target.find('input').blur(handle_blur); |
|
212 | 213 | } |
|
213 | 214 | }); |
|
214 | 215 | // There are times (raw_input) where we remove the element from the DOM before |
|
215 | 216 | // focusout is called. In this case we bind to the remove event of jQueryUI, |
|
216 | 217 | // which gets triggered upon removal, iff it is focused at the time. |
|
217 | 218 | // is_focused must be used to check for the case where an element within |
|
218 | 219 | // the element being removed is focused. |
|
219 | 220 | e.on('remove', function () { |
|
220 | 221 | if (utils.is_focused(e[0])) { |
|
221 | 222 | that.enable(); |
|
222 | 223 | } |
|
223 | 224 | }); |
|
224 | 225 | }; |
|
225 | 226 | |
|
226 | 227 | |
|
227 | 228 | // For backwards compatibility. |
|
228 | 229 | IPython.KeyboardManager = KeyboardManager; |
|
229 | 230 | |
|
230 | 231 | return {'KeyboardManager': KeyboardManager}; |
|
231 | 232 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now