/
core.js
1287 lines (1090 loc) · 54.9 KB
/
core.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () {
'use strict';
// Event handlers that shouldn't be exposed externally
function handleDisableExtraSpaces(event) {
var node = MediumEditor.selection.getSelectionStart(this.options.ownerDocument),
textContent = node.textContent,
caretPositions = MediumEditor.selection.getCaretOffsets(node);
if ((textContent[caretPositions.left - 1] === undefined) || (textContent[caretPositions.left - 1].trim() === '') || (textContent[caretPositions.left] !== undefined && textContent[caretPositions.left].trim() === '')) {
event.preventDefault();
}
}
function handleDisabledEnterKeydown(event, element) {
if (this.options.disableReturn || element.getAttribute('data-disable-return')) {
event.preventDefault();
} else if (this.options.disableDoubleReturn || element.getAttribute('data-disable-double-return')) {
var node = MediumEditor.selection.getSelectionStart(this.options.ownerDocument);
// if current text selection is empty OR previous sibling text is empty OR it is not a list
if ((node && node.textContent.trim() === '' && node.nodeName.toLowerCase() !== 'li') ||
(node.previousElementSibling && node.previousElementSibling.nodeName.toLowerCase() !== 'br' &&
node.previousElementSibling.textContent.trim() === '')) {
event.preventDefault();
}
}
}
function handleTabKeydown(event) {
// Override tab only for pre nodes
var node = MediumEditor.selection.getSelectionStart(this.options.ownerDocument),
tag = node && node.nodeName.toLowerCase();
if (tag === 'pre') {
event.preventDefault();
MediumEditor.util.insertHTMLCommand(this.options.ownerDocument, ' ');
}
// Tab to indent list structures!
if (MediumEditor.util.isListItem(node)) {
event.preventDefault();
// If Shift is down, outdent, otherwise indent
if (event.shiftKey) {
this.options.ownerDocument.execCommand('outdent', false, null);
} else {
this.options.ownerDocument.execCommand('indent', false, null);
}
}
}
function handleBlockDeleteKeydowns(event) {
var p, node = MediumEditor.selection.getSelectionStart(this.options.ownerDocument),
tagName = node.nodeName.toLowerCase(),
isEmpty = /^(\s+|<br\/?>)?$/i,
isHeader = /h\d/i;
if (MediumEditor.util.isKey(event, [MediumEditor.util.keyCode.BACKSPACE, MediumEditor.util.keyCode.ENTER]) &&
// has a preceeding sibling
node.previousElementSibling &&
// in a header
isHeader.test(tagName) &&
// at the very end of the block
MediumEditor.selection.getCaretOffsets(node).left === 0) {
if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) && isEmpty.test(node.previousElementSibling.innerHTML)) {
// backspacing the begining of a header into an empty previous element will
// change the tagName of the current node to prevent one
// instead delete previous node and cancel the event.
node.previousElementSibling.parentNode.removeChild(node.previousElementSibling);
event.preventDefault();
} else if (!this.options.disableDoubleReturn && MediumEditor.util.isKey(event, MediumEditor.util.keyCode.ENTER)) {
// hitting return in the begining of a header will create empty header elements before the current one
// instead, make "<p><br></p>" element, which are what happens if you hit return in an empty paragraph
p = this.options.ownerDocument.createElement('p');
p.innerHTML = '<br>';
node.previousElementSibling.parentNode.insertBefore(p, node);
event.preventDefault();
}
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.DELETE) &&
// between two sibling elements
node.nextElementSibling &&
node.previousElementSibling &&
// not in a header
!isHeader.test(tagName) &&
// in an empty tag
isEmpty.test(node.innerHTML) &&
// when the next tag *is* a header
isHeader.test(node.nextElementSibling.nodeName.toLowerCase())) {
// hitting delete in an empty element preceding a header, ex:
// <p>[CURSOR]</p><h1>Header</h1>
// Will cause the h1 to become a paragraph.
// Instead, delete the paragraph node and move the cursor to the begining of the h1
// remove node and move cursor to start of header
MediumEditor.selection.moveCursor(this.options.ownerDocument, node.nextElementSibling);
node.previousElementSibling.parentNode.removeChild(node);
event.preventDefault();
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) &&
tagName === 'li' &&
// hitting backspace inside an empty li
isEmpty.test(node.innerHTML) &&
// is first element (no preceeding siblings)
!node.previousElementSibling &&
// parent also does not have a sibling
!node.parentElement.previousElementSibling &&
// is not the only li in a list
node.nextElementSibling &&
node.nextElementSibling.nodeName.toLowerCase() === 'li') {
// backspacing in an empty first list element in the first list (with more elements) ex:
// <ul><li>[CURSOR]</li><li>List Item 2</li></ul>
// will remove the first <li> but add some extra element before (varies based on browser)
// Instead, this will:
// 1) remove the list element
// 2) create a paragraph before the list
// 3) move the cursor into the paragraph
// create a paragraph before the list
p = this.options.ownerDocument.createElement('p');
p.innerHTML = '<br>';
node.parentElement.parentElement.insertBefore(p, node.parentElement);
// move the cursor into the new paragraph
MediumEditor.selection.moveCursor(this.options.ownerDocument, p);
// remove the list element
node.parentElement.removeChild(node);
event.preventDefault();
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) &&
(MediumEditor.util.getClosestTag(node, 'blockquote') !== false) &&
MediumEditor.selection.getCaretOffsets(node).left === 0) {
// when cursor is at the begining of the element and the element is <blockquote>
// then pressing backspace key should change the <blockquote> to a <p> tag
event.preventDefault();
MediumEditor.util.execFormatBlock(this.options.ownerDocument, 'p');
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.ENTER) &&
(MediumEditor.util.getClosestTag(node, 'blockquote') !== false) &&
MediumEditor.selection.getCaretOffsets(node).right === 0) {
// when cursor is at the end of <blockquote>,
// then pressing enter key should create <p> tag, not <blockquote>
p = this.options.ownerDocument.createElement('p');
p.innerHTML = '<br>';
node.parentElement.insertBefore(p, node.nextSibling);
// move the cursor into the new paragraph
MediumEditor.selection.moveCursor(this.options.ownerDocument, p);
event.preventDefault();
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) &&
MediumEditor.util.isMediumEditorElement(node.parentElement) &&
!node.previousElementSibling &&
node.nextElementSibling &&
isEmpty.test(node.innerHTML)) {
// when cursor is in the first element, it's empty and user presses backspace,
// do delete action instead to get rid of the first element and move caret to 2nd
event.preventDefault();
MediumEditor.selection.moveCursor(this.options.ownerDocument, node.nextSibling);
node.parentElement.removeChild(node);
}
}
function handleKeyup(event) {
var node = MediumEditor.selection.getSelectionStart(this.options.ownerDocument),
tagName;
if (!node) {
return;
}
// https://github.com/yabwe/medium-editor/issues/994
// Firefox thrown an error when calling `formatBlock` on an empty editable blockContainer that's not a <div>
if (MediumEditor.util.isMediumEditorElement(node) && node.children.length === 0 && !MediumEditor.util.isBlockContainer(node)) {
this.options.ownerDocument.execCommand('formatBlock', false, 'p');
}
// https://github.com/yabwe/medium-editor/issues/834
// https://github.com/yabwe/medium-editor/pull/382
// Don't call format block if this is a block element (ie h1, figCaption, etc.)
if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.ENTER) &&
!MediumEditor.util.isListItem(node) &&
!MediumEditor.util.isBlockContainer(node)) {
tagName = node.nodeName.toLowerCase();
// For anchor tags, unlink
if (tagName === 'a') {
this.options.ownerDocument.execCommand('unlink', false, null);
} else if (!event.shiftKey && !event.ctrlKey) {
this.options.ownerDocument.execCommand('formatBlock', false, 'p');
}
}
}
function handleEditableInput(event, editable) {
var textarea = editable.parentNode.querySelector('textarea[medium-editor-textarea-id="' + editable.getAttribute('medium-editor-textarea-id') + '"]');
if (textarea) {
textarea.value = editable.innerHTML.trim();
}
}
// Internal helper methods which shouldn't be exposed externally
function addToEditors(win) {
if (!win._mediumEditors) {
// To avoid breaking users who are assuming that the unique id on
// medium-editor elements will start at 1, inserting a 'null' in the
// array so the unique-id can always map to the index of the editor instance
win._mediumEditors = [null];
}
// If this already has a unique id, re-use it
if (!this.id) {
this.id = win._mediumEditors.length;
}
win._mediumEditors[this.id] = this;
}
function removeFromEditors(win) {
if (!win._mediumEditors || !win._mediumEditors[this.id]) {
return;
}
/* Setting the instance to null in the array instead of deleting it allows:
* 1) Each instance to preserve its own unique-id, even after being destroyed
* and initialized again
* 2) The unique-id to always correspond to an index in the array of medium-editor
* instances. Thus, we will be able to look at a contenteditable, and determine
* which instance it belongs to, by indexing into the global array.
*/
win._mediumEditors[this.id] = null;
}
function createElementsArray(selector, doc, filterEditorElements) {
var elements = [];
if (!selector) {
selector = [];
}
// If string, use as query selector
if (typeof selector === 'string') {
selector = doc.querySelectorAll(selector);
}
// If element, put into array
if (MediumEditor.util.isElement(selector)) {
selector = [selector];
}
if (filterEditorElements) {
// Remove elements that have already been initialized by the editor
// selecotr might not be an array (ie NodeList) so use for loop
for (var i = 0; i < selector.length; i++) {
var el = selector[i];
if (MediumEditor.util.isElement(el) &&
!el.getAttribute('data-medium-editor-element') &&
!el.getAttribute('medium-editor-textarea-id')) {
elements.push(el);
}
}
} else {
// Convert NodeList (or other array like object) into an array
elements = Array.prototype.slice.apply(selector);
}
return elements;
}
function cleanupTextareaElement(element) {
var textarea = element.parentNode.querySelector('textarea[medium-editor-textarea-id="' + element.getAttribute('medium-editor-textarea-id') + '"]');
if (textarea) {
// Un-hide the textarea
textarea.classList.remove('medium-editor-hidden');
textarea.removeAttribute('medium-editor-textarea-id');
}
if (element.parentNode) {
element.parentNode.removeChild(element);
}
}
function setExtensionDefaults(extension, defaults) {
Object.keys(defaults).forEach(function (prop) {
if (extension[prop] === undefined) {
extension[prop] = defaults[prop];
}
});
return extension;
}
function initExtension(extension, name, instance) {
var extensionDefaults = {
'window': instance.options.contentWindow,
'document': instance.options.ownerDocument,
'base': instance
};
// Add default options into the extension
extension = setExtensionDefaults(extension, extensionDefaults);
// Call init on the extension
if (typeof extension.init === 'function') {
extension.init();
}
// Set extension name (if not already set)
if (!extension.name) {
extension.name = name;
}
return extension;
}
function isToolbarEnabled() {
// If any of the elements don't have the toolbar disabled
// We need a toolbar
if (this.elements.every(function (element) {
return !!element.getAttribute('data-disable-toolbar');
})) {
return false;
}
return this.options.toolbar !== false;
}
function isAnchorPreviewEnabled() {
// If toolbar is disabled, don't add
if (!isToolbarEnabled.call(this)) {
return false;
}
return this.options.anchorPreview !== false;
}
function isPlaceholderEnabled() {
return this.options.placeholder !== false;
}
function isAutoLinkEnabled() {
return this.options.autoLink !== false;
}
function isImageDraggingEnabled() {
return this.options.imageDragging !== false;
}
function isKeyboardCommandsEnabled() {
return this.options.keyboardCommands !== false;
}
function shouldUseFileDraggingExtension() {
// Since the file-dragging extension replaces the image-dragging extension,
// we need to check if the user passed an overrided image-dragging extension.
// If they have, to avoid breaking users, we won't use file-dragging extension.
return !this.options.extensions['imageDragging'];
}
function createContentEditable(textarea) {
var div = this.options.ownerDocument.createElement('div'),
now = Date.now(),
uniqueId = 'medium-editor-' + now,
atts = textarea.attributes;
// Some browsers can move pretty fast, since we're using a timestamp
// to make a unique-id, ensure that the id is actually unique on the page
while (this.options.ownerDocument.getElementById(uniqueId)) {
now++;
uniqueId = 'medium-editor-' + now;
}
div.className = textarea.className;
div.id = uniqueId;
div.innerHTML = textarea.value;
textarea.setAttribute('medium-editor-textarea-id', uniqueId);
// re-create all attributes from the textearea to the new created div
for (var i = 0, n = atts.length; i < n; i++) {
// do not re-create existing attributes
if (!div.hasAttribute(atts[i].nodeName)) {
div.setAttribute(atts[i].nodeName, atts[i].value);
}
}
// If textarea has a form, listen for reset on the form to clear
// the content of the created div
if (textarea.form) {
this.on(textarea.form, 'reset', function (event) {
if (!event.defaultPrevented) {
this.resetContent(this.options.ownerDocument.getElementById(uniqueId));
}
}.bind(this));
}
textarea.classList.add('medium-editor-hidden');
textarea.parentNode.insertBefore(
div,
textarea
);
return div;
}
function initElement(element, editorId) {
if (!element.getAttribute('data-medium-editor-element')) {
if (element.nodeName.toLowerCase() === 'textarea') {
element = createContentEditable.call(this, element);
// Make sure we only attach to editableInput once for <textarea> elements
if (!this.instanceHandleEditableInput) {
this.instanceHandleEditableInput = handleEditableInput.bind(this);
this.subscribe('editableInput', this.instanceHandleEditableInput);
}
}
if (!this.options.disableEditing && !element.getAttribute('data-disable-editing')) {
element.setAttribute('contentEditable', true);
element.setAttribute('spellcheck', this.options.spellcheck);
}
// Make sure we only attach to editableKeydownEnter once for disable-return options
if (!this.instanceHandleEditableKeydownEnter) {
if (element.getAttribute('data-disable-return') || element.getAttribute('data-disable-double-return')) {
this.instanceHandleEditableKeydownEnter = handleDisabledEnterKeydown.bind(this);
this.subscribe('editableKeydownEnter', this.instanceHandleEditableKeydownEnter);
}
}
// if we're not disabling return, add a handler to help handle cleanup
// for certain cases when enter is pressed
if (!this.options.disableReturn && !element.getAttribute('data-disable-return')) {
this.on(element, 'keyup', handleKeyup.bind(this));
}
var elementId = MediumEditor.util.guid();
element.setAttribute('data-medium-editor-element', true);
element.classList.add('medium-editor-element');
element.setAttribute('role', 'textbox');
element.setAttribute('aria-multiline', true);
element.setAttribute('data-medium-editor-editor-index', editorId);
// TODO: Merge data-medium-editor-element and medium-editor-index attributes for 6.0.0
// medium-editor-index is not named correctly anymore and can be re-purposed to signify
// whether the element has been initialized or not
element.setAttribute('medium-editor-index', elementId);
initialContent[elementId] = element.innerHTML;
this.events.attachAllEventsToElement(element);
}
return element;
}
function attachHandlers() {
// attach to tabs
this.subscribe('editableKeydownTab', handleTabKeydown.bind(this));
// Bind keys which can create or destroy a block element: backspace, delete, return
this.subscribe('editableKeydownDelete', handleBlockDeleteKeydowns.bind(this));
this.subscribe('editableKeydownEnter', handleBlockDeleteKeydowns.bind(this));
// Bind double space event
if (this.options.disableExtraSpaces) {
this.subscribe('editableKeydownSpace', handleDisableExtraSpaces.bind(this));
}
// Make sure we only attach to editableKeydownEnter once for disable-return options
if (!this.instanceHandleEditableKeydownEnter) {
// disabling return or double return
if (this.options.disableReturn || this.options.disableDoubleReturn) {
this.instanceHandleEditableKeydownEnter = handleDisabledEnterKeydown.bind(this);
this.subscribe('editableKeydownEnter', this.instanceHandleEditableKeydownEnter);
}
}
}
function initExtensions() {
this.extensions = [];
// Passed in extensions
Object.keys(this.options.extensions).forEach(function (name) {
// Always save the toolbar extension for last
if (name !== 'toolbar' && this.options.extensions[name]) {
this.extensions.push(initExtension(this.options.extensions[name], name, this));
}
}, this);
// 4 Cases for imageDragging + fileDragging extensons:
//
// 1. ImageDragging ON + No Custom Image Dragging Extension:
// * Use fileDragging extension (default options)
// 2. ImageDragging OFF + No Custom Image Dragging Extension:
// * Use fileDragging extension w/ images turned off
// 3. ImageDragging ON + Custom Image Dragging Extension:
// * Don't use fileDragging (could interfere with custom image dragging extension)
// 4. ImageDragging OFF + Custom Image Dragging:
// * Don't use fileDragging (could interfere with custom image dragging extension)
if (shouldUseFileDraggingExtension.call(this)) {
var opts = this.options.fileDragging;
if (!opts) {
opts = {};
// Image is in the 'allowedTypes' list by default.
// If imageDragging is off override the 'allowedTypes' list with an empty one
if (!isImageDraggingEnabled.call(this)) {
opts.allowedTypes = [];
}
}
this.addBuiltInExtension('fileDragging', opts);
}
// Built-in extensions
var builtIns = {
paste: true,
'anchor-preview': isAnchorPreviewEnabled.call(this),
autoLink: isAutoLinkEnabled.call(this),
keyboardCommands: isKeyboardCommandsEnabled.call(this),
placeholder: isPlaceholderEnabled.call(this)
};
Object.keys(builtIns).forEach(function (name) {
if (builtIns[name]) {
this.addBuiltInExtension(name);
}
}, this);
// Users can pass in a custom toolbar extension
// so check for that first and if it's not present
// just create the default toolbar
var toolbarExtension = this.options.extensions['toolbar'];
if (!toolbarExtension && isToolbarEnabled.call(this)) {
// Backwards compatability
var toolbarOptions = MediumEditor.util.extend({}, this.options.toolbar, {
allowMultiParagraphSelection: this.options.allowMultiParagraphSelection // deprecated
});
toolbarExtension = new MediumEditor.extensions.toolbar(toolbarOptions);
}
// If the toolbar is not disabled, so we actually have an extension
// initialize it and add it to the extensions array
if (toolbarExtension) {
this.extensions.push(initExtension(toolbarExtension, 'toolbar', this));
}
}
function mergeOptions(defaults, options) {
var deprecatedProperties = [
['allowMultiParagraphSelection', 'toolbar.allowMultiParagraphSelection']
];
// warn about using deprecated properties
if (options) {
deprecatedProperties.forEach(function (pair) {
if (options.hasOwnProperty(pair[0]) && options[pair[0]] !== undefined) {
MediumEditor.util.deprecated(pair[0], pair[1], 'v6.0.0');
}
});
}
return MediumEditor.util.defaults({}, options, defaults);
}
function execActionInternal(action, opts) {
/*jslint regexp: true*/
var appendAction = /^append-(.+)$/gi,
justifyAction = /justify([A-Za-z]*)$/g, /* Detecting if is justifyCenter|Right|Left */
match,
cmdValueArgument;
/*jslint regexp: false*/
// Actions starting with 'append-' should attempt to format a block of text ('formatBlock') using a specific
// type of block element (ie append-blockquote, append-h1, append-pre, etc.)
match = appendAction.exec(action);
if (match) {
return MediumEditor.util.execFormatBlock(this.options.ownerDocument, match[1]);
}
if (action === 'fontSize') {
// TODO: Deprecate support for opts.size in 6.0.0
if (opts.size) {
MediumEditor.util.deprecated('.size option for fontSize command', '.value', '6.0.0');
}
cmdValueArgument = opts.value || opts.size;
return this.options.ownerDocument.execCommand('fontSize', false, cmdValueArgument);
}
if (action === 'fontName') {
// TODO: Deprecate support for opts.name in 6.0.0
if (opts.name) {
MediumEditor.util.deprecated('.name option for fontName command', '.value', '6.0.0');
}
cmdValueArgument = opts.value || opts.name;
return this.options.ownerDocument.execCommand('fontName', false, cmdValueArgument);
}
if (action === 'createLink') {
return this.createLink(opts);
}
if (action === 'image') {
var src = this.options.contentWindow.getSelection().toString().trim();
return this.options.ownerDocument.execCommand('insertImage', false, src);
}
if (action === 'html') {
var html = this.options.contentWindow.getSelection().toString().trim();
return MediumEditor.util.insertHTMLCommand(this.options.ownerDocument, html);
}
/* Issue: https://github.com/yabwe/medium-editor/issues/595
* If the action is to justify the text */
if (justifyAction.exec(action)) {
var result = this.options.ownerDocument.execCommand(action, false, null),
parentNode = MediumEditor.selection.getSelectedParentElement(MediumEditor.selection.getSelectionRange(this.options.ownerDocument));
if (parentNode) {
cleanupJustifyDivFragments.call(this, MediumEditor.util.getTopBlockContainer(parentNode));
}
return result;
}
cmdValueArgument = opts && opts.value;
return this.options.ownerDocument.execCommand(action, false, cmdValueArgument);
}
/* If we've just justified text within a container block
* Chrome may have removed <br> elements and instead wrapped lines in <div> elements
* with a text-align property. If so, we want to fix this
*/
function cleanupJustifyDivFragments(blockContainer) {
if (!blockContainer) {
return;
}
var textAlign,
childDivs = Array.prototype.slice.call(blockContainer.childNodes).filter(function (element) {
var isDiv = element.nodeName.toLowerCase() === 'div';
if (isDiv && !textAlign) {
textAlign = element.style.textAlign;
}
return isDiv;
});
/* If we found child <div> elements with text-align style attributes
* we should fix this by:
*
* 1) Unwrapping each <div> which has a text-align style
* 2) Insert a <br> element after each set of 'unwrapped' div children
* 3) Set the text-align style of the parent block element
*/
if (childDivs.length) {
// Since we're mucking with the HTML, preserve selection
this.saveSelection();
childDivs.forEach(function (div) {
if (div.style.textAlign === textAlign) {
var lastChild = div.lastChild;
if (lastChild) {
// Instead of a div, extract the child elements and add a <br>
MediumEditor.util.unwrap(div, this.options.ownerDocument);
var br = this.options.ownerDocument.createElement('BR');
lastChild.parentNode.insertBefore(br, lastChild.nextSibling);
}
}
}, this);
blockContainer.style.textAlign = textAlign;
// We're done, so restore selection
this.restoreSelection();
}
}
var initialContent = {};
MediumEditor.prototype = {
// NOT DOCUMENTED - exposed for backwards compatability
init: function (elements, options) {
this.options = mergeOptions.call(this, this.defaults, options);
this.origElements = elements;
if (!this.options.elementsContainer) {
this.options.elementsContainer = this.options.ownerDocument.body;
}
return this.setup();
},
setup: function () {
if (this.isActive) {
return;
}
addToEditors.call(this, this.options.contentWindow);
this.events = new MediumEditor.Events(this);
this.elements = [];
this.addElements(this.origElements);
if (this.elements.length === 0) {
return;
}
this.isActive = true;
// Call initialization helpers
initExtensions.call(this);
attachHandlers.call(this);
},
destroy: function () {
if (!this.isActive) {
return;
}
this.isActive = false;
this.extensions.forEach(function (extension) {
if (typeof extension.destroy === 'function') {
extension.destroy();
}
}, this);
this.events.destroy();
this.elements.forEach(function (element) {
// Reset elements content, fix for issue where after editor destroyed the red underlines on spelling errors are left
if (this.options.spellcheck) {
element.innerHTML = element.innerHTML;
}
// cleanup extra added attributes
element.removeAttribute('contentEditable');
element.removeAttribute('spellcheck');
element.removeAttribute('data-medium-editor-element');
element.classList.remove('medium-editor-element');
element.removeAttribute('role');
element.removeAttribute('aria-multiline');
element.removeAttribute('medium-editor-index');
element.removeAttribute('data-medium-editor-editor-index');
// Remove any elements created for textareas
if (element.getAttribute('medium-editor-textarea-id')) {
cleanupTextareaElement(element);
}
}, this);
this.elements = [];
this.instanceHandleEditableKeydownEnter = null;
this.instanceHandleEditableInput = null;
removeFromEditors.call(this, this.options.contentWindow);
},
on: function (target, event, listener, useCapture) {
this.events.attachDOMEvent(target, event, listener, useCapture);
return this;
},
off: function (target, event, listener, useCapture) {
this.events.detachDOMEvent(target, event, listener, useCapture);
return this;
},
subscribe: function (event, listener) {
this.events.attachCustomEvent(event, listener);
return this;
},
unsubscribe: function (event, listener) {
this.events.detachCustomEvent(event, listener);
return this;
},
trigger: function (name, data, editable) {
this.events.triggerCustomEvent(name, data, editable);
return this;
},
delay: function (fn) {
var self = this;
return setTimeout(function () {
if (self.isActive) {
fn();
}
}, this.options.delay);
},
serialize: function () {
var i,
elementid,
content = {},
len = this.elements.length;
for (i = 0; i < len; i += 1) {
elementid = (this.elements[i].id !== '') ? this.elements[i].id : 'element-' + i;
content[elementid] = {
value: this.elements[i].innerHTML.trim()
};
}
return content;
},
getExtensionByName: function (name) {
var extension;
if (this.extensions && this.extensions.length) {
this.extensions.some(function (ext) {
if (ext.name === name) {
extension = ext;
return true;
}
return false;
});
}
return extension;
},
/**
* NOT DOCUMENTED - exposed as a helper for other extensions to use
*/
addBuiltInExtension: function (name, opts) {
var extension = this.getExtensionByName(name),
merged;
if (extension) {
return extension;
}
switch (name) {
case 'anchor':
merged = MediumEditor.util.extend({}, this.options.anchor, opts);
extension = new MediumEditor.extensions.anchor(merged);
break;
case 'anchor-preview':
extension = new MediumEditor.extensions.anchorPreview(this.options.anchorPreview);
break;
case 'autoLink':
extension = new MediumEditor.extensions.autoLink();
break;
case 'fileDragging':
extension = new MediumEditor.extensions.fileDragging(opts);
break;
case 'fontname':
extension = new MediumEditor.extensions.fontName(this.options.fontName);
break;
case 'fontsize':
extension = new MediumEditor.extensions.fontSize(opts);
break;
case 'keyboardCommands':
extension = new MediumEditor.extensions.keyboardCommands(this.options.keyboardCommands);
break;
case 'paste':
extension = new MediumEditor.extensions.paste(this.options.paste);
break;
case 'placeholder':
extension = new MediumEditor.extensions.placeholder(this.options.placeholder);
break;
default:
// All of the built-in buttons for MediumEditor are extensions
// so check to see if the extension we're creating is a built-in button
if (MediumEditor.extensions.button.isBuiltInButton(name)) {
if (opts) {
merged = MediumEditor.util.defaults({}, opts, MediumEditor.extensions.button.prototype.defaults[name]);
extension = new MediumEditor.extensions.button(merged);
} else {
extension = new MediumEditor.extensions.button(name);
}
}
}
if (extension) {
this.extensions.push(initExtension(extension, name, this));
}
return extension;
},
stopSelectionUpdates: function () {
this.preventSelectionUpdates = true;
},
startSelectionUpdates: function () {
this.preventSelectionUpdates = false;
},
checkSelection: function () {
var toolbar = this.getExtensionByName('toolbar');
if (toolbar) {
toolbar.checkState();
}
return this;
},
// Wrapper around document.queryCommandState for checking whether an action has already
// been applied to the current selection
queryCommandState: function (action) {
var fullAction = /^full-(.+)$/gi,
match,
queryState = null;
// Actions starting with 'full-' need to be modified since this is a medium-editor concept
match = fullAction.exec(action);
if (match) {
action = match[1];
}
try {
queryState = this.options.ownerDocument.queryCommandState(action);
} catch (exc) {
queryState = null;
}
return queryState;
},
execAction: function (action, opts) {
/*jslint regexp: true*/
var fullAction = /^full-(.+)$/gi,
match,
result;
/*jslint regexp: false*/
// Actions starting with 'full-' should be applied to to the entire contents of the editable element
// (ie full-bold, full-append-pre, etc.)
match = fullAction.exec(action);
if (match) {
// Store the current selection to be restored after applying the action
this.saveSelection();
// Select all of the contents before calling the action
this.selectAllContents();
result = execActionInternal.call(this, match[1], opts);
// Restore the previous selection
this.restoreSelection();
} else {
result = execActionInternal.call(this, action, opts);
}
// do some DOM clean-up for known browser issues after the action
if (action === 'insertunorderedlist' || action === 'insertorderedlist') {
MediumEditor.util.cleanListDOM(this.options.contentWindow, this.options.ownerDocument, this.getSelectedParentElement());
}
this.checkSelection();
return result;
},
getSelectedParentElement: function (range) {
if (range === undefined) {
range = this.options.contentWindow.getSelection().getRangeAt(0);
}
return MediumEditor.selection.getSelectedParentElement(range);
},
selectAllContents: function () {
var currNode = MediumEditor.selection.getSelectionElement(this.options.contentWindow);
if (currNode) {
// Move to the lowest descendant node that still selects all of the contents
while (currNode.children.length === 1) {
currNode = currNode.children[0];
}
this.selectElement(currNode);
}
},
selectElement: function (element) {
MediumEditor.selection.selectNode(element, this.options.ownerDocument);
var selElement = MediumEditor.selection.getSelectionElement(this.options.contentWindow);
if (selElement) {
this.events.focusElement(selElement);
}
},
getFocusedElement: function () {
var focused;
this.elements.some(function (element) {
// Find the element that has focus
if (!focused && element.getAttribute('data-medium-focused')) {
focused = element;
}
// bail if we found the element that had focus
return !!focused;
}, this);
return focused;
},
// Export the state of the selection in respect to one of this
// instance of MediumEditor's elements
exportSelection: function () {
var selectionElement = MediumEditor.selection.getSelectionElement(this.options.contentWindow),
editableElementIndex = this.elements.indexOf(selectionElement),
selectionState = null;
if (editableElementIndex >= 0) {