2019-03-22 20:35:11 +08:00
|
|
|
/* eslint no-underscore-dangle: "off" */
|
|
|
|
/* eslint no-use-before-define: "off" */
|
|
|
|
/* eslint no-restricted-syntax: ["off", "BinaryExpression[operator='in']"] */
|
2020-09-30 18:10:49 +08:00
|
|
|
/* global tinymce I18n HelperModule validateFileSize */
|
2019-03-22 20:35:11 +08:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
tinymce.PluginManager.requireLangPack('customimageuploader');
|
|
|
|
|
|
|
|
tinymce.create('tinymce.plugins.CustomImageUploader', {
|
2020-09-29 00:10:01 +08:00
|
|
|
CustomImageUploader: function(ed) {
|
2019-03-22 20:35:11 +08:00
|
|
|
var iframe;
|
|
|
|
var editor = ed;
|
|
|
|
var textAreaElement = $('#' + ed.id);
|
2020-09-29 00:10:01 +08:00
|
|
|
|
|
|
|
function loadFiles() {
|
|
|
|
let $fileInput;
|
2020-10-29 15:45:55 +08:00
|
|
|
let hitFileLimit;
|
2020-09-29 00:10:01 +08:00
|
|
|
$('#tinymce_current_upload').remove();
|
2020-10-29 15:45:55 +08:00
|
|
|
$fileInput = $('<input type="file" multiple accept="image/*" id="tinymce_current_upload" style="display: none;">').prependTo(editor.container);
|
2020-09-29 00:10:01 +08:00
|
|
|
$fileInput.click();
|
|
|
|
|
|
|
|
$fileInput.change(function() {
|
|
|
|
let formData = new FormData();
|
|
|
|
let files = $('#tinymce_current_upload')[0].files;
|
|
|
|
|
|
|
|
Array.from(files).forEach(file => formData.append('files[]', file, file.name));
|
2020-10-29 15:45:55 +08:00
|
|
|
|
|
|
|
Array.from(files).every(file => {
|
|
|
|
if (!validateFileSize(file, true)) {
|
|
|
|
hitFileLimit = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hitFileLimit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-29 00:10:01 +08:00
|
|
|
$.post({
|
2020-09-30 18:10:49 +08:00
|
|
|
url: textAreaElement.data('tinymce-asset-path'),
|
2020-09-29 00:10:01 +08:00
|
|
|
data: formData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function(data) {
|
|
|
|
handleResponse(data);
|
|
|
|
$('#tinymce_current_upload').remove();
|
2019-03-22 20:35:11 +08:00
|
|
|
},
|
2020-09-29 00:10:01 +08:00
|
|
|
error: function(response) {
|
|
|
|
HelperModule.flashAlertMsg(response.responseJSON.errors, 'danger');
|
|
|
|
$('#tinymce_current_upload').remove();
|
2019-03-22 20:35:11 +08:00
|
|
|
}
|
2020-09-29 00:10:01 +08:00
|
|
|
});
|
2019-03-22 20:35:11 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-29 00:10:01 +08:00
|
|
|
function handleResponse(response) {
|
|
|
|
if (response.errors) {
|
|
|
|
handleError(response.errors.join('<br>'));
|
2019-03-22 20:35:11 +08:00
|
|
|
} else {
|
2020-09-29 00:10:01 +08:00
|
|
|
response.images.forEach(el => editor.execCommand('mceInsertContent', false, buildHTML(el)));
|
|
|
|
updateActiveImages(ed);
|
2019-03-22 20:35:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(error) {
|
2020-09-29 00:10:01 +08:00
|
|
|
HelperModule.flashAlertMsg(error, 'danger');
|
2019-03-22 20:35:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-29 00:10:01 +08:00
|
|
|
function buildHTML(image) {
|
2020-09-30 18:10:49 +08:00
|
|
|
return `<img src="${image.url}"
|
|
|
|
data-mce-token="${image.token}"
|
|
|
|
alt="description-${image.token}" />`;
|
2019-03-22 20:35:11 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:24:02 +08:00
|
|
|
// Create hidden field for images
|
|
|
|
function createImageHiddenField() {
|
|
|
|
textAreaElement.parent().find('input#tiny-mce-images').remove();
|
|
|
|
$('<input type="hidden" id="tiny-mce-images" name="tiny_mce_images" value="[]">').insertAfter(textAreaElement);
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:35:11 +08:00
|
|
|
// Finding images in text
|
|
|
|
function updateActiveImages() {
|
|
|
|
var images;
|
2019-05-07 16:24:02 +08:00
|
|
|
var imageContainer = $('#' + editor.id).next()[0];
|
2019-03-22 20:35:11 +08:00
|
|
|
iframe = $('#' + editor.id).prev().find('.mce-edit-area iframe').contents();
|
|
|
|
images = $.map($('img', iframe), e => {
|
2019-04-02 19:41:32 +08:00
|
|
|
return e.dataset.mceToken;
|
2019-03-22 20:35:11 +08:00
|
|
|
});
|
2019-05-07 16:24:02 +08:00
|
|
|
if (imageContainer === undefined) {
|
|
|
|
createImageHiddenField();
|
2019-04-26 23:37:01 +08:00
|
|
|
}
|
2019-09-30 22:09:43 +08:00
|
|
|
|
|
|
|
// Small fix for ResultText when you cancel after change MarvinJS
|
|
|
|
if (imageContainer === undefined) return [];
|
|
|
|
|
2019-05-07 16:24:02 +08:00
|
|
|
imageContainer.value = JSON.stringify(images);
|
2019-03-22 20:35:11 +08:00
|
|
|
return JSON.stringify(images);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a button that opens a window
|
|
|
|
editor.addButton('customimageuploader', {
|
|
|
|
tooltip: I18n.t('tiny_mce.upload_window_label'),
|
|
|
|
icon: 'image',
|
2020-09-29 00:10:01 +08:00
|
|
|
onclick: loadFiles
|
2019-03-22 20:35:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Adds a menu item to the tools menu
|
|
|
|
editor.addMenuItem('customimageuploader', {
|
|
|
|
text: I18n.t('tiny_mce.upload_window_label'),
|
|
|
|
icon: 'image',
|
|
|
|
context: 'insert',
|
2020-09-29 00:10:01 +08:00
|
|
|
onclick: loadFiles
|
2019-03-22 20:35:11 +08:00
|
|
|
});
|
|
|
|
|
2019-05-07 16:24:02 +08:00
|
|
|
ed.on('NodeChange', function() {
|
|
|
|
// Check editor status
|
|
|
|
if (this.initialized) {
|
|
|
|
updateActiveImages(ed);
|
|
|
|
}
|
2019-03-22 20:35:11 +08:00
|
|
|
});
|
|
|
|
|
2019-05-07 16:24:02 +08:00
|
|
|
createImageHiddenField();
|
2019-03-22 20:35:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-03-22 20:42:12 +08:00
|
|
|
tinymce.PluginManager.add(
|
|
|
|
'customimageuploader',
|
|
|
|
tinymce.plugins.CustomImageUploader
|
|
|
|
);
|
2019-04-26 23:37:01 +08:00
|
|
|
}());
|