From 9cf6aa10799b65c6cc6a68d6efbb37b0a6f08733 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Mon, 2 Jan 2012 06:29:30 +0100 Subject: [PATCH] Manage file drag and drop into the window. It will open the QR if it's closed. --- 4chan_x.user.js | 14 +++++++++++++- script.coffee | 23 ++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 8e9fa3bba..38ba6339e 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1260,6 +1260,16 @@ ta.focus(); return ta.selectionEnd = ta.selectionStart = caretPos + text.length; }, + fileDrop: function(e) { + if (!e.dataTransfer.files.length) return; + e.preventDefault(); + e.stopPropagation(); + e.dataTransfer.dropEffect = 'copy'; + if (e.type === 'drop') { + qr.open(); + return qr.fileInput.call(e.dataTransfer); + } + }, fileInput: function() { var file, _i, _len, _ref; qr.cleanError(); @@ -1321,6 +1331,9 @@ return qr.el.classList.toggle('dump'); }); $.on($('form', qr.el), 'submit', qr.submit); + $.on($('[type=file]', qr.el), 'change', qr.fileInput); + $.on(d, 'dragover', qr.fileDrop); + $.on(d, 'drop', qr.fileDrop); qr.inputs = { name: $('[name=name]', qr.el), email: $('[name=email]', qr.el) @@ -1337,7 +1350,6 @@ return qr.inputs[match[1]].value = JSON.parse(e.newValue); } }); - $.on($('[type=file]', qr.el), 'change', qr.fileInput); return $.add(d.body, qr.el); }, submit: function(e) { diff --git a/script.coffee b/script.coffee index 1bdfd92e3..3b9965215 100644 --- a/script.coffee +++ b/script.coffee @@ -926,6 +926,14 @@ qr = #move the caret to the end of the new quote ta.selectionEnd = ta.selectionStart = caretPos + text.length + fileDrop: (e) -> + return unless e.dataTransfer.files.length # let it only drop files + e.preventDefault() + e.stopPropagation() + e.dataTransfer.dropEffect = 'copy' # cursor feedback + if e.type is 'drop' + qr.open() + qr.fileInput.call e.dataTransfer fileInput: -> qr.cleanError() if @files.length is 1 @@ -1067,11 +1075,14 @@ textarea.field {
" unless g.REPLY - $.on $('select', qr.el), 'mousedown', (e) -> e.stopPropagation() - $.on $('#autohide', qr.el), 'click', qr.hide - $.on $('.close', qr.el), 'click', qr.close - $.on $('#dump', qr.el), 'click', -> qr.el.classList.toggle 'dump' - $.on $('form', qr.el), 'submit', qr.submit + $.on $('select', qr.el), 'mousedown', (e) -> e.stopPropagation() + $.on $('#autohide', qr.el), 'click', qr.hide + $.on $('.close', qr.el), 'click', qr.close + $.on $('#dump', qr.el), 'click', -> qr.el.classList.toggle 'dump' + $.on $('form', qr.el), 'submit', qr.submit + $.on $('[type=file]', qr.el), 'change', qr.fileInput + $.on d, 'dragover', qr.fileDrop + $.on d, 'drop', qr.fileDrop # save & load inputs' value with localStorage qr.inputs = @@ -1085,8 +1096,6 @@ textarea.field { if match = e.key.match /qr_(.+)$/ qr.inputs[match[1]].value = JSON.parse e.newValue - $.on $('[type=file]', qr.el), 'change', qr.fileInput - $.add d.body, qr.el submit: (e) ->