Skip to content

Commit

Permalink
Add HTML editor paste support for Chrome
Browse files Browse the repository at this point in the history
Adapted from code provided by João Machado (joao.machado@co.sapo.pt).
  • Loading branch information
slusarz committed Dec 2, 2014
1 parent 1d8eaa7 commit b705010
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions imp/js/ckeditor/pasteattachment.js
Expand Up @@ -62,11 +62,40 @@ CKEDITOR.plugins.add('pasteattachment', {
editor.getThemeSpace('contents').$.dispatchEvent(evt);
}

function addImages(files)
{
var error = 0,
upload = [];

$A(files).each(function(f) {
if (f.type.startsWith('image/')) {
if (f.getAsFile) {
f = f.getAsFile();
}
if (!f.name) {
f.name = DimpCore.text.image_data;
}
upload.push(f);
} else {
++error;
}
});

if (upload.size()) {
uploadAtc(upload);
}

if (error) {
HordeCore.notify(
DimpCore.text.dragdropimg_error.sub('%d', error),
'horde.error'
);
}
}

editor.on('contentDom', function(e1) {
editor.document.on('drop', function(e2) {
var d = e2.data.$,
error = 0,
upload = [];
var d = e2.data.$;

fireEventInParent('drop', e2);

Expand All @@ -86,24 +115,7 @@ CKEDITOR.plugins.add('pasteattachment', {

e2.data.preventDefault();

$A(d.dataTransfer.files).each(function(f) {
if (f.type.startsWith('image/')) {
upload.push(f);
} else {
++error;
}
});

if (upload.size()) {
uploadAtc(upload);
}

if (error) {
HordeCore.notify(
DimpCore.text.dragdropimg_error.sub('%d', error),
'horde.error'
);
}
addImages(d.dataTransfer.files);
});

editor.document.on('dragover', function(e3) {
Expand All @@ -112,6 +124,16 @@ CKEDITOR.plugins.add('pasteattachment', {
}
fireEventInParent('dragover', e3);
});

/* This works on Chrome. 'paste' action below works on FF. */
editor.document.on('paste', function(ev) {
var d = ev.data.$;
try {
addImages(
(d.clipboardData || d.originalEvent.clipboardData).items
);
} catch (e) {}
});
});

editor.on('paste', function(ev) {
Expand Down

0 comments on commit b705010

Please sign in to comment.