Show More
@@ -1,543 +1,596 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // Cell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule Cell |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | var IPython = (function (IPython) { |
|
19 | 19 | "use strict"; |
|
20 | 20 | |
|
21 | 21 | var utils = IPython.utils; |
|
22 | var keycodes = IPython.keyboard.keycodes; | |
|
22 | 23 | |
|
23 | 24 | /** |
|
24 | 25 | * The Base `Cell` class from which to inherit |
|
25 | 26 | * @class Cell |
|
26 | 27 | **/ |
|
27 | 28 | |
|
28 | 29 | /* |
|
29 | 30 | * @constructor |
|
30 | 31 | * |
|
31 | 32 | * @param {object|undefined} [options] |
|
32 | 33 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters |
|
33 | 34 | */ |
|
34 | 35 | var Cell = function (options) { |
|
35 | 36 | |
|
36 | 37 | options = this.mergeopt(Cell, options); |
|
37 | 38 | // superclass default overwrite our default |
|
38 | 39 | |
|
39 | 40 | this.placeholder = options.placeholder || ''; |
|
40 | 41 | this.read_only = options.cm_config.readOnly; |
|
41 | 42 | this.selected = false; |
|
42 | 43 | this.rendered = false; |
|
43 | 44 | this.mode = 'command'; |
|
44 | 45 | this.metadata = {}; |
|
45 | 46 | // load this from metadata later ? |
|
46 | 47 | this.user_highlight = 'auto'; |
|
47 | 48 | this.cm_config = options.cm_config; |
|
48 | 49 | this.cell_id = utils.uuid(); |
|
49 | 50 | this._options = options; |
|
50 | 51 | |
|
51 | 52 | // For JS VM engines optimization, attributes should be all set (even |
|
52 | 53 | // to null) in the constructor, and if possible, if different subclass |
|
53 | 54 | // have new attributes with same name, they should be created in the |
|
54 | 55 | // same order. Easiest is to create and set to null in parent class. |
|
55 | 56 | |
|
56 | 57 | this.element = null; |
|
57 | 58 | this.cell_type = this.cell_type || null; |
|
58 | 59 | this.code_mirror = null; |
|
59 | 60 | |
|
60 | 61 | this.create_element(); |
|
61 | 62 | if (this.element !== null) { |
|
62 | 63 | this.element.data("cell", this); |
|
63 | 64 | this.bind_events(); |
|
64 | 65 | this.init_classes(); |
|
65 | 66 | } |
|
66 | 67 | }; |
|
67 | 68 | |
|
68 | 69 | Cell.options_default = { |
|
69 | 70 | cm_config : { |
|
70 | 71 | indentUnit : 4, |
|
71 | 72 | readOnly: false, |
|
72 | 73 | theme: "default" |
|
73 | 74 | } |
|
74 | 75 | }; |
|
75 | 76 | |
|
76 | 77 | // FIXME: Workaround CM Bug #332 (Safari segfault on drag) |
|
77 | 78 | // by disabling drag/drop altogether on Safari |
|
78 | 79 | // https://github.com/marijnh/CodeMirror/issues/332 |
|
79 | 80 | if (utils.browser[0] == "Safari") { |
|
80 | 81 | Cell.options_default.cm_config.dragDrop = false; |
|
81 | 82 | } |
|
82 | 83 | |
|
83 | 84 | Cell.prototype.mergeopt = function(_class, options, overwrite){ |
|
84 | 85 | options = options || {}; |
|
85 | 86 | overwrite = overwrite || {}; |
|
86 | 87 | return $.extend(true, {}, _class.options_default, options, overwrite); |
|
87 | 88 | }; |
|
88 | 89 | |
|
89 | 90 | /** |
|
90 | 91 | * Empty. Subclasses must implement create_element. |
|
91 | 92 | * This should contain all the code to create the DOM element in notebook |
|
92 | 93 | * and will be called by Base Class constructor. |
|
93 | 94 | * @method create_element |
|
94 | 95 | */ |
|
95 | 96 | Cell.prototype.create_element = function () { |
|
96 | 97 | }; |
|
97 | 98 | |
|
98 | 99 | Cell.prototype.init_classes = function () { |
|
99 | 100 | // Call after this.element exists to initialize the css classes |
|
100 | 101 | // related to selected, rendered and mode. |
|
101 | 102 | if (this.selected) { |
|
102 | 103 | this.element.addClass('selected'); |
|
103 | 104 | } else { |
|
104 | 105 | this.element.addClass('unselected'); |
|
105 | 106 | } |
|
106 | 107 | if (this.rendered) { |
|
107 | 108 | this.element.addClass('rendered'); |
|
108 | 109 | } else { |
|
109 | 110 | this.element.addClass('unrendered'); |
|
110 | 111 | } |
|
111 | 112 | if (this.mode === 'edit') { |
|
112 | 113 | this.element.addClass('edit_mode'); |
|
113 | 114 | } else { |
|
114 | 115 | this.element.addClass('command_mode'); |
|
115 | 116 | } |
|
116 | 117 | }; |
|
117 | 118 | |
|
118 | 119 | /** |
|
119 | 120 | * Subclasses can implement override bind_events. |
|
120 | 121 | * Be carefull to call the parent method when overwriting as it fires event. |
|
121 | 122 | * this will be triggerd after create_element in constructor. |
|
122 | 123 | * @method bind_events |
|
123 | 124 | */ |
|
124 | 125 | Cell.prototype.bind_events = function () { |
|
125 | 126 | var that = this; |
|
126 | 127 | // We trigger events so that Cell doesn't have to depend on Notebook. |
|
127 | 128 | that.element.click(function (event) { |
|
128 | 129 | if (!that.selected) { |
|
129 | 130 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
130 | 131 | } |
|
131 | 132 | }); |
|
132 | 133 | that.element.focusin(function (event) { |
|
133 | 134 | if (!that.selected) { |
|
134 | 135 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
135 | 136 | } |
|
136 | 137 | }); |
|
137 | 138 | if (this.code_mirror) { |
|
138 | 139 | this.code_mirror.on("change", function(cm, change) { |
|
139 | 140 | $([IPython.events]).trigger("set_dirty.Notebook", {value: true}); |
|
140 | 141 | }); |
|
141 | 142 | } |
|
142 | 143 | if (this.code_mirror) { |
|
143 | 144 | this.code_mirror.on('focus', function(cm, change) { |
|
144 | 145 | $([IPython.events]).trigger('edit_mode.Cell', {cell: that}); |
|
145 | 146 | }); |
|
146 | 147 | } |
|
147 | 148 | if (this.code_mirror) { |
|
148 | 149 | this.code_mirror.on('blur', function(cm, change) { |
|
149 | 150 | // Check if this unfocus event is legit. |
|
150 | 151 | if (!that.should_cancel_blur()) { |
|
151 | 152 | $([IPython.events]).trigger('command_mode.Cell', {cell: that}); |
|
152 | 153 | } |
|
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 | 212 | * Triger typsetting of math by mathjax on current cell element |
|
159 | 213 | * @method typeset |
|
160 | 214 | */ |
|
161 | 215 | Cell.prototype.typeset = function () { |
|
162 | 216 | if (window.MathJax) { |
|
163 | 217 | var cell_math = this.element.get(0); |
|
164 | 218 | MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); |
|
165 | 219 | } |
|
166 | 220 | }; |
|
167 | 221 | |
|
168 | 222 | /** |
|
169 | 223 | * handle cell level logic when a cell is selected |
|
170 | 224 | * @method select |
|
171 | 225 | * @return is the action being taken |
|
172 | 226 | */ |
|
173 | 227 | Cell.prototype.select = function () { |
|
174 | 228 | if (!this.selected) { |
|
175 | 229 | this.element.addClass('selected'); |
|
176 | 230 | this.element.removeClass('unselected'); |
|
177 | 231 | this.selected = true; |
|
178 | 232 | return true; |
|
179 | 233 | } else { |
|
180 | 234 | return false; |
|
181 | 235 | } |
|
182 | 236 | }; |
|
183 | 237 | |
|
184 | 238 | /** |
|
185 | 239 | * handle cell level logic when a cell is unselected |
|
186 | 240 | * @method unselect |
|
187 | 241 | * @return is the action being taken |
|
188 | 242 | */ |
|
189 | 243 | Cell.prototype.unselect = function () { |
|
190 | 244 | if (this.selected) { |
|
191 | 245 | this.element.addClass('unselected'); |
|
192 | 246 | this.element.removeClass('selected'); |
|
193 | 247 | this.selected = false; |
|
194 | 248 | return true; |
|
195 | 249 | } else { |
|
196 | 250 | return false; |
|
197 | 251 | } |
|
198 | 252 | }; |
|
199 | 253 | |
|
200 | 254 | /** |
|
201 | 255 | * handle cell level logic when a cell is rendered |
|
202 | 256 | * @method render |
|
203 | 257 | * @return is the action being taken |
|
204 | 258 | */ |
|
205 | 259 | Cell.prototype.render = function () { |
|
206 | 260 | if (!this.rendered) { |
|
207 | 261 | this.element.addClass('rendered'); |
|
208 | 262 | this.element.removeClass('unrendered'); |
|
209 | 263 | this.rendered = true; |
|
210 | 264 | return true; |
|
211 | 265 | } else { |
|
212 | 266 | return false; |
|
213 | 267 | } |
|
214 | 268 | }; |
|
215 | 269 | |
|
216 | 270 | /** |
|
217 | 271 | * handle cell level logic when a cell is unrendered |
|
218 | 272 | * @method unrender |
|
219 | 273 | * @return is the action being taken |
|
220 | 274 | */ |
|
221 | 275 | Cell.prototype.unrender = function () { |
|
222 | 276 | if (this.rendered) { |
|
223 | 277 | this.element.addClass('unrendered'); |
|
224 | 278 | this.element.removeClass('rendered'); |
|
225 | 279 | this.rendered = false; |
|
226 | 280 | return true; |
|
227 | 281 | } else { |
|
228 | 282 | return false; |
|
229 | 283 | } |
|
230 | 284 | }; |
|
231 | 285 | |
|
232 | 286 | /** |
|
233 | 287 | * Either delegates keyboard shortcut handling to either IPython keyboard |
|
234 | 288 | * manager when in command mode, or CodeMirror when in edit mode |
|
235 | 289 | * |
|
236 | 290 | * @method handle_keyevent |
|
237 | 291 | * @param {CodeMirror} editor - The codemirror instance bound to the cell |
|
238 | 292 | * @param {event} event - |
|
239 | 293 | * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise |
|
240 | 294 | */ |
|
241 | 295 | Cell.prototype.handle_keyevent = function (editor, event) { |
|
242 | 296 | |
|
243 | 297 | // console.log('CM', this.mode, event.which, event.type) |
|
244 | 298 | |
|
245 | 299 | if (this.mode === 'command') { |
|
246 | 300 | return true; |
|
247 | 301 | } else if (this.mode === 'edit') { |
|
248 | 302 | return this.handle_codemirror_keyevent(editor, event); |
|
249 | 303 | } |
|
250 | 304 | }; |
|
251 | 305 | |
|
252 | 306 | /** |
|
253 | 307 | * @method at_top |
|
254 | 308 | * @return {Boolean} |
|
255 | 309 | */ |
|
256 | 310 | Cell.prototype.at_top = function () { |
|
257 | 311 | var cm = this.code_mirror |
|
258 | 312 | var cursor = cm.getCursor(); |
|
259 | 313 | if (cursor.line === 0 && cm.findPosV(cursor, -1, 'line').hitSide) { |
|
260 | console.log('at top'); | |
|
261 | 314 | return true; |
|
262 | 315 | } else { |
|
263 | 316 | return false; |
|
264 | 317 | } |
|
265 | 318 | }; |
|
266 | 319 | |
|
267 | 320 | /** |
|
268 | 321 | * @method at_bottom |
|
269 | 322 | * @return {Boolean} |
|
270 | 323 | * */ |
|
271 | 324 | Cell.prototype.at_bottom = function () { |
|
272 | 325 | var cm = this.code_mirror |
|
273 | 326 | var cursor = cm.getCursor(); |
|
274 | 327 | if (cursor.line === (cm.lineCount()-1) && cm.findPosV(cursor, 1, 'line').hitSide) { |
|
275 | 328 | return true; |
|
276 | 329 | } else { |
|
277 | 330 | return false; |
|
278 | 331 | } |
|
279 | 332 | }; |
|
280 | 333 | /** |
|
281 | 334 | * enter the command mode for the cell |
|
282 | 335 | * @method command_mode |
|
283 | 336 | * @return is the action being taken |
|
284 | 337 | */ |
|
285 | 338 | Cell.prototype.command_mode = function () { |
|
286 | 339 | if (this.mode !== 'command') { |
|
287 | 340 | this.element.addClass('command_mode'); |
|
288 | 341 | this.element.removeClass('edit_mode'); |
|
289 | 342 | this.mode = 'command'; |
|
290 | 343 | return true; |
|
291 | 344 | } else { |
|
292 | 345 | return false; |
|
293 | 346 | } |
|
294 | 347 | }; |
|
295 | 348 | |
|
296 | 349 | /** |
|
297 | 350 | * enter the edit mode for the cell |
|
298 | 351 | * @method command_mode |
|
299 | 352 | * @return is the action being taken |
|
300 | 353 | */ |
|
301 | 354 | Cell.prototype.edit_mode = function () { |
|
302 | 355 | if (this.mode !== 'edit') { |
|
303 | 356 | this.element.addClass('edit_mode'); |
|
304 | 357 | this.element.removeClass('command_mode'); |
|
305 | 358 | this.mode = 'edit'; |
|
306 | 359 | return true; |
|
307 | 360 | } else { |
|
308 | 361 | return false; |
|
309 | 362 | } |
|
310 | 363 | }; |
|
311 | 364 | |
|
312 | 365 | /** |
|
313 | 366 | * Determine whether or not the unfocus event should be aknowledged. |
|
314 | 367 | * |
|
315 | 368 | * @method should_cancel_blur |
|
316 | 369 | * |
|
317 | 370 | * @return results {bool} Whether or not to ignore the cell's blur event. |
|
318 | 371 | **/ |
|
319 | 372 | Cell.prototype.should_cancel_blur = function () { |
|
320 | 373 | return false; |
|
321 | 374 | }; |
|
322 | 375 | |
|
323 | 376 | /** |
|
324 | 377 | * Focus the cell in the DOM sense |
|
325 | 378 | * @method focus_cell |
|
326 | 379 | */ |
|
327 | 380 | Cell.prototype.focus_cell = function () { |
|
328 | 381 | this.element.focus(); |
|
329 | 382 | }; |
|
330 | 383 | |
|
331 | 384 | /** |
|
332 | 385 | * Focus the editor area so a user can type |
|
333 | 386 | * |
|
334 | 387 | * NOTE: If codemirror is focused via a mouse click event, you don't want to |
|
335 | 388 | * call this because it will cause a page jump. |
|
336 | 389 | * @method focus_editor |
|
337 | 390 | */ |
|
338 | 391 | Cell.prototype.focus_editor = function () { |
|
339 | 392 | this.refresh(); |
|
340 | 393 | this.code_mirror.focus(); |
|
341 | 394 | }; |
|
342 | 395 | |
|
343 | 396 | /** |
|
344 | 397 | * Refresh codemirror instance |
|
345 | 398 | * @method refresh |
|
346 | 399 | */ |
|
347 | 400 | Cell.prototype.refresh = function () { |
|
348 | 401 | this.code_mirror.refresh(); |
|
349 | 402 | }; |
|
350 | 403 | |
|
351 | 404 | /** |
|
352 | 405 | * should be overritten by subclass |
|
353 | 406 | * @method get_text |
|
354 | 407 | */ |
|
355 | 408 | Cell.prototype.get_text = function () { |
|
356 | 409 | }; |
|
357 | 410 | |
|
358 | 411 | /** |
|
359 | 412 | * should be overritten by subclass |
|
360 | 413 | * @method set_text |
|
361 | 414 | * @param {string} text |
|
362 | 415 | */ |
|
363 | 416 | Cell.prototype.set_text = function (text) { |
|
364 | 417 | }; |
|
365 | 418 | |
|
366 | 419 | /** |
|
367 | 420 | * should be overritten by subclass |
|
368 | 421 | * serialise cell to json. |
|
369 | 422 | * @method toJSON |
|
370 | 423 | **/ |
|
371 | 424 | Cell.prototype.toJSON = function () { |
|
372 | 425 | var data = {}; |
|
373 | 426 | data.metadata = this.metadata; |
|
374 | 427 | data.cell_type = this.cell_type; |
|
375 | 428 | return data; |
|
376 | 429 | }; |
|
377 | 430 | |
|
378 | 431 | |
|
379 | 432 | /** |
|
380 | 433 | * should be overritten by subclass |
|
381 | 434 | * @method fromJSON |
|
382 | 435 | **/ |
|
383 | 436 | Cell.prototype.fromJSON = function (data) { |
|
384 | 437 | if (data.metadata !== undefined) { |
|
385 | 438 | this.metadata = data.metadata; |
|
386 | 439 | } |
|
387 | 440 | this.celltoolbar.rebuild(); |
|
388 | 441 | }; |
|
389 | 442 | |
|
390 | 443 | |
|
391 | 444 | /** |
|
392 | 445 | * can the cell be split into two cells |
|
393 | 446 | * @method is_splittable |
|
394 | 447 | **/ |
|
395 | 448 | Cell.prototype.is_splittable = function () { |
|
396 | 449 | return true; |
|
397 | 450 | }; |
|
398 | 451 | |
|
399 | 452 | |
|
400 | 453 | /** |
|
401 | 454 | * can the cell be merged with other cells |
|
402 | 455 | * @method is_mergeable |
|
403 | 456 | **/ |
|
404 | 457 | Cell.prototype.is_mergeable = function () { |
|
405 | 458 | return true; |
|
406 | 459 | }; |
|
407 | 460 | |
|
408 | 461 | |
|
409 | 462 | /** |
|
410 | 463 | * @return {String} - the text before the cursor |
|
411 | 464 | * @method get_pre_cursor |
|
412 | 465 | **/ |
|
413 | 466 | Cell.prototype.get_pre_cursor = function () { |
|
414 | 467 | var cursor = this.code_mirror.getCursor(); |
|
415 | 468 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
416 | 469 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
417 | 470 | return text; |
|
418 | 471 | }; |
|
419 | 472 | |
|
420 | 473 | |
|
421 | 474 | /** |
|
422 | 475 | * @return {String} - the text after the cursor |
|
423 | 476 | * @method get_post_cursor |
|
424 | 477 | **/ |
|
425 | 478 | Cell.prototype.get_post_cursor = function () { |
|
426 | 479 | var cursor = this.code_mirror.getCursor(); |
|
427 | 480 | var last_line_num = this.code_mirror.lineCount()-1; |
|
428 | 481 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
429 | 482 | var end = {line:last_line_num, ch:last_line_len}; |
|
430 | 483 | var text = this.code_mirror.getRange(cursor, end); |
|
431 | 484 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
432 | 485 | return text; |
|
433 | 486 | }; |
|
434 | 487 | |
|
435 | 488 | /** |
|
436 | 489 | * Show/Hide CodeMirror LineNumber |
|
437 | 490 | * @method show_line_numbers |
|
438 | 491 | * |
|
439 | 492 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
440 | 493 | **/ |
|
441 | 494 | Cell.prototype.show_line_numbers = function (value) { |
|
442 | 495 | this.code_mirror.setOption('lineNumbers', value); |
|
443 | 496 | this.code_mirror.refresh(); |
|
444 | 497 | }; |
|
445 | 498 | |
|
446 | 499 | /** |
|
447 | 500 | * Toggle CodeMirror LineNumber |
|
448 | 501 | * @method toggle_line_numbers |
|
449 | 502 | **/ |
|
450 | 503 | Cell.prototype.toggle_line_numbers = function () { |
|
451 | 504 | var val = this.code_mirror.getOption('lineNumbers'); |
|
452 | 505 | this.show_line_numbers(!val); |
|
453 | 506 | }; |
|
454 | 507 | |
|
455 | 508 | /** |
|
456 | 509 | * Force codemirror highlight mode |
|
457 | 510 | * @method force_highlight |
|
458 | 511 | * @param {object} - CodeMirror mode |
|
459 | 512 | **/ |
|
460 | 513 | Cell.prototype.force_highlight = function(mode) { |
|
461 | 514 | this.user_highlight = mode; |
|
462 | 515 | this.auto_highlight(); |
|
463 | 516 | }; |
|
464 | 517 | |
|
465 | 518 | /** |
|
466 | 519 | * Try to autodetect cell highlight mode, or use selected mode |
|
467 | 520 | * @methods _auto_highlight |
|
468 | 521 | * @private |
|
469 | 522 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
470 | 523 | **/ |
|
471 | 524 | Cell.prototype._auto_highlight = function (modes) { |
|
472 | 525 | //Here we handle manually selected modes |
|
473 | 526 | var mode; |
|
474 | 527 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) |
|
475 | 528 | { |
|
476 | 529 | mode = this.user_highlight; |
|
477 | 530 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
478 | 531 | this.code_mirror.setOption('mode', mode); |
|
479 | 532 | return; |
|
480 | 533 | } |
|
481 | 534 | var current_mode = this.code_mirror.getOption('mode', mode); |
|
482 | 535 | var first_line = this.code_mirror.getLine(0); |
|
483 | 536 | // loop on every pairs |
|
484 | 537 | for(mode in modes) { |
|
485 | 538 | var regs = modes[mode].reg; |
|
486 | 539 | // only one key every time but regexp can't be keys... |
|
487 | 540 | for(var i=0; i<regs.length; i++) { |
|
488 | 541 | // here we handle non magic_modes |
|
489 | 542 | if(first_line.match(regs[i]) !== null) { |
|
490 | 543 | if(current_mode == mode){ |
|
491 | 544 | return; |
|
492 | 545 | } |
|
493 | 546 | if (mode.search('magic_') !== 0) { |
|
494 | 547 | this.code_mirror.setOption('mode', mode); |
|
495 | 548 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
496 | 549 | return; |
|
497 | 550 | } |
|
498 | 551 | var open = modes[mode].open || "%%"; |
|
499 | 552 | var close = modes[mode].close || "%%end"; |
|
500 | 553 | var mmode = mode; |
|
501 | 554 | mode = mmode.substr(6); |
|
502 | 555 | if(current_mode == mode){ |
|
503 | 556 | return; |
|
504 | 557 | } |
|
505 | 558 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
506 | 559 | // create on the fly a mode that swhitch between |
|
507 | 560 | // plain/text and smth else otherwise `%%` is |
|
508 | 561 | // source of some highlight issues. |
|
509 | 562 | // we use patchedGetMode to circumvent a bug in CM |
|
510 | 563 | CodeMirror.defineMode(mmode , function(config) { |
|
511 | 564 | return CodeMirror.multiplexingMode( |
|
512 | 565 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
513 | 566 | // always set someting on close |
|
514 | 567 | {open: open, close: close, |
|
515 | 568 | mode: CodeMirror.patchedGetMode(config, mode), |
|
516 | 569 | delimStyle: "delimit" |
|
517 | 570 | } |
|
518 | 571 | ); |
|
519 | 572 | }); |
|
520 | 573 | this.code_mirror.setOption('mode', mmode); |
|
521 | 574 | return; |
|
522 | 575 | } |
|
523 | 576 | } |
|
524 | 577 | } |
|
525 | 578 | // fallback on default |
|
526 | 579 | var default_mode; |
|
527 | 580 | try { |
|
528 | 581 | default_mode = this._options.cm_config.mode; |
|
529 | 582 | } catch(e) { |
|
530 | 583 | default_mode = 'text/plain'; |
|
531 | 584 | } |
|
532 | 585 | if( current_mode === default_mode){ |
|
533 | 586 | return; |
|
534 | 587 | } |
|
535 | 588 | this.code_mirror.setOption('mode', default_mode); |
|
536 | 589 | }; |
|
537 | 590 | |
|
538 | 591 | IPython.Cell = Cell; |
|
539 | 592 | |
|
540 | 593 | return IPython; |
|
541 | 594 | |
|
542 | 595 | }(IPython)); |
|
543 | 596 |
@@ -1,549 +1,514 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2011 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // CodeCell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | /** |
|
12 | 12 | * An extendable module that provide base functionnality to create cell for notebook. |
|
13 | 13 | * @module IPython |
|
14 | 14 | * @namespace IPython |
|
15 | 15 | * @submodule CodeCell |
|
16 | 16 | */ |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | /* local util for codemirror */ |
|
20 | 20 | var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}; |
|
21 | 21 | |
|
22 | 22 | /** |
|
23 | 23 | * |
|
24 | 24 | * function to delete until previous non blanking space character |
|
25 | 25 | * or first multiple of 4 tabstop. |
|
26 | 26 | * @private |
|
27 | 27 | */ |
|
28 | 28 | CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ |
|
29 | 29 | var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); |
|
30 | 30 | if (!posEq(from, to)) { cm.replaceRange("", from, to); return; } |
|
31 | 31 | var cur = cm.getCursor(), line = cm.getLine(cur.line); |
|
32 | 32 | var tabsize = cm.getOption('tabSize'); |
|
33 | 33 | var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; |
|
34 | 34 | from = {ch:cur.ch-chToPrevTabStop,line:cur.line}; |
|
35 | 35 | var select = cm.getRange(from,cur); |
|
36 | 36 | if( select.match(/^\ +$/) !== null){ |
|
37 | 37 | cm.replaceRange("",from,cur); |
|
38 | 38 | } else { |
|
39 | 39 | cm.deleteH(-1,"char"); |
|
40 | 40 | } |
|
41 | 41 | }; |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | var IPython = (function (IPython) { |
|
45 | 45 | "use strict"; |
|
46 | 46 | |
|
47 | 47 | var utils = IPython.utils; |
|
48 | 48 | var keycodes = IPython.keyboard.keycodes; |
|
49 | 49 | |
|
50 | 50 | /** |
|
51 | 51 | * A Cell conceived to write code. |
|
52 | 52 | * |
|
53 | 53 | * The kernel doesn't have to be set at creation time, in that case |
|
54 | 54 | * it will be null and set_kernel has to be called later. |
|
55 | 55 | * @class CodeCell |
|
56 | 56 | * @extends IPython.Cell |
|
57 | 57 | * |
|
58 | 58 | * @constructor |
|
59 | 59 | * @param {Object|null} kernel |
|
60 | 60 | * @param {object|undefined} [options] |
|
61 | 61 | * @param [options.cm_config] {object} config to pass to CodeMirror |
|
62 | 62 | */ |
|
63 | 63 | var CodeCell = function (kernel, options) { |
|
64 | 64 | this.kernel = kernel || null; |
|
65 | 65 | this.collapsed = false; |
|
66 | 66 | |
|
67 | 67 | // create all attributed in constructor function |
|
68 | 68 | // even if null for V8 VM optimisation |
|
69 | 69 | this.input_prompt_number = null; |
|
70 | 70 | this.celltoolbar = null; |
|
71 | 71 | this.output_area = null; |
|
72 | 72 | this.last_msg_id = null; |
|
73 | 73 | this.completer = null; |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | var cm_overwrite_options = { |
|
77 | 77 | onKeyEvent: $.proxy(this.handle_keyevent,this) |
|
78 | 78 | }; |
|
79 | 79 | |
|
80 | 80 | options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options}); |
|
81 | 81 | |
|
82 | 82 | IPython.Cell.apply(this,[options]); |
|
83 | 83 | |
|
84 | 84 | // Attributes we want to override in this subclass. |
|
85 | 85 | this.cell_type = "code"; |
|
86 | 86 | |
|
87 | 87 | var that = this; |
|
88 | 88 | this.element.focusout( |
|
89 | 89 | function() { that.auto_highlight(); } |
|
90 | 90 | ); |
|
91 | 91 | }; |
|
92 | 92 | |
|
93 | 93 | CodeCell.options_default = { |
|
94 | 94 | cm_config : { |
|
95 | 95 | extraKeys: { |
|
96 | 96 | "Tab" : "indentMore", |
|
97 | 97 | "Shift-Tab" : "indentLess", |
|
98 | 98 | "Backspace" : "delSpaceToPrevTabStop", |
|
99 | 99 | "Cmd-/" : "toggleComment", |
|
100 | 100 | "Ctrl-/" : "toggleComment" |
|
101 | 101 | }, |
|
102 | 102 | mode: 'ipython', |
|
103 | 103 | theme: 'ipython', |
|
104 | 104 | matchBrackets: true, |
|
105 | 105 | autoCloseBrackets: true |
|
106 | 106 | } |
|
107 | 107 | }; |
|
108 | 108 | |
|
109 | 109 | CodeCell.msg_cells = {}; |
|
110 | 110 | |
|
111 | 111 | CodeCell.prototype = new IPython.Cell(); |
|
112 | 112 | |
|
113 | 113 | /** |
|
114 | 114 | * @method auto_highlight |
|
115 | 115 | */ |
|
116 | 116 | CodeCell.prototype.auto_highlight = function () { |
|
117 | 117 | this._auto_highlight(IPython.config.cell_magic_highlight); |
|
118 | 118 | }; |
|
119 | 119 | |
|
120 | 120 | /** @method create_element */ |
|
121 | 121 | CodeCell.prototype.create_element = function () { |
|
122 | 122 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
123 | 123 | |
|
124 | 124 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell'); |
|
125 | 125 | cell.attr('tabindex','2'); |
|
126 | 126 | |
|
127 | 127 | var input = $('<div></div>').addClass('input'); |
|
128 | 128 | var prompt = $('<div/>').addClass('prompt input_prompt'); |
|
129 | 129 | var inner_cell = $('<div/>').addClass('inner_cell'); |
|
130 | 130 | this.celltoolbar = new IPython.CellToolbar(this); |
|
131 | 131 | inner_cell.append(this.celltoolbar.element); |
|
132 | 132 | var input_area = $('<div/>').addClass('input_area'); |
|
133 | 133 | this.code_mirror = CodeMirror(input_area.get(0), this.cm_config); |
|
134 | 134 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); |
|
135 | 135 | inner_cell.append(input_area); |
|
136 | 136 | input.append(prompt).append(inner_cell); |
|
137 | 137 | |
|
138 | 138 | var widget_area = $('<div/>') |
|
139 | 139 | .addClass('widget-area') |
|
140 | 140 | .hide(); |
|
141 | 141 | this.widget_area = widget_area; |
|
142 | 142 | var widget_prompt = $('<div/>') |
|
143 | 143 | .addClass('prompt') |
|
144 | 144 | .appendTo(widget_area); |
|
145 | 145 | var widget_subarea = $('<div/>') |
|
146 | 146 | .addClass('widget-subarea') |
|
147 | 147 | .appendTo(widget_area); |
|
148 | 148 | this.widget_subarea = widget_subarea; |
|
149 | 149 | var widget_clear_buton = $('<button />') |
|
150 | 150 | .addClass('close') |
|
151 | 151 | .html('×') |
|
152 | 152 | .click(function() { |
|
153 | 153 | widget_area.slideUp('', function(){ widget_subarea.html(''); }); |
|
154 | 154 | }) |
|
155 | 155 | .appendTo(widget_prompt); |
|
156 | 156 | |
|
157 | 157 | var output = $('<div></div>'); |
|
158 | 158 | cell.append(input).append(widget_area).append(output); |
|
159 | 159 | this.element = cell; |
|
160 | 160 | this.output_area = new IPython.OutputArea(output, true); |
|
161 | 161 | this.completer = new IPython.Completer(this); |
|
162 | 162 | }; |
|
163 | 163 | |
|
164 | 164 | /** @method bind_events */ |
|
165 | 165 | CodeCell.prototype.bind_events = function () { |
|
166 | 166 | IPython.Cell.prototype.bind_events.apply(this); |
|
167 | 167 | var that = this; |
|
168 | 168 | |
|
169 | 169 | this.element.focusout( |
|
170 | 170 | function() { that.auto_highlight(); } |
|
171 | 171 | ); |
|
172 | 172 | }; |
|
173 | 173 | |
|
174 | 174 | |
|
175 | 175 | /** |
|
176 | 176 | * This method gets called in CodeMirror's onKeyDown/onKeyPress |
|
177 | 177 | * handlers and is used to provide custom key handling. Its return |
|
178 | 178 | * value is used to determine if CodeMirror should ignore the event: |
|
179 | 179 | * true = ignore, false = don't ignore. |
|
180 | 180 | * @method handle_codemirror_keyevent |
|
181 | 181 | */ |
|
182 | 182 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { |
|
183 | 183 | |
|
184 | 184 | var that = this; |
|
185 | 185 | // whatever key is pressed, first, cancel the tooltip request before |
|
186 | 186 | // they are sent, and remove tooltip if any, except for tab again |
|
187 | 187 | var tooltip_closed = null; |
|
188 | 188 | if (event.type === 'keydown' && event.which != keycodes.tab ) { |
|
189 | 189 | tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip(); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | var cur = editor.getCursor(); |
|
193 | 193 | if (event.keyCode === keycodes.enter){ |
|
194 | 194 | this.auto_highlight(); |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | if (event.keyCode === keycodes.enter && (event.shiftKey || event.ctrlKey || event.altKey)) { | |
|
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) { | |
|
197 | if (event.which === keycodes.down && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | |
|
201 | 198 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform |
|
202 | 199 | // browser and keyboard layout ! |
|
203 | 200 | // Pressing '(' , request tooltip, don't forget to reappend it |
|
204 | 201 | // The second argument says to hide the tooltip if the docstring |
|
205 | 202 | // is actually empty |
|
206 | 203 | IPython.tooltip.pending(that, true); |
|
207 |
} else if (event.which === keycodes. |
|
|
208 | // If we are not at the top, let CM handle the up arrow and | |
|
209 | // prevent the global keydown handler from handling it. | |
|
210 | if (!that.at_top()) { | |
|
211 | event.stop(); | |
|
212 | return false; | |
|
213 | } else { | |
|
214 | return 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; | |
|
204 | } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') { | |
|
205 | // If tooltip is active, cancel it. | |
|
206 | // The call to remove_and_cancel_tooltip above in L177 doesn't pass | |
|
207 | // force=true. Because of this it won't actually close the tooltip | |
|
208 | // if it is in sticky mode. Thus, we have to check again if it is open | |
|
209 | // and close it with force=true. | |
|
210 | if (!IPython.tooltip._hidden) { | |
|
211 | IPython.tooltip.remove_and_cancel_tooltip(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 | 217 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) { |
|
252 | 218 | if (editor.somethingSelected()){ |
|
253 | 219 | var anchor = editor.getCursor("anchor"); |
|
254 | 220 | var head = editor.getCursor("head"); |
|
255 | 221 | if( anchor.line != head.line){ |
|
256 | 222 | return false; |
|
257 | 223 | } |
|
258 | 224 | } |
|
259 | 225 | IPython.tooltip.request(that); |
|
260 | 226 | event.stop(); |
|
261 | 227 | return true; |
|
262 | 228 | } else if (event.keyCode === keycodes.tab && event.type == 'keydown') { |
|
263 | 229 | // Tab completion. |
|
264 | 230 | IPython.tooltip.remove_and_cancel_tooltip(); |
|
265 | 231 | if (editor.somethingSelected()) { |
|
266 | 232 | return false; |
|
267 | 233 | } |
|
268 | 234 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); |
|
269 | 235 | if (pre_cursor.trim() === "") { |
|
270 | 236 | // Don't autocomplete if the part of the line before the cursor |
|
271 | 237 | // is empty. In this case, let CodeMirror handle indentation. |
|
272 | 238 | return false; |
|
273 | 239 | } else { |
|
274 | 240 | event.stop(); |
|
275 | 241 | this.completer.startCompletion(); |
|
276 | 242 | return true; |
|
277 | 243 | } |
|
278 |
} |
|
|
279 | // keypress/keyup also trigger on TAB press, and we don't want to | |
|
280 | // use those to disable tab completion. | |
|
281 | return false; | |
|
282 | } | |
|
283 | return false; | |
|
244 | } | |
|
245 | ||
|
246 | // keyboard event wasn't one of those unique to code cells, let's see | |
|
247 | // if it's one of the generic ones (i.e. check edit mode shortcuts) | |
|
248 | return IPython.Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]) | |
|
284 | 249 | }; |
|
285 | 250 | |
|
286 | 251 | // Kernel related calls. |
|
287 | 252 | |
|
288 | 253 | CodeCell.prototype.set_kernel = function (kernel) { |
|
289 | 254 | this.kernel = kernel; |
|
290 | 255 | }; |
|
291 | 256 | |
|
292 | 257 | /** |
|
293 | 258 | * Execute current code cell to the kernel |
|
294 | 259 | * @method execute |
|
295 | 260 | */ |
|
296 | 261 | CodeCell.prototype.execute = function () { |
|
297 | 262 | this.output_area.clear_output(); |
|
298 | 263 | |
|
299 | 264 | // Clear widget area |
|
300 | 265 | this.widget_subarea.html(''); |
|
301 | 266 | this.widget_subarea.height(''); |
|
302 | 267 | this.widget_area.height(''); |
|
303 | 268 | this.widget_area.hide(); |
|
304 | 269 | |
|
305 | 270 | this.set_input_prompt('*'); |
|
306 | 271 | this.element.addClass("running"); |
|
307 | 272 | if (this.last_msg_id) { |
|
308 | 273 | this.kernel.clear_callbacks_for_msg(this.last_msg_id); |
|
309 | 274 | } |
|
310 | 275 | var callbacks = this.get_callbacks(); |
|
311 | 276 | |
|
312 | 277 | var old_msg_id = this.last_msg_id; |
|
313 | 278 | this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); |
|
314 | 279 | if (old_msg_id) { |
|
315 | 280 | delete CodeCell.msg_cells[old_msg_id]; |
|
316 | 281 | } |
|
317 | 282 | CodeCell.msg_cells[this.last_msg_id] = this; |
|
318 | 283 | }; |
|
319 | 284 | |
|
320 | 285 | /** |
|
321 | 286 | * Construct the default callbacks for |
|
322 | 287 | * @method get_callbacks |
|
323 | 288 | */ |
|
324 | 289 | CodeCell.prototype.get_callbacks = function () { |
|
325 | 290 | return { |
|
326 | 291 | shell : { |
|
327 | 292 | reply : $.proxy(this._handle_execute_reply, this), |
|
328 | 293 | payload : { |
|
329 | 294 | set_next_input : $.proxy(this._handle_set_next_input, this), |
|
330 | 295 | page : $.proxy(this._open_with_pager, this) |
|
331 | 296 | } |
|
332 | 297 | }, |
|
333 | 298 | iopub : { |
|
334 | 299 | output : $.proxy(this.output_area.handle_output, this.output_area), |
|
335 | 300 | clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area), |
|
336 | 301 | }, |
|
337 | 302 | input : $.proxy(this._handle_input_request, this) |
|
338 | 303 | }; |
|
339 | 304 | }; |
|
340 | 305 | |
|
341 | 306 | CodeCell.prototype._open_with_pager = function (payload) { |
|
342 | 307 | $([IPython.events]).trigger('open_with_text.Pager', payload); |
|
343 | 308 | }; |
|
344 | 309 | |
|
345 | 310 | /** |
|
346 | 311 | * @method _handle_execute_reply |
|
347 | 312 | * @private |
|
348 | 313 | */ |
|
349 | 314 | CodeCell.prototype._handle_execute_reply = function (msg) { |
|
350 | 315 | this.set_input_prompt(msg.content.execution_count); |
|
351 | 316 | this.element.removeClass("running"); |
|
352 | 317 | $([IPython.events]).trigger('set_dirty.Notebook', {value: true}); |
|
353 | 318 | }; |
|
354 | 319 | |
|
355 | 320 | /** |
|
356 | 321 | * @method _handle_set_next_input |
|
357 | 322 | * @private |
|
358 | 323 | */ |
|
359 | 324 | CodeCell.prototype._handle_set_next_input = function (payload) { |
|
360 | 325 | var data = {'cell': this, 'text': payload.text}; |
|
361 | 326 | $([IPython.events]).trigger('set_next_input.Notebook', data); |
|
362 | 327 | }; |
|
363 | 328 | |
|
364 | 329 | /** |
|
365 | 330 | * @method _handle_input_request |
|
366 | 331 | * @private |
|
367 | 332 | */ |
|
368 | 333 | CodeCell.prototype._handle_input_request = function (msg) { |
|
369 | 334 | this.output_area.append_raw_input(msg); |
|
370 | 335 | }; |
|
371 | 336 | |
|
372 | 337 | |
|
373 | 338 | // Basic cell manipulation. |
|
374 | 339 | |
|
375 | 340 | CodeCell.prototype.select = function () { |
|
376 | 341 | var cont = IPython.Cell.prototype.select.apply(this); |
|
377 | 342 | if (cont) { |
|
378 | 343 | this.code_mirror.refresh(); |
|
379 | 344 | this.auto_highlight(); |
|
380 | 345 | } |
|
381 | 346 | return cont; |
|
382 | 347 | }; |
|
383 | 348 | |
|
384 | 349 | CodeCell.prototype.render = function () { |
|
385 | 350 | var cont = IPython.Cell.prototype.render.apply(this); |
|
386 | 351 | // Always execute, even if we are already in the rendered state |
|
387 | 352 | return cont; |
|
388 | 353 | }; |
|
389 | 354 | |
|
390 | 355 | CodeCell.prototype.unrender = function () { |
|
391 | 356 | // CodeCell is always rendered |
|
392 | 357 | return false; |
|
393 | 358 | }; |
|
394 | 359 | |
|
395 | 360 | /** |
|
396 | 361 | * Determine whether or not the unfocus event should be aknowledged. |
|
397 | 362 | * |
|
398 | 363 | * @method should_cancel_blur |
|
399 | 364 | * |
|
400 | 365 | * @return results {bool} Whether or not to ignore the cell's blur event. |
|
401 | 366 | **/ |
|
402 | 367 | CodeCell.prototype.should_cancel_blur = function () { |
|
403 | 368 | // Cancel this unfocus event if the base wants to cancel or the cell |
|
404 | 369 | // completer is open or the tooltip is open. |
|
405 | 370 | return IPython.Cell.prototype.should_cancel_blur.apply(this) || |
|
406 | 371 | (this.completer && this.completer.is_visible()) || |
|
407 | 372 | (IPython.tooltip && IPython.tooltip.is_visible()); |
|
408 | 373 | }; |
|
409 | 374 | |
|
410 | 375 | CodeCell.prototype.select_all = function () { |
|
411 | 376 | var start = {line: 0, ch: 0}; |
|
412 | 377 | var nlines = this.code_mirror.lineCount(); |
|
413 | 378 | var last_line = this.code_mirror.getLine(nlines-1); |
|
414 | 379 | var end = {line: nlines-1, ch: last_line.length}; |
|
415 | 380 | this.code_mirror.setSelection(start, end); |
|
416 | 381 | }; |
|
417 | 382 | |
|
418 | 383 | |
|
419 | 384 | CodeCell.prototype.collapse_output = function () { |
|
420 | 385 | this.collapsed = true; |
|
421 | 386 | this.output_area.collapse(); |
|
422 | 387 | }; |
|
423 | 388 | |
|
424 | 389 | |
|
425 | 390 | CodeCell.prototype.expand_output = function () { |
|
426 | 391 | this.collapsed = false; |
|
427 | 392 | this.output_area.expand(); |
|
428 | 393 | this.output_area.unscroll_area(); |
|
429 | 394 | }; |
|
430 | 395 | |
|
431 | 396 | CodeCell.prototype.scroll_output = function () { |
|
432 | 397 | this.output_area.expand(); |
|
433 | 398 | this.output_area.scroll_if_long(); |
|
434 | 399 | }; |
|
435 | 400 | |
|
436 | 401 | CodeCell.prototype.toggle_output = function () { |
|
437 | 402 | this.collapsed = Boolean(1 - this.collapsed); |
|
438 | 403 | this.output_area.toggle_output(); |
|
439 | 404 | }; |
|
440 | 405 | |
|
441 | 406 | CodeCell.prototype.toggle_output_scroll = function () { |
|
442 | 407 | this.output_area.toggle_scroll(); |
|
443 | 408 | }; |
|
444 | 409 | |
|
445 | 410 | |
|
446 | 411 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { |
|
447 | 412 | var ns; |
|
448 | 413 | if (prompt_value == undefined) { |
|
449 | 414 | ns = " "; |
|
450 | 415 | } else { |
|
451 | 416 | ns = encodeURIComponent(prompt_value); |
|
452 | 417 | } |
|
453 | 418 | return 'In [' + ns + ']:'; |
|
454 | 419 | }; |
|
455 | 420 | |
|
456 | 421 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { |
|
457 | 422 | var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; |
|
458 | 423 | for(var i=1; i < lines_number; i++) { |
|
459 | 424 | html.push(['...:']); |
|
460 | 425 | } |
|
461 | 426 | return html.join('<br/>'); |
|
462 | 427 | }; |
|
463 | 428 | |
|
464 | 429 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; |
|
465 | 430 | |
|
466 | 431 | |
|
467 | 432 | CodeCell.prototype.set_input_prompt = function (number) { |
|
468 | 433 | var nline = 1; |
|
469 | 434 | if (this.code_mirror !== undefined) { |
|
470 | 435 | nline = this.code_mirror.lineCount(); |
|
471 | 436 | } |
|
472 | 437 | this.input_prompt_number = number; |
|
473 | 438 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); |
|
474 | 439 | // This HTML call is okay because the user contents are escaped. |
|
475 | 440 | this.element.find('div.input_prompt').html(prompt_html); |
|
476 | 441 | }; |
|
477 | 442 | |
|
478 | 443 | |
|
479 | 444 | CodeCell.prototype.clear_input = function () { |
|
480 | 445 | this.code_mirror.setValue(''); |
|
481 | 446 | }; |
|
482 | 447 | |
|
483 | 448 | |
|
484 | 449 | CodeCell.prototype.get_text = function () { |
|
485 | 450 | return this.code_mirror.getValue(); |
|
486 | 451 | }; |
|
487 | 452 | |
|
488 | 453 | |
|
489 | 454 | CodeCell.prototype.set_text = function (code) { |
|
490 | 455 | return this.code_mirror.setValue(code); |
|
491 | 456 | }; |
|
492 | 457 | |
|
493 | 458 | |
|
494 | 459 | CodeCell.prototype.clear_output = function (wait) { |
|
495 | 460 | this.output_area.clear_output(wait); |
|
496 | 461 | this.set_input_prompt(); |
|
497 | 462 | }; |
|
498 | 463 | |
|
499 | 464 | |
|
500 | 465 | // JSON serialization |
|
501 | 466 | |
|
502 | 467 | CodeCell.prototype.fromJSON = function (data) { |
|
503 | 468 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
504 | 469 | if (data.cell_type === 'code') { |
|
505 | 470 | if (data.input !== undefined) { |
|
506 | 471 | this.set_text(data.input); |
|
507 | 472 | // make this value the starting point, so that we can only undo |
|
508 | 473 | // to this state, instead of a blank cell |
|
509 | 474 | this.code_mirror.clearHistory(); |
|
510 | 475 | this.auto_highlight(); |
|
511 | 476 | } |
|
512 | 477 | if (data.prompt_number !== undefined) { |
|
513 | 478 | this.set_input_prompt(data.prompt_number); |
|
514 | 479 | } else { |
|
515 | 480 | this.set_input_prompt(); |
|
516 | 481 | } |
|
517 | 482 | this.output_area.trusted = data.trusted || false; |
|
518 | 483 | this.output_area.fromJSON(data.outputs); |
|
519 | 484 | if (data.collapsed !== undefined) { |
|
520 | 485 | if (data.collapsed) { |
|
521 | 486 | this.collapse_output(); |
|
522 | 487 | } else { |
|
523 | 488 | this.expand_output(); |
|
524 | 489 | } |
|
525 | 490 | } |
|
526 | 491 | } |
|
527 | 492 | }; |
|
528 | 493 | |
|
529 | 494 | |
|
530 | 495 | CodeCell.prototype.toJSON = function () { |
|
531 | 496 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
532 | 497 | data.input = this.get_text(); |
|
533 | 498 | // is finite protect against undefined and '*' value |
|
534 | 499 | if (isFinite(this.input_prompt_number)) { |
|
535 | 500 | data.prompt_number = this.input_prompt_number; |
|
536 | 501 | } |
|
537 | 502 | var outputs = this.output_area.toJSON(); |
|
538 | 503 | data.outputs = outputs; |
|
539 | 504 | data.language = 'python'; |
|
540 | 505 | data.trusted = this.output_area.trusted; |
|
541 | 506 | data.collapsed = this.collapsed; |
|
542 | 507 | return data; |
|
543 | 508 | }; |
|
544 | 509 | |
|
545 | 510 | |
|
546 | 511 | IPython.CodeCell = CodeCell; |
|
547 | 512 | |
|
548 | 513 | return IPython; |
|
549 | 514 | }(IPython)); |
@@ -1,506 +1,455 b'' | |||
|
1 | 1 | //---------------------------------------------------------------------------- |
|
2 | 2 | // Copyright (C) 2008-2012 The IPython Development Team |
|
3 | 3 | // |
|
4 | 4 | // Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | // the file COPYING, distributed as part of this software. |
|
6 | 6 | //---------------------------------------------------------------------------- |
|
7 | 7 | |
|
8 | 8 | //============================================================================ |
|
9 | 9 | // TextCell |
|
10 | 10 | //============================================================================ |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | /** |
|
15 | 15 | A module that allow to create different type of Text Cell |
|
16 | 16 | @module IPython |
|
17 | 17 | @namespace IPython |
|
18 | 18 | */ |
|
19 | 19 | var IPython = (function (IPython) { |
|
20 | 20 | "use strict"; |
|
21 | 21 | |
|
22 | 22 | // TextCell base class |
|
23 | 23 | var keycodes = IPython.keyboard.keycodes; |
|
24 | 24 | var security = IPython.security; |
|
25 | 25 | |
|
26 | 26 | /** |
|
27 | 27 | * Construct a new TextCell, codemirror mode is by default 'htmlmixed', and cell type is 'text' |
|
28 | 28 | * cell start as not redered. |
|
29 | 29 | * |
|
30 | 30 | * @class TextCell |
|
31 | 31 | * @constructor TextCell |
|
32 | 32 | * @extend IPython.Cell |
|
33 | 33 | * @param {object|undefined} [options] |
|
34 | 34 | * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config |
|
35 | 35 | * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass) |
|
36 | 36 | */ |
|
37 | 37 | var TextCell = function (options) { |
|
38 | 38 | // in all TextCell/Cell subclasses |
|
39 | 39 | // do not assign most of members here, just pass it down |
|
40 | 40 | // in the options dict potentially overwriting what you wish. |
|
41 | 41 | // they will be assigned in the base class. |
|
42 | 42 | |
|
43 | 43 | // we cannot put this as a class key as it has handle to "this". |
|
44 | 44 | var cm_overwrite_options = { |
|
45 | 45 | onKeyEvent: $.proxy(this.handle_keyevent,this) |
|
46 | 46 | }; |
|
47 | 47 | |
|
48 | 48 | options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options}); |
|
49 | 49 | |
|
50 | 50 | this.cell_type = this.cell_type || 'text'; |
|
51 | 51 | |
|
52 | 52 | IPython.Cell.apply(this, [options]); |
|
53 | 53 | |
|
54 | 54 | this.rendered = false; |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | TextCell.prototype = new IPython.Cell(); |
|
58 | 58 | |
|
59 | 59 | TextCell.options_default = { |
|
60 | 60 | cm_config : { |
|
61 | 61 | extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"}, |
|
62 | 62 | mode: 'htmlmixed', |
|
63 | 63 | lineWrapping : true, |
|
64 | 64 | } |
|
65 | 65 | }; |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | /** |
|
69 | 69 | * Create the DOM element of the TextCell |
|
70 | 70 | * @method create_element |
|
71 | 71 | * @private |
|
72 | 72 | */ |
|
73 | 73 | TextCell.prototype.create_element = function () { |
|
74 | 74 | IPython.Cell.prototype.create_element.apply(this, arguments); |
|
75 | 75 | |
|
76 | 76 | var cell = $("<div>").addClass('cell text_cell border-box-sizing'); |
|
77 | 77 | cell.attr('tabindex','2'); |
|
78 | 78 | |
|
79 | 79 | var prompt = $('<div/>').addClass('prompt input_prompt'); |
|
80 | 80 | cell.append(prompt); |
|
81 | 81 | var inner_cell = $('<div/>').addClass('inner_cell'); |
|
82 | 82 | this.celltoolbar = new IPython.CellToolbar(this); |
|
83 | 83 | inner_cell.append(this.celltoolbar.element); |
|
84 | 84 | var input_area = $('<div/>').addClass('input_area'); |
|
85 | 85 | this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config); |
|
86 | 86 | // The tabindex=-1 makes this div focusable. |
|
87 | 87 | var render_area = $('<div/>').addClass('text_cell_render border-box-sizing'). |
|
88 | 88 | addClass('rendered_html').attr('tabindex','-1'); |
|
89 | 89 | inner_cell.append(input_area).append(render_area); |
|
90 | 90 | cell.append(inner_cell); |
|
91 | 91 | this.element = cell; |
|
92 | 92 | }; |
|
93 | 93 | |
|
94 | 94 | |
|
95 | 95 | /** |
|
96 | 96 | * Bind the DOM evet to cell actions |
|
97 | 97 | * Need to be called after TextCell.create_element |
|
98 | 98 | * @private |
|
99 | 99 | * @method bind_event |
|
100 | 100 | */ |
|
101 | 101 | TextCell.prototype.bind_events = function () { |
|
102 | 102 | IPython.Cell.prototype.bind_events.apply(this); |
|
103 | 103 | var that = this; |
|
104 | 104 | |
|
105 | 105 | this.element.dblclick(function () { |
|
106 | 106 | if (that.selected === false) { |
|
107 | 107 | $([IPython.events]).trigger('select.Cell', {'cell':that}); |
|
108 | 108 | } |
|
109 | 109 | var cont = that.unrender(); |
|
110 | 110 | if (cont) { |
|
111 | 111 | that.focus_editor(); |
|
112 | 112 | } |
|
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 | 116 | // Cell level actions |
|
168 | 117 | |
|
169 | 118 | TextCell.prototype.select = function () { |
|
170 | 119 | var cont = IPython.Cell.prototype.select.apply(this); |
|
171 | 120 | if (cont) { |
|
172 | 121 | if (this.mode === 'edit') { |
|
173 | 122 | this.code_mirror.refresh(); |
|
174 | 123 | } |
|
175 | 124 | } |
|
176 | 125 | return cont; |
|
177 | 126 | }; |
|
178 | 127 | |
|
179 | 128 | TextCell.prototype.unrender = function () { |
|
180 | 129 | if (this.read_only) return; |
|
181 | 130 | var cont = IPython.Cell.prototype.unrender.apply(this); |
|
182 | 131 | if (cont) { |
|
183 | 132 | var text_cell = this.element; |
|
184 | 133 | var output = text_cell.find("div.text_cell_render"); |
|
185 | 134 | output.hide(); |
|
186 | 135 | text_cell.find('div.input_area').show(); |
|
187 | 136 | if (this.get_text() === this.placeholder) { |
|
188 | 137 | this.set_text(''); |
|
189 | 138 | } |
|
190 | 139 | this.refresh(); |
|
191 | 140 | } |
|
192 | 141 | return cont; |
|
193 | 142 | }; |
|
194 | 143 | |
|
195 | 144 | TextCell.prototype.execute = function () { |
|
196 | 145 | this.render(); |
|
197 | 146 | }; |
|
198 | 147 | |
|
199 | 148 | /** |
|
200 | 149 | * setter: {{#crossLink "TextCell/set_text"}}{{/crossLink}} |
|
201 | 150 | * @method get_text |
|
202 | 151 | * @retrun {string} CodeMirror current text value |
|
203 | 152 | */ |
|
204 | 153 | TextCell.prototype.get_text = function() { |
|
205 | 154 | return this.code_mirror.getValue(); |
|
206 | 155 | }; |
|
207 | 156 | |
|
208 | 157 | /** |
|
209 | 158 | * @param {string} text - Codemiror text value |
|
210 | 159 | * @see TextCell#get_text |
|
211 | 160 | * @method set_text |
|
212 | 161 | * */ |
|
213 | 162 | TextCell.prototype.set_text = function(text) { |
|
214 | 163 | this.code_mirror.setValue(text); |
|
215 | 164 | this.code_mirror.refresh(); |
|
216 | 165 | }; |
|
217 | 166 | |
|
218 | 167 | /** |
|
219 | 168 | * setter :{{#crossLink "TextCell/set_rendered"}}{{/crossLink}} |
|
220 | 169 | * @method get_rendered |
|
221 | 170 | * @return {html} html of rendered element |
|
222 | 171 | * */ |
|
223 | 172 | TextCell.prototype.get_rendered = function() { |
|
224 | 173 | return this.element.find('div.text_cell_render').html(); |
|
225 | 174 | }; |
|
226 | 175 | |
|
227 | 176 | /** |
|
228 | 177 | * @method set_rendered |
|
229 | 178 | */ |
|
230 | 179 | TextCell.prototype.set_rendered = function(text) { |
|
231 | 180 | this.element.find('div.text_cell_render').html(text); |
|
232 | 181 | }; |
|
233 | 182 | |
|
234 | 183 | |
|
235 | 184 | /** |
|
236 | 185 | * Create Text cell from JSON |
|
237 | 186 | * @param {json} data - JSON serialized text-cell |
|
238 | 187 | * @method fromJSON |
|
239 | 188 | */ |
|
240 | 189 | TextCell.prototype.fromJSON = function (data) { |
|
241 | 190 | IPython.Cell.prototype.fromJSON.apply(this, arguments); |
|
242 | 191 | if (data.cell_type === this.cell_type) { |
|
243 | 192 | if (data.source !== undefined) { |
|
244 | 193 | this.set_text(data.source); |
|
245 | 194 | // make this value the starting point, so that we can only undo |
|
246 | 195 | // to this state, instead of a blank cell |
|
247 | 196 | this.code_mirror.clearHistory(); |
|
248 | 197 | // TODO: This HTML needs to be treated as potentially dangerous |
|
249 | 198 | // user input and should be handled before set_rendered. |
|
250 | 199 | this.set_rendered(data.rendered || ''); |
|
251 | 200 | this.rendered = false; |
|
252 | 201 | this.render(); |
|
253 | 202 | } |
|
254 | 203 | } |
|
255 | 204 | }; |
|
256 | 205 | |
|
257 | 206 | /** Generate JSON from cell |
|
258 | 207 | * @return {object} cell data serialised to json |
|
259 | 208 | */ |
|
260 | 209 | TextCell.prototype.toJSON = function () { |
|
261 | 210 | var data = IPython.Cell.prototype.toJSON.apply(this); |
|
262 | 211 | data.source = this.get_text(); |
|
263 | 212 | if (data.source == this.placeholder) { |
|
264 | 213 | data.source = ""; |
|
265 | 214 | } |
|
266 | 215 | return data; |
|
267 | 216 | }; |
|
268 | 217 | |
|
269 | 218 | |
|
270 | 219 | /** |
|
271 | 220 | * @class MarkdownCell |
|
272 | 221 | * @constructor MarkdownCell |
|
273 | 222 | * @extends IPython.HTMLCell |
|
274 | 223 | */ |
|
275 | 224 | var MarkdownCell = function (options) { |
|
276 | 225 | options = this.mergeopt(MarkdownCell, options); |
|
277 | 226 | |
|
278 | 227 | this.cell_type = 'markdown'; |
|
279 | 228 | TextCell.apply(this, [options]); |
|
280 | 229 | }; |
|
281 | 230 | |
|
282 | 231 | MarkdownCell.options_default = { |
|
283 | 232 | cm_config: { |
|
284 | 233 | mode: 'gfm' |
|
285 | 234 | }, |
|
286 | 235 | placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$" |
|
287 | 236 | }; |
|
288 | 237 | |
|
289 | 238 | MarkdownCell.prototype = new TextCell(); |
|
290 | 239 | |
|
291 | 240 | /** |
|
292 | 241 | * @method render |
|
293 | 242 | */ |
|
294 | 243 | MarkdownCell.prototype.render = function () { |
|
295 | 244 | var cont = IPython.TextCell.prototype.render.apply(this); |
|
296 | 245 | if (cont) { |
|
297 | 246 | var text = this.get_text(); |
|
298 | 247 | var math = null; |
|
299 | 248 | if (text === "") { text = this.placeholder; } |
|
300 | 249 | var text_and_math = IPython.mathjaxutils.remove_math(text); |
|
301 | 250 | text = text_and_math[0]; |
|
302 | 251 | math = text_and_math[1]; |
|
303 | 252 | var html = marked.parser(marked.lexer(text)); |
|
304 | 253 | html = IPython.mathjaxutils.replace_math(html, math); |
|
305 | 254 | html = security.sanitize_html(html); |
|
306 | 255 | html = $(html); |
|
307 | 256 | // links in markdown cells should open in new tabs |
|
308 | 257 | html.find("a[href]").not('[href^="#"]').attr("target", "_blank"); |
|
309 | 258 | this.set_rendered(html); |
|
310 | 259 | this.element.find('div.input_area').hide(); |
|
311 | 260 | this.element.find("div.text_cell_render").show(); |
|
312 | 261 | this.typeset(); |
|
313 | 262 | } |
|
314 | 263 | return cont; |
|
315 | 264 | }; |
|
316 | 265 | |
|
317 | 266 | |
|
318 | 267 | // RawCell |
|
319 | 268 | |
|
320 | 269 | /** |
|
321 | 270 | * @class RawCell |
|
322 | 271 | * @constructor RawCell |
|
323 | 272 | * @extends IPython.TextCell |
|
324 | 273 | */ |
|
325 | 274 | var RawCell = function (options) { |
|
326 | 275 | |
|
327 | 276 | options = this.mergeopt(RawCell,options); |
|
328 | 277 | TextCell.apply(this, [options]); |
|
329 | 278 | this.cell_type = 'raw'; |
|
330 | 279 | // RawCell should always hide its rendered div |
|
331 | 280 | this.element.find('div.text_cell_render').hide(); |
|
332 | 281 | }; |
|
333 | 282 | |
|
334 | 283 | RawCell.options_default = { |
|
335 | 284 | placeholder : "Write raw LaTeX or other formats here, for use with nbconvert.\n" + |
|
336 | 285 | "It will not be rendered in the notebook.\n" + |
|
337 | 286 | "When passing through nbconvert, a Raw Cell's content is added to the output unmodified." |
|
338 | 287 | }; |
|
339 | 288 | |
|
340 | 289 | RawCell.prototype = new TextCell(); |
|
341 | 290 | |
|
342 | 291 | /** @method bind_events **/ |
|
343 | 292 | RawCell.prototype.bind_events = function () { |
|
344 | 293 | TextCell.prototype.bind_events.apply(this); |
|
345 | 294 | var that = this; |
|
346 | 295 | this.element.focusout(function() { |
|
347 | 296 | that.auto_highlight(); |
|
348 | 297 | }); |
|
349 | 298 | }; |
|
350 | 299 | |
|
351 | 300 | /** |
|
352 | 301 | * Trigger autodetection of highlight scheme for current cell |
|
353 | 302 | * @method auto_highlight |
|
354 | 303 | */ |
|
355 | 304 | RawCell.prototype.auto_highlight = function () { |
|
356 | 305 | this._auto_highlight(IPython.config.raw_cell_highlight); |
|
357 | 306 | }; |
|
358 | 307 | |
|
359 | 308 | /** @method render **/ |
|
360 | 309 | RawCell.prototype.render = function () { |
|
361 | 310 | // Make sure that this cell type can never be rendered |
|
362 | 311 | if (this.rendered) { |
|
363 | 312 | this.unrender(); |
|
364 | 313 | } |
|
365 | 314 | var text = this.get_text(); |
|
366 | 315 | if (text === "") { text = this.placeholder; } |
|
367 | 316 | this.set_text(text); |
|
368 | 317 | }; |
|
369 | 318 | |
|
370 | 319 | |
|
371 | 320 | /** |
|
372 | 321 | * @class HeadingCell |
|
373 | 322 | * @extends IPython.TextCell |
|
374 | 323 | */ |
|
375 | 324 | |
|
376 | 325 | /** |
|
377 | 326 | * @constructor HeadingCell |
|
378 | 327 | * @extends IPython.TextCell |
|
379 | 328 | */ |
|
380 | 329 | var HeadingCell = function (options) { |
|
381 | 330 | options = this.mergeopt(HeadingCell, options); |
|
382 | 331 | |
|
383 | 332 | this.level = 1; |
|
384 | 333 | this.cell_type = 'heading'; |
|
385 | 334 | TextCell.apply(this, [options]); |
|
386 | 335 | |
|
387 | 336 | /** |
|
388 | 337 | * heading level of the cell, use getter and setter to access |
|
389 | 338 | * @property level |
|
390 | 339 | */ |
|
391 | 340 | }; |
|
392 | 341 | |
|
393 | 342 | HeadingCell.options_default = { |
|
394 | 343 | placeholder: "Type Heading Here" |
|
395 | 344 | }; |
|
396 | 345 | |
|
397 | 346 | HeadingCell.prototype = new TextCell(); |
|
398 | 347 | |
|
399 | 348 | /** @method fromJSON */ |
|
400 | 349 | HeadingCell.prototype.fromJSON = function (data) { |
|
401 | 350 | if (data.level !== undefined){ |
|
402 | 351 | this.level = data.level; |
|
403 | 352 | } |
|
404 | 353 | TextCell.prototype.fromJSON.apply(this, arguments); |
|
405 | 354 | }; |
|
406 | 355 | |
|
407 | 356 | |
|
408 | 357 | /** @method toJSON */ |
|
409 | 358 | HeadingCell.prototype.toJSON = function () { |
|
410 | 359 | var data = TextCell.prototype.toJSON.apply(this); |
|
411 | 360 | data.level = this.get_level(); |
|
412 | 361 | return data; |
|
413 | 362 | }; |
|
414 | 363 | |
|
415 | 364 | /** |
|
416 | 365 | * can the cell be split into two cells |
|
417 | 366 | * @method is_splittable |
|
418 | 367 | **/ |
|
419 | 368 | HeadingCell.prototype.is_splittable = function () { |
|
420 | 369 | return false; |
|
421 | 370 | }; |
|
422 | 371 | |
|
423 | 372 | |
|
424 | 373 | /** |
|
425 | 374 | * can the cell be merged with other cells |
|
426 | 375 | * @method is_mergeable |
|
427 | 376 | **/ |
|
428 | 377 | HeadingCell.prototype.is_mergeable = function () { |
|
429 | 378 | return false; |
|
430 | 379 | }; |
|
431 | 380 | |
|
432 | 381 | /** |
|
433 | 382 | * Change heading level of cell, and re-render |
|
434 | 383 | * @method set_level |
|
435 | 384 | */ |
|
436 | 385 | HeadingCell.prototype.set_level = function (level) { |
|
437 | 386 | this.level = level; |
|
438 | 387 | if (this.rendered) { |
|
439 | 388 | this.rendered = false; |
|
440 | 389 | this.render(); |
|
441 | 390 | } |
|
442 | 391 | }; |
|
443 | 392 | |
|
444 | 393 | /** The depth of header cell, based on html (h1 to h6) |
|
445 | 394 | * @method get_level |
|
446 | 395 | * @return {integer} level - for 1 to 6 |
|
447 | 396 | */ |
|
448 | 397 | HeadingCell.prototype.get_level = function () { |
|
449 | 398 | return this.level; |
|
450 | 399 | }; |
|
451 | 400 | |
|
452 | 401 | |
|
453 | 402 | HeadingCell.prototype.set_rendered = function (html) { |
|
454 | 403 | this.element.find("div.text_cell_render").html(html); |
|
455 | 404 | }; |
|
456 | 405 | |
|
457 | 406 | |
|
458 | 407 | HeadingCell.prototype.get_rendered = function () { |
|
459 | 408 | var r = this.element.find("div.text_cell_render"); |
|
460 | 409 | return r.children().first().html(); |
|
461 | 410 | }; |
|
462 | 411 | |
|
463 | 412 | |
|
464 | 413 | HeadingCell.prototype.render = function () { |
|
465 | 414 | var cont = IPython.TextCell.prototype.render.apply(this); |
|
466 | 415 | if (cont) { |
|
467 | 416 | var text = this.get_text(); |
|
468 | 417 | var math = null; |
|
469 | 418 | // Markdown headings must be a single line |
|
470 | 419 | text = text.replace(/\n/g, ' '); |
|
471 | 420 | if (text === "") { text = this.placeholder; } |
|
472 | 421 | text = Array(this.level + 1).join("#") + " " + text; |
|
473 | 422 | var text_and_math = IPython.mathjaxutils.remove_math(text); |
|
474 | 423 | text = text_and_math[0]; |
|
475 | 424 | math = text_and_math[1]; |
|
476 | 425 | var html = marked.parser(marked.lexer(text)); |
|
477 | 426 | html = IPython.mathjaxutils.replace_math(html, math); |
|
478 | 427 | html = security.sanitize_html(html); |
|
479 | 428 | var h = $(html); |
|
480 | 429 | // add id and linkback anchor |
|
481 | 430 | var hash = h.text().replace(/ /g, '-'); |
|
482 | 431 | h.attr('id', hash); |
|
483 | 432 | h.append( |
|
484 | 433 | $('<a/>') |
|
485 | 434 | .addClass('anchor-link') |
|
486 | 435 | .attr('href', '#' + hash) |
|
487 | 436 | .text('¶') |
|
488 | 437 | ); |
|
489 | 438 | this.set_rendered(h); |
|
490 | 439 | this.element.find('div.input_area').hide(); |
|
491 | 440 | this.element.find("div.text_cell_render").show(); |
|
492 | 441 | this.typeset(); |
|
493 | 442 | } |
|
494 | 443 | return cont; |
|
495 | 444 | }; |
|
496 | 445 | |
|
497 | 446 | IPython.TextCell = TextCell; |
|
498 | 447 | IPython.MarkdownCell = MarkdownCell; |
|
499 | 448 | IPython.RawCell = RawCell; |
|
500 | 449 | IPython.HeadingCell = HeadingCell; |
|
501 | 450 | |
|
502 | 451 | |
|
503 | 452 | return IPython; |
|
504 | 453 | |
|
505 | 454 | }(IPython)); |
|
506 | 455 |
General Comments 0
You need to be logged in to leave comments.
Login now