Show More
@@ -1,557 +1,557 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/ |
|
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/ |
|
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 | data.metadata = this.metadata; |
|
388 | data.metadata = this.metadata; | |
389 | data.cell_type = this.cell_type; |
|
389 | data.cell_type = this.cell_type; | |
390 | return data; |
|
390 | return data; | |
391 | }; |
|
391 | }; | |
392 |
|
392 | |||
393 |
|
393 | |||
394 | /** |
|
394 | /** | |
395 | * should be overritten by subclass |
|
395 | * should be overritten by subclass | |
396 | * @method fromJSON |
|
396 | * @method fromJSON | |
397 | **/ |
|
397 | **/ | |
398 | Cell.prototype.fromJSON = function (data) { |
|
398 | Cell.prototype.fromJSON = function (data) { | |
399 | if (data.metadata !== undefined) { |
|
399 | if (data.metadata !== undefined) { | |
400 | this.metadata = data.metadata; |
|
400 | this.metadata = data.metadata; | |
401 | } |
|
401 | } | |
402 | this.celltoolbar.rebuild(); |
|
402 | this.celltoolbar.rebuild(); | |
403 | }; |
|
403 | }; | |
404 |
|
404 | |||
405 |
|
405 | |||
406 | /** |
|
406 | /** | |
407 | * can the cell be split into two cells |
|
407 | * can the cell be split into two cells | |
408 | * @method is_splittable |
|
408 | * @method is_splittable | |
409 | **/ |
|
409 | **/ | |
410 | Cell.prototype.is_splittable = function () { |
|
410 | Cell.prototype.is_splittable = function () { | |
411 | return true; |
|
411 | return true; | |
412 | }; |
|
412 | }; | |
413 |
|
413 | |||
414 |
|
414 | |||
415 | /** |
|
415 | /** | |
416 | * can the cell be merged with other cells |
|
416 | * can the cell be merged with other cells | |
417 | * @method is_mergeable |
|
417 | * @method is_mergeable | |
418 | **/ |
|
418 | **/ | |
419 | Cell.prototype.is_mergeable = function () { |
|
419 | Cell.prototype.is_mergeable = function () { | |
420 | return true; |
|
420 | return true; | |
421 | }; |
|
421 | }; | |
422 |
|
422 | |||
423 |
|
423 | |||
424 | /** |
|
424 | /** | |
425 | * @return {String} - the text before the cursor |
|
425 | * @return {String} - the text before the cursor | |
426 | * @method get_pre_cursor |
|
426 | * @method get_pre_cursor | |
427 | **/ |
|
427 | **/ | |
428 | Cell.prototype.get_pre_cursor = function () { |
|
428 | Cell.prototype.get_pre_cursor = function () { | |
429 | var cursor = this.code_mirror.getCursor(); |
|
429 | var cursor = this.code_mirror.getCursor(); | |
430 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); |
|
430 | var text = this.code_mirror.getRange({line:0, ch:0}, cursor); | |
431 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
431 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
432 | return text; |
|
432 | return text; | |
433 | }; |
|
433 | }; | |
434 |
|
434 | |||
435 |
|
435 | |||
436 | /** |
|
436 | /** | |
437 | * @return {String} - the text after the cursor |
|
437 | * @return {String} - the text after the cursor | |
438 | * @method get_post_cursor |
|
438 | * @method get_post_cursor | |
439 | **/ |
|
439 | **/ | |
440 | Cell.prototype.get_post_cursor = function () { |
|
440 | Cell.prototype.get_post_cursor = function () { | |
441 | var cursor = this.code_mirror.getCursor(); |
|
441 | var cursor = this.code_mirror.getCursor(); | |
442 | var last_line_num = this.code_mirror.lineCount()-1; |
|
442 | var last_line_num = this.code_mirror.lineCount()-1; | |
443 | var last_line_len = this.code_mirror.getLine(last_line_num).length; |
|
443 | var last_line_len = this.code_mirror.getLine(last_line_num).length; | |
444 | var end = {line:last_line_num, ch:last_line_len}; |
|
444 | var end = {line:last_line_num, ch:last_line_len}; | |
445 | var text = this.code_mirror.getRange(cursor, end); |
|
445 | var text = this.code_mirror.getRange(cursor, end); | |
446 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); |
|
446 | text = text.replace(/^\n+/, '').replace(/\n+$/, ''); | |
447 | return text; |
|
447 | return text; | |
448 | }; |
|
448 | }; | |
449 |
|
449 | |||
450 | /** |
|
450 | /** | |
451 | * Show/Hide CodeMirror LineNumber |
|
451 | * Show/Hide CodeMirror LineNumber | |
452 | * @method show_line_numbers |
|
452 | * @method show_line_numbers | |
453 | * |
|
453 | * | |
454 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror |
|
454 | * @param value {Bool} show (true), or hide (false) the line number in CodeMirror | |
455 | **/ |
|
455 | **/ | |
456 | Cell.prototype.show_line_numbers = function (value) { |
|
456 | Cell.prototype.show_line_numbers = function (value) { | |
457 | this.code_mirror.setOption('lineNumbers', value); |
|
457 | this.code_mirror.setOption('lineNumbers', value); | |
458 | this.code_mirror.refresh(); |
|
458 | this.code_mirror.refresh(); | |
459 | }; |
|
459 | }; | |
460 |
|
460 | |||
461 | /** |
|
461 | /** | |
462 | * Toggle CodeMirror LineNumber |
|
462 | * Toggle CodeMirror LineNumber | |
463 | * @method toggle_line_numbers |
|
463 | * @method toggle_line_numbers | |
464 | **/ |
|
464 | **/ | |
465 | Cell.prototype.toggle_line_numbers = function () { |
|
465 | Cell.prototype.toggle_line_numbers = function () { | |
466 | var val = this.code_mirror.getOption('lineNumbers'); |
|
466 | var val = this.code_mirror.getOption('lineNumbers'); | |
467 | this.show_line_numbers(!val); |
|
467 | this.show_line_numbers(!val); | |
468 | }; |
|
468 | }; | |
469 |
|
469 | |||
470 | /** |
|
470 | /** | |
471 | * Force codemirror highlight mode |
|
471 | * Force codemirror highlight mode | |
472 | * @method force_highlight |
|
472 | * @method force_highlight | |
473 | * @param {object} - CodeMirror mode |
|
473 | * @param {object} - CodeMirror mode | |
474 | **/ |
|
474 | **/ | |
475 | Cell.prototype.force_highlight = function(mode) { |
|
475 | Cell.prototype.force_highlight = function(mode) { | |
476 | this.user_highlight = mode; |
|
476 | this.user_highlight = mode; | |
477 | this.auto_highlight(); |
|
477 | this.auto_highlight(); | |
478 | }; |
|
478 | }; | |
479 |
|
479 | |||
480 | /** |
|
480 | /** | |
481 | * Try to autodetect cell highlight mode, or use selected mode |
|
481 | * Try to autodetect cell highlight mode, or use selected mode | |
482 | * @methods _auto_highlight |
|
482 | * @methods _auto_highlight | |
483 | * @private |
|
483 | * @private | |
484 | * @param {String|object|undefined} - CodeMirror mode | 'auto' |
|
484 | * @param {String|object|undefined} - CodeMirror mode | 'auto' | |
485 | **/ |
|
485 | **/ | |
486 | Cell.prototype._auto_highlight = function (modes) { |
|
486 | Cell.prototype._auto_highlight = function (modes) { | |
487 | //Here we handle manually selected modes |
|
487 | //Here we handle manually selected modes | |
488 | var mode; |
|
488 | var mode; | |
489 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) |
|
489 | if( this.user_highlight !== undefined && this.user_highlight != 'auto' ) | |
490 | { |
|
490 | { | |
491 | mode = this.user_highlight; |
|
491 | mode = this.user_highlight; | |
492 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
492 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
493 | this.code_mirror.setOption('mode', mode); |
|
493 | this.code_mirror.setOption('mode', mode); | |
494 | return; |
|
494 | return; | |
495 | } |
|
495 | } | |
496 | var current_mode = this.code_mirror.getOption('mode', mode); |
|
496 | var current_mode = this.code_mirror.getOption('mode', mode); | |
497 | var first_line = this.code_mirror.getLine(0); |
|
497 | var first_line = this.code_mirror.getLine(0); | |
498 | // loop on every pairs |
|
498 | // loop on every pairs | |
499 | for(mode in modes) { |
|
499 | for(mode in modes) { | |
500 | var regs = modes[mode].reg; |
|
500 | var regs = modes[mode].reg; | |
501 | // only one key every time but regexp can't be keys... |
|
501 | // only one key every time but regexp can't be keys... | |
502 | for(var i=0; i<regs.length; i++) { |
|
502 | for(var i=0; i<regs.length; i++) { | |
503 | // here we handle non magic_modes |
|
503 | // here we handle non magic_modes | |
504 | if(first_line.match(regs[i]) !== null) { |
|
504 | if(first_line.match(regs[i]) !== null) { | |
505 | if(current_mode == mode){ |
|
505 | if(current_mode == mode){ | |
506 | return; |
|
506 | return; | |
507 | } |
|
507 | } | |
508 | if (mode.search('magic_') !== 0) { |
|
508 | if (mode.search('magic_') !== 0) { | |
509 | this.code_mirror.setOption('mode', mode); |
|
509 | this.code_mirror.setOption('mode', mode); | |
510 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
510 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
511 | return; |
|
511 | return; | |
512 | } |
|
512 | } | |
513 | var open = modes[mode].open || "%%"; |
|
513 | var open = modes[mode].open || "%%"; | |
514 | var close = modes[mode].close || "%%end"; |
|
514 | var close = modes[mode].close || "%%end"; | |
515 | var mmode = mode; |
|
515 | var mmode = mode; | |
516 | mode = mmode.substr(6); |
|
516 | mode = mmode.substr(6); | |
517 | if(current_mode == mode){ |
|
517 | if(current_mode == mode){ | |
518 | return; |
|
518 | return; | |
519 | } |
|
519 | } | |
520 | CodeMirror.autoLoadMode(this.code_mirror, mode); |
|
520 | CodeMirror.autoLoadMode(this.code_mirror, mode); | |
521 | // create on the fly a mode that swhitch between |
|
521 | // create on the fly a mode that swhitch between | |
522 | // plain/text and smth else otherwise `%%` is |
|
522 | // plain/text and smth else otherwise `%%` is | |
523 | // source of some highlight issues. |
|
523 | // source of some highlight issues. | |
524 | // we use patchedGetMode to circumvent a bug in CM |
|
524 | // we use patchedGetMode to circumvent a bug in CM | |
525 | CodeMirror.defineMode(mmode , function(config) { |
|
525 | CodeMirror.defineMode(mmode , function(config) { | |
526 | return CodeMirror.multiplexingMode( |
|
526 | return CodeMirror.multiplexingMode( | |
527 | CodeMirror.patchedGetMode(config, 'text/plain'), |
|
527 | CodeMirror.patchedGetMode(config, 'text/plain'), | |
528 | // always set someting on close |
|
528 | // always set someting on close | |
529 | {open: open, close: close, |
|
529 | {open: open, close: close, | |
530 | mode: CodeMirror.patchedGetMode(config, mode), |
|
530 | mode: CodeMirror.patchedGetMode(config, mode), | |
531 | delimStyle: "delimit" |
|
531 | delimStyle: "delimit" | |
532 | } |
|
532 | } | |
533 | ); |
|
533 | ); | |
534 | }); |
|
534 | }); | |
535 | this.code_mirror.setOption('mode', mmode); |
|
535 | this.code_mirror.setOption('mode', mmode); | |
536 | return; |
|
536 | return; | |
537 | } |
|
537 | } | |
538 | } |
|
538 | } | |
539 | } |
|
539 | } | |
540 | // fallback on default |
|
540 | // fallback on default | |
541 | var default_mode; |
|
541 | var default_mode; | |
542 | try { |
|
542 | try { | |
543 | default_mode = this._options.cm_config.mode; |
|
543 | default_mode = this._options.cm_config.mode; | |
544 | } catch(e) { |
|
544 | } catch(e) { | |
545 | default_mode = 'text/plain'; |
|
545 | default_mode = 'text/plain'; | |
546 | } |
|
546 | } | |
547 | if( current_mode === default_mode){ |
|
547 | if( current_mode === default_mode){ | |
548 | return; |
|
548 | return; | |
549 | } |
|
549 | } | |
550 | this.code_mirror.setOption('mode', default_mode); |
|
550 | this.code_mirror.setOption('mode', default_mode); | |
551 | }; |
|
551 | }; | |
552 |
|
552 | |||
553 | // Backwards compatibility. |
|
553 | // Backwards compatibility. | |
554 | IPython.Cell = Cell; |
|
554 | IPython.Cell = Cell; | |
555 |
|
555 | |||
556 | return {'Cell': Cell}; |
|
556 | return {'Cell': Cell}; | |
557 | }); |
|
557 | }); |
@@ -1,44 +1,44 b'' | |||||
1 | // IPython GFM (GitHub Flavored Markdown) mode is just a slightly altered GFM |
|
1 | // IPython GFM (GitHub Flavored Markdown) mode is just a slightly altered GFM | |
2 | // Mode with support for latex. |
|
2 | // Mode with support for latex. | |
3 | // |
|
3 | // | |
4 | // Latex support was supported by Codemirror GFM as of |
|
4 | // Latex support was supported by Codemirror GFM as of | |
5 |
// https://github.com/ |
|
5 | // https://github.com/codemirror/CodeMirror/pull/567 | |
6 | // But was later removed in |
|
6 | // But was later removed in | |
7 |
// https://github.com/ |
|
7 | // https://github.com/codemirror/CodeMirror/commit/d9c9f1b1ffe984aee41307f3e927f80d1f23590c | |
8 |
|
8 | |||
9 | CodeMirror.requireMode('gfm', function(){ |
|
9 | CodeMirror.requireMode('gfm', function(){ | |
10 | CodeMirror.requireMode('stex', function(){ |
|
10 | CodeMirror.requireMode('stex', function(){ | |
11 | CodeMirror.defineMode("ipythongfm", function(config, parserConfig) { |
|
11 | CodeMirror.defineMode("ipythongfm", function(config, parserConfig) { | |
12 |
|
12 | |||
13 | var gfm_mode = CodeMirror.getMode(config, "gfm"); |
|
13 | var gfm_mode = CodeMirror.getMode(config, "gfm"); | |
14 | var tex_mode = CodeMirror.getMode(config, "stex"); |
|
14 | var tex_mode = CodeMirror.getMode(config, "stex"); | |
15 |
|
15 | |||
16 | return CodeMirror.multiplexingMode( |
|
16 | return CodeMirror.multiplexingMode( | |
17 | gfm_mode, |
|
17 | gfm_mode, | |
18 | { |
|
18 | { | |
19 | open: "$", close: "$", |
|
19 | open: "$", close: "$", | |
20 | mode: tex_mode, |
|
20 | mode: tex_mode, | |
21 | delimStyle: "delimit" |
|
21 | delimStyle: "delimit" | |
22 | }, |
|
22 | }, | |
23 | { |
|
23 | { | |
24 | open: "$$", close: "$$", |
|
24 | open: "$$", close: "$$", | |
25 | mode: tex_mode, |
|
25 | mode: tex_mode, | |
26 | delimStyle: "delimit" |
|
26 | delimStyle: "delimit" | |
27 | }, |
|
27 | }, | |
28 | { |
|
28 | { | |
29 | open: "\\(", close: "\\)", |
|
29 | open: "\\(", close: "\\)", | |
30 | mode: tex_mode, |
|
30 | mode: tex_mode, | |
31 | delimStyle: "delimit" |
|
31 | delimStyle: "delimit" | |
32 | }, |
|
32 | }, | |
33 | { |
|
33 | { | |
34 | open: "\\[", close: "\\]", |
|
34 | open: "\\[", close: "\\]", | |
35 | mode: tex_mode, |
|
35 | mode: tex_mode, | |
36 | delimStyle: "delimit" |
|
36 | delimStyle: "delimit" | |
37 | } |
|
37 | } | |
38 | // .. more multiplexed styles can follow here |
|
38 | // .. more multiplexed styles can follow here | |
39 | ); |
|
39 | ); | |
40 | }, 'gfm'); |
|
40 | }, 'gfm'); | |
41 |
|
41 | |||
42 | CodeMirror.defineMIME("text/x-ipythongfm", "ipythongfm"); |
|
42 | CodeMirror.defineMIME("text/x-ipythongfm", "ipythongfm"); | |
43 | }); |
|
43 | }); | |
44 | }); |
|
44 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now