comparison src/sceditor.js @ 30:db061869f28f

remove sceditor.command, add options.onCreate
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Aug 2022 00:54:39 -0600
parents ea32a44b5a6e
children 4eae156ca604
comparison
equal deleted inserted replaced
29:ea32a44b5a6e 30:db061869f28f
56 * 56 *
57 * @param {!Object|boolean} targetArg 57 * @param {!Object|boolean} targetArg
58 * @param {...Object} source 58 * @param {...Object} source
59 * @return {Object} 59 * @return {Object}
60 */ 60 */
61 function extend(targetArg, sourceArg) { 61 function doExtend(isDeep, args) {
62 var isTargetBoolean = targetArg === !!targetArg; 62 let target = args[0];
63 var i = isTargetBoolean ? 2 : 1;
64 var target = isTargetBoolean ? sourceArg : targetArg;
65 var isDeep = isTargetBoolean ? targetArg : false;
66 63
67 function isObject(value) { 64 function isObject(value) {
68 return value !== null && typeof value === 'object' && 65 return value !== null && typeof value === 'object' &&
69 Object.getPrototypeOf(value) === Object.prototype; 66 Object.getPrototypeOf(value) === Object.prototype;
70 } 67 }
71 68
72 for (; i < arguments.length; i++) { 69 for ( let i = 1; i < args.length; i++) {
73 var source = arguments[i]; 70 var source = args[i];
74 71
75 // Copy all properties for jQuery compatibility 72 // Copy all properties for jQuery compatibility
76 /* eslint guard-for-in: off */ 73 /* eslint guard-for-in: off */
77 for (var key in source) { 74 for (var key in source) {
78 var targetValue = target[key]; 75 var targetValue = target[key];
95 // Can only merge if target type matches otherwise create 92 // Can only merge if target type matches otherwise create
96 // new target to merge into 93 // new target to merge into
97 var isSameType = isObject(targetValue) === isValueObject && 94 var isSameType = isObject(targetValue) === isValueObject &&
98 Array.isArray(targetValue) === isValueArray; 95 Array.isArray(targetValue) === isValueArray;
99 96
100 target[key] = extend( 97 target[key] = extendDeep(
101 true,
102 isSameType ? targetValue : (isValueArray ? [] : {}), 98 isSameType ? targetValue : (isValueArray ? [] : {}),
103 value 99 value
104 ); 100 );
105 } else { 101 } else {
106 target[key] = value; 102 target[key] = value;
107 } 103 }
108 } 104 }
109 } 105 }
110 106
111 return target; 107 return target;
108 }
109
110 function extend(targetArg, sourceArgs) {
111 return doExtend(false,arguments);
112 }
113
114 function extendDeep(targetArg, sourceArgs) {
115 return doExtend(true,arguments);
112 } 116 }
113 117
114 /** 118 /**
115 * Removes an item from the passed array 119 * Removes an item from the passed array
116 * 120 *
4450 /** 4454 /**
4451 * All the commands supported by the editor 4455 * All the commands supported by the editor
4452 * @name commands 4456 * @name commands
4453 * @memberOf SCEditor.prototype 4457 * @memberOf SCEditor.prototype
4454 */ 4458 */
4455 base.commands = extend(true, {}, (userOptions.commands || defaultCmds)); 4459 base.commands = extendDeep({}, (userOptions.commands || defaultCmds));
4456 4460
4457 /** 4461 /**
4458 * Options for this editor instance 4462 * Options for this editor instance
4459 * @name opts 4463 * @name opts
4460 * @memberOf SCEditor.prototype 4464 * @memberOf SCEditor.prototype
4461 */ 4465 */
4462 var options = base.opts = extend( 4466 var options = base.opts = extendDeep(
4463 true, {}, defaultOptions, userOptions 4467 {}, defaultOptions, userOptions
4464 ); 4468 );
4465 4469
4466 // Don't deep extend emoticons (fixes #565) 4470 // Don't deep extend emoticons (fixes #565)
4467 base.opts.emoticons = userOptions.emoticons || defaultOptions.emoticons; 4471 base.opts.emoticons = userOptions.emoticons || defaultOptions.emoticons;
4468 4472
4505 (options.plugins || '').split(',').forEach(function (plugin) { 4509 (options.plugins || '').split(',').forEach(function (plugin) {
4506 pluginManager.register(plugin.trim()); 4510 pluginManager.register(plugin.trim());
4507 }); 4511 });
4508 if ('init' in format) { 4512 if ('init' in format) {
4509 format.init(base); 4513 format.init(base);
4514 }
4515
4516 if ('onCreate' in options) {
4517 options.onCreate(base);
4510 } 4518 }
4511 4519
4512 // create the editor 4520 // create the editor
4513 initEmoticons(); 4521 initEmoticons();
4514 initToolBar(); 4522 initToolBar();
7556 7564
7557 var _formats = {}; 7565 var _formats = {};
7558 7566
7559 7567
7560 /** 7568 /**
7561 * Static command helper class
7562 * @class command
7563 * @name sceditor.command
7564 */
7565 var _command =
7566 /** @lends sceditor.command */
7567 {
7568 /**
7569 * Gets a command
7570 *
7571 * @param {string} name
7572 * @return {Object|null}
7573 * @since v1.3.5
7574 */
7575 get: function (name) {
7576 return defaultCmds[name] || null;
7577 },
7578
7579 /**
7580 * <p>Adds a command to the editor or updates an existing
7581 * command if a command with the specified name already exists.</p>
7582 *
7583 * <p>Once a command is add it can be included in the toolbar by
7584 * adding it's name to the toolbar option in the constructor. It
7585 * can also be executed manually by calling
7586 * {@link sceditor.execCommand}</p>
7587 *
7588 * @example
7589 * _command.set("hello",
7590 * {
7591 * exec: function () {
7592 * alert("Hello World!");
7593 * }
7594 * });
7595 *
7596 * @param {string} name
7597 * @param {Object} cmd
7598 * @return {true|false} Returns false if name or cmd is false
7599 * @since v1.3.5
7600 */
7601 set: function (name, cmd) {
7602 if (!name || !cmd) {
7603 return false;
7604 }
7605
7606 // merge any existing command properties
7607 cmd = extend(defaultCmds[name] || {}, cmd);
7608
7609 cmd.remove = function () {
7610 _command.remove(name);
7611 };
7612
7613 defaultCmds[name] = cmd;
7614 return true;
7615 },
7616
7617 /**
7618 * Removes a command
7619 *
7620 * @param {string} name
7621 * @since v1.3.5
7622 */
7623 remove: function (name) {
7624 if (defaultCmds[name]) {
7625 delete defaultCmds[name];
7626 }
7627 }
7628 };
7629
7630 /**
7631 * SCEditor 7569 * SCEditor
7632 * http://www.sceditor.com/ 7570 * http://www.sceditor.com/
7633 * 7571 *
7634 * Copyright (C) 2017, Sam Clarke (samclarke.com) 7572 * Copyright (C) 2017, Sam Clarke (samclarke.com)
7635 * 7573 *
7645 textarea = document.querySelector(textarea); 7583 textarea = document.querySelector(textarea);
7646 return textarea; 7584 return textarea;
7647 } 7585 }
7648 7586
7649 window.sceditor = { 7587 window.sceditor = {
7650 command: _command,
7651 commands: defaultCmds, 7588 commands: defaultCmds,
7652 defaultOptions: defaultOptions, 7589 defaultOptions: defaultOptions,
7653 7590
7654 ios: ios, 7591 ios: ios,
7655 isWysiwygSupported: isWysiwygSupported, 7592 isWysiwygSupported: isWysiwygSupported,
7686 }, 7623 },
7687 locale: _locale, 7624 locale: _locale,
7688 utils: { 7625 utils: {
7689 each: each, 7626 each: each,
7690 isEmptyObject: isEmptyObject, 7627 isEmptyObject: isEmptyObject,
7691 extend: extend 7628 extend: extend,
7629 extendDeep: extendDeep,
7692 }, 7630 },
7693 //plugins: PluginManager.plugins, 7631 //plugins: PluginManager.plugins,
7694 plugins: plugins, 7632 plugins: plugins,
7695 formats: _formats, 7633 formats: _formats,
7696 create: function (textarea, options) { 7634 create: function (textarea, options) {
7703 return; 7641 return;
7704 } 7642 }
7705 7643
7706 if (options.runWithoutWysiwygSupport || isWysiwygSupported) { 7644 if (options.runWithoutWysiwygSupport || isWysiwygSupported) {
7707 /*eslint no-new: off*/ 7645 /*eslint no-new: off*/
7708 (newSCEditor(textarea, options)); 7646 return newSCEditor(textarea, options);
7709 } 7647 }
7710 }, 7648 },
7711 instance: function(textarea) { 7649 instance: function(textarea) {
7712 textarea = getTextarea(textarea); 7650 textarea = getTextarea(textarea);
7713 return textarea._sceditor; 7651 return textarea._sceditor;