From 49084b98dd140c07bb393bec821399e240947353 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 21 Sep 2016 10:48:47 +0800 Subject: [PATCH] =?UTF-8?q?omg=20import=20bookmarks=20=F0=9F=98=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javascripts/src/Metamaps.PasteInput.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.PasteInput.js b/app/assets/javascripts/src/Metamaps.PasteInput.js index 1b89b0af..55798587 100644 --- a/app/assets/javascripts/src/Metamaps.PasteInput.js +++ b/app/assets/javascripts/src/Metamaps.PasteInput.js @@ -9,16 +9,19 @@ */ Metamaps.PasteInput = { + // thanks to https://github.com/kevva/url-regex + URL_REGEX: new RegExp('^(?:(?:(?:[a-z]+:)?//)|www\.)(?:\S+(?::\S*)?@)?(?:localhost|(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#][^\s"]*)?$'), + init: function () { var self = Metamaps.PasteInput // intercept dragged files // see http://stackoverflow.com/questions/6756583 - window.addEventListener("dragover", function(e){ + window.addEventListener("dragover", function(e) { e = e || event; e.preventDefault(); }, false); - window.addEventListener("drop", function(e){ + window.addEventListener("drop", function(e) { e = e || event; e.preventDefault(); var coords = Metamaps.Util.pixelsToCoords({ x: e.clientX, y: e.clientY }) @@ -34,6 +37,14 @@ Metamaps.PasteInput = { self.handle(text, coords) } } + // OMG import bookmarks 😍 + if (e.dataTransfer.items.length > 0) { + e.dataTransfer.items[0].getAsString(function(text) { + if (text.match(self.URL_REGEX)) { + self.handle(text, coords) + } + }) + } }, false); // allow pasting onto canvas (but don't break existing inputs/textareas) @@ -48,10 +59,8 @@ Metamaps.PasteInput = { handle: function(text, coords) { var self = Metamaps.PasteInput - // thanks to https://github.com/kevva/url-regex - const URL_REGEX = new RegExp('^(?:(?:(?:[a-z]+:)?//)|www\.)(?:\S+(?::\S*)?@)?(?:localhost|(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#][^\s"]*)?$') - if (text.match(URL_REGEX)) { + if (text.match(self.URL_REGEX)) { self.handleURL(text, coords) } else if (text[0] === '{') { self.handleJSON(text)