-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Editor.js
1758 lines (1437 loc) · 48.8 KB
/
Editor.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
/**
* $Id$
*
* @author Moxiecode
* @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
*/
(function() {
var DOM = tinymce.DOM, Event = tinymce.dom.Event, extend = tinymce.extend, Dispatcher = tinymce.util.Dispatcher;
var each = tinymce.each, isGecko = tinymce.isGecko, isIE = tinymce.isIE, isWebKit = tinymce.isWebKit;
var is = tinymce.is, ThemeManager = tinymce.ThemeManager, PluginManager = tinymce.PluginManager, EditorManager = tinymce.EditorManager;
var inArray = tinymce.inArray, grep = tinymce.grep;
/**#@+
* @class This class contains the core logic for a TinyMCE editor.
* @member tinymce.Editor
*/
tinymce.create('tinymce.Editor', {
/**
* Constructs a editor instance by id.
*
* @constructor
* @member tinymce.Editor
* @param {String} id Unique id for the editor.
* @param {Object} s Optional settings string for the editor.
*/
Editor : function(id, s) {
var t = this;
t.id = t.editorId = id;
t.execCommands = {};
t.queryStateCommands = {};
t.queryValueCommands = {};
t.plugins = {};
// Add events to the editor
each([
'onPreInit',
'onBeforeRenderUI',
'onPostRender',
'onInit',
'onRemove',
'onActivate',
'onDeactivate',
'onClick',
'onEvent',
'onMouseUp',
'onMouseDown',
'onDblClick',
'onKeyDown',
'onKeyUp',
'onKeyPress',
'onContextMenu',
'onSubmit',
'onReset',
'onPaste',
'onPreProcess',
'onPostProcess',
'onBeforeSetContent',
'onBeforeGetContent',
'onSetContent',
'onGetContent',
'onLoadContent',
'onSaveContent',
'onNodeChange',
'onChange',
'onBeforeExecCommand',
'onExecCommand',
'onUndo',
'onRedo',
'onVisualAid',
'onSetProgressState'
], function(e) {
t[e] = new Dispatcher(t);
});
// Default editor config
t.settings = s = extend({
id : id,
language : 'en',
docs_language : 'en',
theme : 'simple',
skin : 'default',
delta_width : 0,
delta_height : 0,
popup_css : '',
plugins : '',
document_base_url : tinymce.documentBaseURL,
add_form_submit_trigger : 1,
submit_patch : 1,
add_unload_trigger : 1,
convert_urls : 1,
relative_urls : 1,
remove_script_host : 1,
table_inline_editing : 0,
object_resizing : 1,
cleanup : 1,
accessibility_focus : 0,
custom_shortcuts : 1,
custom_undo_redo_keyboard_shortcuts : 1,
custom_undo_redo_restore_selection : 1,
custom_undo_redo : 1,
doctype : '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">',
visual_table_class : 'mceItemTable',
visual : 1,
inline_styles : true,
convert_fonts_to_spans : true,
font_size_style_values : 'xx-small,x-small,small,medium,large,x-large,xx-large',
apply_source_formatting : 1,
directionality : 'ltr',
forced_root_block : 'p',
valid_elements : '@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,#p[align],-ol,-ul,-li,br,img[longdesc|usemap|src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,-span,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],object[classid|width|height|codebase|*],param[name|value],embed[type|width|height|src|*]',
hidden_input : 1
}, s);
// Setup URIs
t.documentBaseURI = new tinymce.util.URI(s.document_base_url || tinymce.documentBaseURL, {
base_uri : tinyMCE.baseURI
});
t.baseURI = EditorManager.baseURI;
// Call setup
t.execCallback('setup', t);
},
/**#@+
* @method
*/
/**
* Renderes the editor/adds it to the page.
*/
render : function() {
var t = this, s = t.settings, id = t.id, sl = tinymce.ScriptLoader;
// Add hidden input for non input elements inside form elements
if (!/TEXTAREA|INPUT/i.test(DOM.get(id).nodeName) && s.hidden_input && DOM.getParent(id, 'form'))
DOM.insertAfter(DOM.create('input', {type : 'hidden', name : id}), id);
t.windowManager = new tinymce.WindowManager(t);
if (s.encoding == 'xml') {
t.onGetContent.add(function(ed, o) {
if (o.get)
o.content = DOM.encode(o.content);
});
}
if (s.add_form_submit_trigger) {
t.onSubmit.add(function() {
if (t.initialized)
t.save();
});
}
Event.add(document, 'unload', function() {
if (t.initialized && !t.destroyed && s.add_unload_trigger)
t.save({format : 'raw', no_events : true});
});
tinymce.addUnload(t._destroy, t);
if (s.submit_patch) {
t.onBeforeRenderUI.add(function() {
var t = this, n = DOM.get(t.id).form;
if (!n)
return;
// Already patched
if (n._mceOldSubmit)
return;
n._mceOldSubmit = n.submit;
t.formElement = n;
n.submit = function() {
// Save all instances
EditorManager.triggerSave();
return this._mceOldSubmit(this);
};
n = null;
});
}
// Load scripts
function loadScripts() {
sl.add(tinymce.baseURL + '/langs/' + s.language + '.js');
if (s.theme.charAt(0) != '-')
ThemeManager.load(s.theme, 'themes/' + s.theme + '/editor_template' + tinymce.suffix + '.js');
each(s.plugins.split(','), function(p) {
if (p && p.charAt(0) != '-') {
// Skip safari plugin for other browsers
if (!isWebKit && p == 'safari')
return;
PluginManager.load(p, 'plugins/' + p + '/editor_plugin' + tinymce.suffix + '.js');
}
});
// Init when que is loaded
sl.loadQueue(function() {
if (s.ask) {
function ask() {
t.windowManager.confirm(t.getLang('edit_confirm'), function(s) {
if (s)
t.init();
else
Event.remove(t.id, 'focus', ask);
});
};
Event.add(t.id, 'focus', ask);
return;
}
t.init();
});
};
// Load compat2x first
if (s.plugins.indexOf('compat2x') != -1) {
PluginManager.load('compat2x', 'plugins/compat2x/editor_plugin' + tinymce.suffix + '.js');
sl.loadQueue(loadScripts);
} else
loadScripts();
},
/**
* Initializes the editor this will be called automatically when
* all plugins/themes and language packs are loaded by the rendered method.
* This method will setup the iframe and create the theme and plugin instances.
*/
init : function() {
var n, t = this, s = t.settings, w, h, e = DOM.get(s.id), o;
EditorManager.add(t);
// Create theme
s.theme = s.theme.replace(/-/, '');
o = ThemeManager.get(s.theme);
t.theme = new o();
if (t.theme.init)
t.theme.init(t, ThemeManager.urls[s.theme] || tinymce.documentBaseURL.replace(/\/$/, ''));
// Create all plugins
each(s.plugins.replace(/\-/g, '').split(','), function(p) {
var c = PluginManager.get(p), u = PluginManager.urls[p] || tinymce.documentBaseURL.replace(/\/$/, ''), po;
if (c) {
po = new c(t, u);
t.plugins[p] = po;
if (po.init)
po.init(t, u);
}
});
// Setup popup CSS path(s)
s.popup_css = t.baseURI.toAbsolute(s.popup_css || "themes/" + s.theme + "/skins/" + s.skin + "/dialog.css");
if (s.popup_css_add)
s.popup_css += ',' + s.popup_css_add;
// Setup control factory
t.controlManager = new tinymce.ControlManager(t);
t.undoManager = new tinymce.UndoManager(t);
// Pass through
t.undoManager.onAdd.add(function(um, l) {
return t.onChange.dispatch(t, l, um);
});
t.undoManager.onUndo.add(function(um, l) {
return t.onUndo.dispatch(t, l, um);
});
t.undoManager.onRedo.add(function(um, l) {
return t.onRedo.dispatch(t, l, um);
});
if (s.custom_undo_redo) {
t.onExecCommand.add(function(ed, cmd) {
if (cmd != 'Undo' && cmd != 'Redo' && cmd != 'mceRepaint')
t.undoManager.add();
});
}
t.onExecCommand.add(function(ed, c) {
// Don't refresh the select lists until caret move
if (!/^(FontName|FontSize)$/.test(c))
t.nodeChanged();
});
// Remove ghost selections on images and tables in Gecko
if (isGecko) {
function repaint() {
t.execCommand('mceRepaint');
};
t.onUndo.add(repaint);
t.onRedo.add(repaint);
t.onSetContent.add(repaint);
}
// Enables users to override the control factory
t.onBeforeRenderUI.dispatch(t, t.controlManager);
// Measure box
w = s.width || e.style.width || e.clientWidth;
h = s.height || e.style.height || e.clientHeight;
t.orgDisplay = e.style.display;
if (('' + w).indexOf('%') == -1)
w = Math.max(parseInt(w) + (o.deltaWidth || 0), 100);
if (('' + h).indexOf('%') == -1)
h = Math.max(parseInt(h) + (o.deltaHeight || 0), 100);
// Render UI
o = t.theme.renderUI({
targetNode : e,
width : w,
height : h,
deltaWidth : s.delta_width,
deltaHeight : s.delta_height
});
// Resize editor
DOM.setStyles(o.sizeContainer || o.editorContainer, {
width : w,
height : h
});
// Create iframe
n = DOM.add(o.iframeContainer, 'iframe', {
id : s.id + "_ifr",
src : 'javascript:""', // Workaround for HTTPS warning in IE6/7
frameBorder : '0',
style : {
width : '100%',
height : (o.iframeHeight || h)
}
});
t.contentAreaContainer = o.iframeContainer;
DOM.get(o.editorContainer).style.display = t.orgDisplay;
DOM.get(s.id).style.display = 'none';
// Safari 2.x requires us to wait for the load event and load a real HTML doc
if (tinymce.isOldWebKit) {
Event.add(n, 'load', t.setupIframe, t);
n.src = tinymce.baseURL + '/plugins/safari/blank.htm';
} else {
t.setupIframe();
e = n = o = null; // Cleanup
}
},
/**
* This method get called by the init method ones the iframe is loaded.
* It will fill the iframe with contents, setups DOM and selection objects for the iframe.
* This method should not be called directly.
*/
setupIframe : function() {
var t = this, s = t.settings, e = DOM.get(s.id), d = t.getDoc();
// Design mode needs to be added here Ctrl+A will fail otherwise
if (!isIE)
d.designMode = 'On';
// Setup body
d.open();
d.write(s.doctype + '<html><head><base href="' + t.documentBaseURI.getURI() + '" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body id="tinymce" class="mceContentBody"></body></html>');
d.close();
// IE needs to use contentEditable or it will display non secure items for HTTPS
if (isIE)
t.getBody().contentEditable = true;
// Setup objects
t.dom = new tinymce.DOM.DOMUtils(t.getDoc(), {
keep_values : true,
url_converter : t.convertURL,
url_converter_scope : t,
hex_colors : s.force_hex_style_colors,
class_filter : s.class_filter
});
t.serializer = new tinymce.dom.Serializer({
entity_encoding : s.entity_encoding,
entities : s.entities,
valid_elements : s.verify_html === false ? '*[*]' : s.valid_elements,
extended_valid_elements : s.extended_valid_elements,
valid_child_elements : s.valid_child_elements,
invalid_elements : s.invalid_elements,
fix_table_elements : s.fix_table_elements,
fix_list_elements : s.fix_list_elements,
fix_content_duplication : s.fix_content_duplication,
convert_fonts_to_spans : s.convert_fonts_to_spans,
font_size_classes : s.font_size_classes,
font_size_style_values : s.font_size_style_values,
apply_source_formatting : s.apply_source_formatting,
dom : t.dom
});
t.selection = new tinymce.dom.Selection(t.dom, t.getWin(), t.serializer);
t.forceBlocks = new tinymce.ForceBlocks(t, {
forced_root_block : s.forced_root_block
});
t.editorCommands = new tinymce.EditorCommands(t);
// Pass through
t.serializer.onPreProcess.add(function(se, o) {
return t.onPreProcess.dispatch(t, o, se);
});
t.serializer.onPostProcess.add(function(se, o) {
return t.onPostProcess.dispatch(t, o, se);
});
if (s.content_css)
t.dom.loadCSS(t.documentBaseURI.toAbsolute(s.content_css));
t.onPreInit.dispatch(t);
if (!s.gecko_spellcheck)
t.getBody().spellcheck = 0;
t._addEvents();
t.controlManager.onPostRender.dispatch(t, t.controlManager);
t.onPostRender.dispatch(t);
if (s.directionality)
t.getBody().dir = s.directionality;
if (s.nowrap)
t.getBody().style.whiteSpace = "nowrap";
if (s.auto_resize)
t.onNodeChange.add(t.resizeToContent, t);
if (s.handle_node_change_callback) {
t.onNodeChange.add(function(ed, cm, n) {
t.execCallback('handle_node_change_callback', t.id, n, -1, -1, true, t.selection.isCollapsed());
});
}
if (s.save_callback) {
t.onSaveContent.add(function(ed, o) {
var h = t.execCallback('save_callback', t.id, o.content, t.getBody());
if (h)
o.content = h;
});
}
if (s.onchange_callback) {
t.onChange.add(function(ed, l) {
t.execCallback('onchange_callback', t, l);
});
}
if (s.convert_newlines_to_brs) {
t.onBeforeSetContent.add(function(ed, o) {
if (o.initial)
o.content = o.content.replace(/\r?\n/g, '<br />');
});
}
if (s.preformatted) {
t.onPostProcess.add(function(ed, o) {
o.content = o.content.replace(/^\s*<pre.*?>/, '');
o.content = o.content.replace(/<\/pre>\s*$/, '');
if (o.set)
o.content = '<pre class="mceItemHidden">' + o.content + '</pre>';
});
}
if (s.verify_css_classes) {
t.serializer.attribValueFilter = function(n, v) {
var s, cl;
if (n == 'class') {
// Build regexp for classes
if (!t.classesRE) {
cl = t.dom.getClasses();
if (cl.length > 0) {
s = '';
each (cl, function(o) {
s += (s ? '|' : '') + o['class'];
});
t.classesRE = new RegExp('(' + s + ')', 'gi');
}
}
return !t.classesRE || /(\bmceItem\w+\b|\bmceTemp\w+\b)/g.test(v) || t.classesRE.test(v) ? v : '';
}
return v;
};
}
if (s.convert_fonts_to_spans)
t._convertFonts();
if (s.inline_styles)
t._convertInlineElements();
if (s.cleanup_callback) {
t.onSetContent.add(function(ed, o) {
if (o.initial) {
t.setContent(t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o), {format : 'raw', no_events : true});
t.execCallback('cleanup_callback', 'insert_to_editor_dom', t.getDoc(), o);
}
});
t.onPreProcess.add(function(ed, o) {
if (o.set)
t.execCallback('cleanup_callback', 'insert_to_editor_dom', o.node, o);
if (o.get)
t.execCallback('cleanup_callback', 'get_from_editor_dom', o.node, o);
});
t.onPostProcess.add(function(ed, o) {
if (o.set)
o.content = t.execCallback('cleanup_callback', 'insert_to_editor', o.content, o);
if (o.get)
o.content = t.execCallback('cleanup_callback', 'get_from_editor', o.content, o);
});
}
if (s.save_callback) {
t.onGetContent.add(function(ed, o) {
if (o.save)
o.content = t.execCallback('save_callback', t.id, o.content, t.getBody());
});
}
if (s.handle_event_callback) {
t.onEvent.add(function(ed, e, o) {
if (t.execCallback('handle_event_callback', e, ed, o) === false)
Event.cancel(e);
});
}
t.onSetContent.add(function() {
// Safari needs some time, it will crash the browser when a link is created otherwise
window.setTimeout(function() {
t.addVisual(t.getBody());
}, 1);
});
t.load({initial : true, format : (s.cleanup_on_startup ? 'html' : 'raw')});
t.startContent = t.getContent({format : 'raw'});
t.undoManager.add({initial : true});
t.initialized = true;
t.onInit.dispatch(t);
t.execCallback('setupcontent_callback', t.id, t.getBody(), t.getDoc());
t.execCallback('init_instance_callback', t);
t.focus(true);
t.nodeChanged({initial : 1});
// Handle auto focus
if (s.auto_focus) {
setTimeout(function () {
var ed = EditorManager.get(s.auto_focus);
ed.selection.select(ed.getBody(), 1);
ed.selection.collapse(1);
ed.getWin().focus();
}, 100);
}
e = null;
},
/**
* Focuses/activates the editor. This will set this editor as the activeEditor in the EditorManager
* it will also place DOM focus inside the editor.
*
* @param {bool} sf Skip DOM focus. Just set is as the active editor.
*/
focus : function(sf) {
var oed, t = this;
if (!sf)
t.getWin().focus();
if (EditorManager.activeEditor != t) {
if ((oed = EditorManager.activeEditor) != null)
oed.onDeactivate.dispatch(oed, t);
t.onActivate.dispatch(t, oed);
}
EditorManager._setActive(t);
},
/**
* Executes a legacy callback. This method is useful to call old 2.x option callbacks.
* There new event model is a better way to add callback so this method might be removed in the future.
*
* @param {String} n Name of the callback to execute.
* @return {Object} Return value passed from callback function.
*/
execCallback : function(n) {
var t = this, f = t.settings[n], s;
if (!f)
return;
// Look through lookup
if (t.callbackLookup && (s = t.callbackLookup[n])) {
f = s.func;
s = s.scope;
}
if (is(f, 'string')) {
s = f.replace(/\.\w+$/, '');
s = s ? tinymce.resolve(s) : 0;
f = tinymce.resolve(f);
t.callbackLookup = t.callbackLookup || {};
t.callbackLookup[n] = {func : f, scope : s};
}
return f.apply(s || t, Array.prototype.slice.call(arguments, 1));
},
/**
* Translates the specified string by replacing variables with language pack items it will also check if there is
* a key mathcin the input.
*
* @param {String} s String to translate by the language pack data.
* @return {String} Translated string.
*/
translate : function(s) {
var c = this.settings.language, i18n = EditorManager.i18n;
if (!s)
return '';
return i18n[c + '.' + s] || s.replace(/{\#([^}]+)\}/g, function(a, b) {
return i18n[c + '.' + b] || '{#' + b + '}';
});
},
/**
* Returns a language pack item by name/key.
*
* @param {String} n Name/key to get from the language pack.
* @param {String} dv Optional default value to retrive.
*/
getLang : function(n, dv) {
return EditorManager.i18n[this.settings.language + '.' + n] || (is(dv) ? dv : '{#' + n + '}');
},
/**
* Returns a configuration parameter by name.
*
* @param {String} n Configruation parameter to retrive.
* @param {String} dv Optional default value to return.
* @return {String} Configuration parameter value or default value.
*/
getParam : function(n, dv) {
return is(this.settings[n]) ? this.settings[n] : dv;
},
/**
* Distpaches out a onNodeChange event to all observers. This method should be called when you
* need to update the UI states or element path etc.
*
* @param {Object} o Optional object to pass along for the node changed event.
*/
nodeChanged : function(o) {
var t = this, s = t.selection, n = s.getNode() || this.getBody();
this.onNodeChange.dispatch(
t,
o ? o.controlManager || t.controlManager : t.controlManager,
isIE && n.ownerDocument != t.getDoc() ? this.getBody() : n, // Fix for IE initial state
s.isCollapsed(),
o
);
},
/**
* Adds a button that later gets created by the ControlManager. This is a shorter and easier method
* of adding buttons without the need to deal with the ControlManager directly. But it's also less
* powerfull if you need more control use the ControlManagers factory methods instead.
*
* @param {String} n Button name to add.
* @param {Object} s Settings object with title, cmd etc.
*/
addButton : function(n, s) {
var t = this;
t.buttons = t.buttons || {};
t.buttons[n] = s;
},
/**
* Adds a custom command to the editor, you can also override existing commands with this method.
* The command that you add can be executed with execCommand.
*
* @param {String} n Command name to add/override.
* @param {function} f Function to execute when the command occurs.
* @param {Object} s Optional scope to execute the function in.
*/
addCommand : function(n, f, s) {
this.execCommands[n] = {func : f, scope : s || this};
},
/**
* Adds a custom query state command to the editor, you can also override existing commands with this method.
* The command that you add can be executed with queryCommandState function.
*
* @param {String} n Command name to add/override.
* @param {function} f Function to execute when the command state retrival occurs.
* @param {Object} s Optional scope to execute the function in.
*/
addQueryStateHandler : function(n, f, s) {
this.queryStateCommands[n] = {func : f, scope : s || this};
},
/**
* Adds a custom query value command to the editor, you can also override existing commands with this method.
* The command that you add can be executed with queryCommandValue function.
*
* @param {String} n Command name to add/override.
* @param {function} f Function to execute when the command value retrival occurs.
* @param {Object} s Optional scope to execute the function in.
*/
addQueryValueHandler : function(n, f, s) {
this.queryValueCommands[n] = {func : f, scope : s || this};
},
/**
* Adds a keyboard shortcut for some command or function.
*
* @param {String} pa Shortcut pattern. Like for example: ctrl+alt+o.
* @param {String} desc Text description for the command.
* @param {String/Function} cmd_func Command name string or function to execute when the key is pressed.
* @param {Object} sc Optional scope to execute the function in.
* @return {bool} true/false state if the shortcut was added or not.
*/
addShortcut : function(pa, desc, cmd_func, sc) {
var t = this, c;
if (!t.settings.custom_shortcuts)
return false;
t.shortcuts = t.shortcuts || {};
if (is(cmd_func, 'string')) {
c = cmd_func;
cmd_func = function() {
t.execCommand(c, false, null);
};
}
if (is(cmd_func, 'object')) {
c = cmd_func;
cmd_func = function() {
t.execCommand(c[0], c[1], c[2]);
};
}
each(pa.split(','), function(pa) {
var o = {
func : cmd_func,
scope : sc || this,
desc : desc,
alt : false,
ctrl : false,
shift : false
};
each(pa.split('+'), function(v) {
switch (v) {
case 'alt':
case 'ctrl':
case 'shift':
o[v] = true;
break;
default:
o.charCode = v.charCodeAt(0);
o.keyCode = v.toUpperCase().charCodeAt(0);
}
});
t.shortcuts[(o.ctrl ? 'ctrl' : '') + ',' + (o.alt ? 'alt' : '') + ',' + (o.shift ? 'shift' : '') + ',' + o.keyCode] = o;
});
return true;
},
/**
* Executes a command on the current instance. These commands can be TinyMCE internal commands prefixed with "mce" or
* they can be build in browser commands such as "Bold". A compleate list of browser commands is available on MSDN or Mozilla.org.
* This function will dispatch the execCommand function on each plugin, theme or the execcommand_callback option if none of these
* return true it will handle the command as a internal browser command.
*
* @param {String} cmd Command name to execute, for example mceLink or Bold.
* @param {bool} ui True/false state if a UI (dialog) should be presented or not.
* @param {mixed} val Optional command value, this can be anything.
* @return {bool} True/false if the command was executed or not.
*/
execCommand : function(cmd, ui, val) {
var t = this, s = 0, o;
if (!/^(mceAddUndoLevel|mceEndUndoLevel|mceBeginUndoLevel)$/.test(cmd))
t.focus();
t.onBeforeExecCommand.dispatch(t, cmd, ui, val);
// Comamnd callback
if (t.execCallback('execcommand_callback', null, t.id, t.selection.getNode(), cmd, ui, val)) {
t.onExecCommand.dispatch(t, cmd, ui, val);
return true;
}
// Registred commands
if (o = t.execCommands[cmd]) {
s = o.func.call(o.scope, ui, val);
t.onExecCommand.dispatch(t, cmd, ui, val);
return s;
}
// Plugin commands
each(t.plugins, function(p) {
if (p.execCommand && p.execCommand(cmd, ui, val)) {
t.onExecCommand.dispatch(t, cmd, ui, val);
s = 1;
return false;
}
});
if (s)
return true;
// Theme commands
if (t.theme.execCommand && t.theme.execCommand(cmd, ui, val)) {
t.onExecCommand.dispatch(t, cmd, ui, val);
return true;
}
// Editor commands
if (t.editorCommands.execCommand(cmd, ui, val)) {
t.onExecCommand.dispatch(t, cmd, ui, val);
return true;
}
// Browser commands
t.getDoc().execCommand(cmd, ui, val);
t.onExecCommand.dispatch(t, cmd, ui, val);
},
/**
* Returns a command specific state, for example if bold is enabled or not.
*
* @param {string} c Command to query state from.
* @return {bool} Command specific state, for example if bold is enabled or not.
*/
queryCommandState : function(c) {
var t = this, o;
// Registred commands
if (o = t.queryStateCommands[c])
return o.func.call(o.scope);
// Registred commands
o = t.editorCommands.queryCommandState(c);
if (o !== -1)
return o;
// Browser commands
return this.getDoc().queryCommandState(c);
},
/**
* Returns a command specific value, for example the current font size.
*
* @param {string} c Command to query value from.
* @return {Object} Command specific value, for example the current font size.
*/
queryCommandValue : function(c) {
var t = this, o;
// Registred commands
if (o = t.queryValueCommands[c])
return o.func.call(o.scope);
// Registred commands
o = t.editorCommands.queryCommandValue(c);
if (is(o))
return o;
// Browser commands
return this.getDoc().queryCommandValue(c);
},
/**
* Shows the editor and hides any textarea/div that the editor is supposed to replace.
*/
show : function() {
var t = this;
DOM.show(t.getContainer());
DOM.hide(t.id);
t.load();
},
/**
* Hides the editor and shows any textarea/div that the editor is supposed to replace.
*/
hide : function() {
var t = this, s = t.settings;
// Fixed bug where IE has a blinking cursor left from the editor
if (isIE)
t.execCommand('SelectAll');
DOM.hide(t.getContainer());
DOM.setStyle(s.id, 'display', t.orgDisplay);
t.save();
},
/**
* Returns true/false if the editor is hidden or not.
*
* @return {bool} True/false if the editor is hidden or not.
*/
isHidden : function() {
return !DOM.isHidden(this.id);
},
/**
* Sets the progress state, this will display a throbber/progess for the editor.
* This is ideal for asycronous operations like an AJAX save call.
*
* @param {bool} b Boolean state if the progress should be shown or hidden.
* @param {Number} ti Optional time to wait before the progress gets shown.
* @param {Object} o Optional object to pass to the progress observers.
* @return {bool} Same as the input state.
*/
setProgressState : function(b, ti, o) {
this.onSetProgressState.dispatch(this, b, ti, o);
return b;
},
/**
* Removes the editor from the dom and EditorManager collection.
*/
remove : function() {
var t = this;
t.hide();
DOM.remove(t.getContainer());
t.execCallback('remove_instance_callback', t);
t.onRemove.dispatch(t);
EditorManager.remove(t);
},
/**
* Resizes the editor to the current contents width and height.
*/
resizeToContent : function() {
var t = this;
DOM.setStyle(t.id + "_ifr", 'height', t.getBody().scrollHeight);
},
/**
* Loads contents from the textarea or div element that got converted into an editor instance.
* This method will move the contents from that textarea or div into the editor by using setContent
* so all events etc that method has will get dispatched as well.
*
* @param {Object} o Optional content object, this gets passed around through the whole load process.