-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
core.js
executable file
·9391 lines (8143 loc) · 425 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
/*
* wysiwyg web editor
*
* suneditor.js
* Copyright 2017 JiHong Lee.
* MIT license.
*/
'use strict';
import _Constructor from './constructor';
import _Context from './context';
import _history from './history';
import _util from './util';
import _notice from '../plugins/modules/_notice';
/**
* @description SunEditor constuctor function.
* create core object and event registration.
* core, event, functions
* @param {Object} context
* @param {Object} pluginCallButtons
* @param {Object} plugins
* @param {Object} lang
* @param {Object} options
* @param {Object} _responsiveButtons
* @returns {Object} functions Object
*/
export default function (context, pluginCallButtons, plugins, lang, options, _responsiveButtons) {
const _d = context.element.originElement.ownerDocument || document;
const _w = _d.defaultView || window;
const util = _util;
const icons = options.icons;
/**
* @description editor core object
* should always bind this object when registering an event in the plug-in.
*/
const core = {
_d: _d,
_w: _w,
_parser: new _w.DOMParser(),
_prevRtl: options.rtl,
_editorHeight: 0,
_editorHeightPadding: 0,
_listCamel: options.__listCommonStyle,
_listKebab: util.camelToKebabCase(options.__listCommonStyle),
__focusTemp: context.element._focusTemp,
/**
* @description Document object of the iframe if created as an iframe || _d
* @private
*/
_wd: null,
/**
* @description Window object of the iframe if created as an iframe || _w
* @private
*/
_ww: null,
/**
* @description Closest ShadowRoot to editor if found
* @private
*/
_shadowRoot: null,
/**
* @description Block controller mousedown events in "shadowRoot" environment
* @private
*/
_shadowRootControllerEventTarget: null,
/**
* @description Util object
*/
util: util,
/**
* @description Functions object
*/
functions: null,
/**
* @description Editor options
*/
options: null,
/**
* @description Computed style of the wysiwyg area (window.getComputedStyle(context.element.wysiwyg))
*/
wwComputedStyle: null,
/**
* @description Notice object
*/
notice: _notice,
/**
* @description Default icons object
*/
icons: icons,
/**
* @description History object for undo, redo
*/
history: null,
/**
* @description Elements and user options parameters of the suneditor
*/
context: context,
/**
* @description Plugin buttons
*/
pluginCallButtons: pluginCallButtons,
/**
* @description Loaded plugins
*/
plugins: plugins || {},
/**
* @description Whether the plugin is initialized
*/
initPlugins: {},
/**
* @description Object for managing submenu elements
* @private
*/
_targetPlugins: {},
/**
* @description Save rendered submenus and containers
* @private
*/
_menuTray: {},
/**
* @description loaded language
*/
lang: lang,
/**
* @description The selection node (core.getSelectionNode()) to which the effect was last applied
*/
effectNode: null,
/**
* @description submenu element
*/
submenu: null,
/**
* @description container element
*/
container: null,
/**
* @description current subment name
* @private
*/
_submenuName: '',
/**
* @description binded submenuOff method
* @private
*/
_bindedSubmenuOff: null,
/**
* @description binded containerOff method
* @private
*/
_bindedContainerOff: null,
/**
* @description active button element in submenu
*/
submenuActiveButton: null,
/**
* @description active button element in container
*/
containerActiveButton: null,
/**
* @description The elements array to be processed unvisible when the controllersOff function is executed (resizing, link modified button, table controller)
*/
controllerArray: [],
/**
* @description The name of the plugin that called the currently active controller
*/
currentControllerName: '',
/**
* @description The target element of current controller
*/
currentControllerTarget: null,
/**
* @description The file component object of current selected file tag (getFileComponent)
*/
currentFileComponentInfo: null,
/**
* @description An array of buttons whose class name is not "se-code-view-enabled"
*/
codeViewDisabledButtons: [],
/**
* @description An array of buttons whose class name is not "se-resizing-enabled"
*/
resizingDisabledButtons: [],
/**
* @description active more layer element in submenu
* @private
*/
_moreLayerActiveButton: null,
/**
* @description Tag whitelist RegExp object used in "_consistencyCheckOfHTML" method
* ^(options._editorTagsWhitelist)$
* @private
*/
_htmlCheckWhitelistRegExp: null,
/**
* @description Tag blacklist RegExp object used in "_consistencyCheckOfHTML" method
* @private
*/
_htmlCheckBlacklistRegExp: null,
/**
* @description RegExp when using check disallowd tags. (b, i, ins, strike, s)
* @private
*/
_disallowedTextTagsRegExp: null,
/**
* @description Editor tags whitelist (RegExp object)
* util.createTagsWhitelist(options._editorTagsWhitelist)
*/
editorTagsWhitelistRegExp: null,
/**
* @description Editor tags blacklist (RegExp object)
* util.createTagsBlacklist(options.tagsBlacklist)
*/
editorTagsBlacklistRegExp: null,
/**
* @description Tag whitelist when pasting (RegExp object)
* util.createTagsWhitelist(options.pasteTagsWhitelist)
*/
pasteTagsWhitelistRegExp: null,
/**
* @description Tag blacklist when pasting (RegExp object)
* util.createTagsBlacklist(options.pasteTagsBlacklist)
*/
pasteTagsBlacklistRegExp: null,
/**
* @description Boolean value of whether the editor has focus
*/
hasFocus: false,
/**
* @description Boolean value of whether the editor is disabled
*/
isDisabled: false,
/**
* @description Boolean value of whether the editor is readOnly
*/
isReadOnly: false,
/**
* @description Attributes whitelist used by the cleanHTML method
* @private
*/
_attributesWhitelistRegExp: null,
_attributesWhitelistRegExp_all_data: null,
/**
* @description Attributes blacklist used by the cleanHTML method
* @private
*/
_attributesBlacklistRegExp: null,
/**
* @description Attributes of tags whitelist used by the cleanHTML method
* @private
*/
_attributesTagsWhitelist: null,
/**
* @description Attributes of tags blacklist used by the cleanHTML method
* @private
*/
_attributesTagsBlacklist: null,
/**
* @description binded controllersOff method
* @private
*/
_bindControllersOff: null,
/**
* @description Is inline mode?
* @private
*/
_isInline: null,
/**
* @description Is balloon|balloon-always mode?
* @private
*/
_isBalloon: null,
/**
* @description Is balloon-always mode?
* @private
*/
_isBalloonAlways: null,
/**
* @description Required value when using inline mode to sticky toolbar
* @private
*/
_inlineToolbarAttr: {top: '', width: '', isShow: false},
/**
* @description Variable that controls the "blur" event in the editor of inline or balloon mode when the focus is moved to submenu
* @private
*/
_notHideToolbar: false,
/**
* @description Variable value that sticky toolbar mode
* @private
*/
_sticky: false,
/**
* @description Variables for controlling focus and blur events
* @private
*/
_antiBlur: false,
/**
* @description Component line breaker element
* @private
*/
_lineBreaker: null,
_lineBreakerButton: null,
/**
* @description If true, (initialize, reset) all indexes of image, video information
* @private
*/
_componentsInfoInit: true,
_componentsInfoReset: false,
/**
* @description Plugins array with "active" method.
* "activePlugins" runs the "add" method when creating the editor.
*/
activePlugins: null,
/**
* @description Information of tags that should maintain HTML structure, style, class name, etc. (In use by "math" plugin)
* When inserting "html" such as paste, it is executed on the "html" to be inserted. (core.cleanHTML)
* Basic Editor Actions:
* 1. All classes not starting with "__se__" or "se-" in the editor are removed.
* 2. The style of all tags except the "span" tag is removed from the editor.
* "managedTagsInfo" structure ex:
* managedTagsInfo: {
* query: '.__se__xxx, se-xxx'
* map: {
* '__se__xxx': method.bind(core),
* 'se-xxx': method.bind(core),
* }
* }
* @example
* Define in the following return format in the "managedTagInfo" function of the plugin.
* managedTagInfo() => {
* return {
* className: 'string', // Class name to identify the tag. ("__se__xxx", "se-xxx")
* // Change the html of the "element". ("element" is the element found with "className".)
* // "method" is executed by binding "core".
* method: function (element) {
* // this === core
* element.innerHTML = // (rendered html);
* }
* }
* }
*/
managedTagsInfo: null,
/**
* @description cashing: options.charCounterType === 'byte-html'
* @private
*/
_charTypeHTML: false,
/**
* @description Array of "checkFileInfo" functions with the core bound
* (Plugins with "checkFileInfo" and "resetFileInfo" methods)
* "fileInfoPlugins" runs the "add" method when creating the editor.
* "checkFileInfo" method is always call just before the "change" event.
* @private
*/
_fileInfoPluginsCheck: null,
/**
* @description Array of "resetFileInfo" functions with the core bound
* (Plugins with "checkFileInfo" and "resetFileInfo" methods)
* "checkFileInfo" method is always call just before the "functions.setOptions" method.
* @private
*/
_fileInfoPluginsReset: null,
/**
* @description Variables for file component management
* @private
*/
_fileManager: {
tags: null,
regExp: null,
queryString: null,
pluginRegExp: null,
pluginMap: null
},
/**
* @description Elements that need to change text or className for each selection change
* After creating the editor, "activePlugins" are added.
* @property {Element} STRONG bold button
* @property {Element} U underline button
* @property {Element} EM italic button
* @property {Element} DEL strike button
* @property {Element} SUB subscript button
* @property {Element} SUP superscript button
* @property {Element} OUTDENT outdent button
* @property {Element} INDENT indent button
*/
commandMap: {},
/**
* @description CSS properties related to style tags
* @private
*/
_commandMapStyles: {
STRONG: ['font-weight'],
U: ['text-decoration'],
EM: ['font-style'],
DEL: ['text-decoration']
},
/**
* @description Style button related to edit area
* @property {Element} fullScreen fullScreen button element
* @property {Element} showBlocks showBlocks button element
* @property {Element} codeView codeView button element
* @private
*/
_styleCommandMap: null,
/**
* @private
*/
_cleanStyleRegExp: {
div: new _w.RegExp('\\s*[^-a-zA-Z](.+)\\s*:[^;]+(?!;)*', 'ig'),
span: new _w.RegExp('\\s*[^-a-zA-Z](font-family|font-size|color|background-color)\\s*:[^;]+(?!;)*', 'ig'),
format: new _w.RegExp('\\s*[^-a-zA-Z](text-align|margin-left|margin-right|width|height|line-height)\\s*:[^;]+(?!;)*', 'ig'),
fontSizeUnit: new _w.RegExp('\\d+' + options.fontSizeUnit + '$', 'i'),
},
/**
* @description Variables used internally in editor operation
* @property {Boolean} isCodeView State of code view
* @property {Boolean} isFullScreen State of full screen
* @property {Number} innerHeight_fullScreen InnerHeight in editor when in full screen
* @property {Number} resizeClientY Remember the vertical size of the editor before resizing the editor (Used when calculating during resize operation)
* @property {Number} tabSize Indent size of tab (4)
* @property {Number} codeIndent Indent size of Code view mode (2)
* @property {Number} minResizingSize Minimum size of editing area when resized {Number} (.se-wrapper-inner {min-height: 65px;} || 65)
* @property {Array} currentNodes An array of the current cursor's node structure
* @private
*/
_variable: {
isChanged: false,
isCodeView: false,
isFullScreen: false,
innerHeight_fullScreen: 0,
resizeClientY: 0,
tabSize: 4,
codeIndent: 2,
minResizingSize: util.getNumber((context.element.wysiwygFrame.style.minHeight || '65'), 0),
currentNodes: [],
currentNodesMap: [],
_range: null,
_selectionNode: null,
_originCssText: context.element.topArea.style.cssText,
_bodyOverflow: '',
_editorAreaOriginCssText: '',
_wysiwygOriginCssText: '',
_codeOriginCssText: '',
_fullScreenAttrs: {sticky: false, balloon: false, inline: false},
_lineBreakComp: null,
_lineBreakDir: ''
},
/**
* @description Temp variable for set line attrs
* @private
*/
_formatAttrsTemp: null,
/**
* @description Save the current buttons states to "allCommandButtons" object
* @private
*/
_saveButtonStates: function () {
if (!this.allCommandButtons) this.allCommandButtons = {};
const currentButtons = this.context.element._buttonTray.querySelectorAll('.se-menu-list button[data-display]');
for (let i = 0, element, command; i < currentButtons.length; i++) {
element = currentButtons[i];
command = element.getAttribute('data-command');
this.allCommandButtons[command] = element;
}
},
/**
* @description Recover the current buttons states from "allCommandButtons" object
* @private
*/
_recoverButtonStates: function () {
if (this.allCommandButtons) {
const currentButtons = this.context.element._buttonTray.querySelectorAll('.se-menu-list button[data-display]');
for (let i = 0, button, command, oldButton; i < currentButtons.length; i++) {
button = currentButtons[i];
command = button.getAttribute('data-command');
oldButton = this.allCommandButtons[command];
if (oldButton) {
button.parentElement.replaceChild(oldButton, button);
if (this.context.tool[command]) this.context.tool[command] = oldButton;
}
}
}
},
/**
* @description If the plugin is not added, add the plugin and call the 'add' function.
* If the plugin is added call callBack function.
* @param {String} pluginName The name of the plugin to call
* @param {function} callBackFunction Function to be executed immediately after module call
* @param {Element|null} _target Plugin target button (This is not necessary if you have a button list when creating the editor)
*/
callPlugin: function (pluginName, callBackFunction, _target) {
_target = _target || pluginCallButtons[pluginName];
if (!this.plugins[pluginName]) {
throw Error('[SUNEDITOR.core.callPlugin.fail] The called plugin does not exist or is in an invalid format. (pluginName:"' + pluginName + '")');
} else if (!this.initPlugins[pluginName]) {
this.plugins[pluginName].add(this, _target);
this.initPlugins[pluginName] = true;
} else if (typeof this._targetPlugins[pluginName] === 'object' && !!_target) {
this.initMenuTarget(pluginName, _target, this._targetPlugins[pluginName]);
}
if (this.plugins[pluginName].active && !this.commandMap[pluginName] && !!_target) {
this.commandMap[pluginName] = _target;
this.activePlugins.push(pluginName);
}
if (typeof callBackFunction === 'function') callBackFunction();
},
/**
* @description If the module is not added, add the module and call the 'add' function
* @param {Array} moduleArray module object's Array [dialog, resizing]
*/
addModule: function (moduleArray) {
for (let i = 0, len = moduleArray.length, moduleName; i < len; i++) {
moduleName = moduleArray[i].name;
if (!this.plugins[moduleName]) {
this.plugins[moduleName] = moduleArray[i];
}
if (!this.initPlugins[moduleName]) {
this.initPlugins[moduleName] = true;
if (typeof this.plugins[moduleName].add === 'function') this.plugins[moduleName].add(this);
}
}
},
/**
* @description Gets the current editor-relative scroll offset.
* @returns {Object} {top, left}
*/
getGlobalScrollOffset: function () {
let t = 0, l = 0;
let el = context.element.topArea;
while (el) {
t += el.scrollTop;
l += el.scrollLeft;
el = el.parentElement;
}
el = this._shadowRoot ? this._shadowRoot.host : null;
while (el) {
t += el.scrollTop;
l += el.scrollLeft;
el = el.parentElement;
}
return {
top: t,
left: l
};
},
/**
* @description Method for managing submenu element.
* You must add the "submenu" element using the this method at custom plugin.
* @param {String} pluginName Plugin name
* @param {Element|null} target Target button
* @param {Element} menu Submenu element
*/
initMenuTarget: function (pluginName, target, menu) {
if (!target) {
this._targetPlugins[pluginName] = menu;
} else {
context.element._menuTray.appendChild(menu);
this._targetPlugins[pluginName] = true;
this._menuTray[target.getAttribute('data-command')] = menu;
}
},
/**
* @description Enable submenu
* @param {Element} element Submenu's button element to call
*/
submenuOn: function (element) {
if (this._bindedSubmenuOff) this._bindedSubmenuOff();
if (this._bindControllersOff) this.controllersOff();
const submenuName = this._submenuName = element.getAttribute('data-command');
const menu = this.submenu = this._menuTray[submenuName];
this.submenuActiveButton = element;
this._setMenuPosition(element, menu);
this._bindedSubmenuOff = this.submenuOff.bind(this);
this.addDocEvent('mousedown', this._bindedSubmenuOff, false);
if (this.plugins[submenuName].on) this.plugins[submenuName].on.call(this);
this._antiBlur = true;
},
/**
* @description Disable submenu
*/
submenuOff: function () {
this.removeDocEvent('mousedown', this._bindedSubmenuOff);
this._bindedSubmenuOff = null;
if (this.submenu) {
this._submenuName = '';
this.submenu.style.display = 'none';
this.submenu = null;
util.removeClass(this.submenuActiveButton, 'on');
this.submenuActiveButton = null;
this._notHideToolbar = false;
}
this._antiBlur = false;
},
/**
* @description Disable more layer
*/
moreLayerOff: function() {
if (this._moreLayerActiveButton) {
const layer = context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'));
layer.style.display = 'none';
util.removeClass(this._moreLayerActiveButton, 'on');
this._moreLayerActiveButton = null;
}
},
/**
* @description Enable container
* @param {Element} element Container's button element to call
*/
containerOn: function (element) {
if (this._bindedContainerOff) this._bindedContainerOff();
const containerName = this._containerName = element.getAttribute('data-command');
const menu = this.container = this._menuTray[containerName];
this.containerActiveButton = element;
this._setMenuPosition(element, menu);
this._bindedContainerOff = this.containerOff.bind(this);
this.addDocEvent('mousedown', this._bindedContainerOff, false);
if (this.plugins[containerName].on) this.plugins[containerName].on.call(this);
this._antiBlur = true;
},
/**
* @description Disable container
*/
containerOff: function () {
this.removeDocEvent('mousedown', this._bindedContainerOff);
this._bindedContainerOff = null;
if (this.container) {
this._containerName = '';
this.container.style.display = 'none';
this.container = null;
util.removeClass(this.containerActiveButton, 'on');
this.containerActiveButton = null;
this._notHideToolbar = false;
}
this._antiBlur = false;
},
/**
* @description Set the menu position. (submenu, container)
* @param {*} element Button element
* @param {*} menu Menu element
* @private
*/
_setMenuPosition: function (element, menu) {
menu.style.visibility = 'hidden';
menu.style.display = 'block';
menu.style.height = '';
util.addClass(element, 'on');
const toolbar = this.context.element.toolbar;
const toolbarW = toolbar.offsetWidth;
const toolbarOffset = event._getEditorOffsets(context.element.toolbar);
const menuW = menu.offsetWidth;
const l = element.parentElement.offsetLeft + 3;
// rtl
if (options.rtl) {
const elementW = element.offsetWidth;
const rtlW = menuW > elementW ? menuW - elementW : 0;
const rtlL = rtlW > 0 ? 0 : elementW - menuW;
menu.style.left = (l - rtlW + rtlL) + 'px';
if (toolbarOffset.left > event._getEditorOffsets(menu).left) {
menu.style.left = '0px';
}
} else {
const overLeft = toolbarW <= menuW ? 0 : toolbarW - (l + menuW);
if (overLeft < 0) menu.style.left = (l + overLeft) + 'px';
else menu.style.left = l + 'px';
}
// get element top
let t = 0;
let offsetEl = element;
while (offsetEl && offsetEl !== toolbar) {
t += offsetEl.offsetTop;
offsetEl = offsetEl.offsetParent;
}
const bt = t;
if (this._isBalloon) {
t += toolbar.offsetTop + element.offsetHeight;
} else {
t -= element.offsetHeight;
}
// set menu position
const toolbarTop = toolbarOffset.top;
const menuHeight = menu.offsetHeight;
const scrollTop = this.getGlobalScrollOffset().top;
const menuHeight_bottom = _w.innerHeight - (toolbarTop - scrollTop + bt + element.parentElement.offsetHeight);
if (menuHeight_bottom < menuHeight) {
let menuTop = -1 * (menuHeight - bt + 3);
const insTop = toolbarTop - scrollTop + menuTop;
const menuHeight_top = menuHeight + (insTop < 0 ? insTop : 0);
if (menuHeight_top > menuHeight_bottom) {
menu.style.height = menuHeight_top + 'px';
menuTop = -1 * (menuHeight_top - bt + 3);
} else {
menu.style.height = menuHeight_bottom + 'px';
menuTop = bt + element.parentElement.offsetHeight;
}
menu.style.top = menuTop + 'px';
} else {
menu.style.top = (bt + element.parentElement.offsetHeight) + 'px';
}
menu.style.visibility = '';
},
/**
* @description Show controller at editor area (controller elements, function, "controller target element(@Required)", "controller name(@Required)", etc..)
* @param {*} arguments controller elements, functions..
*/
controllersOn: function () {
if (this._bindControllersOff) this._bindControllersOff();
this.controllerArray = [];
for (let i = 0, arg; i < arguments.length; i++) {
arg = arguments[i];
if (!arg) continue;
if (typeof arg === 'string') {
this.currentControllerName = arg;
continue;
}
if (typeof arg === 'function') {
this.controllerArray.push(arg);
continue;
}
if (!util.hasClass(arg, 'se-controller')) {
this.currentControllerTarget = arg;
this.currentFileComponentInfo = this.getFileComponent(arg);
continue;
}
if (arg.style) {
arg.style.display = 'block';
if (this._shadowRoot && this._shadowRootControllerEventTarget.indexOf(arg) === -1) {
arg.addEventListener('mousedown', function (e) { e.preventDefault(); e.stopPropagation(); });
this._shadowRootControllerEventTarget.push(arg);
}
}
this.controllerArray.push(arg);
}
this._bindControllersOff = this.controllersOff.bind(this);
this.addDocEvent('mousedown', this._bindControllersOff, false);
this.addDocEvent('keydown', this._bindControllersOff, false);
this._antiBlur = true;
if (typeof functions.showController === 'function') functions.showController(this.currentControllerName, this.controllerArray, this);
},
/**
* @description Hide controller at editor area (link button, image resize button..)
* @param {KeyboardEvent|MouseEvent|null} e Event object when called from mousedown and keydown events registered in "core.controllersOn"
*/
controllersOff: function (e) {
this._lineBreaker.style.display = 'none';
const len = this.controllerArray.length;
if (e && e.target && len > 0) {
for (let i = 0; i < len; i++) {
if (typeof this.controllerArray[i].contains === 'function' && this.controllerArray[i].contains(e.target)) return;
}
}
if (this._fileManager.pluginRegExp.test(this.currentControllerName) && e && e.type === 'keydown' && e.keyCode !== 27) return;
context.element.lineBreaker_t.style.display = context.element.lineBreaker_b.style.display = 'none';
this._variable._lineBreakComp = null;
this.currentControllerName = '';
this.currentControllerTarget = null;
this.currentFileComponentInfo = null;
this.effectNode = null;
if (!this._bindControllersOff) return;
this.removeDocEvent('mousedown', this._bindControllersOff);
this.removeDocEvent('keydown', this._bindControllersOff);
this._bindControllersOff = null;
if (len > 0) {
for (let i = 0; i < len; i++) {
if (typeof this.controllerArray[i] === 'function') this.controllerArray[i]();
else this.controllerArray[i].style.display = 'none';
}
this.controllerArray = [];
}
this._antiBlur = false;
},
/**
* @description Specify the position of the controller.
* @param {Element} controller Controller element.
* @param {Element} referEl Element that is the basis of the controller's position.
* @param {String} position Type of position ("top" | "bottom")
* When using the "top" position, there should not be an arrow on the controller.
* When using the "bottom" position there should be an arrow on the controller.
* @param {Object} addOffset These are the left and top values that need to be added specially.
* This argument is required. - {left: 0, top: 0}
* Please enter the value based on ltr mode.
* Calculated automatically in rtl mode.
*/
setControllerPosition: function (controller, referEl, position, addOffset) {
if (options.rtl) addOffset.left *= -1;
const offset = util.getOffset(referEl, context.element.wysiwygFrame);
controller.style.visibility = 'hidden';
controller.style.display = 'block';
// Height value of the arrow element is 11px
const topMargin = position === 'top' ? -(controller.offsetHeight + 2) : (referEl.offsetHeight + 12);
controller.style.top = (offset.top + topMargin + addOffset.top) + 'px';
const l = offset.left - context.element.wysiwygFrame.scrollLeft + addOffset.left;
const controllerW = controller.offsetWidth;
const referElW = referEl.offsetWidth;
const allow = util.hasClass(controller.firstElementChild, 'se-arrow') ? controller.firstElementChild : null;
// rtl (Width value of the arrow element is 22px)
if (options.rtl) {
const rtlW = (controllerW > referElW) ? controllerW - referElW : 0;
const rtlL = rtlW > 0 ? 0 : referElW - controllerW;
controller.style.left = (l - rtlW + rtlL) + 'px';
if (rtlW > 0) {
if (allow) allow.style.left = ((controllerW - 14 < 10 + rtlW) ? (controllerW - 14) : (10 + rtlW)) + 'px';
}
const overSize = context.element.wysiwygFrame.offsetLeft - controller.offsetLeft;
if (overSize > 0) {
controller.style.left = '0px';
if (allow) allow.style.left = overSize + 'px';
}
} else {
controller.style.left = l + 'px';
const overSize = context.element.wysiwygFrame.offsetWidth - (controller.offsetLeft + controllerW);
if (overSize < 0) {
controller.style.left = (controller.offsetLeft + overSize) + 'px';
if (allow) allow.style.left = (20 - overSize) + 'px';
} else {
if (allow) allow.style.left = '20px';
}
}
controller.style.visibility = '';
},
/**
* @description javascript execCommand
* @param {String} command javascript execCommand function property
* @param {Boolean|undefined} showDefaultUI javascript execCommand function property
* @param {String|undefined} value javascript execCommand function property
*/
execCommand: function (command, showDefaultUI, value) {
this._wd.execCommand(command, showDefaultUI, (command === 'formatBlock' ? '<' + value + '>' : value));
// history stack
this.history.push(true);
},
/**
* @description Focus to wysiwyg area using "native focus function"
*/
nativeFocus: function () {
this.__focus();
this._editorRange();
},
/**
* @description Focus method
* @private
*/
__focus: function () {
const caption = util.getParentElement(this.getSelectionNode(), 'figcaption');
if (caption) {
caption.focus();
} else {
context.element.wysiwyg.focus();
}
},
/**
* @description Focus to wysiwyg area
*/
focus: function () {
if (context.element.wysiwygFrame.style.display === 'none') return;
if (options.iframe) {
this.nativeFocus();
} else {
try {
const range = this.getRange();
if (range.startContainer === range.endContainer && util.isWysiwygDiv(range.startContainer)) {
const currentNode = range.commonAncestorContainer.children[range.startOffset];
if (!util.isFormatElement(currentNode) && !util.isComponent(currentNode)) {