Skip to content

Commit

Permalink
Merge pull request #5352 from ErwinRussel/refactoring
Browse files Browse the repository at this point in the history
Code refactoring notebook.js
  • Loading branch information
Zsailer authored May 18, 2020
2 parents 3ec00ad + bd507b2 commit ae7747f
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,28 @@ define([
var len = this.ncells();
return this.insert_cell_below(type,len-1);
};

/**
* Transfer contents from one cell to a new type cell
*/
Notebook.prototype.transfer_to_new_cell = function (source_cell, target_cell){
var text = source_cell.get_text();

if (text === source_cell.placeholder) {
text = '';
}
// metadata
target_cell.metadata = source_cell.metadata;
target_cell.attachments = source_cell.attachments;

// We must show the editor before setting its contents
target_cell.unrender();
target_cell.set_text(text);
// make this value the starting point, so that we can only undo
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_cell.element.remove();
}

/**
* Turn one or more cells into code.
Expand Down Expand Up @@ -1497,23 +1519,10 @@ define([

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

if (text === source_cell.placeholder) {
text = '';
}
// metadata
target_cell.metadata = source_cell.metadata;
target_cell.attachments = source_cell.attachments;

// We must show the editor before setting its contents
target_cell.unrender();
target_cell.set_text(text);
// make this value the starting point, so that we can only undo
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_cell.element.remove();
this.transfer_to_new_cell(source_cell, target_cell);
this.select(i);

if ((source_cell instanceof textcell.TextCell) && source_cell.rendered) {
target_cell.render();
}
Expand Down Expand Up @@ -1552,24 +1561,10 @@ define([

if (!(source_cell instanceof textcell.RawCell) && source_cell.is_editable()) {
target_cell = this.insert_cell_below('raw',i);
var text = source_cell.get_text();
if (text === source_cell.placeholder) {
text = '';
}
//metadata
target_cell.metadata = source_cell.metadata;
// attachments (we transfer them so they aren't lost if the
// cell is turned back into markdown)
target_cell.attachments = source_cell.attachments;

// We must show the editor before setting its contents
target_cell.unrender();
target_cell.set_text(text);
// make this value the starting point, so that we can only undo
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_cell.element.remove();

this.transfer_to_new_cell(source_cell, target_cell);
this.select(i);

var cursor = source_cell.code_mirror.getCursor();
target_cell.code_mirror.setCursor(cursor);
this.set_dirty(true);
Expand Down

0 comments on commit ae7747f

Please sign in to comment.