Skip to content

Commit 7278372

Browse files
lafrikstechknowlogick
authored andcommitted
Implement pasting image from clipboard for browsers that supports that (#5317)
1 parent e110943 commit 7278372

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

public/js/index.js

+82
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,95 @@ function initReactionSelector(parent) {
171171
});
172172
}
173173

174+
function insertAtCursor(field, value) {
175+
if (field.selectionStart || field.selectionStart === 0) {
176+
var startPos = field.selectionStart;
177+
var endPos = field.selectionEnd;
178+
field.value = field.value.substring(0, startPos)
179+
+ value
180+
+ field.value.substring(endPos, field.value.length);
181+
field.selectionStart = startPos + value.length;
182+
field.selectionEnd = startPos + value.length;
183+
} else {
184+
field.value += value;
185+
}
186+
}
187+
188+
function replaceAndKeepCursor(field, oldval, newval) {
189+
if (field.selectionStart || field.selectionStart === 0) {
190+
var startPos = field.selectionStart;
191+
var endPos = field.selectionEnd;
192+
field.value = field.value.replace(oldval, newval);
193+
field.selectionStart = startPos + newval.length - oldval.length;
194+
field.selectionEnd = endPos + newval.length - oldval.length;
195+
} else {
196+
field.value = field.value.replace(oldval, newval);
197+
}
198+
}
199+
200+
function retrieveImageFromClipboardAsBlob(pasteEvent, callback){
201+
if (!pasteEvent.clipboardData) {
202+
return;
203+
}
204+
205+
var items = pasteEvent.clipboardData.items;
206+
if (typeof(items) === "undefined") {
207+
return;
208+
}
209+
210+
for (var i = 0; i < items.length; i++) {
211+
if (items[i].type.indexOf("image") === -1) continue;
212+
var blob = items[i].getAsFile();
213+
214+
if (typeof(callback) === "function") {
215+
pasteEvent.preventDefault();
216+
pasteEvent.stopPropagation();
217+
callback(blob);
218+
}
219+
}
220+
}
221+
222+
function uploadFile(file, callback) {
223+
var xhr = new XMLHttpRequest();
224+
225+
xhr.onload = function() {
226+
if (xhr.status == 200) {
227+
callback(xhr.responseText);
228+
}
229+
};
230+
231+
xhr.open("post", suburl + "/attachments", true);
232+
xhr.setRequestHeader("X-Csrf-Token", csrf);
233+
var formData = new FormData();
234+
formData.append('file', file, file.name);
235+
xhr.send(formData);
236+
}
237+
238+
function initImagePaste(target) {
239+
target.each(function(i, field) {
240+
field.addEventListener('paste', function(event){
241+
retrieveImageFromClipboardAsBlob(event, function(img) {
242+
var name = img.name.substr(0, img.name.lastIndexOf('.'));
243+
insertAtCursor(field, '![' + name + ']()');
244+
uploadFile(img, function(res) {
245+
var data = JSON.parse(res);
246+
replaceAndKeepCursor(field, '![' + name + ']()', '![' + name + '](' + suburl + '/attachments/' + data.uuid + ')');
247+
var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
248+
$('.files').append(input);
249+
});
250+
});
251+
}, false);
252+
});
253+
}
254+
174255
function initCommentForm() {
175256
if ($('.comment.form').length == 0) {
176257
return
177258
}
178259

179260
initBranchSelector();
180261
initCommentPreviewTab($('.comment.form'));
262+
initImagePaste($('.comment.form textarea'));
181263

182264
// Listsubmit
183265
function initListSubmits(selector, outerSelector) {

0 commit comments

Comments
 (0)