tg
tg copied to clipboard
Attachments: support for copy/paste
Description
Currently attachments can be uploaded using D'n'D or selecting files from a dialog. It would be great to also support the copy/paste mechanism, which is especially convenient for uploading screenshots without the need to save them separate files.
Expected outcome
A convenient copy/paste mechanism for creating file attachments.
Implementation inspiration
https://stackoverflow.com/questions/6219197/how-to-manage-image-pasting-from-clipboard-in-html5
var CLIPBOARD = new CLIPBOARD_CLASS("my_canvas", true);
/**
* image pasting into canvas
*
* @param {string} canvas_id - canvas id
* @param {boolean} autoresize - if canvas will be resized
*/
function CLIPBOARD_CLASS(canvas_id, autoresize) {
var _self = this;
var canvas = document.getElementById(canvas_id);
var ctx = document.getElementById(canvas_id).getContext("2d");
//handlers
document.addEventListener('paste', function (e) { _self.paste_auto(e); }, false);
//on paste
this.paste_auto = function (e) {
if (e.clipboardData) {
var items = e.clipboardData.items;
if (!items) return;
//access data directly
var is_image = false;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
//image
var blob = items[i].getAsFile();
var URLObj = window.URL || window.webkitURL;
var source = URLObj.createObjectURL(blob);
this.paste_createImage(source);
is_image = true;
}
}
if(is_image == true){
e.preventDefault();
}
}
};
//draw pasted image to canvas
this.paste_createImage = function (source) {
var pastedImage = new Image();
pastedImage.onload = function () {
if(autoresize == true){
//resize
canvas.width = pastedImage.width;
canvas.height = pastedImage.height;
}
else{
//clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
ctx.drawImage(pastedImage, 0, 0);
};
pastedImage.src = source;
};
}