const electron = require('electron');
const clipboard = electron.clipboard;
const _ = require('lodash');
const Image = require('../models').Image;
const IMAGE_TAG_TEMP = _.template('![<%- filename %>](<%- fileurl %>)\n');

function flashSelection(cm) {
	cm.setExtending(false);
	cm.setCursor(cm.getCursor());
}


/* Electron things */
function killLine(cm) {
	flashSelection(cm);
	var c = cm.getCursor();
	var thisLine = cm.getRange(c, {line: c.line + 1, ch: 0});
	if (thisLine == '\n') {
		clipboard.writeText('\n');
		cm.replaceRange('', c, {line: c.line + 1, ch: 0});
	} else {
		clipboard.writeText(cm.getRange(c, {line: c.line}));
		cm.replaceRange('', c, {line: c.line});
	}
}

function copyText(cm) {
	var text = cm.getSelection();
	if (text.length > 0) {
		clipboard.writeText(text);
	}
}

function cutText(cm) {
	var text = cm.getSelection();
	if (text.length > 0) {
		clipboard.writeText(text);
		cm.replaceSelection('');
	}
}

function pasteText(cm) {
	//cm.replaceSelection(clipboard.readText());
	if(clipboard.availableFormats().indexOf('image/png') != -1 || clipboard.availableFormats().indexOf('image/jpg') != -1){
 		var im = clipboard.readImage();
 		var image = Image.fromClipboard(im);
 		cm.doc.replaceRange(
 			IMAGE_TAG_TEMP({filename: image.name, fileurl: image.pilemdURL}),
 			cm.doc.getCursor()
 		);
 	} else {
 		var pasted = clipboard.readText();
 		if(pasted.split('.').pop() === 'png' || pasted.split('.').pop() === 'jpg'){
 			var f = {name: pasted.split('/').pop(), path: pasted};
 			uploadFile(cm, f);
 		} else {
 			cm.replaceSelection(clipboard.readText());
 		}
 	}
}

function uploadFile(cm, file){
 	try {
		var image = Image.fromBinary(file.name, file.path);
 	} catch (e) {
 		console.log(e);
 		return
 	}

	cm.doc.replaceRange(
		IMAGE_TAG_TEMP({filename: file.name, fileurl: image.pilemdURL}),
		cm.doc.getCursor()
	);
}

module.exports = {
	killLine: killLine,
	copyText: copyText,
	cutText: cutText,
	pasteText: pasteText
};

@@ -960,54 +960,8 @@
 ) %7B%0A
-%09//cm.replaceSelection(clipboard.readText());%0A
 %09if(
@@ -1324,17 +1324,16 @@
 Text();%0A
- 
 %09%09if(pas
@@ -1340,71 +1340,126 @@
 ted.
-split('.').pop() === 'png' %7C%7C pasted.split('.').pop() === 'jpg'
+indexOf('http') == 0) %7B%0A%09%09%09pasted = pasted.replace(new RegExp('(%5C.jpg%7C%5C.png)%5B?&%5D.*$'), '$1');%0A%09%09%7D%0A%09%09if(isImage(pasted)
 )%7B%0A 
@@ -1520,16 +1520,20 @@
 d%7D;%0A %09%09%09
+if(!
 uploadFi
@@ -1541,16 +1541,45 @@
 e(cm, f)
+) cm.replaceSelection(pasted)
 ;%0A %09%09%7D e
@@ -1612,39 +1612,128 @@
 ion(
-clipboard.readText());%0A %09%09%7D%0A %09%7D
+pasted);%0A %09%09%7D%0A %09%7D%0A%7D%0A%0Afunction isImage(text)%7B%0A%09return text.split('.').pop() === 'png' %7C%7C text.split('.').pop() === 'jpg';
 %0A%7D%0A%0A
@@ -1841,13 +1841,14 @@
 h (e
+rr
 ) %7B%0A
- 
 %09%09co
@@ -1857,13 +1857,30 @@
 ole.
-log(e
+warn('uploadFile', err
 );%0A 
@@ -1887,16 +1887,23 @@
 %09%09return
+ false;
 %0A %09%7D%0A%0A%09c
@@ -2014,16 +2014,30 @@
 r()%0A%09);%0A
+%09return true;%0A
 %7D%0A%0Amodul

paste image url