##// END OF EJS Templates
edit text cells on double-click instead of single-click...
MinRK -
Show More
@@ -1,272 +1,272 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 // TextCell
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 // TextCell base class
15 15
16 16 var TextCell = function (notebook) {
17 17 this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
18 18 this.placeholder = this.placeholder || '\u0000';
19 19 IPython.Cell.apply(this, arguments);
20 20 this.rendered = false;
21 21 this.cell_type = this.cell_type || 'text';
22 22 };
23 23
24 24
25 25 TextCell.prototype = new IPython.Cell();
26 26
27 27
28 28 TextCell.prototype.create_element = function () {
29 29 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
30 30 cell.attr('tabindex','2');
31 31 var input_area = $('<div/>').addClass('text_cell_input');
32 32 this.code_mirror = CodeMirror(input_area.get(0), {
33 33 indentUnit : 4,
34 34 mode: this.code_mirror_mode,
35 35 theme: 'default',
36 36 value: this.placeholder,
37 37 readOnly: this.read_only
38 38 });
39 39 // The tabindex=-1 makes this div focusable.
40 40 var render_area = $('<div/>').addClass('text_cell_render').
41 41 addClass('rendered_html').attr('tabindex','-1');
42 42 cell.append(input_area).append(render_area);
43 43 this.element = cell;
44 44 };
45 45
46 46
47 47 TextCell.prototype.bind_events = function () {
48 48 IPython.Cell.prototype.bind_events.apply(this);
49 49 var that = this;
50 50 this.element.keydown(function (event) {
51 51 if (event.which === 13) {
52 52 if (that.rendered) {
53 53 that.edit();
54 54 event.preventDefault();
55 55 }
56 56 }
57 57 });
58 58 };
59 59
60 60
61 61 TextCell.prototype.select = function () {
62 62 IPython.Cell.prototype.select.apply(this);
63 63 var output = this.element.find("div.text_cell_render");
64 64 output.trigger('focus');
65 65 };
66 66
67 67
68 68 TextCell.prototype.edit = function () {
69 69 if ( this.read_only ) return;
70 70 if (this.rendered === true) {
71 71 var text_cell = this.element;
72 72 var output = text_cell.find("div.text_cell_render");
73 73 output.hide();
74 74 text_cell.find('div.text_cell_input').show();
75 75 this.code_mirror.focus();
76 76 this.code_mirror.refresh();
77 77 this.rendered = false;
78 78 if (this.get_source() === this.placeholder) {
79 79 this.set_source('');
80 80 }
81 81 }
82 82 };
83 83
84 84
85 85 // Subclasses must define render.
86 86 TextCell.prototype.render = function () {};
87 87
88 88
89 89 TextCell.prototype.config_mathjax = function () {
90 90 var text_cell = this.element;
91 91 var that = this;
92 text_cell.click(function () {
92 text_cell.dblclick(function () {
93 93 that.edit();
94 94 }).focusout(function () {
95 95 that.render();
96 96 });
97 97
98 98 text_cell.trigger("focusout");
99 99 };
100 100
101 101
102 102 TextCell.prototype.get_source = function() {
103 103 return this.code_mirror.getValue();
104 104 };
105 105
106 106
107 107 TextCell.prototype.set_source = function(text) {
108 108 this.code_mirror.setValue(text);
109 109 this.code_mirror.refresh();
110 110 };
111 111
112 112
113 113 TextCell.prototype.get_rendered = function() {
114 114 return this.element.find('div.text_cell_render').html();
115 115 };
116 116
117 117
118 118 TextCell.prototype.set_rendered = function(text) {
119 119 this.element.find('div.text_cell_render').html(text);
120 120 };
121 121
122 122
123 123 TextCell.prototype.at_top = function () {
124 124 if (this.rendered) {
125 125 return true;
126 126 } else {
127 127 return false;
128 128 }
129 129 };
130 130
131 131
132 132 TextCell.prototype.at_bottom = function () {
133 133 if (this.rendered) {
134 134 return true;
135 135 } else {
136 136 return false;
137 137 }
138 138 };
139 139
140 140
141 141 TextCell.prototype.fromJSON = function (data) {
142 142 if (data.cell_type === this.cell_type) {
143 143 if (data.source !== undefined) {
144 144 this.set_source(data.source);
145 145 this.set_rendered(data.rendered || '');
146 146 this.rendered = false;
147 147 this.render();
148 148 }
149 149 }
150 150 };
151 151
152 152
153 153 TextCell.prototype.toJSON = function () {
154 154 var data = {};
155 155 data.cell_type = this.cell_type;
156 156 data.source = this.get_source();
157 157 return data;
158 158 };
159 159
160 160
161 161 // HTMLCell
162 162
163 163 var HTMLCell = function (notebook) {
164 164 this.placeholder = "\u0000Type <strong>HTML</strong> and LaTeX: $\\alpha^2$";
165 165 IPython.TextCell.apply(this, arguments);
166 166 this.cell_type = 'html';
167 167 };
168 168
169 169
170 170 HTMLCell.prototype = new TextCell();
171 171
172 172
173 173 HTMLCell.prototype.render = function () {
174 174 if (this.rendered === false) {
175 175 var text = this.get_source();
176 176 if (text === "") { text = this.placeholder; }
177 177 this.set_rendered(text);
178 178 this.typeset();
179 179 this.element.find('div.text_cell_input').hide();
180 180 this.element.find("div.text_cell_render").show();
181 181 this.rendered = true;
182 182 }
183 183 };
184 184
185 185
186 186 // MarkdownCell
187 187
188 188 var MarkdownCell = function (notebook) {
189 189 this.placeholder = "\u0000Type *Markdown* and LaTeX: $\\alpha^2$";
190 190 IPython.TextCell.apply(this, arguments);
191 191 this.cell_type = 'markdown';
192 192 };
193 193
194 194
195 195 MarkdownCell.prototype = new TextCell();
196 196
197 197
198 198 MarkdownCell.prototype.render = function () {
199 199 if (this.rendered === false) {
200 200 var text = this.get_source();
201 201 if (text === "") { text = this.placeholder; }
202 202 var html = IPython.markdown_converter.makeHtml(text);
203 203 this.set_rendered(html);
204 204 this.typeset()
205 205 this.element.find('div.text_cell_input').hide();
206 206 this.element.find("div.text_cell_render").show();
207 207 var code_snippets = this.element.find("pre > code");
208 208 code_snippets.replaceWith(function () {
209 209 var code = $(this).html();
210 210 /* Substitute br for newlines and &nbsp; for spaces
211 211 before highlighting, since prettify doesn't
212 212 preserve those on all browsers */
213 213 code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
214 214 code = code.replace(/ /gm, '&nbsp;');
215 215 code = prettyPrintOne(code);
216 216
217 217 return '<code class="prettyprint">' + code + '</code>';
218 218 });
219 219 this.rendered = true;
220 220 }
221 221 };
222 222
223 223
224 224 // RSTCell
225 225
226 226 var RSTCell = function (notebook) {
227 227 this.placeholder = "\u0000Type *ReStructured Text* and LaTeX: $\\alpha^2$";
228 228 IPython.TextCell.apply(this, arguments);
229 229 this.cell_type = 'rst';
230 230 };
231 231
232 232
233 233 RSTCell.prototype = new TextCell();
234 234
235 235
236 236 RSTCell.prototype.render = function () {
237 237 if (this.rendered === false) {
238 238 var text = this.get_source();
239 239 if (text === "") { text = this.placeholder; }
240 240 var settings = {
241 241 processData : false,
242 242 cache : false,
243 243 type : "POST",
244 244 data : text,
245 245 headers : {'Content-Type': 'application/x-rst'},
246 246 success : $.proxy(this.handle_render,this)
247 247 };
248 248 $.ajax("/rstservice/render", settings);
249 249 this.element.find('div.text_cell_input').hide();
250 250 this.element.find("div.text_cell_render").show();
251 251 this.set_rendered("Rendering...");
252 252 }
253 253 };
254 254
255 255
256 256 RSTCell.prototype.handle_render = function (data, status, xhr) {
257 257 this.set_rendered(data);
258 258 this.typeset();
259 259 this.rendered = true;
260 260 };
261 261
262 262
263 263 IPython.TextCell = TextCell;
264 264 IPython.HTMLCell = HTMLCell;
265 265 IPython.MarkdownCell = MarkdownCell;
266 266 IPython.RSTCell = RSTCell;
267 267
268 268
269 269 return IPython;
270 270
271 271 }(IPython));
272 272
General Comments 0
You need to be logged in to leave comments. Login now