Skip to content
This repository has been archived by the owner on Dec 21, 2021. It is now read-only.

Commit

Permalink
Correct Firefox text and image paste
Browse files Browse the repository at this point in the history
  • Loading branch information
thorin committed Dec 16, 2016
1 parent 838ad4c commit 6e47e38
Showing 1 changed file with 48 additions and 74 deletions.
122 changes: 48 additions & 74 deletions assets/javascripts/image_paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,7 @@ function pasteImageName(e, name) {
text = prefix + name + suffix;
}
})
var scrollPos = e.scrollTop;
var method = ((e.selectionStart || e.selectionStart == '0') ? 1 : (document.selection ? 2 : false ) );
if (method == 2) {
e.focus();
var range = document.selection.createRange();
range.moveStart ('character', -e.value.length);
strPos = range.text.length;
}
else if (method == 1) strPos = e.selectionStart;

var front = (e.value).substring(0,strPos);
var back = (e.value).substring(strPos,e.value.length);
if (front.length == 0 || front.slice(-1) == '\n') {
e.value=front+text+back;
} else {
e.value=front+' '+text+back;
}
strPos = strPos + text.length;
if (method == 2) {
e.focus();
var range = document.selection.createRange();
range.moveStart ('character', -e.value.length);
range.moveStart ('character', strPos);
range.moveEnd ('character', 0);
range.select();
}
else if (method == 1) {
e.selectionStart = strPos;
e.selectionEnd = strPos;
e.focus();
}
e.scrollTop = scrollPos;
window.imgpaste.context.insertHtmlForTextarea(text);
}

/**
Expand Down Expand Up @@ -121,7 +90,12 @@ function uploadImage(type, blob, editElement) {
var name = 'screenshot_'+addFile.nextAttachmentId+'_'+timestamp+ext;

/* Upload pasted image */
blob.name = name; /* Not very elegent, but we pretent the Blob is actually a File */
if (Object.defineProperty) {
Object.defineProperty(blob, 'name', { value: name });
} else {
blob.name = name;
}
blob = $.extend(blob, {name: name});
uploadAndAttachFiles([blob], fileinput);

/* Inset text into input */
Expand Down Expand Up @@ -152,7 +126,12 @@ $( document ).ready(function() {
var timestamp = Math.round(+new Date()/1000);
var name = 'screenshot_'+addFile.nextAttachmentId+'_'+timestamp+'_'+e.dataTransfer.files[file].name.replace(/[ !"#%&\'()*:<=>?\[\\\]|]/g, '_');
var blob = e.dataTransfer.files[file].slice();
blob.name = name;
if (Object.defineProperty) {
Object.defineProperty(blob, 'name', { value: name });
} else {
blob.name = name;
}

uploadAndAttachFiles([blob], $('input:file.file_selector'));
pasteImageName(this, name);

Expand Down Expand Up @@ -298,50 +277,35 @@ $( document ).ready(function() {

// if the image is inserted into the textarea, then return focus
self.returnFocusForTextArea();
var items = $.makeArray(e.clipboardData.items).concat($.makeArray(e.clipboardData.files));

// read data from the clipborad and upload the first file

if ( e.clipboardData.items ) {
var items = e.clipboardData.items;
for (var i = 0; i < items.length; ++i) {
if (items[i].kind === 'file' && items[i].type.indexOf('image/') !== -1) {
// read data from the clipboard and upload the first file
for (var i = 0; i < items.length; ++i) {
if ((!items[i].kind || items[i].kind === 'file') && items[i].type.indexOf('image/') !== -1) {

if ( options.before ) options.before();
if ( options.before ) options.before();

// only paste 1 image at a time
e.preventDefault();
// only paste 1 image at a time
e.preventDefault();

// uploads image on a server
this.uploadImage({
image: items[i].getAsFile(),
type: items[i].type,
ref: 'clipboard'
}, options);
// uploads image on a server
this.uploadImage({
image: items[i].getAsFile ? items[i].getAsFile() : items[i],
type: items[i].type,
ref: 'clipboard'
}, options);

return;
}
return;
}
}
var items = e.clipboardData.items;
for (var i = 0; i < items.length; i++) {
if (items[i].kind === 'string' && items[i].type === 'text/plain') {
items[i].getAsString($.proxy(this.insertHtmlForTextarea, this));

if ( e.clipboardData.files ) {
var items = e.clipboardData.files;
for (var i = 0; i < items.length; ++i) {
if (items[i].type.indexOf('image/') !== -1) {

if ( options.before ) options.before();

// only paste 1 image at a time
e.preventDefault();

// uploads image on a server
this.uploadImage({
image: items[i],
type: items[i].type,
ref: 'clipboard'
}, options);
e.preventDefault();

return;
}
return;
}
}
},
Expand Down Expand Up @@ -371,7 +335,7 @@ $( document ).ready(function() {
},


/**
/**
* Uploads image by using the capture.
*/
uploadFromCapture: function(options) {
Expand Down Expand Up @@ -481,10 +445,20 @@ $( document ).ready(function() {
+ html
+ this.selection.editor.value.slice(this.selection.end);

$(this.selection.editor).focus();
this.selection.editor.selectionStart = this.selection.end + html.length;
this.selection.editor.selectionEnd = this.selection.editor.selectionStart;

// $(this.selection.editor).focus();

var elem = this.selection.editor;
var caretPos = this.selection.start + html.length;
if (elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', caretPos);
range.select();
} else if(elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
} else {
elem.focus();
}
this.selection = null;
},

Expand Down

0 comments on commit 6e47e38

Please sign in to comment.