diff 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
line wrap: on
line diff
--- a/src/sceditor.js	Thu Aug 11 19:54:03 2022 -0600
+++ b/src/sceditor.js	Fri Aug 12 00:54:39 2022 -0600
@@ -58,19 +58,16 @@
 	 * @param {...Object} source
 	 * @return {Object}
 	 */
-	function extend(targetArg, sourceArg) {
-		var isTargetBoolean = targetArg === !!targetArg;
-		var i      = isTargetBoolean ? 2 : 1;
-		var target = isTargetBoolean ? sourceArg : targetArg;
-		var isDeep = isTargetBoolean ? targetArg : false;
+	function doExtend(isDeep, args) {
+		let target = args[0];
 
 		function isObject(value) {
 			return value !== null && typeof value === 'object' &&
 				Object.getPrototypeOf(value) === Object.prototype;
 		}
 
-		for (; i < arguments.length; i++) {
-			var source = arguments[i];
+		for ( let i = 1; i < args.length; i++) {
+			var source = args[i];
 
 			// Copy all properties for jQuery compatibility
 			/* eslint guard-for-in: off */
@@ -97,8 +94,7 @@
 					var isSameType = isObject(targetValue) === isValueObject &&
 						Array.isArray(targetValue) === isValueArray;
 
-					target[key] = extend(
-						true,
+					target[key] = extendDeep(
 						isSameType ? targetValue : (isValueArray ? [] : {}),
 						value
 					);
@@ -111,6 +107,14 @@
 		return target;
 	}
 
+	function extend(targetArg, sourceArgs) {
+		return doExtend(false,arguments);
+	}
+
+	function extendDeep(targetArg, sourceArgs) {
+		return doExtend(true,arguments);
+	}
+
 	/**
 	 * Removes an item from the passed array
 	 *
@@ -4452,15 +4456,15 @@
 		 * @name commands
 		 * @memberOf SCEditor.prototype
 		 */
-		base.commands = extend(true, {}, (userOptions.commands || defaultCmds));
+		base.commands = extendDeep({}, (userOptions.commands || defaultCmds));
 
 		/**
 		 * Options for this editor instance
 		 * @name opts
 		 * @memberOf SCEditor.prototype
 		 */
-		var options = base.opts = extend(
-			true, {}, defaultOptions, userOptions
+		var options = base.opts = extendDeep(
+			{}, defaultOptions, userOptions
 		);
 
 		// Don't deep extend emoticons (fixes #565)
@@ -4509,6 +4513,10 @@
 				format.init(base);
 			}
 
+			if ('onCreate' in options) {
+				options.onCreate(base);
+			}
+
 			// create the editor
 			initEmoticons();
 			initToolBar();
@@ -7558,76 +7566,6 @@
 
 
 	/**
-	 * Static command helper class
-	 * @class command
-	 * @name sceditor.command
-	 */
-	var _command =
-	/** @lends sceditor.command */
-	{
-		/**
-		 * Gets a command
-		 *
-		 * @param {string} name
-		 * @return {Object|null}
-		 * @since v1.3.5
-		 */
-		get: function (name) {
-			return defaultCmds[name] || null;
-		},
-
-		/**
-		 * <p>Adds a command to the editor or updates an existing
-		 * command if a command with the specified name already exists.</p>
-		 *
-		 * <p>Once a command is add it can be included in the toolbar by
-		 * adding it's name to the toolbar option in the constructor. It
-		 * can also be executed manually by calling
-		 * {@link sceditor.execCommand}</p>
-		 *
-		 * @example
-		 * _command.set("hello",
-		 * {
-		 *     exec: function () {
-		 *         alert("Hello World!");
-		 *     }
-		 * });
-		 *
-		 * @param {string} name
-		 * @param {Object} cmd
-		 * @return {true|false} Returns false if name or cmd is false
-		 * @since v1.3.5
-		 */
-		set: function (name, cmd) {
-			if (!name || !cmd) {
-				return false;
-			}
-
-			// merge any existing command properties
-			cmd = extend(defaultCmds[name] || {}, cmd);
-
-			cmd.remove = function () {
-				_command.remove(name);
-			};
-
-			defaultCmds[name] = cmd;
-			return true;
-		},
-
-		/**
-		 * Removes a command
-		 *
-		 * @param {string} name
-		 * @since v1.3.5
-		 */
-		remove: function (name) {
-			if (defaultCmds[name]) {
-				delete defaultCmds[name];
-			}
-		}
-	};
-
-	/**
 	 * SCEditor
 	 * http://www.sceditor.com/
 	 *
@@ -7647,7 +7585,6 @@
 	}
 
 	window.sceditor = {
-		command: _command,
 		commands: defaultCmds,
 		defaultOptions: defaultOptions,
 
@@ -7688,7 +7625,8 @@
 		utils: {
 			each: each,
 			isEmptyObject: isEmptyObject,
-			extend: extend
+			extend: extend,
+			extendDeep: extendDeep,
 		},
 		//plugins: PluginManager.plugins,
 		plugins: plugins,
@@ -7705,7 +7643,7 @@
 
 			if (options.runWithoutWysiwygSupport || isWysiwygSupported) {
 				/*eslint no-new: off*/
-				(newSCEditor(textarea, options));
+				return newSCEditor(textarea, options);
 			}
 		},
 		instance: function(textarea) {