##// END OF EJS Templates
DRY: refactor common keyboard handling to Cell...
Paul Ivanov -
Show More
@@ -19,6 +19,7 b' var IPython = (function (IPython) {'
19 "use strict";
19 "use strict";
20
20
21 var utils = IPython.utils;
21 var utils = IPython.utils;
22 var keycodes = IPython.keyboard.keycodes;
22
23
23 /**
24 /**
24 * The Base `Cell` class from which to inherit
25 * The Base `Cell` class from which to inherit
@@ -153,6 +154,59 b' var IPython = (function (IPython) {'
153 });
154 });
154 }
155 }
155 };
156 };
157
158 /**
159 * This method gets called in CodeMirror's onKeyDown/onKeyPress
160 * handlers and is used to provide custom key handling.
161 *
162 * To have custom handling, subclasses should override this method, but still call it
163 * in order to process the Edit mode keyboard shortcuts.
164 *
165 * @method handle_codemirror_keyevent
166 * @param {CodeMirror} editor - The codemirror instance bound to the cell
167 * @param {event} event -
168 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
169 */
170 Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
171 var that = this;
172
173 if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) {
174 // Always ignore shift-enter in CodeMirror as we handle it.
175 return true;
176 } else if (event.which === keycodes.up && event.type === 'keydown') {
177 // If we are not at the top, let CM handle the up arrow and
178 // prevent the global keydown handler from handling it.
179 if (!that.at_top()) {
180 event.stop();
181 return false;
182 } else {
183 return true;
184 };
185 } else if (event.which === keycodes.down && event.type === 'keydown') {
186 // If we are not at the bottom, let CM handle the down arrow and
187 // prevent the global keydown handler from handling it.
188 if (!that.at_bottom()) {
189 event.stop();
190 return false;
191 } else {
192 return true;
193 };
194 } else if (event.which === keycodes.esc && event.type === 'keydown') {
195 if (that.code_mirror.options.keyMap === "vim-insert") {
196 // vim keyMap is active and in insert mode. In this case we leave vim
197 // insert mode, but remain in notebook edit mode.
198 // Let' CM handle this event and prevent global handling.
199 event.stop();
200 return false;
201 } else {
202 // vim keyMap is not active. Leave notebook edit mode.
203 // Don't let CM handle the event, defer to global handling.
204 return true;
205 }
206 }
207 return false;
208 };
209
156
210
157 /**
211 /**
158 * Triger typsetting of math by mathjax on current cell element
212 * Triger typsetting of math by mathjax on current cell element
@@ -257,7 +311,6 b' var IPython = (function (IPython) {'
257 var cm = this.code_mirror
311 var cm = this.code_mirror
258 var cursor = cm.getCursor();
312 var cursor = cm.getCursor();
259 if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) {
313 if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) {
260 console.log('at top');
261 return true;
314 return true;
262 } else {
315 } else {
263 return false;
316 return false;
@@ -194,60 +194,26 b' var IPython = (function (IPython) {'
194 this.auto_highlight();
194 this.auto_highlight();
195 }
195 }
196
196
197 if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) {
197 if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
198 // Always ignore shift-enter in CodeMirror as we handle it.
199 return true;
200 } else if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
201 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
198 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
202 // browser and keyboard layout !
199 // browser and keyboard layout !
203 // Pressing '(' , request tooltip, don't forget to reappend it
200 // Pressing '(' , request tooltip, don't forget to reappend it
204 // The second argument says to hide the tooltip if the docstring
201 // The second argument says to hide the tooltip if the docstring
205 // is actually empty
202 // is actually empty
206 IPython.tooltip.pending(that, true);
203 IPython.tooltip.pending(that, true);
207 } else if (event.which === keycodes.up && event.type === 'keydown') {
204 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
208 // If we are not at the top, let CM handle the up arrow and
205 // If tooltip is active, cancel it.
209 // prevent the global keydown handler from handling it.
206 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
210 if (!that.at_top()) {
207 // force=true. Because of this it won't actually close the tooltip
211 event.stop();
208 // if it is in sticky mode. Thus, we have to check again if it is open
212 return false;
209 // and close it with force=true.
213 } else {
210 if (!IPython.tooltip._hidden) {
214 return true;
211 IPython.tooltip.remove_and_cancel_tooltip(true);
215 }
216 } else if (event.which === keycodes.esc && event.type === 'keydown') {
217 // First see if the tooltip is active and if so cancel it.
218 if (tooltip_closed) {
219 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
220 // force=true. Because of this it won't actually close the tooltip
221 // if it is in sticky mode. Thus, we have to check again if it is open
222 // and close it with force=true.
223 if (!IPython.tooltip._hidden) {
224 IPython.tooltip.remove_and_cancel_tooltip(true);
225 }
226 // If we closed the tooltip, don't let CM or the global handlers
227 // handle this event.
228 event.stop();
229 return true;
230 }
231 if (that.code_mirror.options.keyMap === "vim-insert") {
232 // vim keyMap is active and in insert mode. In this case we leave vim
233 // insert mode, but remain in notebook edit mode.
234 // Let' CM handle this event and prevent global handling.
235 event.stop();
236 return false;
237 } else {
238 // vim keyMap is not active. Leave notebook edit mode.
239 // Don't let CM handle the event, defer to global handling.
240 return true;
241 }
242 } else if (event.which === keycodes.down && event.type === 'keydown') {
243 // If we are not at the bottom, let CM handle the down arrow and
244 // prevent the global keydown handler from handling it.
245 if (!that.at_bottom()) {
246 event.stop();
247 return false;
248 } else {
249 return true;
250 }
212 }
213 // If we closed the tooltip, don't let CM or the global handlers
214 // handle this event.
215 event.stop();
216 return true;
251 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
217 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
252 if (editor.somethingSelected()){
218 if (editor.somethingSelected()){
253 var anchor = editor.getCursor("anchor");
219 var anchor = editor.getCursor("anchor");
@@ -275,12 +241,11 b' var IPython = (function (IPython) {'
275 this.completer.startCompletion();
241 this.completer.startCompletion();
276 return true;
242 return true;
277 }
243 }
278 } else {
244 }
279 // keypress/keyup also trigger on TAB press, and we don't want to
245
280 // use those to disable tab completion.
246 // keyboard event wasn't one of those unique to code cells, let's see
281 return false;
247 // if it's one of the generic ones (i.e. check edit mode shortcuts)
282 }
248 return IPython.Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event])
283 return false;
284 };
249 };
285
250
286 // Kernel related calls.
251 // Kernel related calls.
@@ -113,57 +113,6 b' var IPython = (function (IPython) {'
113 });
113 });
114 };
114 };
115
115
116 /**
117 * This method gets called in CodeMirror's onKeyDown/onKeyPress
118 * handlers and is used to provide custom key handling.
119 *
120 * Subclass should override this method to have custom handling
121 *
122 * @method handle_codemirror_keyevent
123 * @param {CodeMirror} editor - The codemirror instance bound to the cell
124 * @param {event} event -
125 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
126 */
127 TextCell.prototype.handle_codemirror_keyevent = function (editor, event) {
128 var that = this;
129
130 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey || event.altKey)) {
131 // Always ignore shift-enter in CodeMirror as we handle it.
132 return true;
133 } else if (event.which === keycodes.up && event.type === 'keydown') {
134 // If we are not at the top, let CM handle the up arrow and
135 // prevent the global keydown handler from handling it.
136 if (!that.at_top()) {
137 event.stop();
138 return false;
139 } else {
140 return true;
141 };
142 } else if (event.which === keycodes.down && event.type === 'keydown') {
143 // If we are not at the bottom, let CM handle the down arrow and
144 // prevent the global keydown handler from handling it.
145 if (!that.at_bottom()) {
146 event.stop();
147 return false;
148 } else {
149 return true;
150 };
151 } else if (event.which === keycodes.esc && event.type === 'keydown') {
152 if (that.code_mirror.options.keyMap === "vim-insert") {
153 // vim keyMap is active and in insert mode. In this case we leave vim
154 // insert mode, but remain in notebook edit mode.
155 // Let' CM handle this event and prevent global handling.
156 event.stop();
157 return false;
158 } else {
159 // vim keyMap is not active. Leave notebook edit mode.
160 // Don't let CM handle the event, defer to global handling.
161 return true;
162 }
163 }
164 return false;
165 };
166
167 // Cell level actions
116 // Cell level actions
168
117
169 TextCell.prototype.select = function () {
118 TextCell.prototype.select = function () {
General Comments 0
You need to be logged in to leave comments. Login now