From 865de472336c3624e4faf502216367a4991df0c3 Mon Sep 17 00:00:00 2001 From: Julien Rebetez Date: Fri, 30 Oct 2015 10:52:36 +0100 Subject: [PATCH] Add Cut/Copy/Paste Cell attachments menu items --- notebook/static/notebook/js/actions.js | 21 +++++++++ notebook/static/notebook/js/notebook.js | 63 ++++++++++++++++++++++++- notebook/templates/notebook.html | 4 ++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/notebook/static/notebook/js/actions.js b/notebook/static/notebook/js/actions.js index 682c8d0da..83df33bee 100644 --- a/notebook/static/notebook/js/actions.js +++ b/notebook/static/notebook/js/actions.js @@ -160,6 +160,27 @@ define(function(require){ env.notebook.insert_image(); } }, + 'cut-cell-attachments': { + help : 'cut cell attachments', + help_index : 'dza', + handler: function (env) { + env.notebook.cut_cell_attachments(); + } + }, + 'copy-cell-attachments': { + help : 'copy cell attachments', + help_index: 'dzb', + handler: function (env) { + env.notebook.copy_cell_attachments(); + } + }, + 'paste-cell-attachments': { + help : 'paste cell attachments', + help_index: 'dzc', + handler: function (env) { + env.notebook.paste_cell_attachments(); + } + }, 'split-cell-at-cursor': { help : 'split cell', help_index : 'ea', diff --git a/notebook/static/notebook/js/notebook.js b/notebook/static/notebook/js/notebook.js index 1c0d80d4a..28cc30c3c 100644 --- a/notebook/static/notebook/js/notebook.js +++ b/notebook/static/notebook/js/notebook.js @@ -128,6 +128,7 @@ define(function (require) { this.clipboard = null; this.undelete_backup_stack = []; this.paste_enabled = false; + this.paste_attachments_enabled = false; this.writable = false; // It is important to start out in command mode to match the intial mode // of the KeyboardManager. @@ -1673,7 +1674,7 @@ define(function (require) { this.merge_cells([index, index+1], false); }; - // Image insertion + // Attachments handling /** * Shows a dialog letting the user pick an image from her computer and @@ -1703,6 +1704,66 @@ define(function (require) { }); }; + /** + * Cut the attachments of a cell + */ + Notebook.prototype.cut_cell_attachments = function() { + var cell = this.get_selected_cell(); + this.clipboard_attachments = cell.attachments; + this.enable_attachments_paste(); + cell.attachments = {}; + cell.unrender(); + cell.render(); + }; + + /** + * Copy the attachments of a cell + */ + Notebook.prototype.copy_cell_attachments = function() { + var cell = this.get_selected_cell(); + // Do a deep copy of attachments to avoid subsequent modification + // to the cell to modify the clipboard + this.clipboard_attachments = $.extend(true, {}, cell.attachments); + this.enable_attachments_paste(); + cell.unrender(); + cell.render(); + }; + + /** + * Paste the attachments in the clipboard into the currently selected + * cell + */ + Notebook.prototype.paste_cell_attachments = function() { + if (this.clipboard_attachments !== null && + this.paste_attachments_enabled) { + var cell = this.get_selected_cell(); + $.extend(cell.attachments, this.clipboard_attachments); + cell.unrender(); + cell.render(); + } + }; + + /** + * Disable the "Paste Cell Attachments" menu item + */ + Notebook.prototype.disable_attachments_paste = function () { + if (this.paste_attachments_enabled) { + $('#paste_cell_attachments').addClass('disabled'); + this.paste_attachments_enabled = false; + } + }; + + /** + * Enable the "Paste Cell Attachments" menu item + */ + Notebook.prototype.enable_attachments_paste = function () { + var that = this; + if (!this.paste_attachments_enabled) { + $('#paste_cell_attachments').removeClass('disabled'); + this.paste_attachments_enabled = true; + } + }; + /** * Enable/disable the "Insert image" menu item */ diff --git a/notebook/templates/notebook.html b/notebook/templates/notebook.html index efa614b2c..c14d38ab1 100644 --- a/notebook/templates/notebook.html +++ b/notebook/templates/notebook.html @@ -144,6 +144,10 @@ data-notebook-path="{{notebook_path | urlencode}}"
  • Find and Replace
  • +
  • Cut Cell Attachments
  • +
  • Copy Cell Attachments
  • +
  • Paste Cell Attachments
  • +
  • Insert Image