Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG#674479 Handle multiple images upload, show paste dialog #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ckeditor/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ CKEDITOR.plugins.addExternal('tauuploader', '../custom/plugins/tauuploader/');
CKEDITOR.plugins.addExternal('mentions', '../custom/plugins/mentions/');
CKEDITOR.plugins.addExternal('tableresize', '../custom/plugins/tableresize/');
CKEDITOR.plugins.addExternal('clipboard', '../custom/plugins/clipboard/');

// workaround for paste button
// see https://github.com/ckeditor/ckeditor4/issues/469
CKEDITOR.on("instanceReady", function(event) {
event.editor.on("beforeCommandExec", function(event) {
// Show the paste dialog for the paste buttons and right-click paste
if (event.data.name == "paste") {
event.editor._.forcePasteDialog = true;
}
// Don't show the paste dialog for Ctrl+Shift+V
if (event.data.name == "pastetext" && event.data.commandData.from == "keystrokeHandler") {
event.cancel();
}
})
});
65 changes: 46 additions & 19 deletions custom/plugins/tauuploader/plugin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*global CKEDITOR, $, _*/
CKEDITOR.plugins.add('tauuploader',
{
init: function(editor) {
init: function (editor) {
var uploadFiles = editor.config.uploaderConfig.uploadFiles;
var onUploadError = editor.config.uploaderConfig.onUploadError || _.noop;
var b64toBlob = function(b64Data, contentType, sliceSize) {
var b64toBlob = function (b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
Expand All @@ -24,7 +24,7 @@ CKEDITOR.plugins.add('tauuploader',
return new Blob(byteArrays, {type: contentType});
};

var insertToEditor = function(file) {
var insertToEditor = function (file) {
var element;
if (uploadFiles.isImage(file)) {
element = CKEDITOR.dom.element
Expand All @@ -40,10 +40,10 @@ CKEDITOR.plugins.add('tauuploader',
allowedContent: 'img[!src,id];'
});

editor.on('instanceReady', function() {
editor.on('instanceReady', function () {
var $editor = $(editor.editable().$);
$editor.addClass('i-role-uploader-area').fileupload({
add: function(event, data) {
add: function (event, data) {
if (!data.files || data.files.length === 0) {
return;
}
Expand All @@ -54,13 +54,13 @@ CKEDITOR.plugins.add('tauuploader',
dropZone: $editor,
pasteZone: $editor
});

editor.on('beforePaste', function (e) {
if (e.data.dataTransfer) {
var dt = e.data.dataTransfer.$;
if(dt && dt.items){
$.each(dt.items, function(i, val) {
if(val.kind === 'file') {
if (dt && dt.items) {
$.each(dt.items, function (i, val) {
if (val.kind === 'file') {
e.cancel();
}
});
Expand All @@ -69,22 +69,49 @@ CKEDITOR.plugins.add('tauuploader',
});

// Paste from clipboard workaround for Firefox.
editor.on('paste', function(e) {
editor.on('paste', function (e) {
var data = e.data,
html = (data.html || ( data.type && data.type == 'html' && data.dataValue));
html = (data.html || (data.type && data.type == 'html' && data.dataValue));

if (!html || !_.isString(html)) {
return;
}
var match = html.match(/<img src="data:image\/png;base64,(.*?)".*?>/);
if (match && match[1]) {
var imageData = match[1];
var container = document.createElement('div');
container.innerHTML = html;

var mimeTypeRegExp = /:(.*?);/;
var imagesWithDataUrl = Array.from(container.querySelectorAll('img'))
.filter(x => x.src && x.src.startsWith('data'));

if (imagesWithDataUrl.length) {
e.cancel();
var blob = b64toBlob(imageData, 'image/png');
blob.name = new Date().toDateString();
uploadFiles.handleUploadResponse($editor.fileupload('send', {
files: [blob]
})).then(insertToEditor, onUploadError);
editor.setReadOnly(true);

Promise.all(imagesWithDataUrl.map((image) => {
const content = image.src.split(',')[1];
const mimeType = image.src.match(mimeTypeRegExp)[1];
const blob = b64toBlob(content, mimeType);
blob.name = new Date().toDateString();

return uploadFiles.handleUploadResponse($editor.fileupload('send', {
files: [blob]
}))
})).then(responses => {
imagesWithDataUrl.forEach((image, index) => {
const img = document.createElement('img');
img.src = responses[index].uri;
img.style = image.style;
img.alt = image.alt;

image.parentElement.replaceChild(img, image);
});

editor.setReadOnly(false);
editor.insertHtml(container.innerHTML);
}).catch(error => {
editor.setReadOnly(false);
onUploadError(error);
})
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tp-ckeditor",
"version": "3.2.12",
"version": "3.2.13-alpha.2",
"description": "CKEditor build for TargetProcess",
"main": "index.js",
"scripts": {
Expand Down