##// END OF EJS Templates
Make cell be undeletable ONLY when metadata is explicitly false
Jessica B. Hamrick -
Show More
@@ -1,568 +1,571 b''
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 ], function(IPython, $, utils) {
8 ], function(IPython, $, utils) {
9 // TODO: remove IPython dependency here
9 // TODO: remove IPython dependency here
10 "use strict";
10 "use strict";
11
11
12 // monkey patch CM to be able to syntax highlight cell magics
12 // monkey patch CM to be able to syntax highlight cell magics
13 // bug reported upstream,
13 // bug reported upstream,
14 // see https://github.com/codemirror/CodeMirror/issues/670
14 // see https://github.com/codemirror/CodeMirror/issues/670
15 if(CodeMirror.getMode(1,'text/plain').indent === undefined ){
15 if(CodeMirror.getMode(1,'text/plain').indent === undefined ){
16 CodeMirror.modes.null = function() {
16 CodeMirror.modes.null = function() {
17 return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0;}};
17 return {token: function(stream) {stream.skipToEnd();},indent : function(){return 0;}};
18 };
18 };
19 }
19 }
20
20
21 CodeMirror.patchedGetMode = function(config, mode){
21 CodeMirror.patchedGetMode = function(config, mode){
22 var cmmode = CodeMirror.getMode(config, mode);
22 var cmmode = CodeMirror.getMode(config, mode);
23 if(cmmode.indent === null) {
23 if(cmmode.indent === null) {
24 console.log('patch mode "' , mode, '" on the fly');
24 console.log('patch mode "' , mode, '" on the fly');
25 cmmode.indent = function(){return 0;};
25 cmmode.indent = function(){return 0;};
26 }
26 }
27 return cmmode;
27 return cmmode;
28 };
28 };
29 // end monkey patching CodeMirror
29 // end monkey patching CodeMirror
30
30
31 var Cell = function (options) {
31 var Cell = function (options) {
32 // Constructor
32 // Constructor
33 //
33 //
34 // The Base `Cell` class from which to inherit.
34 // The Base `Cell` class from which to inherit.
35 //
35 //
36 // Parameters:
36 // Parameters:
37 // options: dictionary
37 // options: dictionary
38 // Dictionary of keyword arguments.
38 // Dictionary of keyword arguments.
39 // events: $(Events) instance
39 // events: $(Events) instance
40 // config: dictionary
40 // config: dictionary
41 // keyboard_manager: KeyboardManager instance
41 // keyboard_manager: KeyboardManager instance
42 options = options || {};
42 options = options || {};
43 this.keyboard_manager = options.keyboard_manager;
43 this.keyboard_manager = options.keyboard_manager;
44 this.events = options.events;
44 this.events = options.events;
45 var config = utils.mergeopt(Cell, options.config);
45 var config = utils.mergeopt(Cell, options.config);
46 // superclass default overwrite our default
46 // superclass default overwrite our default
47
47
48 this.placeholder = config.placeholder || '';
48 this.placeholder = config.placeholder || '';
49 this.read_only = config.cm_config.readOnly;
49 this.read_only = config.cm_config.readOnly;
50 this.selected = false;
50 this.selected = false;
51 this.rendered = false;
51 this.rendered = false;
52 this.mode = 'command';
52 this.mode = 'command';
53 this.metadata = {};
53 this.metadata = {};
54 // load this from metadata later ?
54 // load this from metadata later ?
55 this.user_highlight = 'auto';
55 this.user_highlight = 'auto';
56 this.cm_config = config.cm_config;
56 this.cm_config = config.cm_config;
57 this.cell_id = utils.uuid();
57 this.cell_id = utils.uuid();
58 this._options = config;
58 this._options = config;
59
59
60 // For JS VM engines optimization, attributes should be all set (even
60 // For JS VM engines optimization, attributes should be all set (even
61 // to null) in the constructor, and if possible, if different subclass
61 // to null) in the constructor, and if possible, if different subclass
62 // have new attributes with same name, they should be created in the
62 // have new attributes with same name, they should be created in the
63 // same order. Easiest is to create and set to null in parent class.
63 // same order. Easiest is to create and set to null in parent class.
64
64
65 this.element = null;
65 this.element = null;
66 this.cell_type = this.cell_type || null;
66 this.cell_type = this.cell_type || null;
67 this.code_mirror = null;
67 this.code_mirror = null;
68
68
69 this.create_element();
69 this.create_element();
70 if (this.element !== null) {
70 if (this.element !== null) {
71 this.element.data("cell", this);
71 this.element.data("cell", this);
72 this.bind_events();
72 this.bind_events();
73 this.init_classes();
73 this.init_classes();
74 }
74 }
75 };
75 };
76
76
77 Cell.options_default = {
77 Cell.options_default = {
78 cm_config : {
78 cm_config : {
79 indentUnit : 4,
79 indentUnit : 4,
80 readOnly: false,
80 readOnly: false,
81 theme: "default",
81 theme: "default",
82 extraKeys: {
82 extraKeys: {
83 "Cmd-Right":"goLineRight",
83 "Cmd-Right":"goLineRight",
84 "End":"goLineRight",
84 "End":"goLineRight",
85 "Cmd-Left":"goLineLeft"
85 "Cmd-Left":"goLineLeft"
86 }
86 }
87 }
87 }
88 };
88 };
89
89
90 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
90 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
91 // by disabling drag/drop altogether on Safari
91 // by disabling drag/drop altogether on Safari
92 // https://github.com/codemirror/CodeMirror/issues/332
92 // https://github.com/codemirror/CodeMirror/issues/332
93 if (utils.browser[0] == "Safari") {
93 if (utils.browser[0] == "Safari") {
94 Cell.options_default.cm_config.dragDrop = false;
94 Cell.options_default.cm_config.dragDrop = false;
95 }
95 }
96
96
97 /**
97 /**
98 * Empty. Subclasses must implement create_element.
98 * Empty. Subclasses must implement create_element.
99 * This should contain all the code to create the DOM element in notebook
99 * This should contain all the code to create the DOM element in notebook
100 * and will be called by Base Class constructor.
100 * and will be called by Base Class constructor.
101 * @method create_element
101 * @method create_element
102 */
102 */
103 Cell.prototype.create_element = function () {
103 Cell.prototype.create_element = function () {
104 };
104 };
105
105
106 Cell.prototype.init_classes = function () {
106 Cell.prototype.init_classes = function () {
107 // Call after this.element exists to initialize the css classes
107 // Call after this.element exists to initialize the css classes
108 // related to selected, rendered and mode.
108 // related to selected, rendered and mode.
109 if (this.selected) {
109 if (this.selected) {
110 this.element.addClass('selected');
110 this.element.addClass('selected');
111 } else {
111 } else {
112 this.element.addClass('unselected');
112 this.element.addClass('unselected');
113 }
113 }
114 if (this.rendered) {
114 if (this.rendered) {
115 this.element.addClass('rendered');
115 this.element.addClass('rendered');
116 } else {
116 } else {
117 this.element.addClass('unrendered');
117 this.element.addClass('unrendered');
118 }
118 }
119 if (this.mode === 'edit') {
119 if (this.mode === 'edit') {
120 this.element.addClass('edit_mode');
120 this.element.addClass('edit_mode');
121 } else {
121 } else {
122 this.element.addClass('command_mode');
122 this.element.addClass('command_mode');
123 }
123 }
124 };
124 };
125
125
126 /**
126 /**
127 * Subclasses can implement override bind_events.
127 * Subclasses can implement override bind_events.
128 * Be carefull to call the parent method when overwriting as it fires event.
128 * Be carefull to call the parent method when overwriting as it fires event.
129 * this will be triggerd after create_element in constructor.
129 * this will be triggerd after create_element in constructor.
130 * @method bind_events
130 * @method bind_events
131 */
131 */
132 Cell.prototype.bind_events = function () {
132 Cell.prototype.bind_events = function () {
133 var that = this;
133 var that = this;
134 // We trigger events so that Cell doesn't have to depend on Notebook.
134 // We trigger events so that Cell doesn't have to depend on Notebook.
135 that.element.click(function (event) {
135 that.element.click(function (event) {
136 if (!that.selected) {
136 if (!that.selected) {
137 that.events.trigger('select.Cell', {'cell':that});
137 that.events.trigger('select.Cell', {'cell':that});
138 }
138 }
139 });
139 });
140 that.element.focusin(function (event) {
140 that.element.focusin(function (event) {
141 if (!that.selected) {
141 if (!that.selected) {
142 that.events.trigger('select.Cell', {'cell':that});
142 that.events.trigger('select.Cell', {'cell':that});
143 }
143 }
144 });
144 });
145 if (this.code_mirror) {
145 if (this.code_mirror) {
146 this.code_mirror.on("change", function(cm, change) {
146 this.code_mirror.on("change", function(cm, change) {
147 that.events.trigger("set_dirty.Notebook", {value: true});
147 that.events.trigger("set_dirty.Notebook", {value: true});
148 });
148 });
149 }
149 }
150 if (this.code_mirror) {
150 if (this.code_mirror) {
151 this.code_mirror.on('focus', function(cm, change) {
151 this.code_mirror.on('focus', function(cm, change) {
152 that.events.trigger('edit_mode.Cell', {cell: that});
152 that.events.trigger('edit_mode.Cell', {cell: that});
153 });
153 });
154 }
154 }
155 if (this.code_mirror) {
155 if (this.code_mirror) {
156 this.code_mirror.on('blur', function(cm, change) {
156 this.code_mirror.on('blur', function(cm, change) {
157 that.events.trigger('command_mode.Cell', {cell: that});
157 that.events.trigger('command_mode.Cell', {cell: that});
158 });
158 });
159 }
159 }
160 };
160 };
161
161
162 /**
162 /**
163 * This method gets called in CodeMirror's onKeyDown/onKeyPress
163 * This method gets called in CodeMirror's onKeyDown/onKeyPress
164 * handlers and is used to provide custom key handling.
164 * handlers and is used to provide custom key handling.
165 *
165 *
166 * To have custom handling, subclasses should override this method, but still call it
166 * To have custom handling, subclasses should override this method, but still call it
167 * in order to process the Edit mode keyboard shortcuts.
167 * in order to process the Edit mode keyboard shortcuts.
168 *
168 *
169 * @method handle_codemirror_keyevent
169 * @method handle_codemirror_keyevent
170 * @param {CodeMirror} editor - The codemirror instance bound to the cell
170 * @param {CodeMirror} editor - The codemirror instance bound to the cell
171 * @param {event} event - key press event which either should or should not be handled by CodeMirror
171 * @param {event} event - key press event which either should or should not be handled by CodeMirror
172 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
172 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
173 */
173 */
174 Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
174 Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
175 var shortcuts = this.keyboard_manager.edit_shortcuts;
175 var shortcuts = this.keyboard_manager.edit_shortcuts;
176
176
177 // if this is an edit_shortcuts shortcut, the global keyboard/shortcut
177 // if this is an edit_shortcuts shortcut, the global keyboard/shortcut
178 // manager will handle it
178 // manager will handle it
179 if (shortcuts.handles(event)) { return true; }
179 if (shortcuts.handles(event)) { return true; }
180
180
181 return false;
181 return false;
182 };
182 };
183
183
184
184
185 /**
185 /**
186 * Triger typsetting of math by mathjax on current cell element
186 * Triger typsetting of math by mathjax on current cell element
187 * @method typeset
187 * @method typeset
188 */
188 */
189 Cell.prototype.typeset = function () {
189 Cell.prototype.typeset = function () {
190 if (window.MathJax) {
190 if (window.MathJax) {
191 var cell_math = this.element.get(0);
191 var cell_math = this.element.get(0);
192 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
192 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
193 }
193 }
194 };
194 };
195
195
196 /**
196 /**
197 * handle cell level logic when a cell is selected
197 * handle cell level logic when a cell is selected
198 * @method select
198 * @method select
199 * @return is the action being taken
199 * @return is the action being taken
200 */
200 */
201 Cell.prototype.select = function () {
201 Cell.prototype.select = function () {
202 if (!this.selected) {
202 if (!this.selected) {
203 this.element.addClass('selected');
203 this.element.addClass('selected');
204 this.element.removeClass('unselected');
204 this.element.removeClass('unselected');
205 this.selected = true;
205 this.selected = true;
206 return true;
206 return true;
207 } else {
207 } else {
208 return false;
208 return false;
209 }
209 }
210 };
210 };
211
211
212 /**
212 /**
213 * handle cell level logic when a cell is unselected
213 * handle cell level logic when a cell is unselected
214 * @method unselect
214 * @method unselect
215 * @return is the action being taken
215 * @return is the action being taken
216 */
216 */
217 Cell.prototype.unselect = function () {
217 Cell.prototype.unselect = function () {
218 if (this.selected) {
218 if (this.selected) {
219 this.element.addClass('unselected');
219 this.element.addClass('unselected');
220 this.element.removeClass('selected');
220 this.element.removeClass('selected');
221 this.selected = false;
221 this.selected = false;
222 return true;
222 return true;
223 } else {
223 } else {
224 return false;
224 return false;
225 }
225 }
226 };
226 };
227
227
228 /**
228 /**
229 * handle cell level logic when a cell is rendered
229 * handle cell level logic when a cell is rendered
230 * @method render
230 * @method render
231 * @return is the action being taken
231 * @return is the action being taken
232 */
232 */
233 Cell.prototype.render = function () {
233 Cell.prototype.render = function () {
234 if (!this.rendered) {
234 if (!this.rendered) {
235 this.element.addClass('rendered');
235 this.element.addClass('rendered');
236 this.element.removeClass('unrendered');
236 this.element.removeClass('unrendered');
237 this.rendered = true;
237 this.rendered = true;
238 return true;
238 return true;
239 } else {
239 } else {
240 return false;
240 return false;
241 }
241 }
242 };
242 };
243
243
244 /**
244 /**
245 * handle cell level logic when a cell is unrendered
245 * handle cell level logic when a cell is unrendered
246 * @method unrender
246 * @method unrender
247 * @return is the action being taken
247 * @return is the action being taken
248 */
248 */
249 Cell.prototype.unrender = function () {
249 Cell.prototype.unrender = function () {
250 if (this.rendered) {
250 if (this.rendered) {
251 this.element.addClass('unrendered');
251 this.element.addClass('unrendered');
252 this.element.removeClass('rendered');
252 this.element.removeClass('rendered');
253 this.rendered = false;
253 this.rendered = false;
254 return true;
254 return true;
255 } else {
255 } else {
256 return false;
256 return false;
257 }
257 }
258 };
258 };
259
259
260 /**
260 /**
261 * Delegates keyboard shortcut handling to either IPython keyboard
261 * Delegates keyboard shortcut handling to either IPython keyboard
262 * manager when in command mode, or CodeMirror when in edit mode
262 * manager when in command mode, or CodeMirror when in edit mode
263 *
263 *
264 * @method handle_keyevent
264 * @method handle_keyevent
265 * @param {CodeMirror} editor - The codemirror instance bound to the cell
265 * @param {CodeMirror} editor - The codemirror instance bound to the cell
266 * @param {event} - key event to be handled
266 * @param {event} - key event to be handled
267 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
267 * @return {Boolean} `true` if CodeMirror should ignore the event, `false` Otherwise
268 */
268 */
269 Cell.prototype.handle_keyevent = function (editor, event) {
269 Cell.prototype.handle_keyevent = function (editor, event) {
270
270
271 // console.log('CM', this.mode, event.which, event.type)
271 // console.log('CM', this.mode, event.which, event.type)
272
272
273 if (this.mode === 'command') {
273 if (this.mode === 'command') {
274 return true;
274 return true;
275 } else if (this.mode === 'edit') {
275 } else if (this.mode === 'edit') {
276 return this.handle_codemirror_keyevent(editor, event);
276 return this.handle_codemirror_keyevent(editor, event);
277 }
277 }
278 };
278 };
279
279
280 /**
280 /**
281 * @method at_top
281 * @method at_top
282 * @return {Boolean}
282 * @return {Boolean}
283 */
283 */
284 Cell.prototype.at_top = function () {
284 Cell.prototype.at_top = function () {
285 var cm = this.code_mirror;
285 var cm = this.code_mirror;
286 var cursor = cm.getCursor();
286 var cursor = cm.getCursor();
287 if (cursor.line === 0 && cursor.ch === 0) {
287 if (cursor.line === 0 && cursor.ch === 0) {
288 return true;
288 return true;
289 }
289 }
290 return false;
290 return false;
291 };
291 };
292
292
293 /**
293 /**
294 * @method at_bottom
294 * @method at_bottom
295 * @return {Boolean}
295 * @return {Boolean}
296 * */
296 * */
297 Cell.prototype.at_bottom = function () {
297 Cell.prototype.at_bottom = function () {
298 var cm = this.code_mirror;
298 var cm = this.code_mirror;
299 var cursor = cm.getCursor();
299 var cursor = cm.getCursor();
300 if (cursor.line === (cm.lineCount()-1) && cursor.ch === cm.getLine(cursor.line).length) {
300 if (cursor.line === (cm.lineCount()-1) && cursor.ch === cm.getLine(cursor.line).length) {
301 return true;
301 return true;
302 }
302 }
303 return false;
303 return false;
304 };
304 };
305
305
306 /**
306 /**
307 * enter the command mode for the cell
307 * enter the command mode for the cell
308 * @method command_mode
308 * @method command_mode
309 * @return is the action being taken
309 * @return is the action being taken
310 */
310 */
311 Cell.prototype.command_mode = function () {
311 Cell.prototype.command_mode = function () {
312 if (this.mode !== 'command') {
312 if (this.mode !== 'command') {
313 this.element.addClass('command_mode');
313 this.element.addClass('command_mode');
314 this.element.removeClass('edit_mode');
314 this.element.removeClass('edit_mode');
315 this.mode = 'command';
315 this.mode = 'command';
316 return true;
316 return true;
317 } else {
317 } else {
318 return false;
318 return false;
319 }
319 }
320 };
320 };
321
321
322 /**
322 /**
323 * enter the edit mode for the cell
323 * enter the edit mode for the cell
324 * @method command_mode
324 * @method command_mode
325 * @return is the action being taken
325 * @return is the action being taken
326 */
326 */
327 Cell.prototype.edit_mode = function () {
327 Cell.prototype.edit_mode = function () {
328 if (this.mode !== 'edit') {
328 if (this.mode !== 'edit') {
329 this.element.addClass('edit_mode');
329 this.element.addClass('edit_mode');
330 this.element.removeClass('command_mode');
330 this.element.removeClass('command_mode');
331 this.mode = 'edit';
331 this.mode = 'edit';
332 return true;
332 return true;
333 } else {
333 } else {
334 return false;
334 return false;
335 }
335 }
336 };
336 };
337
337
338 /**
338 /**
339 * Focus the cell in the DOM sense
339 * Focus the cell in the DOM sense
340 * @method focus_cell
340 * @method focus_cell
341 */
341 */
342 Cell.prototype.focus_cell = function () {
342 Cell.prototype.focus_cell = function () {
343 this.element.focus();
343 this.element.focus();
344 };
344 };
345
345
346 /**
346 /**
347 * Focus the editor area so a user can type
347 * Focus the editor area so a user can type
348 *
348 *
349 * NOTE: If codemirror is focused via a mouse click event, you don't want to
349 * NOTE: If codemirror is focused via a mouse click event, you don't want to
350 * call this because it will cause a page jump.
350 * call this because it will cause a page jump.
351 * @method focus_editor
351 * @method focus_editor
352 */
352 */
353 Cell.prototype.focus_editor = function () {
353 Cell.prototype.focus_editor = function () {
354 this.refresh();
354 this.refresh();
355 this.code_mirror.focus();
355 this.code_mirror.focus();
356 };
356 };
357
357
358 /**
358 /**
359 * Refresh codemirror instance
359 * Refresh codemirror instance
360 * @method refresh
360 * @method refresh
361 */
361 */
362 Cell.prototype.refresh = function () {
362 Cell.prototype.refresh = function () {
363 this.code_mirror.refresh();
363 this.code_mirror.refresh();
364 };
364 };
365
365
366 /**
366 /**
367 * should be overritten by subclass
367 * should be overritten by subclass
368 * @method get_text
368 * @method get_text
369 */
369 */
370 Cell.prototype.get_text = function () {
370 Cell.prototype.get_text = function () {
371 };
371 };
372
372
373 /**
373 /**
374 * should be overritten by subclass
374 * should be overritten by subclass
375 * @method set_text
375 * @method set_text
376 * @param {string} text
376 * @param {string} text
377 */
377 */
378 Cell.prototype.set_text = function (text) {
378 Cell.prototype.set_text = function (text) {
379 };
379 };
380
380
381 /**
381 /**
382 * should be overritten by subclass
382 * should be overritten by subclass
383 * serialise cell to json.
383 * serialise cell to json.
384 * @method toJSON
384 * @method toJSON
385 **/
385 **/
386 Cell.prototype.toJSON = function () {
386 Cell.prototype.toJSON = function () {
387 var data = {};
387 var data = {};
388 // deepcopy the metadata so copied cells don't share the same object
388 // deepcopy the metadata so copied cells don't share the same object
389 data.metadata = JSON.parse(JSON.stringify(this.metadata));
389 data.metadata = JSON.parse(JSON.stringify(this.metadata));
390 data.cell_type = this.cell_type;
390 data.cell_type = this.cell_type;
391 return data;
391 return data;
392 };
392 };
393
393
394
394
395 /**
395 /**
396 * should be overritten by subclass
396 * should be overritten by subclass
397 * @method fromJSON
397 * @method fromJSON
398 **/
398 **/
399 Cell.prototype.fromJSON = function (data) {
399 Cell.prototype.fromJSON = function (data) {
400 if (data.metadata !== undefined) {
400 if (data.metadata !== undefined) {
401 this.metadata = data.metadata;
401 this.metadata = data.metadata;
402 }
402 }
403 this.celltoolbar.rebuild();
403 this.celltoolbar.rebuild();
404 };
404 };
405
405
406
406
407 /**
407 /**
408 * can the cell be split into two cells (false if not deletable)
408 * can the cell be split into two cells (false if not deletable)
409 * @method is_splittable
409 * @method is_splittable
410 **/
410 **/
411 Cell.prototype.is_splittable = function () {
411 Cell.prototype.is_splittable = function () {
412 return this.is_deletable();
412 return this.is_deletable();
413 };
413 };
414
414
415
415
416 /**
416 /**
417 * can the cell be merged with other cells (false if not deletable)
417 * can the cell be merged with other cells (false if not deletable)
418 * @method is_mergeable
418 * @method is_mergeable
419 **/
419 **/
420 Cell.prototype.is_mergeable = function () {
420 Cell.prototype.is_mergeable = function () {
421 return this.is_deletable();
421 return this.is_deletable();
422 };
422 };
423
423
424 /**
424 /**
425 * is the cell deletable? (true by default)
425 * is the cell deletable? only false (undeletable) if
426 * metadata.deletable is explicitly false -- everything else
427 * counts as true
428 *
426 * @method is_deletable
429 * @method is_deletable
427 **/
430 **/
428 Cell.prototype.is_deletable = function () {
431 Cell.prototype.is_deletable = function () {
429 if (this.metadata.deletable === undefined) {
432 if (this.metadata.deletable === false) {
430 return true;
433 return false;
431 }
434 }
432 return Boolean(this.metadata.deletable);
435 return true;
433 };
436 };
434
437
435 /**
438 /**
436 * @return {String} - the text before the cursor
439 * @return {String} - the text before the cursor
437 * @method get_pre_cursor
440 * @method get_pre_cursor
438 **/
441 **/
439 Cell.prototype.get_pre_cursor = function () {
442 Cell.prototype.get_pre_cursor = function () {
440 var cursor = this.code_mirror.getCursor();
443 var cursor = this.code_mirror.getCursor();
441 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
444 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
442 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
445 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
443 return text;
446 return text;
444 };
447 };
445
448
446
449
447 /**
450 /**
448 * @return {String} - the text after the cursor
451 * @return {String} - the text after the cursor
449 * @method get_post_cursor
452 * @method get_post_cursor
450 **/
453 **/
451 Cell.prototype.get_post_cursor = function () {
454 Cell.prototype.get_post_cursor = function () {
452 var cursor = this.code_mirror.getCursor();
455 var cursor = this.code_mirror.getCursor();
453 var last_line_num = this.code_mirror.lineCount()-1;
456 var last_line_num = this.code_mirror.lineCount()-1;
454 var last_line_len = this.code_mirror.getLine(last_line_num).length;
457 var last_line_len = this.code_mirror.getLine(last_line_num).length;
455 var end = {line:last_line_num, ch:last_line_len};
458 var end = {line:last_line_num, ch:last_line_len};
456 var text = this.code_mirror.getRange(cursor, end);
459 var text = this.code_mirror.getRange(cursor, end);
457 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
460 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
458 return text;
461 return text;
459 };
462 };
460
463
461 /**
464 /**
462 * Show/Hide CodeMirror LineNumber
465 * Show/Hide CodeMirror LineNumber
463 * @method show_line_numbers
466 * @method show_line_numbers
464 *
467 *
465 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
468 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
466 **/
469 **/
467 Cell.prototype.show_line_numbers = function (value) {
470 Cell.prototype.show_line_numbers = function (value) {
468 this.code_mirror.setOption('lineNumbers', value);
471 this.code_mirror.setOption('lineNumbers', value);
469 this.code_mirror.refresh();
472 this.code_mirror.refresh();
470 };
473 };
471
474
472 /**
475 /**
473 * Toggle CodeMirror LineNumber
476 * Toggle CodeMirror LineNumber
474 * @method toggle_line_numbers
477 * @method toggle_line_numbers
475 **/
478 **/
476 Cell.prototype.toggle_line_numbers = function () {
479 Cell.prototype.toggle_line_numbers = function () {
477 var val = this.code_mirror.getOption('lineNumbers');
480 var val = this.code_mirror.getOption('lineNumbers');
478 this.show_line_numbers(!val);
481 this.show_line_numbers(!val);
479 };
482 };
480
483
481 /**
484 /**
482 * Force codemirror highlight mode
485 * Force codemirror highlight mode
483 * @method force_highlight
486 * @method force_highlight
484 * @param {object} - CodeMirror mode
487 * @param {object} - CodeMirror mode
485 **/
488 **/
486 Cell.prototype.force_highlight = function(mode) {
489 Cell.prototype.force_highlight = function(mode) {
487 this.user_highlight = mode;
490 this.user_highlight = mode;
488 this.auto_highlight();
491 this.auto_highlight();
489 };
492 };
490
493
491 /**
494 /**
492 * Try to autodetect cell highlight mode, or use selected mode
495 * Try to autodetect cell highlight mode, or use selected mode
493 * @methods _auto_highlight
496 * @methods _auto_highlight
494 * @private
497 * @private
495 * @param {String|object|undefined} - CodeMirror mode | 'auto'
498 * @param {String|object|undefined} - CodeMirror mode | 'auto'
496 **/
499 **/
497 Cell.prototype._auto_highlight = function (modes) {
500 Cell.prototype._auto_highlight = function (modes) {
498 //Here we handle manually selected modes
501 //Here we handle manually selected modes
499 var mode;
502 var mode;
500 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
503 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
501 {
504 {
502 mode = this.user_highlight;
505 mode = this.user_highlight;
503 CodeMirror.autoLoadMode(this.code_mirror, mode);
506 CodeMirror.autoLoadMode(this.code_mirror, mode);
504 this.code_mirror.setOption('mode', mode);
507 this.code_mirror.setOption('mode', mode);
505 return;
508 return;
506 }
509 }
507 var current_mode = this.code_mirror.getOption('mode', mode);
510 var current_mode = this.code_mirror.getOption('mode', mode);
508 var first_line = this.code_mirror.getLine(0);
511 var first_line = this.code_mirror.getLine(0);
509 // loop on every pairs
512 // loop on every pairs
510 for(mode in modes) {
513 for(mode in modes) {
511 var regs = modes[mode].reg;
514 var regs = modes[mode].reg;
512 // only one key every time but regexp can't be keys...
515 // only one key every time but regexp can't be keys...
513 for(var i=0; i<regs.length; i++) {
516 for(var i=0; i<regs.length; i++) {
514 // here we handle non magic_modes
517 // here we handle non magic_modes
515 if(first_line.match(regs[i]) !== null) {
518 if(first_line.match(regs[i]) !== null) {
516 if(current_mode == mode){
519 if(current_mode == mode){
517 return;
520 return;
518 }
521 }
519 if (mode.search('magic_') !== 0) {
522 if (mode.search('magic_') !== 0) {
520 this.code_mirror.setOption('mode', mode);
523 this.code_mirror.setOption('mode', mode);
521 CodeMirror.autoLoadMode(this.code_mirror, mode);
524 CodeMirror.autoLoadMode(this.code_mirror, mode);
522 return;
525 return;
523 }
526 }
524 var open = modes[mode].open || "%%";
527 var open = modes[mode].open || "%%";
525 var close = modes[mode].close || "%%end";
528 var close = modes[mode].close || "%%end";
526 var mmode = mode;
529 var mmode = mode;
527 mode = mmode.substr(6);
530 mode = mmode.substr(6);
528 if(current_mode == mode){
531 if(current_mode == mode){
529 return;
532 return;
530 }
533 }
531 CodeMirror.autoLoadMode(this.code_mirror, mode);
534 CodeMirror.autoLoadMode(this.code_mirror, mode);
532 // create on the fly a mode that swhitch between
535 // create on the fly a mode that swhitch between
533 // plain/text and smth else otherwise `%%` is
536 // plain/text and smth else otherwise `%%` is
534 // source of some highlight issues.
537 // source of some highlight issues.
535 // we use patchedGetMode to circumvent a bug in CM
538 // we use patchedGetMode to circumvent a bug in CM
536 CodeMirror.defineMode(mmode , function(config) {
539 CodeMirror.defineMode(mmode , function(config) {
537 return CodeMirror.multiplexingMode(
540 return CodeMirror.multiplexingMode(
538 CodeMirror.patchedGetMode(config, 'text/plain'),
541 CodeMirror.patchedGetMode(config, 'text/plain'),
539 // always set someting on close
542 // always set someting on close
540 {open: open, close: close,
543 {open: open, close: close,
541 mode: CodeMirror.patchedGetMode(config, mode),
544 mode: CodeMirror.patchedGetMode(config, mode),
542 delimStyle: "delimit"
545 delimStyle: "delimit"
543 }
546 }
544 );
547 );
545 });
548 });
546 this.code_mirror.setOption('mode', mmode);
549 this.code_mirror.setOption('mode', mmode);
547 return;
550 return;
548 }
551 }
549 }
552 }
550 }
553 }
551 // fallback on default
554 // fallback on default
552 var default_mode;
555 var default_mode;
553 try {
556 try {
554 default_mode = this._options.cm_config.mode;
557 default_mode = this._options.cm_config.mode;
555 } catch(e) {
558 } catch(e) {
556 default_mode = 'text/plain';
559 default_mode = 'text/plain';
557 }
560 }
558 if( current_mode === default_mode){
561 if( current_mode === default_mode){
559 return;
562 return;
560 }
563 }
561 this.code_mirror.setOption('mode', default_mode);
564 this.code_mirror.setOption('mode', default_mode);
562 };
565 };
563
566
564 // Backwards compatibility.
567 // Backwards compatibility.
565 IPython.Cell = Cell;
568 IPython.Cell = Cell;
566
569
567 return {'Cell': Cell};
570 return {'Cell': Cell};
568 });
571 });
General Comments 0
You need to be logged in to leave comments. Login now