Merge pull request #6472 from parleur/master

Keep metadata through celltype changes
This commit is contained in:
Matthias Bussonnier 2014-09-25 09:16:59 +02:00
commit 4609465161

View File

@ -999,19 +999,21 @@ define([
Notebook.prototype.to_code = function (index) { Notebook.prototype.to_code = function (index) {
var i = this.index_or_selected(index); var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) { if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i); var source_cell = this.get_cell(i);
var source_cell = source_element.data("cell");
if (!(source_cell instanceof codecell.CodeCell)) { if (!(source_cell instanceof codecell.CodeCell)) {
var target_cell = this.insert_cell_below('code',i); var target_cell = this.insert_cell_below('code',i);
var text = source_cell.get_text(); var text = source_cell.get_text();
if (text === source_cell.placeholder) { if (text === source_cell.placeholder) {
text = ''; text = '';
} }
//metadata
target_cell.metadata = source_cell.metadata;
target_cell.set_text(text); target_cell.set_text(text);
// make this value the starting point, so that we can only undo // make this value the starting point, so that we can only undo
// to this state, instead of a blank cell // to this state, instead of a blank cell
target_cell.code_mirror.clearHistory(); target_cell.code_mirror.clearHistory();
source_element.remove(); source_cell.element.remove();
this.select(i); this.select(i);
var cursor = source_cell.code_mirror.getCursor(); var cursor = source_cell.code_mirror.getCursor();
target_cell.code_mirror.setCursor(cursor); target_cell.code_mirror.setCursor(cursor);
@ -1029,21 +1031,24 @@ define([
Notebook.prototype.to_markdown = function (index) { Notebook.prototype.to_markdown = function (index) {
var i = this.index_or_selected(index); var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) { if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i); var source_cell = this.get_cell(i);
var source_cell = source_element.data("cell");
if (!(source_cell instanceof textcell.MarkdownCell)) { if (!(source_cell instanceof textcell.MarkdownCell)) {
var target_cell = this.insert_cell_below('markdown',i); var target_cell = this.insert_cell_below('markdown',i);
var text = source_cell.get_text(); var text = source_cell.get_text();
if (text === source_cell.placeholder) { if (text === source_cell.placeholder) {
text = ''; text = '';
} }
// metadata
target_cell.metadata = source_cell.metadata
// We must show the editor before setting its contents // We must show the editor before setting its contents
target_cell.unrender(); target_cell.unrender();
target_cell.set_text(text); target_cell.set_text(text);
// make this value the starting point, so that we can only undo // make this value the starting point, so that we can only undo
// to this state, instead of a blank cell // to this state, instead of a blank cell
target_cell.code_mirror.clearHistory(); target_cell.code_mirror.clearHistory();
source_element.remove(); source_cell.element.remove();
this.select(i); this.select(i);
if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) { if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) {
target_cell.render(); target_cell.render();
@ -1064,22 +1069,24 @@ define([
Notebook.prototype.to_raw = function (index) { Notebook.prototype.to_raw = function (index) {
var i = this.index_or_selected(index); var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) { if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i);
var source_cell = source_element.data("cell");
var target_cell = null; var target_cell = null;
var source_cell = this.get_cell(i);
if (!(source_cell instanceof textcell.RawCell)) { if (!(source_cell instanceof textcell.RawCell)) {
target_cell = this.insert_cell_below('raw',i); target_cell = this.insert_cell_below('raw',i);
var text = source_cell.get_text(); var text = source_cell.get_text();
if (text === source_cell.placeholder) { if (text === source_cell.placeholder) {
text = ''; text = '';
} }
//metadata
target_cell.metadata = source_cell.metadata;
// We must show the editor before setting its contents // We must show the editor before setting its contents
target_cell.unrender(); target_cell.unrender();
target_cell.set_text(text); target_cell.set_text(text);
// make this value the starting point, so that we can only undo // make this value the starting point, so that we can only undo
// to this state, instead of a blank cell // to this state, instead of a blank cell
target_cell.code_mirror.clearHistory(); target_cell.code_mirror.clearHistory();
source_element.remove(); source_cell.element.remove();
this.select(i); this.select(i);
var cursor = source_cell.code_mirror.getCursor(); var cursor = source_cell.code_mirror.getCursor();
target_cell.code_mirror.setCursor(cursor); target_cell.code_mirror.setCursor(cursor);
@ -1099,8 +1106,7 @@ define([
level = level || 1; level = level || 1;
var i = this.index_or_selected(index); var i = this.index_or_selected(index);
if (this.is_valid_cell_index(i)) { if (this.is_valid_cell_index(i)) {
var source_element = this.get_cell_element(i); var source_cell = this.get_cell(i);
var source_cell = source_element.data("cell");
var target_cell = null; var target_cell = null;
if (source_cell instanceof textcell.HeadingCell) { if (source_cell instanceof textcell.HeadingCell) {
source_cell.set_level(level); source_cell.set_level(level);
@ -1110,6 +1116,8 @@ define([
if (text === source_cell.placeholder) { if (text === source_cell.placeholder) {
text = ''; text = '';
} }
//metadata
target_cell.metadata = source_cell.metadata;
// We must show the editor before setting its contents // We must show the editor before setting its contents
target_cell.set_level(level); target_cell.set_level(level);
target_cell.unrender(); target_cell.unrender();
@ -1117,7 +1125,7 @@ define([
// make this value the starting point, so that we can only undo // make this value the starting point, so that we can only undo
// to this state, instead of a blank cell // to this state, instead of a blank cell
target_cell.code_mirror.clearHistory(); target_cell.code_mirror.clearHistory();
source_element.remove(); source_cell.element.remove();
this.select(i); this.select(i);
var cursor = source_cell.code_mirror.getCursor(); var cursor = source_cell.code_mirror.getCursor();
target_cell.code_mirror.setCursor(cursor); target_cell.code_mirror.setCursor(cursor);