Skip to content

Commit bd507b2

Browse files
committed
fix code duplication for cell conversion
1 parent d26d1e3 commit bd507b2

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

notebook/static/notebook/js/notebook.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,28 @@ define([
14201420
var len = this.ncells();
14211421
return this.insert_cell_below(type,len-1);
14221422
};
1423+
1424+
/**
1425+
* Transfer contents from one cell to a new type cell
1426+
*/
1427+
Notebook.prototype.transfer_to_new_cell = function (source_cell, target_cell){
1428+
var text = source_cell.get_text();
1429+
1430+
if (text === source_cell.placeholder) {
1431+
text = '';
1432+
}
1433+
// metadata
1434+
target_cell.metadata = source_cell.metadata;
1435+
target_cell.attachments = source_cell.attachments;
1436+
1437+
// We must show the editor before setting its contents
1438+
target_cell.unrender();
1439+
target_cell.set_text(text);
1440+
// make this value the starting point, so that we can only undo
1441+
// to this state, instead of a blank cell
1442+
target_cell.code_mirror.clearHistory();
1443+
source_cell.element.remove();
1444+
}
14231445

14241446
/**
14251447
* Turn one or more cells into code.
@@ -1497,23 +1519,10 @@ define([
14971519

14981520
if (!(source_cell instanceof textcell.MarkdownCell) && source_cell.is_editable()) {
14991521
var target_cell = this.insert_cell_below('markdown',i);
1500-
var text = source_cell.get_text();
15011522

1502-
if (text === source_cell.placeholder) {
1503-
text = '';
1504-
}
1505-
// metadata
1506-
target_cell.metadata = source_cell.metadata;
1507-
target_cell.attachments = source_cell.attachments;
1508-
1509-
// We must show the editor before setting its contents
1510-
target_cell.unrender();
1511-
target_cell.set_text(text);
1512-
// make this value the starting point, so that we can only undo
1513-
// to this state, instead of a blank cell
1514-
target_cell.code_mirror.clearHistory();
1515-
source_cell.element.remove();
1523+
this.transfer_to_new_cell(source_cell, target_cell);
15161524
this.select(i);
1525+
15171526
if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) {
15181527
target_cell.render();
15191528
}
@@ -1552,24 +1561,10 @@ define([
15521561

15531562
if (!(source_cell instanceof textcell.RawCell) && source_cell.is_editable()) {
15541563
target_cell = this.insert_cell_below('raw',i);
1555-
var text = source_cell.get_text();
1556-
if (text === source_cell.placeholder) {
1557-
text = '';
1558-
}
1559-
//metadata
1560-
target_cell.metadata = source_cell.metadata;
1561-
// attachments (we transfer them so they aren't lost if the
1562-
// cell is turned back into markdown)
1563-
target_cell.attachments = source_cell.attachments;
1564-
1565-
// We must show the editor before setting its contents
1566-
target_cell.unrender();
1567-
target_cell.set_text(text);
1568-
// make this value the starting point, so that we can only undo
1569-
// to this state, instead of a blank cell
1570-
target_cell.code_mirror.clearHistory();
1571-
source_cell.element.remove();
1564+
1565+
this.transfer_to_new_cell(source_cell, target_cell);
15721566
this.select(i);
1567+
15731568
var cursor = source_cell.code_mirror.getCursor();
15741569
target_cell.code_mirror.setCursor(cursor);
15751570
this.set_dirty(true);

0 commit comments

Comments
 (0)