Skip to content

Commit 11960d9

Browse files
committed
Cleaned page form JS & spaced tag box
As per #174
1 parent bbd8fff commit 11960d9

File tree

2 files changed

+53
-50
lines changed

2 files changed

+53
-50
lines changed

resources/assets/js/pages/page-form.js

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
11
"use strict";
22

3+
/**
4+
* Handle pasting images from clipboard.
5+
* @param e - event
6+
* @param editor - editor instance
7+
*/
38
function editorPaste(e, editor) {
49
if (!e.clipboardData) return
5-
var items = e.clipboardData.items;
10+
let items = e.clipboardData.items;
611
if (!items) return;
7-
for (var i = 0; i < items.length; i++) {
8-
if (items[i].type.indexOf("image") !== -1) {
9-
10-
var file = items[i].getAsFile();
11-
var formData = new FormData();
12-
var ext = 'png';
13-
var xhr = new XMLHttpRequest();
14-
15-
if (file.name) {
16-
var fileNameMatches = file.name.match(/\.(.+)$/);
17-
if (fileNameMatches) {
18-
ext = fileNameMatches[1];
19-
}
12+
for (let i = 0; i < items.length; i++) {
13+
if (items[i].type.indexOf("image") === -1) return
14+
15+
let file = items[i].getAsFile();
16+
let formData = new FormData();
17+
let ext = 'png';
18+
let xhr = new XMLHttpRequest();
19+
20+
if (file.name) {
21+
let fileNameMatches = file.name.match(/\.(.+)$/);
22+
if (fileNameMatches) {
23+
ext = fileNameMatches[1];
2024
}
21-
22-
var id = "image-" + Math.random().toString(16).slice(2);
23-
var loadingImage = window.baseUrl('/loading.gif');
24-
editor.execCommand('mceInsertContent', false, '<img src="'+ loadingImage +'" id="' + id + '">');
25-
26-
var remoteFilename = "image-" + Date.now() + "." + ext;
27-
formData.append('file', file, remoteFilename);
28-
formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
29-
30-
xhr.open('POST', window.baseUrl('/images/gallery/upload'));
31-
xhr.onload = function () {
32-
if (xhr.status === 200 || xhr.status === 201) {
33-
var result = JSON.parse(xhr.responseText);
34-
editor.dom.setAttrib(id, 'src', result.thumbs.display);
35-
} else {
36-
console.log('An error occurred uploading the image');
37-
console.log(xhr.responseText);
38-
editor.dom.remove(id);
39-
}
40-
};
41-
xhr.send(formData);
4225
}
26+
27+
let id = "image-" + Math.random().toString(16).slice(2);
28+
let loadingImage = window.baseUrl('/loading.gif');
29+
editor.execCommand('mceInsertContent', false, `<img src="${loadingImage}" id="${id}">`);
30+
31+
let remoteFilename = "image-" + Date.now() + "." + ext;
32+
formData.append('file', file, remoteFilename);
33+
formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
34+
35+
xhr.open('POST', window.baseUrl('/images/gallery/upload'));
36+
xhr.onload = function () {
37+
if (xhr.status === 200 || xhr.status === 201) {
38+
let result = JSON.parse(xhr.responseText);
39+
editor.dom.setAttrib(id, 'src', result.thumbs.display);
40+
} else {
41+
console.log('An error occurred uploading the image', xhr.responseText);
42+
editor.dom.remove(id);
43+
}
44+
};
45+
xhr.send(formData);
46+
4347
}
4448
}
4549

@@ -101,7 +105,7 @@ var mceOptions = module.exports = {
101105

102106
if (type === 'file') {
103107
window.showEntityLinkSelector(function(entity) {
104-
var originalField = win.document.getElementById(field_name);
108+
let originalField = win.document.getElementById(field_name);
105109
originalField.value = entity.link;
106110
$(originalField).closest('.mce-form').find('input').eq(2).val(entity.name);
107111
});
@@ -115,24 +119,24 @@ var mceOptions = module.exports = {
115119
// to ensure the new value sticks
116120
win.document.getElementById(field_name).value = image.url;
117121
if ("createEvent" in document) {
118-
var evt = document.createEvent("HTMLEvents");
122+
let evt = document.createEvent("HTMLEvents");
119123
evt.initEvent("change", false, true);
120124
win.document.getElementById(field_name).dispatchEvent(evt);
121125
} else {
122126
win.document.getElementById(field_name).fireEvent("onchange");
123127
}
124128

125129
// Replace the actively selected content with the linked image
126-
var html = '<a href="' + image.url + '" target="_blank">';
127-
html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">';
130+
let html = `<a href="${image.url}" target="_blank">`;
131+
html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
128132
html += '</a>';
129133
win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
130134
});
131135
}
132136

133137
},
134138
paste_preprocess: function (plugin, args) {
135-
var content = args.content;
139+
let content = args.content;
136140
if (content.indexOf('<img src="file://') !== -1) {
137141
args.content = '';
138142
}
@@ -142,7 +146,7 @@ var mceOptions = module.exports = {
142146

143147
// Run additional setup actions
144148
// Used by the angular side of things
145-
for (var i = 0; i < mceOptions.extraSetups.length; i++) {
149+
for (let i = 0; i < mceOptions.extraSetups.length; i++) {
146150
mceOptions.extraSetups[i](editor);
147151
}
148152

@@ -158,12 +162,11 @@ var mceOptions = module.exports = {
158162
editor.on('dragstart', function () {
159163
var node = editor.selection.getNode();
160164

161-
if (node.nodeName === 'IMG') {
162-
wrap = editor.dom.getParent(node, '.mceTemp');
165+
if (node.nodeName !== 'IMG') return;
166+
wrap = editor.dom.getParent(node, '.mceTemp');
163167

164-
if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
165-
wrap = node.parentNode;
166-
}
168+
if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
169+
wrap = node.parentNode;
167170
}
168171
});
169172

@@ -188,15 +191,15 @@ var mceOptions = module.exports = {
188191
});
189192
})();
190193

191-
// Image picker button
194+
// Custom Image picker button
192195
editor.addButton('image-insert', {
193196
title: 'My title',
194197
icon: 'image',
195198
tooltip: 'Insert an image',
196199
onclick: function () {
197200
window.ImageManager.showExternal(function (image) {
198-
var html = '<a href="' + image.url + '" target="_blank">';
199-
html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">';
201+
let html = `<a href="${image.url}" target="_blank">`;
202+
html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
200203
html += '</a>';
201204
editor.execCommand('mceInsertContent', false, html);
202205
});

resources/assets/sass/_pages.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
}
249249

250250
.tag-display {
251-
margin: $-xl $-xs;
251+
margin: $-xl $-m;
252252
border: 1px solid #DDD;
253253
min-width: 180px;
254254
max-width: 320px;

0 commit comments

Comments
 (0)