-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap4-wyswyg.js
753 lines (713 loc) · 40.4 KB
/
bootstrap4-wyswyg.js
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*!
* Bootstrap4 wyswyg 1.0
* By leefine chan
*/
(function (window, $) {
"use strict";
function Wysiwyg(element, userOptions) {
this.selectedRange = null;
this.editor = $(element);
var editor = $(element);
var defaults = {
toolbarSelector: "[data-role=editor-toolbar]",
commandRole: "edit",
activeToolbarClass: "active",
selectionMarker: "edit-focus-marker",
selectionColor: "blue",
dragAndDropImages: true,
upload_img_type: 'bmp|gif|jpg|jpeg|png',
upload_doc_type: 'doc|docx|xls|xlsx|ppt|pptx|ppsx|pdf|zip',
upload_video_type: 'mp4',
upload_file_max_size_m: 10,
fileUploadError: function (reason, detail) {
console.log("File upload error", reason, detail);
}
};
var options = $.extend(true, {}, defaults, userOptions);
var toolbarBtnSelector = "a[data-" + options.commandRole + "],button[data-" + options.commandRole + "],input[type=button][data-" + options.commandRole + "]";
var toolbar = $(options.toolbarSelector);
this.bindToolbar(editor, toolbar, options, toolbarBtnSelector);
if (options.dragAndDropImages) {
this.initFileDrops(editor, toolbar, options, toolbarBtnSelector);
}
editor.attr({"contenteditable": true, "designMode": 'On'})
.on("mouseup mouseout keyup", function () {
this.saveSelection();
this.updateToolbar(editor, toolbarBtnSelector, options);
}.bind(this))
.on('keydown', function (e) {
if (e.keyCode === 13) {
document.execCommand('insertHTML', false, '<br/><br/>');
return false;
}
}.bind(this));
}
Wysiwyg.prototype.readFileIntoDataUrl = function (fileInfo) {
var loader = $.Deferred(),
fReader = new FileReader();
fReader.onload = function (e) {
loader.resolve(e.target.result);
};
fReader.onerror = loader.reject;
fReader.onprogress = loader.notify;
fReader.readAsDataURL(fileInfo);
return loader.promise();
};
Wysiwyg.prototype.updateToolbar = function (editor, toolbarBtnSelector, options) {
if (options.activeToolbarClass) {
$(options.toolbarSelector).find(toolbarBtnSelector).each(function () {
var self = $(this);
var commandArr = self.data(options.commandRole).split(" ");
var command = commandArr[0];
// If the command has an argument and its value matches this button. == used for string/number comparison
if (commandArr.length > 1 && document.queryCommandEnabled(command) && document.queryCommandValue(command) === commandArr[1]) {
self.addClass(options.activeToolbarClass);
}
// Else if the command has no arguments and it is active
else if (commandArr.length === 1 && document.queryCommandEnabled(command) && document.queryCommandState(command)) {
self.addClass(options.activeToolbarClass);
}
// Else the command is not active
else {
self.removeClass(options.activeToolbarClass);
}
});
}
};
Wysiwyg.prototype.execCommand = function (commandWithArgs, valueArg, editor, options, toolbarBtnSelector) {
if (commandWithArgs == null || commandWithArgs == '')return;
var commandArr = commandWithArgs.split(" "), command = commandArr.shift(),
args = commandArr.join(" ") + ( valueArg || "" );
//var parts = commandWithArgs.split("-");
//if (parts.length === 1) {
document.execCommand(command, false, args);
/*} else if (parts[0] === "format" && parts.length === 2) {
document.execCommand("formatBlock", false, parts[1]);
}*/
( editor ).trigger("change");
this.updateToolbar(editor, toolbarBtnSelector, options);
};
Wysiwyg.prototype.getCurrentRange = function () {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
}
} else if (document.selection) {
range = document.selection.createRange();
}
return range;
};
Wysiwyg.prototype.saveSelection = function () {
this.selectedRange = this.getCurrentRange();
};
Wysiwyg.prototype.restoreSelection = function () {
var selection;
if (window.getSelection || document.createRange) {
selection = window.getSelection();
if (this.selectedRange) {
try {
selection.removeAllRanges();
}
catch (ex) {
document.body.createTextRange().select();
document.selection.empty();
}
selection.addRange(this.selectedRange);
}
} else if (document.selection && this.selectedRange) {
this.selectedRange.select();
}
};
Wysiwyg.prototype.insertFiles = function (files, fileInputName, options, editor, toolbar, toolbarBtnSelector, fileSelector) {
var self = this;
var imgReg = new RegExp(options.upload_img_type);
var docReg = new RegExp(options.upload_doc_type);
var videoReg = new RegExp(options.upload_video_type);
var checkFile = true;
$.each(files, function (idx, fileInfo) {
if (fileInfo.size > (options.upload_file_max_size_m * 1024 * 1024)) {
$.fn_ex.alert(fileInfo.name + ' is too big,max size is ' + options.upload_file_max_size_m + 'M');
return;
}
var eMsg = "";
if (fileInputName == 'insertImage') {
if (!(/^image\//.test(fileInfo.type) && imgReg.test(fileInfo.name.toLowerCase()))) {
eMsg = options.upload_img_type;
$.fn_ex.alert('Unsupported file type! Please upload type:' + eMsg);
checkFile = false;
return false;
}
}
if (fileInputName == 'insertFile') {
if (!(/^application\//.test(fileInfo.type) && docReg.test(fileInfo.name.toLowerCase()))) {
eMsg = options.upload_doc_type;
$.fn_ex.alert('Unsupported file type! Please upload type:' + eMsg);
checkFile = false;
return false;
}
}
if (fileInputName == 'insertVideo') {
if (!(/^video\//.test(fileInfo.type) && videoReg.test(fileInfo.name.toLowerCase()))) {
eMsg = options.upload_video_type;
$.fn_ex.alert('Unsupported file type! Please upload type:' + eMsg);
checkFile = false;
return false;
}
}
}
);
if (!checkFile)return;
if (fileInputName == 'insertFile' && fileSelector !== undefined) {//大文件通过按钮上传
// input:file value不可以动态赋值,如何处理???
// $(editor).after("<input value='" + $(fileSelector).val() + "' type='file' name='" + fileInfo.name + "'/>");
//移动选中的file并从新复制一个file到选择框
var uuid = $.fn_ex.guid()
if (files.length > 1) {
$(editor).after(fileSelector);
$('#attachFileBtn' + $(editor).attr("id")).prepend($(fileSelector).clone());
$(fileSelector).removeAttr('data-role data-edit data-target');
$(fileSelector).attr("name", uuid);
$.each(files, function (idx, fileInfo) {
var mimeType = fileInfo.name.substring(fileInfo.name.lastIndexOf("."), fileInfo.name.length);
document.execCommand("insertHTML", false, '<a href="' + options.upload_file_dir + uuid + '_' + idx + mimeType + '" class="icon-file text-danger">' + fileInfo.name + '</a><br/><br/>');
});
} else {
var mimeType = files[0].name.substring(files[0].name.lastIndexOf("."), files[0].name.length);
$(editor).after(fileSelector);
$('#attachFileBtn' + $(editor).attr("id")).prepend($(fileSelector).clone());
$(fileSelector).removeAttr('data-role data-edit data-target');
$(fileSelector).attr("name", uuid);
document.execCommand("insertHTML", false, '<a href="' + options.upload_file_dir + uuid + mimeType + '" class="icon-file text-danger">' + files[0].name + '</a><br/><br/>');
}
self.bindFileSelect(editor, toolbar, options, toolbarBtnSelector)
}
else if (fileInputName == 'insertVideo' && fileSelector !== undefined) {//大视频文件通过按钮上传
// input:file value不可以动态赋值,如何处理???
// $(editor).after("<input value='" + $(fileSelector).val() + "' type='file' name='" + fileInfo.name + "'/>");
//移动选中的file并从新复制一个file到选择框
var uuid = $.fn_ex.guid();
if (files.length > 1) {
$(editor).after(fileSelector);
$('#attachVideoBtn' + $(editor).attr("id")).prepend($(fileSelector).clone());
$(fileSelector).removeAttr('data-role data-edit data-target');
$(fileSelector).attr("name", uuid);
$.each(files, function (idx, fileInfo) {
var mimeType = fileInfo.name.substring(fileInfo.name.lastIndexOf("."), fileInfo.name.length);
document.execCommand("insertHTML", false, '<video src="' + options.upload_file_dir + uuid + '_' + idx + mimeType + '" controls></video><br/><br/>');
});
} else {
var mimeType = files[0].name.substring(files[0].name.lastIndexOf("."), files[0].name.length);
$(editor).after(fileSelector);
$('#attachVideoBtn' + $(editor).attr("id")).prepend($(fileSelector).clone());
$(fileSelector).removeAttr('data-role data-edit data-target');
$(fileSelector).attr("name", uuid);
document.execCommand("insertHTML", false, '<video src="' + options.upload_file_dir + uuid + mimeType + '" controls></video><br/><br/>');
}
self.bindFileSelect(editor, toolbar, options, toolbarBtnSelector)
}
else {//文件视频拖动不能大于2M (保证速度), 图片上传及拖动
$.each(files, function (idx, fileInfo) {
if (fileInfo.size > (2 * 1024 * 1024) && fileInputName == 'drag') {
$.fn_ex.alert('The file you draging cant be bigger than 2M!');
return false;
}
if (/^application\//.test(fileInfo.type) && docReg.test(fileInfo.name.toLowerCase())) {
$.when(self.readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
var uuid = $.fn_ex.guid();
var fileName = options.upload_file_dir + uuid + fileInfo.name.substring(fileInfo.name.lastIndexOf("."), fileInfo.name.length);
document.execCommand("insertHTML", false, '<a href="' + fileName + '" class="icon-file text-danger">' + fileInfo.name + '</a><br/><br/>');
$(editor).after("<input value='" + uuid + "~" + dataUrl + "' type='hidden' name='post_file'/>");
}).fail(function (e) {
$.fn_ex.alert('Read file failed');
options.fileUploadError("file-reader", e);
});
}
if (/^image\//.test(fileInfo.type) && imgReg.test(fileInfo.name.toLowerCase())) {
$.when(self.readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
self.execCommand("insertimage", dataUrl, editor, options, toolbarBtnSelector);
editor.trigger("image-inserted");
}).fail(function (e) {
$.fn_ex.alert('Read file failed');
options.fileUploadError("file-reader", e);
});
}
if (/^video\//.test(fileInfo.type) && videoReg.test(fileInfo.name.toLowerCase())) {
$.when(self.readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
document.execCommand("insertHTML", false, '<video src="' + dataUrl + '" controls></video><br/><br/>');
}).fail(function (e) {
$.fn_ex.alert('Read file failed');
options.fileUploadError("file-reader", e);
});
}
});
}
};
Wysiwyg.prototype.markSelection = function (color, options) {
this.restoreSelection();
if (document.queryCommandSupported("hiliteColor")) {
document.execCommand("hiliteColor", false, color || "transparent");
}
this.saveSelection();
};
//Move selection to a particular element
function selectElementContents(element) {
if (window.getSelection && document.createRange) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(element);
textRange.select();
}
}
Wysiwyg.prototype.bindToolbar = function (editor, toolbar, options, toolbarBtnSelector) {
var self = this;
toolbar.find(toolbarBtnSelector).click(function () {
self.restoreSelection();
editor.focus();
if ($(this).data(options.commandRole) == "html") {
self.toggleHtmlEdit(editor);
} else {
self.execCommand($(this).data(options.commandRole), null, editor, options, toolbarBtnSelector);
}
self.saveSelection();
});
toolbar.find("[data-toggle=dropdown]").on('click', (function () {
self.markSelection(options.selectionColor, options);
}));
toolbar.on("hide.bs.dropdown", function () {
self.markSelection(false, options);
});
toolbar.find("input[type=text][data-" + options.commandRole + "]").on("webkitspeechchange change", function () {
var newValue = $(this).val(); // Ugly but prevents fake double-calls due to selection restoration
$(this).val('');//.value = "";
self.restoreSelection();
var text = window.getSelection();
if (text.toString().trim() === '' && newValue) {
//create selection if there is no selection
self.editor.append('<span>' + newValue + '</span>');
selectElementContents($('span:last', self.editor)[0]);
}
if (newValue) {
editor.focus();
self.execCommand($(this).data(options.commandRole), newValue, editor, options, toolbarBtnSelector);
}
self.saveSelection();
}).on("blur", function () {
var input = $(this);
self.markSelection(false, options);
});
toolbar.find(".colorSelector").on("click", function () {
var cmdBtn = $(this).data(options.commandRole);
self.restoreSelection();
if (cmdBtn == ('hiliteColor') || cmdBtn == 'backColor') {
document.execCommand(cmdBtn, false, $(this).attr('data-color'));
return;
}
else if (cmdBtn == 'foreColor') {
document.execCommand(cmdBtn, false, $(this).attr('data-color'));
self.saveSelection();
}
else {
return;
}
}).on("blur", function () {
var input = $(this);
self.markSelection(false, options);
});
self.bindFileSelect(editor, toolbar, options, toolbarBtnSelector);
};
Wysiwyg.prototype.bindFileSelect = function (editor, toolbar, options, toolbarBtnSelector) {
var self = this;
toolbar.find("input[type=file][data-" + options.commandRole + "]").change(function () {
self.restoreSelection();
if (this.type === "file" && this.files && this.files.length > 0) {
self.insertFiles(this.files, $(this).attr("data-" + options.commandRole), options, editor, toolbar, toolbarBtnSelector, this);
}
self.saveSelection();
});
};
Wysiwyg.prototype.initFileDrops = function (editor, toolbar, options, toolbarBtnSelector) {
var self = this;
editor.on("dragenter dragover", false).on("drop", function (e) {
var dataTransfer = e.originalEvent.dataTransfer;
e.stopPropagation();
e.preventDefault();
if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
self.insertFiles(dataTransfer.files, 'drag', options, editor, toolbar, toolbarBtnSelector);
}
});
};
/*
* Represenets an editor
* @constructor
* @param {object} userOptions - The default options selected by the user.
*/
$.fn.wysiwyg = function (userOptions) {
var wysiwyg = new Wysiwyg(this, userOptions);
};
/* $.fn.wysiwyg.getHtml = function (container, options) {
if (options.base64_to_file !== undefined && options.base64_to_file == true) {
var gGal = $.parseHTML('<div>' + $(container).html() + '</div>');
if ($(gGal).has("img").length) {
var gImages = $("img", $(gGal));
$.each(gImages, function (i, v) {
if ($(v).attr("src").match(/^data:image\/.*$/)) {
var uuid = $.fn.wysiwyg.guid();
$(container).after("<input value='" + uuid + "~" + $(v).attr("src") + "' type='hidden' name='post_img'/>");
var picName = uuid + '.' + $(v).attr("src").substring($(v).attr("src").indexOf("/") + 1, $(v).attr("src").indexOf(";"));//后缀名
$(v).attr("src", options.upload_img_dir + picName);
}
});
}
if ($(gGal).has("video").length) {
var gImages = $("video", $(gGal));
$.each(gImages, function (i, v) {
if ($(v).attr("src").match(/^data:video\/.*$/)) {
var uuid = $.fn.wysiwyg.guid();
$(container).after("<input value='" + uuid + "~" + $(v).attr("src") + "' type='hidden' name='post_file'/>");
var videoName = uuid + '.' + $(v).attr("src").substring($(v).attr("src").indexOf("/") + 1, $(v).attr("src").indexOf(";"));//后缀名
$(v).attr("src", options.upload_file_dir + videoName);
}
});
}
return $(gGal).html();
}
else {
return $(container).html();
}
};*/
$.fn.getWysiwygHtml = function (options) {
var container = this;
if (options.base64_to_file !== undefined && options.base64_to_file == true) {
var gGal = $.parseHTML('<div>' + $(container).html() + '</div>');
if ($(gGal).has("img").length) {
var gImages = $("img", $(gGal));
$.each(gImages, function (i, v) {
if ($(v).attr("src").match(/^data:image\/.*$/)) {
var uuid = $.fn_ex.guid();
$(container).after("<input value='" + uuid + "~" + $(v).attr("src") + "' type='hidden' name='post_img'/>");
var picName = uuid + '.' + $(v).attr("src").substring($(v).attr("src").indexOf("/") + 1, $(v).attr("src").indexOf(";"));//后缀名
$(v).attr("src", options.upload_img_dir + picName);
}
});
}
if ($(gGal).has("video").length) {
var gImages = $("video", $(gGal));
$.each(gImages, function (i, v) {
if ($(v).attr("src").match(/^data:video\/.*$/)) {
var uuid = $.fn_ex.guid();
$(container).after("<input value='" + uuid + "~" + $(v).attr("src") + "' type='hidden' name='post_file'/>");
var videoName = uuid + '.' + $(v).attr("src").substring($(v).attr("src").indexOf("/") + 1, $(v).attr("src").indexOf(";"));//后缀名
$(v).attr("src", options.upload_file_dir + videoName);
}
});
}
return $(gGal).html();
}
else {
return $(container).html();
}
};
/***
* 初始化编辑器
* @param ifImgConvert 是否转换Base64图片
*/
$.fn.initWysiwyg = function (userOptions) {
if (userOptions.editor_height === undefined) userOptions.editor_height = "300";
if (userOptions.upload_file_dir === undefined || userOptions.upload_img_dir === undefined) {
alert("userOptions.upload_file_dir or userOptions.upload_img_dir can't be empty!");
return;
}
var getColors = function (t) {
var clr = new Array('00', '50', 'A0', 'FF'),
colorsSelector = "";
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
colorsSelector += "<div class='colorSelector' data-edit='" + t + "' data-color='#" + clr[3 - i] + clr[3 - j] + clr[3 - k] + "' style='background-color: #" + clr[3 - i] + clr[3 - j] + clr[3 - k] + "'></div>";
}
}
}
return colorsSelector;
};
var editorID = $(this).attr("id") + "Editor", thisInput = $(this);
var toolBar = [
{
"btns": [
{"ico": "󰫙", "title": "Undo", "dataedit": "undo"},
{"ico": "󰫰", "title": "Redo", "dataedit": "redo"}
]
},
{
"btns": [
{"ico": "󰢛", "title": "Bold", "dataedit": "bold"},
{"ico": "󰢜", "title": "Italic", "dataedit": "italic"},
{"ico": "󰜵", "title": "Strikethrough", "dataedit": "strikethrough"},
{"ico": "󰮸", "title": "Underline", "dataedit": "underline"},
{"ico": "󰠉", "title": "Horizontal", "dataedit": "insertHorizontalRule"}
]
},
{
"dmenu": {
"ico": "󰜬", "title": "Font",
"dropdownMenu": [
{"dataedit": "fontName Serif", "style": "display: block;font-family:'Serif'", "tit": "Serif"},
{"dataedit": "fontName Arial", "style": "display: block;font-family:'Arial'", "tit": "Arial"},
{
"dataedit": "fontName Impact",
"style": "display: block;font-family:'Impact'",
"tit": "Impact"
},
{
"dataedit": "fontName Arial Black",
"style": "display: block;font-family:'Arial Black'",
"tit": "Arial Black"
},
{
"dataedit": "fontName Helvetica",
"style": "display: block;font-family:'Helvetica'",
"tit": "Helvetica"
},
{
"dataedit": "fontName Sans-serif",
"style": "display: block;font-family:'Sans-serif'",
"tit": "Sans-serif"
},
{
"dataedit": "fontName verdana",
"style": "display: block;font-family:'verdana'",
"tit": "verdana"
}
]
}
},
{
"strHtml": '<a class="btn btn-outline-info dropdown-toggle" data-toggle="dropdown" title="Fore Color"><i data-icon="󰢚" class="text-success"></i></a><div class="dropdown-menu dropdown-menu-right" style="width: 200px;">' + getColors('foreColor') + '</div>'
},
{
"strHtml": '<a class="btn btn-outline-info dropdown-toggle bg-success" data-toggle="dropdown" title="Back Color"><i data-icon="󰢚"></i></a><div class="dropdown-menu dropdown-menu-right" style="width: 200px;">' + getColors('hiliteColor') + '</div>'
},
{
"dmenu": {
"ico": "󰜱", "title": "Font Size",
"dropdownMenu": [
{"dataedit": "fontSize 3", "style": "display: block", "tit": "<font size=\"3\">size 3</font>"},
{"dataedit": "fontSize 4", "style": "display: block", "tit": "<font size=\"4\">size 4</font>"},
{"dataedit": "fontSize 5", "style": "display: block", "tit": "<font size=\"5\">size 5</font>"},
{"dataedit": "fontSize 6", "style": "display: block", "tit": "<font size=\"6\">size 6</font>"},
{"dataedit": "fontSize 7", "style": "display: block", "tit": "<font size=\"7\">size 7</font>"}
]
}
},
{
"dmenu": {
"ico": "H1", "title": "Heading",
"dropdownMenu": [
{"dataedit": "formatBlock p", "style": "display: block", "tit": "<h1>P</h1>"},
{"dataedit": "formatBlock h1", "style": "display: block", "tit": "<h1>h1</h1>"},
{"dataedit": "formatBlock h2", "style": "display: block", "tit": "<h2>h2</h2>"},
{"dataedit": "formatBlock h3", "style": "display: block", "tit": "<h3>h3</h3>"},
{"dataedit": "formatBlock h4", "style": "display: block", "tit": "<h4>h4</h4>"},
{"dataedit": "formatBlock h5", "style": "display: block", "tit": "<h5>h5</h5>"}
]
}
},
{
"btns": [
{"ico": "󰠝", "title": "Bullet list", "dataedit": "insertunorderedlist"},
{"ico": "󰠜", "title": "Number list", "dataedit": "insertorderedlist"},
{"ico": "󰝌", "title": "Reduce indent", "dataedit": "outdent"},
{"ico": "󰝋", "title": "Indent", "dataedit": "indent"}
]
},
{
"btns": [
{"ico": "󰝇", "title": "Align Left", "dataedit": "justifyleft"},
{"ico": "󰝈", "title": "Center", "dataedit": "justifycenter"},
{"ico": "󰝉", "title": "Align Right", "dataedit": "justifyright"},
{"ico": "󰝊", "title": "Justify", "dataedit": "justifyfull"}
]
},
{
"btns": [
{"ico": "󰓟", "title": "RemoveFormat", "dataedit": "removeFormat"},
{"ico": "󰥳", "title": "Remove Hyperlink", "dataedit": "unlink"}
]
},
{
"strHtml": '<a class="btn btn-outline-info dropdown-toggle" data-toggle="dropdown" title="Hyperlink"><i data-icon="󰴭"></i></a><div class="dropdown-menu dropdown-menu-right" style="width: 300px;"><div class="input-group p-xl-2"> <input placeholder="URL" type="text" data-edit="createLink" class="form-control"/><div class="input-group-append"> <button class="btn-info" type="button">Add</button></div> </div></div>'
},
{
"strHtml": '<a class="btn btn-outline-info" title="Insert Table" id="insertTable' + editorID + '"><i data-icon="󰝀"></i></a>'
},
{
"strHtml": '<span class="btn btn-outline-info" title="Insert picture (or just drag & drop)" id="pictureBtn' + editorID + '"><input style="z-index:10;width: 2rem;height:2rem;position: absolute;left: 0px;top: 0px; overflow: hidden;font-size: 100px;filter: alpha(opacity=0);opacity: 0;" type="file" accept="image/*" data-role="magic-overlay" multiple="multiple" data-target="#pictureBtn' + editorID + '" data-edit="insertImage"/><i data-icon="󰪀" style="z-index: 10"></i></span>'
},
{
"strHtml": '<span class="btn btn-outline-info" title="Attach file (or just drag & drop)" id="attachFileBtn' + editorID + '"><input style="z-index:10;width: 2rem;height:2rem;position: absolute;left: 0px;top: 0px; overflow: hidden;font-size: 100px;filter: alpha(opacity=0);opacity: 0;" type="file" data-role="magic-overlay" multiple="multiple" data-target="#attachFileBtn' + editorID + '" data-edit="insertFile"/><i data-icon="󰃀" style="z-index: 10"></i></span>'
},
{
"strHtml": '<span class="btn btn-outline-info" title="Attach video (or just drag & drop)" id="attachVideoBtn' + editorID + '"><input style="z-index:10;width: 2rem;height:2rem;position: absolute;left: 0px;top: 0px; overflow: hidden;font-size: 100px;filter: alpha(opacity=0);opacity: 0;" type="file" data-role="magic-overlay" multiple="multiple" data-target="#attachVideoBtn' + editorID + '" data-edit="insertVideo"/><i data-icon="󰗣" style="z-index: 10"></i></span>'
},
{
"strHtml": '<span title="Html" class="btn btn-info" data-htmlview="' + editorID + '">Html</span>'
},
{
"strHtml": '<a class="btn btn-outline-info" title="Full Screen" data-fullscreen="' + editorID + '"><i data-icon="󰅗"></i></a>'
}
];
var toolBarHtml = "<style> #" + editorID + "{padding: 4px} .btn-toolbar ul li{cursor: pointer;}.colorSelector{height: 20px;width: 20px;margin: 2px;float: left;} .btn-toolbar .btn{padding: 0.2rem 0.4rem;font-size: 0.75rem;height: 1.6rem;}</style>";
toolBarHtml += '<div id="' + editorID + 'Wrap" style="height:' + userOptions.editor_height + 'px;width:100%;z-index:1050;left:0;top:0"><div id="' + editorID + 'ToolBar" class="btn-toolbar w-100 bg-light p-2 border border-bottom-0 rounded-top" data-role="editor-toolbar" data-target="#' + editorID + '">';
for (var i in toolBar) {
toolBarHtml += ' <div class="btn-group btn-group-sm mr-1">';
if (toolBar[i].btns !== undefined)
for (var j in toolBar[i].btns) {
toolBarHtml += ' <a class="btn btn-outline-info" data-edit="' + toolBar[i].btns[j].dataedit + '" title="' + toolBar[i].btns[j].title + '"><i data-icon="' + toolBar[i].btns[j].ico + '"></i></a>';
}
if (toolBar[i].dmenu !== undefined) {
toolBarHtml += ' <a class="btn btn-outline-info dropdown-toggle" data-toggle="dropdown" title="' + toolBar[i].dmenu.title + '"><i data-icon="' + toolBar[i].dmenu.ico + '"></i><b class="caret"></b></a>';
toolBarHtml += ' <ul class="dropdown-menu">';
for (var k in toolBar[i].dmenu.dropdownMenu) {
if (toolBar[i].dmenu.dropdownMenu[k].dataedit != null && toolBar[i].dmenu.dropdownMenu[k].dataedit != '')
toolBarHtml += '<li><a data-edit="' + toolBar[i].dmenu.dropdownMenu[k].dataedit + '" style="' + toolBar[i].dmenu.dropdownMenu[k].style + '">' + toolBar[i].dmenu.dropdownMenu[k].tit + '</a></li>';
else
toolBarHtml += '<li><a style="' + toolBar[i].dmenu.dropdownMenu[k].style + '">' + toolBar[i].dmenu.dropdownMenu[k].tit + '</a></li>';
}
toolBarHtml += '</ul>';
}
if (toolBar[i].strHtml !== undefined) {
toolBarHtml += toolBar[i].strHtml;
}
toolBarHtml += '</div>';
}
toolBarHtml += '</div>';
toolBarHtml += '<div id="' + editorID + '" data-editor="' + editorID + '" class="ignore lead bg-white w-100 border border-top-0 rounded-bottom" data-placeholder="Enter here..." style="overflow: scroll; ">' + $(this).val() + '</div>';
toolBarHtml += '<textarea id="' + editorID + 'Html" class="bg-white w-100 border border-top-0 rounded-bottom d-none"></textarea>';
toolBarHtml += '</div>';
$(this).before(toolBarHtml);
$('#' + editorID).wysiwyg(userOptions);
$("#" + editorID).height(($("#" + editorID + "Wrap").height() - 50) + 'px');
$("#" + editorID + "Html").height(($("#" + editorID + "Wrap").height() - 50) + 'px');
$(document).on("click", "#insertTable" + editorID, function () {
alertify.confirm("Table propertes", "Rows:<input type='number' id='" + editorID + "TableRows' value='3' class='form-control' />" +
"Colums:<input type='number' id='" + editorID + "TableColums' value='3' class='form-control' />" +
"Width:<div class='input-group'><input id='" + editorID + "TableWidth' type='number' value='100' class='form-control'/><select id='" + editorID + "TableWidthUnit' class='form-control'><option value='%'>%</option><option value='px'>px</option></select></div>" +
"Border:<div class='input-group'><input id='" + editorID + "TableBorder' type='number' class='form-control' value='1'/><input id='" + editorID + "TableBorderColor' type='color' class='form-control' value='#9a9a9a'/></div>" +
"Row height(px):<input id='" + editorID + "TableRowHeight' class='form-control' type='number' value='30'/>",
function () {
var tabelRows = parseInt($("#" + editorID + "TableRows").val()),
tabelColums = parseInt($("#" + editorID + "TableColums").val()),
tabelWidth = parseInt($("#" + editorID + "TableWidth").val()),
tableWidthUnit = $("#" + editorID + "TableWidthUnit").val(),
tableBorder = parseInt($("#" + editorID + "TableBorder").val()),
tableBorderColor = $("#" + editorID + "TableBorderColor").val(),
tableRowHeight = parseInt($("#" + editorID + "TableRowHeight").val());
var tableHtml = '<table style="width:' + tabelWidth + tableWidthUnit + '" border="' + tableBorder + 'px" bordercolor="' + tableBorderColor + '">';
for (var x = 0; x < tabelRows; x++) {
tableHtml += '<tr style="height: ' + tableRowHeight + 'px">';
for (var y = 0; y < tabelColums; y++) {
tableHtml += '<td border="' + tableBorder + 'px" bordercolor="' + tableBorderColor + '"></td>';
}
tableHtml += '</tr>';
}
$('#' + editorID).append(tableHtml);
}, function () {
});
});
$('#' + editorID).focus();
$($(this).parents('form')[0]).submit(function (e) {
$(thisInput).val($('#' + editorID).getWysiwygHtml(userOptions));
});
$(document).on("dblclick", "#" + editorID + " a", function () {
var url = $(this).attr("href");
var obj = $(this);
alertify.prompt('URL', '', url
, function (evt, value) {
$(obj).attr("href", value);
}
, function () {
}
);
});
$(document).on("dblclick", "#" + editorID + " img", function () {
var obj = $(this);
var widthOfImg = $(this).width();
var heightOfImg = $(this).height();
alertify.confirm("Image propertes", "Width:<input type='range' id='" + editorID + "ImgWidth' min='1' max='2000' value='" + widthOfImg + "' class='form-control' />" +
"Height:<input type='range' id='" + editorID + "ImgHeight' min='1' max='2000' value='" + heightOfImg + "' class='form-control' />" +
"Position:<select id='" + editorID + "ImgPos' class='form-control'><option value='float-none'>float none</option><option value='mx-auto d-block'>center</option><option value='float-left'>float left</option><option value='float-right'>float right</option></select>",
function () {
$(obj).height($("#" + editorID + "ImgHeight").val());
$(obj).width($("#" + editorID + "ImgWidth").val());
$(obj).removeClass();
$(obj).addClass($("#" + editorID + "ImgPos").val());
}, function () {
});
});
$(document).on("mouseenter", "#" + editorID + " img,#" + editorID + " video", function () {
$(this).draggable({axis: "x", containment: "#" + editorID});
}).on("mouseleave", "#" + editorID + " img", function () {
$(this).draggable("destroy");
});
$(document).on("click", "#" + editorID + " video", function () {
var obj = $(this);
var widthOfVideo = $(this).width();
var heightOfVideo = $(this).height();
alertify.confirm("Video propertes", "Width:<input type='range' id='" + editorID + "VideoWidth' min='1' max='2000' value='" + widthOfVideo + "' class='form-control' />" +
"Height:<input type='range' id='" + editorID + "VideoHeight' min='1' max='2000' value='" + heightOfVideo + "' class='form-control' />" +
"Position:<select id='" + editorID + "VideoPos' class='form-control'><option value='float-none'>float none</option><option value='mx-auto d-block'>center</option><option value='float-left'>float left</option><option value='float-right'>float right</option></select>",
function () {
$(obj).height($("#" + editorID + "VideoHeight").val());
$(obj).width($("#" + editorID + "VideoWidth").val());
$(obj).removeClass();
$(obj).addClass($("#" + editorID + "VideoPos").val());
}, function () {
});
});
$(document).on("click", "[data-fullscreen='" + editorID + "']", function () {
var editorID = $(this).attr("data-fullscreen");
var h = $("#" + editorID + "Wrap").height();
if (h == userOptions.editor_height) {
$("#" + editorID + "Wrap").css({'height': '100vh', 'position': 'fixed'});
$("#" + editorID).height(($("#" + editorID + "Wrap").height() - 40) + 'px');
$("#" + editorID + "Html").height(($("#" + editorID + "Wrap").height() - 40) + 'px');
}
else {
$("#" + editorID + "Wrap").css({'height': userOptions.editor_height + 'px', 'position': 'relative'});
$("#" + editorID).height(($("#" + editorID + "Wrap").height() - 40) + 'px');
$("#" + editorID + "Html").height(($("#" + editorID + "Wrap").height() - 40) + 'px');
}
});
$(document).on("click", "[data-htmlview='" + editorID + "']", function () {
var editorID = $(this).attr("data-htmlview");
if ($("#" + editorID).is(':visible')) {
$("#" + editorID).hide();
$("#" + editorID + "Html").val($("#" + editorID).html());
$("#" + editorID + "Html").removeClass("d-none");
$("#" + editorID + "Wrap").find('.btn-toolbar .btn').each(function () {
$(this).addClass("disabled");
});
$("#" + editorID + "Wrap").find('.btn-toolbar input').each(function () {
$(this).attr("disabled", "disabled");
});
} else {
$("#" + editorID + "Html").addClass("d-none");
$("#" + editorID).html($("#" + editorID + "Html").val());
$("#" + editorID).show();
$("#" + editorID + "Wrap").find('.btn-toolbar .btn').each(function () {
$(this).removeClass("disabled");
});
$("#" + editorID + "Wrap").find('.btn-toolbar input').each(function () {
$(this).removeAttr("disabled");
});
}
});
};
})
(window, window.jQuery);