comparison src/sceditor.js @ 23:c2a85b2ec677

remove more oo
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 09 Aug 2022 13:25:17 -0600
parents 499f38b5eeff
children 80a86fb8f2b7
comparison
equal deleted inserted replaced
22:499f38b5eeff 23:c2a85b2ec677
2838 /** 2838 /**
2839 * Plugin Manager class 2839 * Plugin Manager class
2840 * @class PluginManager 2840 * @class PluginManager
2841 * @name PluginManager 2841 * @name PluginManager
2842 */ 2842 */
2843 function PluginManager(thisObj) { 2843 function newPluginManager(thisObj) {
2844 /** 2844 /**
2845 * Alias of this 2845 * Alias of this
2846 * 2846 *
2847 * @private 2847 * @private
2848 * @type {Object} 2848 * @type {Object}
2849 */ 2849 */
2850 var base = this; 2850 var base = {};
2851 2851
2852 /** 2852 /**
2853 * Array of all currently registered plugins 2853 * Array of all currently registered plugins
2854 * 2854 *
2855 * @type {Array} 2855 * @type {Array}
3063 } 3063 }
3064 3064
3065 registeredPlugins = []; 3065 registeredPlugins = [];
3066 thisObj = null; 3066 thisObj = null;
3067 }; 3067 };
3068
3069 return base;
3068 } 3070 }
3069 PluginManager.plugins = plugins; 3071 //PluginManager.plugins = plugins;
3070 3072
3071 /** 3073 /**
3072 * Gets the text, start/end node and offset for 3074 * Gets the text, start/end node and offset for
3073 * length chars left or right of the passed node 3075 * length chars left or right of the passed node
3074 * at the specified offset. 3076 * at the specified offset.
3135 * Range helper 3137 * Range helper
3136 * 3138 *
3137 * @class RangeHelper 3139 * @class RangeHelper
3138 * @name RangeHelper 3140 * @name RangeHelper
3139 */ 3141 */
3140 function RangeHelper(win, d) { 3142 function newRangeHelper(win, d) {
3141 var _createMarker, _prepareInput, 3143 var _createMarker, _prepareInput,
3142 doc = d || win.contentDocument || win.document, 3144 doc = d || win.contentDocument || win.document,
3143 startMarker = 'sceditor-start-marker', 3145 startMarker = 'sceditor-start-marker',
3144 endMarker = 'sceditor-end-marker', 3146 endMarker = 'sceditor-end-marker',
3145 base = this; 3147 base = {};
3146 3148
3147 /** 3149 /**
3148 * Inserts HTML into the current range replacing any selected 3150 * Inserts HTML into the current range replacing any selected
3149 * text. 3151 * text.
3150 * 3152 *
3852 } else if (sel.empty) { 3854 } else if (sel.empty) {
3853 sel.empty(); 3855 sel.empty();
3854 } 3856 }
3855 } 3857 }
3856 }; 3858 };
3859
3860 return base;
3857 } 3861 }
3858 3862
3859 var USER_AGENT = navigator.userAgent; 3863 var USER_AGENT = navigator.userAgent;
3860 3864
3861 /** 3865 /**
4139 * @param {HTMLTextAreaElement} original The textarea to be converted 4143 * @param {HTMLTextAreaElement} original The textarea to be converted
4140 * @param {Object} userOptions 4144 * @param {Object} userOptions
4141 * @class SCEditor 4145 * @class SCEditor
4142 * @name SCEditor 4146 * @name SCEditor
4143 */ 4147 */
4144 function SCEditor(original, userOptions) { 4148 function newSCEditor(original, userOptions) {
4145 /** 4149 /**
4146 * Alias of this 4150 * Alias of this
4147 * 4151 *
4148 * @private 4152 * @private
4149 */ 4153 */
4150 var base = this; 4154 var base = {};
4151 4155
4152 /** 4156 /**
4153 * Editor format like BBCode or HTML 4157 * Editor format like BBCode or HTML
4154 */ 4158 */
4155 var format; 4159 var format;
4487 css(editorContainer, 'z-index', options.zIndex); 4491 css(editorContainer, 'z-index', options.zIndex);
4488 4492
4489 isRequired = original.required; 4493 isRequired = original.required;
4490 original.required = false; 4494 original.required = false;
4491 4495
4492 var FormatCtor = SCEditor.formats[options.format]; 4496 var FormatCtor = _formats[options.format];
4493 format = FormatCtor ? new FormatCtor() : {}; 4497 format = FormatCtor ? new FormatCtor() : {};
4494 /* 4498 /*
4495 * Plugins should be initialized before the formatters since 4499 * Plugins should be initialized before the formatters since
4496 * they may wish to add or change formatting handlers and 4500 * they may wish to add or change formatting handlers and
4497 * since the bbcode format caches its handlers, 4501 * since the bbcode format caches its handlers,
4498 * such changes must be done first. 4502 * such changes must be done first.
4499 */ 4503 */
4500 pluginManager = new PluginManager(base); 4504 pluginManager = newPluginManager(base);
4501 (options.plugins || '').split(',').forEach(function (plugin) { 4505 (options.plugins || '').split(',').forEach(function (plugin) {
4502 pluginManager.register(plugin.trim()); 4506 pluginManager.register(plugin.trim());
4503 }); 4507 });
4504 if ('init' in format) { 4508 if ('init' in format) {
4505 format.init.call(base); 4509 format.init.call(base);
4547 * @return void 4551 * @return void
4548 */ 4552 */
4549 initLocale = function () { 4553 initLocale = function () {
4550 var lang; 4554 var lang;
4551 4555
4552 locale = SCEditor.locale[options.locale]; 4556 locale = _locale[options.locale];
4553 4557
4554 if (!locale) { 4558 if (!locale) {
4555 lang = options.locale.split('-'); 4559 lang = options.locale.split('-');
4556 locale = SCEditor.locale[lang[0]]; 4560 locale = _locale[lang[0]];
4557 } 4561 }
4558 4562
4559 // Locale DateTime format overrides any specified in the options 4563 // Locale DateTime format overrides any specified in the options
4560 if (locale && locale.dateFormat) { 4564 if (locale && locale.dateFormat) {
4561 options.dateFormat = locale.dateFormat; 4565 options.dateFormat = locale.dateFormat;
4629 4633
4630 var tabIndex = attr(original, 'tabindex'); 4634 var tabIndex = attr(original, 'tabindex');
4631 attr(sourceEditor, 'tabindex', tabIndex); 4635 attr(sourceEditor, 'tabindex', tabIndex);
4632 attr(wysiwygEditor, 'tabindex', tabIndex); 4636 attr(wysiwygEditor, 'tabindex', tabIndex);
4633 4637
4634 rangeHelper = new RangeHelper(wysiwygWindow, null); 4638 rangeHelper = newRangeHelper(wysiwygWindow, null);
4635 4639
4636 // load any textarea value into the editor 4640 // load any textarea value into the editor
4637 hide(original); 4641 hide(original);
4638 base.val(original.value); 4642 base.val(original.value);
4639 4643
5901 5905
5902 if (position) { 5906 if (position) {
5903 sourceEditor.selectionStart = position.start; 5907 sourceEditor.selectionStart = position.start;
5904 sourceEditor.selectionEnd = position.end; 5908 sourceEditor.selectionEnd = position.end;
5905 5909
5906 return this; 5910 return base;
5907 } 5911 }
5908 5912
5909 return { 5913 return {
5910 start: sourceEditor.selectionStart, 5914 start: sourceEditor.selectionStart,
5911 end: sourceEditor.selectionEnd 5915 end: sourceEditor.selectionEnd
6298 handleCommand = function (caller, cmd) { 6302 handleCommand = function (caller, cmd) {
6299 // check if in text mode and handle text commands 6303 // check if in text mode and handle text commands
6300 if (base.inSourceMode()) { 6304 if (base.inSourceMode()) {
6301 if (cmd.txtExec) { 6305 if (cmd.txtExec) {
6302 if (Array.isArray(cmd.txtExec)) { 6306 if (Array.isArray(cmd.txtExec)) {
6303 base.sourceEditorInsertText.apply(base, cmd.txtExec); 6307 base.sourceEditorInsertText(cmd.txtExec[0],cmd.txtExec[1]);
6304 } else { 6308 } else {
6305 cmd.txtExec(base, caller, sourceEditorSelectedText()); 6309 cmd.txtExec(base, caller, sourceEditorSelectedText());
6306 } 6310 }
6307 } 6311 }
6308 } else if (cmd.exec) { 6312 } else if (cmd.exec) {
7275 shortcut.push(character); 7279 shortcut.push(character);
7276 } 7280 }
7277 7281
7278 shortcut = shortcut.join('+'); 7282 shortcut = shortcut.join('+');
7279 if (shortcutHandlers[shortcut] && 7283 if (shortcutHandlers[shortcut] &&
7280 shortcutHandlers[shortcut].call(base) === false) { 7284 shortcutHandlers[shortcut](base) === false) {
7281 7285
7282 e.stopPropagation(); 7286 e.stopPropagation();
7283 e.preventDefault(); 7287 e.preventDefault();
7284 } 7288 }
7285 }; 7289 };
7536 base.updateOriginal(); 7540 base.updateOriginal();
7537 }; 7541 };
7538 7542
7539 // run the initializer 7543 // run the initializer
7540 init(); 7544 init();
7545
7546 return base;
7541 } 7547 }
7542 7548
7543 /** 7549 /**
7544 * Map containing the loaded SCEditor locales 7550 * Map containing the loaded SCEditor locales
7545 * @type {Object} 7551 * @type {Object}
7546 * @name locale 7552 * @name locale
7547 * @memberOf sceditor 7553 * @memberOf sceditor
7548 */ 7554 */
7549 SCEditor.locale = {}; 7555 var _locale = {};
7550 7556
7551 SCEditor.formats = {}; 7557 var _formats = {};
7552 7558
7553 7559
7554 /** 7560 /**
7555 * Static command helper class 7561 * Static command helper class
7556 * @class command 7562 * @class command
7557 * @name sceditor.command 7563 * @name sceditor.command
7558 */ 7564 */
7559 SCEditor.command = 7565 var _command =
7560 /** @lends sceditor.command */ 7566 /** @lends sceditor.command */
7561 { 7567 {
7562 /** 7568 /**
7563 * Gets a command 7569 * Gets a command
7564 * 7570 *
7578 * adding it's name to the toolbar option in the constructor. It 7584 * adding it's name to the toolbar option in the constructor. It
7579 * can also be executed manually by calling 7585 * can also be executed manually by calling
7580 * {@link sceditor.execCommand}</p> 7586 * {@link sceditor.execCommand}</p>
7581 * 7587 *
7582 * @example 7588 * @example
7583 * SCEditor.command.set("hello", 7589 * _command.set("hello",
7584 * { 7590 * {
7585 * exec: function () { 7591 * exec: function () {
7586 * alert("Hello World!"); 7592 * alert("Hello World!");
7587 * } 7593 * }
7588 * }); 7594 * });
7599 7605
7600 // merge any existing command properties 7606 // merge any existing command properties
7601 cmd = extend(defaultCmds[name] || {}, cmd); 7607 cmd = extend(defaultCmds[name] || {}, cmd);
7602 7608
7603 cmd.remove = function () { 7609 cmd.remove = function () {
7604 SCEditor.command.remove(name); 7610 _command.remove(name);
7605 }; 7611 };
7606 7612
7607 defaultCmds[name] = cmd; 7613 defaultCmds[name] = cmd;
7608 return true; 7614 return true;
7609 }, 7615 },
7639 textarea = document.querySelector(textarea); 7645 textarea = document.querySelector(textarea);
7640 return textarea; 7646 return textarea;
7641 } 7647 }
7642 7648
7643 window.sceditor = { 7649 window.sceditor = {
7644 command: SCEditor.command, 7650 command: _command,
7645 commands: defaultCmds, 7651 commands: defaultCmds,
7646 defaultOptions: defaultOptions, 7652 defaultOptions: defaultOptions,
7647 7653
7648 ios: ios, 7654 ios: ios,
7649 isWysiwygSupported: isWysiwygSupported, 7655 isWysiwygSupported: isWysiwygSupported,
7676 extractContents: extractContents, 7682 extractContents: extractContents,
7677 getOffset: getOffset, 7683 getOffset: getOffset,
7678 getStyle: getStyle, 7684 getStyle: getStyle,
7679 hasStyle: hasStyle 7685 hasStyle: hasStyle
7680 }, 7686 },
7681 locale: SCEditor.locale, 7687 locale: _locale,
7682 utils: { 7688 utils: {
7683 each: each, 7689 each: each,
7684 isEmptyObject: isEmptyObject, 7690 isEmptyObject: isEmptyObject,
7685 extend: extend 7691 extend: extend
7686 }, 7692 },
7687 plugins: PluginManager.plugins, 7693 //plugins: PluginManager.plugins,
7688 formats: SCEditor.formats, 7694 plugins: plugins,
7695 formats: _formats,
7689 create: function (textarea, options) { 7696 create: function (textarea, options) {
7690 textarea = getTextarea(textarea); 7697 textarea = getTextarea(textarea);
7691 options = options || {}; 7698 options = options || {};
7692 7699
7693 // Don't allow the editor to be initialised 7700 // Don't allow the editor to be initialised
7696 return; 7703 return;
7697 } 7704 }
7698 7705
7699 if (options.runWithoutWysiwygSupport || isWysiwygSupported) { 7706 if (options.runWithoutWysiwygSupport || isWysiwygSupported) {
7700 /*eslint no-new: off*/ 7707 /*eslint no-new: off*/
7701 (new SCEditor(textarea, options)); 7708 (newSCEditor(textarea, options));
7702 } 7709 }
7703 }, 7710 },
7704 instance: function(textarea) { 7711 instance: function(textarea) {
7705 textarea = getTextarea(textarea); 7712 textarea = getTextarea(textarea);
7706 return textarea._sceditor; 7713 return textarea._sceditor;