changeset 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
files src/changes.txt src/formats/bbcode.js src/formats/xhtml.js src/plugins/alternative-lists.js src/sceditor.js
diffstat 5 files changed, 41 insertions(+), 103 deletions(-) [+]
line wrap: on
line diff
--- a/src/changes.txt	Thu Aug 11 19:54:03 2022 -0600
+++ b/src/changes.txt	Fri Aug 12 00:54:39 2022 -0600
@@ -1,6 +1,8 @@
 changes, most recent at top
 
 
+Removed sceditor.command with its get(), set(), and remove().  It is better to work with sceditor.commands directly.  I also added an options.onCreate callback which lets one work with the final editor.commands .
+
 Remove DOMPurify.  This was a huge amount of incomprehensible code that adds little value.  XSS should basically be handled on the server side, and if one is using bbcode then it isn't an issue anyway.
 
 Removed "this" from command functions and instead pass "editor" as first arg.  Javascript's object-oriented features are a disgusting hack and should never be used.  I will remove uses of this as I encounter them.
--- a/src/formats/bbcode.js	Thu Aug 11 19:54:03 2022 -0600
+++ b/src/formats/bbcode.js	Fri Aug 12 00:54:39 2022 -0600
@@ -23,11 +23,14 @@
 	var attr   = dom.attr;
 	var is     = dom.is;
 	var extend = utils.extend;
+	var extendDeep = utils.extendDeep;
 	var each   = utils.each;
 
 	var EMOTICON_DATA_ATTR = 'data-sceditor-emoticon';
 
-	var getEditorCommand = sceditor.command.get;
+	function getEditorCommand(cmd) {
+		return sceditor.commands[cmd];
+	}
 
 	var QuoteType = {
 		/** @lends BBCodeParser.QuoteType */
@@ -2506,8 +2509,8 @@
 			// build the BBCode cache
 			buildBbcodeCache();
 
-			editor.commands = extend(
-				true, {}, defaultCommandsOverrides, editor.commands
+			editor.commands = extendDeep(
+				{}, defaultCommandsOverrides, editor.commands
 			);
 
 			// Add BBCode helper methods
--- a/src/formats/xhtml.js	Thu Aug 11 19:54:03 2022 -0600
+++ b/src/formats/xhtml.js	Fri Aug 12 00:54:39 2022 -0600
@@ -24,7 +24,9 @@
 	var each = utils.each;
 	var isEmptyObject = utils.isEmptyObject;
 
-	var getEditorCommand = sceditor.command.get;
+	function getEditorCommand(cmd) {
+		return sceditor.commands[cmd];
+	}
 
 	var defaultCommandsOverrides = {
 		bold: {
--- a/src/plugins/alternative-lists.js	Thu Aug 11 19:54:03 2022 -0600
+++ b/src/plugins/alternative-lists.js	Fri Aug 12 00:54:39 2022 -0600
@@ -34,7 +34,6 @@
 		 */
 		var bulletHandler;
 		var orderedHandler;
-		var insertListTag;
 
 		base.init = function (editor) {
 			var opts = editor.opts;
@@ -45,8 +44,8 @@
 			}
 
 			// Override only txtExec implementation
-			sceditor.command.get('orderedlist').txtExec = orderedHandler;
-			sceditor.command.get('bulletlist').txtExec = bulletHandler;
+			editor.commands.orderedlist.txtExec = orderedHandler;
+			editor.commands.bulletlist.txtExec = bulletHandler;
 
 			// Override current implementation
 			sceditor.formats.bbcode.set('list', {
@@ -119,10 +118,10 @@
 			});
 		};
 
-		insertListTag = function (editor, listType, selected) {
-			var content = '';
+		function insertListTag(editor, listType, selected) {
+			let content = '';
 
-			utils.each(selected.split(/\r?\n/), function (item) {
+			utils.each(selected.split(/\r?\n/), function (_, item) {
 				content += (content ? '\n' : '') +
 					'[*]' + item;
 			});
@@ -141,17 +140,11 @@
 		 * @param  {node} caller
 		 * @private
 		 */
-		orderedHandler = function (caller, selected) {
-console.log('a');
-			var editor = this;
-
+		orderedHandler = function (editor, caller, selected) {
 			insertListTag(editor, '1', selected);
 		};
 
-		bulletHandler = function (caller, selected) {
-console.log('b');
-			var editor = this;
-
+		bulletHandler = function (editor, caller, selected) {
 			insertListTag(editor, '', selected);
 		};
 
--- 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) {