##// END OF EJS Templates
Lots of small notebook improvements....
Brian Granger -
Show More
@@ -238,10 +238,14 b' div.output_jpeg {'
238
238
239 div.text_cell {
239 div.text_cell {
240 background-color: white;
240 background-color: white;
241 padding: 5px 5px 5px 5px;
241 }
242 }
242
243
243 div.text_cell_input {
244 div.text_cell_input {
244 color: black;
245 color: black;
246 border: 1px solid #ddd;
247 border-radius: 3px;
248 background: #f7f7f7;
245 }
249 }
246
250
247 div.text_cell_render {
251 div.text_cell_render {
@@ -98,6 +98,29 b' var IPython = (function (IPython) {'
98 };
98 };
99
99
100
100
101 Cell.prototype.is_splittable = function () {
102 return true;
103 };
104
105
106 Cell.prototype.get_pre_cursor = function () {
107 var cursor = this.code_mirror.getCursor();
108 var text = this.code_mirror.getRange({line:0,ch:0}, cursor);
109 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
110 return text;
111 }
112
113
114 Cell.prototype.get_post_cursor = function () {
115 var cursor = this.code_mirror.getCursor();
116 var last_line_num = this.code_mirror.lineCount()-1;
117 var last_line_len = this.code_mirror.getLine(last_line_num).length;
118 var end = {line:last_line_num, ch:last_line_len}
119 var text = this.code_mirror.getRange(cursor, end);
120 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
121 return text;
122 };
123
101 Cell.prototype.grow = function(element) {
124 Cell.prototype.grow = function(element) {
102 // Grow the cell by hand. This is used upon reloading from JSON, when the
125 // Grow the cell by hand. This is used upon reloading from JSON, when the
103 // autogrow handler is not called.
126 // autogrow handler is not called.
@@ -360,7 +360,7 b' var IPython = (function (IPython) {'
360 Notebook.prototype.select = function (index) {
360 Notebook.prototype.select = function (index) {
361 if (index !== undefined && index >= 0 && index < this.ncells()) {
361 if (index !== undefined && index >= 0 && index < this.ncells()) {
362 sindex = this.get_selected_index()
362 sindex = this.get_selected_index()
363 if (sindex !== null) {
363 if (sindex !== null && index !== sindex) {
364 this.get_cell(sindex).unselect();
364 this.get_cell(sindex).unselect();
365 };
365 };
366 this.get_cell(index).select();
366 this.get_cell(index).select();
@@ -471,21 +471,18 b' var IPython = (function (IPython) {'
471 cell.set_input_prompt();
471 cell.set_input_prompt();
472 } else if (type === 'markdown') {
472 } else if (type === 'markdown') {
473 var cell = new IPython.MarkdownCell(this);
473 var cell = new IPython.MarkdownCell(this);
474 cell.config_mathjax();
475 } else if (type === 'html') {
474 } else if (type === 'html') {
476 var cell = new IPython.HTMLCell(this);
475 var cell = new IPython.HTMLCell(this);
477 cell.config_mathjax();
478 };
476 };
479 if (cell !== null) {
477 if (cell !== null) {
480 if (this.ncells() === 0) {
478 if (this.ncells() === 0) {
481 this.element.find('div.end_space').before(cell.element);
479 this.element.find('div.end_space').before(cell.element);
482 this.select(this.find_cell_index(cell));
483 this.dirty = true;
484 } else if (this.is_valid_cell_index(index)) {
480 } else if (this.is_valid_cell_index(index)) {
485 this.get_cell_element(index).after(cell.element);
481 this.get_cell_element(index).after(cell.element);
486 this.select(this.find_cell_index(cell));
487 this.dirty = true;
488 };
482 };
483 cell.render();
484 this.select(this.find_cell_index(cell));
485 this.dirty = true;
489 return cell;
486 return cell;
490 };
487 };
491 };
488 };
@@ -503,21 +500,18 b' var IPython = (function (IPython) {'
503 cell.set_input_prompt();
500 cell.set_input_prompt();
504 } else if (type === 'markdown') {
501 } else if (type === 'markdown') {
505 var cell = new IPython.MarkdownCell(this);
502 var cell = new IPython.MarkdownCell(this);
506 cell.config_mathjax();
507 } else if (type === 'html') {
503 } else if (type === 'html') {
508 var cell = new IPython.HTMLCell(this);
504 var cell = new IPython.HTMLCell(this);
509 cell.config_mathjax();
510 };
505 };
511 if (cell !== null) {
506 if (cell !== null) {
512 if (this.ncells() === 0) {
507 if (this.ncells() === 0) {
513 this.element.find('div.end_space').before(cell.element);
508 this.element.find('div.end_space').before(cell.element);
514 this.select(this.find_cell_index(cell));
515 this.dirty = true;
516 } else if (this.is_valid_cell_index(index)) {
509 } else if (this.is_valid_cell_index(index)) {
517 this.get_cell_element(index).before(cell.element);
510 this.get_cell_element(index).before(cell.element);
518 this.select(this.find_cell_index(cell));
519 this.dirty = true;
520 };
511 };
512 cell.render();
513 this.select(this.find_cell_index(cell));
514 this.dirty = true;
521 return cell;
515 return cell;
522 };
516 };
523 };
517 };
@@ -537,7 +531,6 b' var IPython = (function (IPython) {'
537 }
531 }
538 target_cell.set_text(text);
532 target_cell.set_text(text);
539 source_element.remove();
533 source_element.remove();
540 target_cell.select();
541 };
534 };
542 this.dirty = true;
535 this.dirty = true;
543 };
536 };
@@ -554,15 +547,14 b' var IPython = (function (IPython) {'
554 target_cell = this.insert_cell_below('markdown',i);
547 target_cell = this.insert_cell_below('markdown',i);
555 var text = source_cell.get_text();
548 var text = source_cell.get_text();
556 if (text === source_cell.placeholder) {
549 if (text === source_cell.placeholder) {
557 text = target_cell.placeholder;
550 text = '';
558 };
551 };
559 if (target_cell !== null) {
552 if (target_cell !== null) {
560 if (text === "") {text = target_cell.placeholder;};
553 //if (text === "") {text = target_cell.placeholder;};
561 // The edit must come before the set_text.
554 // The edit must come before the set_text.
562 target_cell.edit();
555 target_cell.edit();
563 target_cell.set_text(text);
556 target_cell.set_text(text);
564 source_element.remove();
557 source_element.remove();
565 target_cell.select();
566 }
558 }
567 this.dirty = true;
559 this.dirty = true;
568 };
560 };
@@ -580,7 +572,7 b' var IPython = (function (IPython) {'
580 target_cell = this.insert_cell_below('html',i);
572 target_cell = this.insert_cell_below('html',i);
581 var text = source_cell.get_text();
573 var text = source_cell.get_text();
582 if (text === source_cell.placeholder) {
574 if (text === source_cell.placeholder) {
583 text = target_cell.placeholder;
575 text = '';
584 };
576 };
585 if (target_cell !== null) {
577 if (target_cell !== null) {
586 if (text === "") {text = target_cell.placeholder;};
578 if (text === "") {text = target_cell.placeholder;};
@@ -588,7 +580,6 b' var IPython = (function (IPython) {'
588 target_cell.edit();
580 target_cell.edit();
589 target_cell.set_text(text);
581 target_cell.set_text(text);
590 source_element.remove();
582 source_element.remove();
591 target_cell.select();
592 }
583 }
593 this.dirty = true;
584 this.dirty = true;
594 };
585 };
@@ -669,52 +660,68 b' var IPython = (function (IPython) {'
669 Notebook.prototype.split_cell = function () {
660 Notebook.prototype.split_cell = function () {
670 // Todo: implement spliting for other cell types.
661 // Todo: implement spliting for other cell types.
671 var cell = this.get_selected_cell();
662 var cell = this.get_selected_cell();
672 if (cell instanceof IPython.CodeCell) {
663 if (cell.is_splittable()) {
673 var cursor = cell.code_mirror.getCursor();
664 texta = cell.get_pre_cursor();
674 var last_line_num = cell.code_mirror.lineCount()-1;
665 textb = cell.get_post_cursor();
675 var last_line_len = cell.code_mirror.getLine(last_line_num).length;
666 if (cell instanceof IPython.CodeCell) {
676 var end = {line:last_line_num, ch:last_line_len}
667 cell.set_text(texta);
677 var texta = cell.code_mirror.getRange({line:0,ch:0}, cursor);
668 var new_cell = this.insert_cell_below('code');
678 var textb = cell.code_mirror.getRange(cursor, end);
669 new_cell.set_text(textb);
679 texta = texta.replace(/^\n+/, '').replace(/\n+$/, '');
670 } else if (cell instanceof IPython.MarkdownCell) {
680 textb = textb.replace(/^\n+/, '').replace(/\n+$/, '');
671 cell.set_text(texta);
681 cell.set_text(texta);
672 cell.render();
682 var new_cell = this.insert_cell_below('code');
673 var new_cell = this.insert_cell_below('markdown');
683 new_cell.set_text(textb);
674 new_cell.edit(); // editor must be visible to call set_text
675 new_cell.set_text(textb);
676 new_cell.render();
677 } else if (cell instanceof IPython.HTMLCell) {
678 cell.set_text(texta);
679 cell.render();
680 var new_cell = this.insert_cell_below('html');
681 new_cell.edit(); // editor must be visible to call set_text
682 new_cell.set_text(textb);
683 new_cell.render();
684 };
684 };
685 };
685 };
686 };
686
687
687
688
688 Notebook.prototype.merge_cell_above = function () {
689 Notebook.prototype.merge_cell_above = function () {
689 // Todo: implement merging for other cell types.
690 var cell = this.get_selected_cell();
691 var index = this.get_selected_index();
690 var index = this.get_selected_index();
691 var cell = this.get_cell(index);
692 if (index > 0) {
692 if (index > 0) {
693 upper_cell = this.get_cell(index-1);
693 upper_cell = this.get_cell(index-1);
694 lower_cell = this.get_cell(index);
694 upper_text = upper_cell.get_text();
695 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
695 text = cell.get_text();
696 upper_text = upper_cell.get_text();
696 if (cell instanceof IPython.CodeCell) {
697 lower_text = lower_cell.get_text();
697 cell.set_text(upper_text+'\n'+text);
698 lower_cell.set_text(upper_text+'\n'+lower_text);
698 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
699 this.delete_cell(index-1);
699 cell.edit();
700 cell.set_text(upper_text+'\n'+text);
701 cell.render();
700 };
702 };
703 this.delete_cell(index-1);
704 this.select(this.find_cell_index(cell));
701 };
705 };
702 };
706 };
703
707
704
708
705 Notebook.prototype.merge_cell_below = function () {
709 Notebook.prototype.merge_cell_below = function () {
706 // Todo: implement merging for other cell types.
707 var cell = this.get_selected_cell();
708 var index = this.get_selected_index();
710 var index = this.get_selected_index();
711 var cell = this.get_cell(index);
709 if (index < this.ncells()-1) {
712 if (index < this.ncells()-1) {
710 upper_cell = this.get_cell(index);
711 lower_cell = this.get_cell(index+1);
713 lower_cell = this.get_cell(index+1);
712 if (upper_cell instanceof IPython.CodeCell && lower_cell instanceof IPython.CodeCell) {
714 lower_text = lower_cell.get_text();
713 upper_text = upper_cell.get_text();
715 text = cell.get_text();
714 lower_text = lower_cell.get_text();
716 if (cell instanceof IPython.CodeCell) {
715 upper_cell.set_text(upper_text+'\n'+lower_text);
717 cell.set_text(text+'\n'+lower_text);
716 this.delete_cell(index+1);
718 } else if (cell instanceof IPython.MarkdownCell || cell instanceof IPython.HTMLCell) {
719 cell.edit();
720 cell.set_text(text+'\n'+lower_text);
721 cell.render();
717 };
722 };
723 this.delete_cell(index+1);
724 this.select(this.find_cell_index(cell));
718 };
725 };
719 };
726 };
720
727
@@ -27,7 +27,7 b' var IPython = (function (IPython) {'
27 TextCell.prototype.create_element = function () {
27 TextCell.prototype.create_element = function () {
28 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
28 var cell = $("<div>").addClass('cell text_cell border-box-sizing');
29 cell.attr('tabindex','2');
29 cell.attr('tabindex','2');
30 var input_area = $('<div/>').addClass('text_cell_input');
30 var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
31 this.code_mirror = CodeMirror(input_area.get(0), {
31 this.code_mirror = CodeMirror(input_area.get(0), {
32 indentUnit : 4,
32 indentUnit : 4,
33 mode: this.code_mirror_mode,
33 mode: this.code_mirror_mode,
@@ -37,7 +37,7 b' var IPython = (function (IPython) {'
37 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
37 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
38 });
38 });
39 // The tabindex=-1 makes this div focusable.
39 // The tabindex=-1 makes this div focusable.
40 var render_area = $('<div/>').addClass('text_cell_render').
40 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
41 addClass('rendered_html').attr('tabindex','-1');
41 addClass('rendered_html').attr('tabindex','-1');
42 cell.append(input_area).append(render_area);
42 cell.append(input_area).append(render_area);
43 this.element = cell;
43 this.element = cell;
@@ -52,8 +52,11 b' var IPython = (function (IPython) {'
52 if (that.rendered) {
52 if (that.rendered) {
53 that.edit();
53 that.edit();
54 return false;
54 return false;
55 }
55 };
56 }
56 };
57 });
58 this.element.dblclick(function () {
59 that.edit();
57 });
60 });
58 };
61 };
59
62
@@ -87,19 +90,19 b' var IPython = (function (IPython) {'
87
90
88
91
89 TextCell.prototype.edit = function () {
92 TextCell.prototype.edit = function () {
90 var that = this;
91 if ( this.read_only ) return;
93 if ( this.read_only ) return;
92 if (this.rendered === true) {
94 if (this.rendered === true) {
93 var text_cell = this.element;
95 var text_cell = this.element;
94 var output = text_cell.find("div.text_cell_render");
96 var output = text_cell.find("div.text_cell_render");
95 output.hide();
97 output.hide();
96 text_cell.find('div.text_cell_input').show();
98 text_cell.find('div.text_cell_input').show();
97 that.code_mirror.refresh();
99 this.code_mirror.refresh();
98 this.code_mirror.focus();
100 this.code_mirror.focus();
99 that.code_mirror.refresh();
101 this.code_mirror.refresh();
100 this.rendered = false;
102 this.rendered = false;
101 if (this.get_text() === this.placeholder) {
103 if (this.get_text() === this.placeholder) {
102 this.set_text('');
104 this.set_text('');
105 this.refresh();
103 }
106 }
104 }
107 }
105 };
108 };
@@ -109,16 +112,6 b' var IPython = (function (IPython) {'
109 TextCell.prototype.render = function () {};
112 TextCell.prototype.render = function () {};
110
113
111
114
112 TextCell.prototype.config_mathjax = function () {
113 var text_cell = this.element;
114 var that = this;
115 text_cell.dblclick(function () {
116 that.edit();
117 });
118 that.render();
119 };
120
121
122 TextCell.prototype.get_text = function() {
115 TextCell.prototype.get_text = function() {
123 return this.code_mirror.getValue();
116 return this.code_mirror.getValue();
124 };
117 };
General Comments 0
You need to be logged in to leave comments. Login now