From e66ff8f0018481f964ff72ec8610e1fc17935a35 Mon Sep 17 00:00:00 2001 From: Julien Rebetez Date: Tue, 27 Oct 2015 11:09:54 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20drag/drop=20event=20handler=20to=20be=20c?= =?UTF-8?q?ompatible=20with=20codemirror=205.8,=20which=20doesn=E2=80=99t?= =?UTF-8?q?=20allow=20binary=20file=20uploads=20by=20default.=20So=20we=20?= =?UTF-8?q?have=20to=20intercept=20the=20dragstart=20event.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notebook/static/notebook/js/textcell.js | 89 +++++++++++++------------ 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/notebook/static/notebook/js/textcell.js b/notebook/static/notebook/js/textcell.js index 516c8d4ae..edab2b50c 100644 --- a/notebook/static/notebook/js/textcell.js +++ b/notebook/static/notebook/js/textcell.js @@ -331,47 +331,6 @@ define([ reader.readAsDataURL(blob); }; - /** @method bind_events **/ - MarkdownCell.prototype.bind_events = function () { - TextCell.prototype.bind_events.apply(this); - var that = this; - - var attachment_regex = /^image\/.*$/; - - // Event handlers to allow users to insert image using either - // drag'n'drop or copy/paste - var div = that.code_mirror.getWrapperElement(); - $(div).on('paste', function(evt) { - var data = evt.originalEvent.clipboardData; - var items = data.items; - for (var i = 0; i < items.length; ++i) { - var item = items[i]; - if (item.kind == 'file' && attachment_regex.test(item.type)) { - // TODO(julienr): This does not stop code_mirror from pasting - // the filename. - evt.stopPropagation(); - evt.preventDefault(); - that.insert_inline_image_from_blob(item.getAsFile()); - } - } - }); - - this.code_mirror.on("drop", function(cm, evt) { - var files = evt.dataTransfer.files; - for (var i = 0; i < files.length; ++i) { - var file = files[i]; - if (attachment_regex.test(file.type)) { - // Prevent the default code_mirror 'drop' event handler - // (which inserts the file content) if this is a - // recognized media file - evt.stopPropagation(); - evt.preventDefault(); - that.insert_inline_image_from_blob(file); - } - } - }); - }; - /** * @method render */ @@ -438,8 +397,56 @@ define([ that.focus_editor(); } }); + + var attachment_regex = /^image\/.*$/; + + // Event handlers to allow users to insert image using either + // drag'n'drop or copy/paste + var div = that.code_mirror.getWrapperElement(); + $(div).on('paste', function(evt) { + var data = evt.originalEvent.clipboardData; + var items = data.items; + for (var i = 0; i < items.length; ++i) { + var item = items[i]; + if (item.kind == 'file' && attachment_regex.test(item.type)) { + // TODO(julienr): This does not stop code_mirror from pasting + // the filename. + evt.stopPropagation(); + evt.preventDefault(); + that.insert_inline_image_from_blob(item.getAsFile()); + } + } + }); + + // Allow drop event if the dragged file can be used as an attachment + this.code_mirror.on("dragstart", function(cm, evt) { + var files = evt.dataTransfer.files; + for (var i = 0; i < files.length; ++i) { + var file = files[i]; + if (attachment_regex.test(file.type)) { + return false; + } + } + return true; + }); + + this.code_mirror.on("drop", function(cm, evt) { + var files = evt.dataTransfer.files; + for (var i = 0; i < files.length; ++i) { + var file = files[i]; + if (attachment_regex.test(file.type)) { + // Prevent the default code_mirror 'drop' event handler + // (which inserts the file content) if this is a + // recognized media file + evt.stopPropagation(); + evt.preventDefault(); + that.insert_inline_image_from_blob(file); + } + } + }); }; + var RawCell = function (options) { /** * Constructor