Skip to content
This repository was archived by the owner on Jul 14, 2022. It is now read-only.

Commit d02fd0a

Browse files
committed
Allow updating an already inserted image
1 parent 61317d6 commit d02fd0a

File tree

1 file changed

+124
-11
lines changed
  • app/assets/javascripts/tinymce/plugins/uploadimage

1 file changed

+124
-11
lines changed

app/assets/javascripts/tinymce/plugins/uploadimage/plugin.js

Lines changed: 124 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
tinymce.create('tinymce.plugins.UploadImage', {
55
UploadImage: function(ed, url) {
6+
var PLUGIN_CLASS = 'mce-plugins-uploadimage';
67
var form,
78
iframe,
89
win,
@@ -11,11 +12,30 @@
1112
editor = ed;
1213

1314
function showDialog() {
15+
var node = ed.selection.getNode();
16+
17+
if (node && node.tagName === 'IMG') {
18+
showUpdateDialog();
19+
} else {
20+
showInsertDialog();
21+
}
22+
}
23+
24+
function showInsertDialog() {
1425
var classList = getClassList();
1526
var body = [
1627
{ type: 'iframe', url: 'javascript:void(0)' },
17-
{ type: 'textbox', name: 'file', label: ed.translate('Choose an image'), subtype: 'file' },
18-
{ type: 'textbox', name: 'alt', label: ed.translate('Image description') }
28+
{
29+
type: 'textbox',
30+
name: 'file',
31+
label: ed.translate('Choose an image'),
32+
subtype: 'file'
33+
},
34+
{
35+
type: 'textbox',
36+
name: 'alt',
37+
label: ed.translate('Image description')
38+
}
1939
];
2040

2141
if (classList.length > 0) {
@@ -34,7 +54,11 @@
3454
}
3555

3656
body = body.concat([
37-
{ type: 'container', classes: 'error', html: "<p style='color: #b94a48;'>&nbsp;</p>" },
57+
{
58+
type: 'container',
59+
classes: 'error',
60+
html: "<p style='color: #b94a48;'>&nbsp;</p>"
61+
},
3862

3963
// Trick TinyMCE to add a empty div that "preloads" the throbber image
4064
{ type: 'container', classes: 'throbber' }
@@ -80,9 +104,19 @@
80104
// Create some needed hidden inputs
81105
form.appendChild(createElement('input', { type: 'hidden', name: 'utf8', value: '✓' }));
82106
form.appendChild(
83-
createElement('input', { type: 'hidden', name: 'authenticity_token', value: getMetaContents('csrf-token') })
107+
createElement('input', {
108+
type: 'hidden',
109+
name: 'authenticity_token',
110+
value: getMetaContents('csrf-token')
111+
})
112+
);
113+
form.appendChild(
114+
createElement('input', {
115+
type: 'hidden',
116+
name: hintName(),
117+
value: hintValue()
118+
})
84119
);
85-
form.appendChild(createElement('input', { type: 'hidden', name: hintName(), value: hintValue() }));
86120

87121
var el = win.getEl();
88122
var body = document.getElementById(el.id + '-body');
@@ -117,6 +151,64 @@
117151
body.appendChild(form);
118152
}
119153

154+
function showUpdateDialog() {
155+
var node = ed.selection.getNode();
156+
var classList = getClassList();
157+
var body = [
158+
{
159+
type: 'textbox',
160+
name: 'alt',
161+
value: node.getAttribute('alt'),
162+
label: ed.translate('Image description')
163+
}
164+
];
165+
166+
if (classList.length > 0) {
167+
for (var i = 0; i < classList.length; i++) {
168+
if (node.className.indexOf(classList[i].value) > 0) {
169+
selectedClass = classList[i];
170+
}
171+
}
172+
173+
selectedClass = selectedClass || classList[0].value;
174+
body = body.concat([
175+
{
176+
type: 'listbox',
177+
name: 'class',
178+
label: ed.translate('Class'),
179+
value: selectedClass.value,
180+
values: classList,
181+
onSelect: function(e) {
182+
selectedClass = this.value();
183+
}
184+
}
185+
]);
186+
}
187+
188+
win = editor.windowManager.open(
189+
{
190+
title: ed.translate('Update image'),
191+
width: 520 + parseInt(editor.getLang('uploadimage.delta_width', 0), 10),
192+
height: 180 + parseInt(editor.getLang('uploadimage.delta_height', 0), 10),
193+
body: body,
194+
buttons: [
195+
{
196+
text: ed.translate('Update'),
197+
onclick: updateImage,
198+
subtype: 'primary'
199+
},
200+
{
201+
text: ed.translate('Cancel'),
202+
onclick: ed.windowManager.close
203+
}
204+
]
205+
},
206+
{
207+
plugin_url: url
208+
}
209+
);
210+
}
211+
120212
function hintName() {
121213
return inputName(ed.getParam('uploadimage_hint_key', 'hint'));
122214
}
@@ -133,6 +225,14 @@
133225
}
134226
}
135227

228+
function windowData() {
229+
if (ed.windowManager.windows[0]) {
230+
return ed.windowManager.windows[0].toJSON();
231+
} else {
232+
return {};
233+
}
234+
}
235+
136236
function insertImage() {
137237
if (getInputValue(inputName('file')) === '') {
138238
return handleError('You must choose a file');
@@ -159,6 +259,15 @@
159259
form.submit();
160260
}
161261

262+
function updateImage(e) {
263+
var node = ed.selection.getNode();
264+
var data = windowData();
265+
266+
node.setAttribute('class', defaultClass() + ' ' + data.class);
267+
node.setAttribute('alt', data.alt);
268+
win.close();
269+
}
270+
162271
function uploadDone() {
163272
if (throbber) {
164273
throbber.hide();
@@ -222,14 +331,13 @@
222331

223332
function buildHTML(json) {
224333
var image = json[ed.getParam('uploadimage_model', 'image')];
225-
var defaultClass = ed.getParam('uploadimage_default_img_class', '');
226334
var figure = ed.getParam('uploadimage_figure', false);
227335
var altText = getInputValue(inputName('alt'));
228336

229337
var imgstr = "<img src='" + image['url'] + "'";
230338

231-
if (defaultClass !== '') {
232-
imgstr += " class='" + defaultClass + ' ' + selectedClass + "'";
339+
if (defaultClass() !== '') {
340+
imgstr += " class='" + defaultClass() + ' ' + selectedClass + "'";
233341
}
234342

235343
if (image['height']) {
@@ -288,6 +396,10 @@
288396
return null;
289397
}
290398

399+
function defaultClass() {
400+
return ed.getParam('uploadimage_default_img_class', '') + ' ' + PLUGIN_CLASS;
401+
}
402+
291403
function getClassList() {
292404
var config = ed.getParam('image_class_list', []);
293405
var values = [];
@@ -303,14 +415,15 @@
303415
if (editor.getParam('uploadimage', true)) {
304416
// Add a button that opens a window
305417
editor.addButton('uploadimage', {
306-
tooltip: ed.translate('Insert an image from your computer'),
418+
tooltip: ed.translate('Insert/update an image'),
307419
icon: 'image',
308-
onclick: showDialog
420+
onclick: showDialog,
421+
stateSelector: '.' + PLUGIN_CLASS
309422
});
310423

311424
// Adds a menu item to the tools menu
312425
editor.addMenuItem('uploadimage', {
313-
text: ed.translate('Insert an image from your computer'),
426+
text: ed.translate('Insert/update an image'),
314427
icon: 'image',
315428
context: 'insert',
316429
onclick: showDialog

0 commit comments

Comments
 (0)