plugin.js
18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
*
* Version: 5.5.1 (2020-10-01)
*/
(function () {
'use strict';
var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
var noop = function () {
};
var constant = function (value) {
return function () {
return value;
};
};
function curry(fn) {
var initialArgs = [];
for (var _i = 1; _i < arguments.length; _i++) {
initialArgs[_i - 1] = arguments[_i];
}
return function () {
var restArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
restArgs[_i] = arguments[_i];
}
var all = initialArgs.concat(restArgs);
return fn.apply(null, all);
};
}
var never = constant(false);
var always = constant(true);
var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
var global$2 = tinymce.util.Tools.resolve('tinymce.util.XHR');
var getCreationDateClasses = function (editor) {
return editor.getParam('template_cdate_classes', 'cdate');
};
var getModificationDateClasses = function (editor) {
return editor.getParam('template_mdate_classes', 'mdate');
};
var getSelectedContentClasses = function (editor) {
return editor.getParam('template_selected_content_classes', 'selcontent');
};
var getPreviewReplaceValues = function (editor) {
return editor.getParam('template_preview_replace_values');
};
var getContentStyle = function (editor) {
return editor.getParam('content_style', '', 'string');
};
var shouldUseContentCssCors = function (editor) {
return editor.getParam('content_css_cors', false, 'boolean');
};
var getTemplateReplaceValues = function (editor) {
return editor.getParam('template_replace_values');
};
var getTemplates = function (editor) {
return editor.getParam('templates');
};
var getCdateFormat = function (editor) {
return editor.getParam('template_cdate_format', editor.translate('%Y-%m-%d'));
};
var getMdateFormat = function (editor) {
return editor.getParam('template_mdate_format', editor.translate('%Y-%m-%d'));
};
var getBodyClassFromHash = function (editor) {
var bodyClass = editor.getParam('body_class', '', 'hash');
return bodyClass[editor.id] || '';
};
var getBodyClass = function (editor) {
var bodyClass = editor.getParam('body_class', '', 'string');
if (bodyClass.indexOf('=') === -1) {
return bodyClass;
} else {
return getBodyClassFromHash(editor);
}
};
var addZeros = function (value, len) {
value = '' + value;
if (value.length < len) {
for (var i = 0; i < len - value.length; i++) {
value = '0' + value;
}
}
return value;
};
var getDateTime = function (editor, fmt, date) {
var daysShort = 'Sun Mon Tue Wed Thu Fri Sat Sun'.split(' ');
var daysLong = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' ');
var monthsShort = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
var monthsLong = 'January February March April May June July August September October November December'.split(' ');
date = date || new Date();
fmt = fmt.replace('%D', '%m/%d/%Y');
fmt = fmt.replace('%r', '%I:%M:%S %p');
fmt = fmt.replace('%Y', '' + date.getFullYear());
fmt = fmt.replace('%y', '' + date.getYear());
fmt = fmt.replace('%m', addZeros(date.getMonth() + 1, 2));
fmt = fmt.replace('%d', addZeros(date.getDate(), 2));
fmt = fmt.replace('%H', '' + addZeros(date.getHours(), 2));
fmt = fmt.replace('%M', '' + addZeros(date.getMinutes(), 2));
fmt = fmt.replace('%S', '' + addZeros(date.getSeconds(), 2));
fmt = fmt.replace('%I', '' + ((date.getHours() + 11) % 12 + 1));
fmt = fmt.replace('%p', '' + (date.getHours() < 12 ? 'AM' : 'PM'));
fmt = fmt.replace('%B', '' + editor.translate(monthsLong[date.getMonth()]));
fmt = fmt.replace('%b', '' + editor.translate(monthsShort[date.getMonth()]));
fmt = fmt.replace('%A', '' + editor.translate(daysLong[date.getDay()]));
fmt = fmt.replace('%a', '' + editor.translate(daysShort[date.getDay()]));
fmt = fmt.replace('%%', '%');
return fmt;
};
var createTemplateList = function (editor, callback) {
return function () {
var templateList = getTemplates(editor);
if (typeof templateList === 'function') {
templateList(callback);
return;
}
if (typeof templateList === 'string') {
global$2.send({
url: templateList,
success: function (text) {
callback(JSON.parse(text));
}
});
} else {
callback(templateList);
}
};
};
var replaceTemplateValues = function (html, templateValues) {
global$1.each(templateValues, function (v, k) {
if (typeof v === 'function') {
v = v(k);
}
html = html.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
});
return html;
};
var replaceVals = function (editor, e) {
var dom = editor.dom, vl = getTemplateReplaceValues(editor);
global$1.each(dom.select('*', e), function (e) {
global$1.each(vl, function (v, k) {
if (dom.hasClass(e, k)) {
if (typeof vl[k] === 'function') {
vl[k](e);
}
}
});
});
};
var hasClass = function (n, c) {
return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
};
var insertTemplate = function (editor, _ui, html) {
var el;
var dom = editor.dom;
var sel = editor.selection.getContent();
html = replaceTemplateValues(html, getTemplateReplaceValues(editor));
el = dom.create('div', null, html);
var n = dom.select('.mceTmpl', el);
if (n && n.length > 0) {
el = dom.create('div', null);
el.appendChild(n[0].cloneNode(true));
}
global$1.each(dom.select('*', el), function (n) {
if (hasClass(n, getCreationDateClasses(editor).replace(/\s+/g, '|'))) {
n.innerHTML = getDateTime(editor, getCdateFormat(editor));
}
if (hasClass(n, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
n.innerHTML = getDateTime(editor, getMdateFormat(editor));
}
if (hasClass(n, getSelectedContentClasses(editor).replace(/\s+/g, '|'))) {
n.innerHTML = sel;
}
});
replaceVals(editor, el);
editor.execCommand('mceInsertContent', false, el.innerHTML);
editor.addVisual();
};
var register = function (editor) {
editor.addCommand('mceInsertTemplate', curry(insertTemplate, editor));
};
var setup = function (editor) {
editor.on('PreProcess', function (o) {
var dom = editor.dom, dateFormat = getMdateFormat(editor);
global$1.each(dom.select('div', o.node), function (e) {
if (dom.hasClass(e, 'mceTmpl')) {
global$1.each(dom.select('*', e), function (e) {
if (dom.hasClass(e, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
e.innerHTML = getDateTime(editor, dateFormat);
}
});
replaceVals(editor, e);
}
});
});
};
var none = function () {
return NONE;
};
var NONE = function () {
var eq = function (o) {
return o.isNone();
};
var call = function (thunk) {
return thunk();
};
var id = function (n) {
return n;
};
var me = {
fold: function (n, _s) {
return n();
},
is: never,
isSome: never,
isNone: always,
getOr: id,
getOrThunk: call,
getOrDie: function (msg) {
throw new Error(msg || 'error: getOrDie called on none.');
},
getOrNull: constant(null),
getOrUndefined: constant(undefined),
or: id,
orThunk: call,
map: none,
each: noop,
bind: none,
exists: never,
forall: always,
filter: none,
equals: eq,
equals_: eq,
toArray: function () {
return [];
},
toString: constant('none()')
};
return me;
}();
var some = function (a) {
var constant_a = constant(a);
var self = function () {
return me;
};
var bind = function (f) {
return f(a);
};
var me = {
fold: function (n, s) {
return s(a);
},
is: function (v) {
return a === v;
},
isSome: always,
isNone: never,
getOr: constant_a,
getOrThunk: constant_a,
getOrDie: constant_a,
getOrNull: constant_a,
getOrUndefined: constant_a,
or: self,
orThunk: self,
map: function (f) {
return some(f(a));
},
each: function (f) {
f(a);
},
bind: bind,
exists: bind,
forall: bind,
filter: function (f) {
return f(a) ? me : NONE;
},
toArray: function () {
return [a];
},
toString: function () {
return 'some(' + a + ')';
},
equals: function (o) {
return o.is(a);
},
equals_: function (o, elementEq) {
return o.fold(never, function (b) {
return elementEq(a, b);
});
}
};
return me;
};
var from = function (value) {
return value === null || value === undefined ? NONE : some(value);
};
var Optional = {
some: some,
none: none,
from: from
};
var map = function (xs, f) {
var len = xs.length;
var r = new Array(len);
for (var i = 0; i < len; i++) {
var x = xs[i];
r[i] = f(x, i);
}
return r;
};
var findUntil = function (xs, pred, until) {
for (var i = 0, len = xs.length; i < len; i++) {
var x = xs[i];
if (pred(x, i)) {
return Optional.some(x);
} else if (until(x, i)) {
break;
}
}
return Optional.none();
};
var find = function (xs, pred) {
return findUntil(xs, pred, never);
};
var global$3 = tinymce.util.Tools.resolve('tinymce.Env');
var global$4 = tinymce.util.Tools.resolve('tinymce.util.Promise');
var hasOwnProperty = Object.hasOwnProperty;
var get = function (obj, key) {
return has(obj, key) ? Optional.from(obj[key]) : Optional.none();
};
var has = function (obj, key) {
return hasOwnProperty.call(obj, key);
};
var entitiesAttr = {
'"': '"',
'<': '<',
'>': '>',
'&': '&',
'\'': '''
};
var htmlEscape = function (html) {
return html.replace(/["'<>&]/g, function (match) {
return get(entitiesAttr, match).getOr(match);
});
};
var getPreviewContent = function (editor, html) {
if (html.indexOf('<html>') === -1) {
var contentCssEntries_1 = '';
var contentStyle = getContentStyle(editor);
if (contentStyle) {
contentCssEntries_1 += '<style type="text/css">' + contentStyle + '</style>';
}
var cors_1 = shouldUseContentCssCors(editor) ? ' crossorigin="anonymous"' : '';
global$1.each(editor.contentCSS, function (url) {
contentCssEntries_1 += '<link type="text/css" rel="stylesheet" href="' + editor.documentBaseURI.toAbsolute(url) + '"' + cors_1 + '>';
});
var bodyClass = getBodyClass(editor);
var encode = editor.dom.encode;
var isMetaKeyPressed = global$3.mac ? 'e.metaKey' : 'e.ctrlKey && !e.altKey';
var preventClicksOnLinksScript = '<script>' + 'document.addEventListener && document.addEventListener("click", function(e) {' + 'for (var elm = e.target; elm; elm = elm.parentNode) {' + 'if (elm.nodeName === "A" && !(' + isMetaKeyPressed + ')) {' + 'e.preventDefault();' + '}' + '}' + '}, false);' + '</script> ';
var directionality = editor.getBody().dir;
var dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
html = '<!DOCTYPE html>' + '<html>' + '<head>' + '<base href="' + encode(editor.documentBaseURI.getURI()) + '">' + contentCssEntries_1 + preventClicksOnLinksScript + '</head>' + '<body class="' + encode(bodyClass) + '"' + dirAttr + '>' + html + '</body>' + '</html>';
}
return replaceTemplateValues(html, getPreviewReplaceValues(editor));
};
var open = function (editor, templateList) {
var createTemplates = function () {
if (!templateList || templateList.length === 0) {
var message = editor.translate('No templates defined.');
editor.notificationManager.open({
text: message,
type: 'info'
});
return Optional.none();
}
return Optional.from(global$1.map(templateList, function (template, index) {
var isUrlTemplate = function (t) {
return t.url !== undefined;
};
return {
selected: index === 0,
text: template.title,
value: {
url: isUrlTemplate(template) ? Optional.from(template.url) : Optional.none(),
content: !isUrlTemplate(template) ? Optional.from(template.content) : Optional.none(),
description: template.description
}
};
}));
};
var createSelectBoxItems = function (templates) {
return map(templates, function (t) {
return {
text: t.text,
value: t.text
};
});
};
var findTemplate = function (templates, templateTitle) {
return find(templates, function (t) {
return t.text === templateTitle;
});
};
var loadFailedAlert = function (api) {
editor.windowManager.alert('Could not load the specified template.', function () {
return api.focus('template');
});
};
var getTemplateContent = function (t) {
return new global$4(function (resolve, reject) {
t.value.url.fold(function () {
return resolve(t.value.content.getOr(''));
}, function (url) {
return global$2.send({
url: url,
success: function (html) {
resolve(html);
},
error: function (e) {
reject(e);
}
});
});
});
};
var onChange = function (templates, updateDialog) {
return function (api, change) {
if (change.name === 'template') {
var newTemplateTitle = api.getData().template;
findTemplate(templates, newTemplateTitle).each(function (t) {
api.block('Loading...');
getTemplateContent(t).then(function (previewHtml) {
updateDialog(api, t, previewHtml);
}).catch(function () {
updateDialog(api, t, '');
api.disable('save');
loadFailedAlert(api);
});
});
}
};
};
var onSubmit = function (templates) {
return function (api) {
var data = api.getData();
findTemplate(templates, data.template).each(function (t) {
getTemplateContent(t).then(function (previewHtml) {
insertTemplate(editor, false, previewHtml);
api.close();
}).catch(function () {
api.disable('save');
loadFailedAlert(api);
});
});
};
};
var openDialog = function (templates) {
var selectBoxItems = createSelectBoxItems(templates);
var buildDialogSpec = function (bodyItems, initialData) {
return {
title: 'Insert Template',
size: 'large',
body: {
type: 'panel',
items: bodyItems
},
initialData: initialData,
buttons: [
{
type: 'cancel',
name: 'cancel',
text: 'Cancel'
},
{
type: 'submit',
name: 'save',
text: 'Save',
primary: true
}
],
onSubmit: onSubmit(templates),
onChange: onChange(templates, updateDialog)
};
};
var updateDialog = function (dialogApi, template, previewHtml) {
var content = getPreviewContent(editor, previewHtml);
var bodyItems = [
{
type: 'selectbox',
name: 'template',
label: 'Templates',
items: selectBoxItems
},
{
type: 'htmlpanel',
html: '<p aria-live="polite">' + htmlEscape(template.value.description) + '</p>'
},
{
label: 'Preview',
type: 'iframe',
name: 'preview',
sandboxed: false
}
];
var initialData = {
template: template.text,
preview: content
};
dialogApi.unblock();
dialogApi.redial(buildDialogSpec(bodyItems, initialData));
dialogApi.focus('template');
};
var dialogApi = editor.windowManager.open(buildDialogSpec([], {
template: '',
preview: ''
}));
dialogApi.block('Loading...');
getTemplateContent(templates[0]).then(function (previewHtml) {
updateDialog(dialogApi, templates[0], previewHtml);
}).catch(function () {
updateDialog(dialogApi, templates[0], '');
dialogApi.disable('save');
loadFailedAlert(dialogApi);
});
};
var optTemplates = createTemplates();
optTemplates.each(openDialog);
};
var showDialog = function (editor) {
return function (templates) {
open(editor, templates);
};
};
var register$1 = function (editor) {
editor.ui.registry.addButton('template', {
icon: 'template',
tooltip: 'Insert template',
onAction: createTemplateList(editor, showDialog(editor))
});
editor.ui.registry.addMenuItem('template', {
icon: 'template',
text: 'Insert template...',
onAction: createTemplateList(editor, showDialog(editor))
});
};
function Plugin () {
global.add('template', function (editor) {
register$1(editor);
register(editor);
setup(editor);
});
}
Plugin();
}());