Cleaned page form JS & spaced tag box

As per #174
This commit is contained in:
Dan Brown 2016-09-05 19:33:14 +01:00
parent bbd8fff021
commit 11960d9d3a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 51 additions and 48 deletions

View File

@ -1,45 +1,49 @@
"use strict"; "use strict";
/**
* Handle pasting images from clipboard.
* @param e - event
* @param editor - editor instance
*/
function editorPaste(e, editor) { function editorPaste(e, editor) {
if (!e.clipboardData) return if (!e.clipboardData) return
var items = e.clipboardData.items; let items = e.clipboardData.items;
if (!items) return; if (!items) return;
for (var i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) { if (items[i].type.indexOf("image") === -1) return
var file = items[i].getAsFile(); let file = items[i].getAsFile();
var formData = new FormData(); let formData = new FormData();
var ext = 'png'; let ext = 'png';
var xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
if (file.name) { if (file.name) {
var fileNameMatches = file.name.match(/\.(.+)$/); let fileNameMatches = file.name.match(/\.(.+)$/);
if (fileNameMatches) { if (fileNameMatches) {
ext = fileNameMatches[1]; ext = fileNameMatches[1];
} }
} }
var id = "image-" + Math.random().toString(16).slice(2); let id = "image-" + Math.random().toString(16).slice(2);
var loadingImage = window.baseUrl('/loading.gif'); let loadingImage = window.baseUrl('/loading.gif');
editor.execCommand('mceInsertContent', false, '<img src="'+ loadingImage +'" id="' + id + '">'); editor.execCommand('mceInsertContent', false, `<img src="${loadingImage}" id="${id}">`);
var remoteFilename = "image-" + Date.now() + "." + ext; let remoteFilename = "image-" + Date.now() + "." + ext;
formData.append('file', file, remoteFilename); formData.append('file', file, remoteFilename);
formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content')); formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
xhr.open('POST', window.baseUrl('/images/gallery/upload')); xhr.open('POST', window.baseUrl('/images/gallery/upload'));
xhr.onload = function () { xhr.onload = function () {
if (xhr.status === 200 || xhr.status === 201) { if (xhr.status === 200 || xhr.status === 201) {
var result = JSON.parse(xhr.responseText); let result = JSON.parse(xhr.responseText);
editor.dom.setAttrib(id, 'src', result.thumbs.display); editor.dom.setAttrib(id, 'src', result.thumbs.display);
} else { } else {
console.log('An error occurred uploading the image'); console.log('An error occurred uploading the image', xhr.responseText);
console.log(xhr.responseText);
editor.dom.remove(id); editor.dom.remove(id);
} }
}; };
xhr.send(formData); xhr.send(formData);
}
} }
} }
@ -101,7 +105,7 @@ var mceOptions = module.exports = {
if (type === 'file') { if (type === 'file') {
window.showEntityLinkSelector(function(entity) { window.showEntityLinkSelector(function(entity) {
var originalField = win.document.getElementById(field_name); let originalField = win.document.getElementById(field_name);
originalField.value = entity.link; originalField.value = entity.link;
$(originalField).closest('.mce-form').find('input').eq(2).val(entity.name); $(originalField).closest('.mce-form').find('input').eq(2).val(entity.name);
}); });
@ -115,7 +119,7 @@ var mceOptions = module.exports = {
// to ensure the new value sticks // to ensure the new value sticks
win.document.getElementById(field_name).value = image.url; win.document.getElementById(field_name).value = image.url;
if ("createEvent" in document) { if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents"); let evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true); evt.initEvent("change", false, true);
win.document.getElementById(field_name).dispatchEvent(evt); win.document.getElementById(field_name).dispatchEvent(evt);
} else { } else {
@ -123,8 +127,8 @@ var mceOptions = module.exports = {
} }
// Replace the actively selected content with the linked image // Replace the actively selected content with the linked image
var html = '<a href="' + image.url + '" target="_blank">'; let html = `<a href="${image.url}" target="_blank">`;
html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">'; html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
html += '</a>'; html += '</a>';
win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html); win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
}); });
@ -132,7 +136,7 @@ var mceOptions = module.exports = {
}, },
paste_preprocess: function (plugin, args) { paste_preprocess: function (plugin, args) {
var content = args.content; let content = args.content;
if (content.indexOf('<img src="file://') !== -1) { if (content.indexOf('<img src="file://') !== -1) {
args.content = ''; args.content = '';
} }
@ -142,7 +146,7 @@ var mceOptions = module.exports = {
// Run additional setup actions // Run additional setup actions
// Used by the angular side of things // Used by the angular side of things
for (var i = 0; i < mceOptions.extraSetups.length; i++) { for (let i = 0; i < mceOptions.extraSetups.length; i++) {
mceOptions.extraSetups[i](editor); mceOptions.extraSetups[i](editor);
} }
@ -158,13 +162,12 @@ var mceOptions = module.exports = {
editor.on('dragstart', function () { editor.on('dragstart', function () {
var node = editor.selection.getNode(); var node = editor.selection.getNode();
if (node.nodeName === 'IMG') { if (node.nodeName !== 'IMG') return;
wrap = editor.dom.getParent(node, '.mceTemp'); wrap = editor.dom.getParent(node, '.mceTemp');
if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) { if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
wrap = node.parentNode; wrap = node.parentNode;
} }
}
}); });
editor.on('drop', function (event) { editor.on('drop', function (event) {
@ -188,15 +191,15 @@ var mceOptions = module.exports = {
}); });
})(); })();
// Image picker button // Custom Image picker button
editor.addButton('image-insert', { editor.addButton('image-insert', {
title: 'My title', title: 'My title',
icon: 'image', icon: 'image',
tooltip: 'Insert an image', tooltip: 'Insert an image',
onclick: function () { onclick: function () {
window.ImageManager.showExternal(function (image) { window.ImageManager.showExternal(function (image) {
var html = '<a href="' + image.url + '" target="_blank">'; let html = `<a href="${image.url}" target="_blank">`;
html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">'; html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
html += '</a>'; html += '</a>';
editor.execCommand('mceInsertContent', false, html); editor.execCommand('mceInsertContent', false, html);
}); });

View File

@ -248,7 +248,7 @@
} }
.tag-display { .tag-display {
margin: $-xl $-xs; margin: $-xl $-m;
border: 1px solid #DDD; border: 1px solid #DDD;
min-width: 180px; min-width: 180px;
max-width: 320px; max-width: 320px;