2016-09-22 17:00:12 +08:00
|
|
|
/* global $ */
|
2016-09-13 21:02:55 +08:00
|
|
|
|
2016-09-22 17:00:12 +08:00
|
|
|
import Import from './Import'
|
2016-09-22 18:31:56 +08:00
|
|
|
import Util from './Util'
|
2016-10-25 12:28:51 +08:00
|
|
|
import Visualize from './Visualize'
|
2016-09-13 21:02:55 +08:00
|
|
|
|
2016-09-22 15:21:59 +08:00
|
|
|
const PasteInput = {
|
2016-09-21 10:48:47 +08:00
|
|
|
// 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"]*)?$'),
|
|
|
|
|
2016-09-13 21:02:55 +08:00
|
|
|
init: function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = PasteInput
|
2016-09-13 21:02:55 +08:00
|
|
|
|
|
|
|
// intercept dragged files
|
|
|
|
// see http://stackoverflow.com/questions/6756583
|
2016-09-21 10:48:47 +08:00
|
|
|
window.addEventListener("dragover", function(e) {
|
2016-10-04 23:38:32 +08:00
|
|
|
e = e || window.event;
|
2016-09-13 21:02:55 +08:00
|
|
|
e.preventDefault();
|
|
|
|
}, false);
|
2016-09-21 10:48:47 +08:00
|
|
|
window.addEventListener("drop", function(e) {
|
2016-10-04 23:38:32 +08:00
|
|
|
e = e || window.event;
|
2016-09-13 21:02:55 +08:00
|
|
|
e.preventDefault();
|
2016-10-25 12:28:51 +08:00
|
|
|
var coords = Util.pixelsToCoords(Visualize.mGraph, { x: e.clientX, y: e.clientY })
|
2016-09-13 21:02:55 +08:00
|
|
|
if (e.dataTransfer.files.length > 0) {
|
2016-09-25 00:27:04 +08:00
|
|
|
self.handleFile(e.dataTransfer.files[0], coords)
|
2016-09-13 21:02:55 +08:00
|
|
|
}
|
2016-09-21 10:48:47 +08:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-09-13 21:02:55 +08:00
|
|
|
}, false);
|
|
|
|
|
|
|
|
// allow pasting onto canvas (but don't break existing inputs/textareas)
|
|
|
|
$('body').bind('paste', function (e) {
|
|
|
|
if (e.target.tagName === 'INPUT') return
|
|
|
|
if (e.target.tagName === 'TEXTAREA') return
|
|
|
|
|
|
|
|
var text = e.originalEvent.clipboardData.getData('text/plain').trim()
|
|
|
|
self.handle(text)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2016-09-25 00:27:04 +08:00
|
|
|
handleFile: (file, coords = null) => {
|
|
|
|
var self = PasteInput
|
2016-10-08 00:31:32 +08:00
|
|
|
var fileReader = new window.FileReader()
|
2016-09-25 00:27:04 +08:00
|
|
|
fileReader.readAsText(file)
|
|
|
|
fileReader.onload = function(e) {
|
|
|
|
var text = e.currentTarget.result
|
|
|
|
if (text.substring(0,5) === '<?xml') {
|
|
|
|
// assume this is a macOS .webloc link
|
|
|
|
text = text.replace(/[\s\S]*<string>(.*)<\/string>[\s\S]*/m, '$1')
|
|
|
|
}
|
|
|
|
self.handle(text, coords)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handle: function(text, coords = null) {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = PasteInput
|
2016-09-13 21:02:55 +08:00
|
|
|
|
2016-09-21 10:48:47 +08:00
|
|
|
if (text.match(self.URL_REGEX)) {
|
2016-10-01 12:57:19 +08:00
|
|
|
Import.handleURL(text, coords)
|
2016-09-13 21:02:55 +08:00
|
|
|
} else if (text[0] === '{') {
|
2016-09-24 15:22:42 +08:00
|
|
|
Import.handleJSON(text)
|
2016-09-13 21:02:55 +08:00
|
|
|
} else if (text.match(/\t/)) {
|
2016-09-24 15:22:42 +08:00
|
|
|
Import.handleTSV(text)
|
2016-10-01 11:21:42 +08:00
|
|
|
} else {
|
|
|
|
// just try to see if CSV works
|
2016-09-24 15:22:42 +08:00
|
|
|
Import.handleCSV(text)
|
2016-09-13 21:02:55 +08:00
|
|
|
}
|
2016-10-01 13:47:16 +08:00
|
|
|
}
|
2016-09-13 21:02:55 +08:00
|
|
|
}
|
2016-09-22 15:21:59 +08:00
|
|
|
|
|
|
|
export default PasteInput
|