From 217c96eb59732c86a3004f7eb30535ba81a50605 Mon Sep 17 00:00:00 2001 From: Erwin Date: Wed, 8 Apr 2020 11:43:24 +0200 Subject: [PATCH 1/3] fix test berendjan --- notebook/static/notebook/js/outputarea.js | 43 ++++++----------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/notebook/static/notebook/js/outputarea.js b/notebook/static/notebook/js/outputarea.js index da72caa5ea..5fd02c26b8 100644 --- a/notebook/static/notebook/js/outputarea.js +++ b/notebook/static/notebook/js/outputarea.js @@ -838,56 +838,33 @@ define([ } }; - var append_png = function (png, md, element, handle_inserted) { - var type = MIME_PNG; - var toinsert = this.create_output_subarea(md, "output_png", type); + OutputArea.prototype.append_img = function (src_type, md, element, handle_inserted, MIME, type_string) { + var type = MIME; + var toinsert = this.create_output_subarea(md, 'output_' + type_string, type); var img = $(""); if (handle_inserted !== undefined) { img.on('load', function(){ handle_inserted(img); }); } - img[0].src = 'data:image/png;base64,'+ png; + img[0].src = 'data:image/' + type_string + ';base64,'+ src_type; set_width_height(img, md, type); dblclick_to_reset_size(img); toinsert.append(img); element.append(toinsert); return toinsert; }; - + + var append_png = function (png, md, element, handle_inserted) { + return this.append_img(png, md, element, handle_inserted, MIME_PNG, 'png'); + }; var append_jpeg = function (jpeg, md, element, handle_inserted) { - var type = MIME_JPEG; - var toinsert = this.create_output_subarea(md, "output_jpeg", type); - var img = $(""); - if (handle_inserted !== undefined) { - img.on('load', function(){ - handle_inserted(img); - }); - } - img[0].src = 'data:image/jpeg;base64,'+ jpeg; - set_width_height(img, md, type); - dblclick_to_reset_size(img); - toinsert.append(img); - element.append(toinsert); - return toinsert; + return this.append_img(jpeg, md, element, handle_inserted, MIME_JPEG, 'jpeg'); }; var append_gif = function (gif, md, element, handle_inserted) { - var type = MIME_GIF; - var toinsert = this.create_output_subarea(md, "output_gif", type); - var img = $(""); - if (handle_inserted !== undefined) { - img.on('load', function(){ - handle_inserted(img); - }); - } - img[0].src = 'data:image/gif;base64,'+ gif; - set_width_height(img, md, type); - dblclick_to_reset_size(img); - toinsert.append(img); - element.append(toinsert); - return toinsert; + return this.append_img(gif, md, element, handle_inserted, MIME_GIF, 'gif'); }; From d26d1e385f23a7a4e8a164014a86b715b50344fb Mon Sep 17 00:00:00 2001 From: Erwin Date: Wed, 8 Apr 2020 11:53:54 +0200 Subject: [PATCH 2/3] revert changes --- notebook/static/notebook/js/outputarea.js | 43 +++++++++++++++++------ 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/notebook/static/notebook/js/outputarea.js b/notebook/static/notebook/js/outputarea.js index 5fd02c26b8..da72caa5ea 100644 --- a/notebook/static/notebook/js/outputarea.js +++ b/notebook/static/notebook/js/outputarea.js @@ -838,33 +838,56 @@ define([ } }; - OutputArea.prototype.append_img = function (src_type, md, element, handle_inserted, MIME, type_string) { - var type = MIME; - var toinsert = this.create_output_subarea(md, 'output_' + type_string, type); + var append_png = function (png, md, element, handle_inserted) { + var type = MIME_PNG; + var toinsert = this.create_output_subarea(md, "output_png", type); var img = $(""); if (handle_inserted !== undefined) { img.on('load', function(){ handle_inserted(img); }); } - img[0].src = 'data:image/' + type_string + ';base64,'+ src_type; + img[0].src = 'data:image/png;base64,'+ png; set_width_height(img, md, type); dblclick_to_reset_size(img); toinsert.append(img); element.append(toinsert); return toinsert; }; - - var append_png = function (png, md, element, handle_inserted) { - return this.append_img(png, md, element, handle_inserted, MIME_PNG, 'png'); - }; + var append_jpeg = function (jpeg, md, element, handle_inserted) { - return this.append_img(jpeg, md, element, handle_inserted, MIME_JPEG, 'jpeg'); + var type = MIME_JPEG; + var toinsert = this.create_output_subarea(md, "output_jpeg", type); + var img = $(""); + if (handle_inserted !== undefined) { + img.on('load', function(){ + handle_inserted(img); + }); + } + img[0].src = 'data:image/jpeg;base64,'+ jpeg; + set_width_height(img, md, type); + dblclick_to_reset_size(img); + toinsert.append(img); + element.append(toinsert); + return toinsert; }; var append_gif = function (gif, md, element, handle_inserted) { - return this.append_img(gif, md, element, handle_inserted, MIME_GIF, 'gif'); + var type = MIME_GIF; + var toinsert = this.create_output_subarea(md, "output_gif", type); + var img = $(""); + if (handle_inserted !== undefined) { + img.on('load', function(){ + handle_inserted(img); + }); + } + img[0].src = 'data:image/gif;base64,'+ gif; + set_width_height(img, md, type); + dblclick_to_reset_size(img); + toinsert.append(img); + element.append(toinsert); + return toinsert; }; From bd507b27a677f9bd8634974fe5f0c37d57681e43 Mon Sep 17 00:00:00 2001 From: Erwin Date: Wed, 8 Apr 2020 12:43:55 +0200 Subject: [PATCH 3/3] fix code duplication for cell conversion --- notebook/static/notebook/js/notebook.js | 59 +++++++++++-------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index d2f4ba0b93..ee899205e2 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -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. @@ -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(); } @@ -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);