diff src/sceditor.js @ 33:c23475f3f466

improve emoticons
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 14 Aug 2022 20:36:17 -0600
parents 98f11d0cbbd8
children 21090996a131
line wrap: on
line diff
--- a/src/sceditor.js	Sat Aug 13 22:37:46 2022 -0600
+++ b/src/sceditor.js	Sun Aug 14 20:36:17 2022 -0600
@@ -2541,17 +2541,12 @@
 
 		// START_COMMAND: Emoticons
 		emoticon: {
-			exec: function (editor, caller) {
+			base: function (editor, caller, targetToHtml) {
 				var createContent = function (includeMore) {
 					var	moreLink,
 						opts            = editor.opts,
 						emoticonsRoot   = opts.emoticonsRoot || '',
 						emoticonsCompat = opts.emoticonsCompat,
-						rangeHelper     = editor.getRangeHelper(),
-						startSpace      = emoticonsCompat &&
-							rangeHelper.getOuterText(true, 1) !== ' ' ? ' ' : '',
-						endSpace        = emoticonsCompat &&
-							rangeHelper.getOuterText(false, 1) !== ' ' ? ' ' : '',
 						content         = createElement('div'),
 						line            = createElement('div'),
 						perLine         = 0,
@@ -2566,8 +2561,8 @@
 					perLine = Math.sqrt(Object.keys(emoticons).length);
 
 					onEvent1(content, 'click', 'img', function (target, e) {
-						editor.insert(startSpace + attr(target, 'alt') + endSpace,
-							null, false).closeDropDown(true);
+						editor.insert(targetToHtml(target), null, false);
+						editor.closeDropDown(true);
 
 						e.preventDefault();
 					});
@@ -2575,6 +2570,7 @@
 					each(emoticons, function (code, emoticon) {
 						appendChild(line, createElement('img', {
 							src: emoticonsRoot + (emoticon.url || emoticon),
+							'data-sceditor-emoticon': code,
 							alt: code,
 							title: emoticon.tooltip || code
 						}));
@@ -2609,8 +2605,13 @@
 
 				editor.createDropDown(caller, 'emoticons', createContent(false));
 			},
+			exec: function (editor, caller) {
+				editor.commands.emoticon.base(editor, caller
+					, function(target) { return target.outerHTML; }
+				);
+			},
 			txtExec: function (editor, caller) {
-				defaultCmds.emoticon.exec(editor, caller);
+				editor.commands.emoticon.exec(editor, caller);
 			},
 			tooltip: 'Insert an emoticon'
 		},
@@ -4044,7 +4045,7 @@
 	 * @param {boolean} emoticonsCompat
 	 * @return {void}
 	 */
-	function replace(root, emoticons, emoticonsCompat) {
+	function doReplaceEmoticons(root, emoticons, emoticonsCompat) {
 		var	doc           = root.ownerDocument;
 		var space         = '(^|\\s|\xA0|\u2002|\u2003|\u2009|$)';
 		var emoticonCodes = [];
@@ -4254,14 +4255,6 @@
 		var locale;
 
 		/**
-		 * Stores a cache of preloaded images
-		 *
-		 * @private
-		 * @type {Array.<HTMLImageElement>}
-		 */
-		var preLoadCache = [];
-
-		/**
 		 * The editors rangeHelper instance
 		 *
 		 * @type {RangeHelper}
@@ -4998,13 +4991,6 @@
 					url: root + (url.url || url),
 					tooltip: url.tooltip || key
 				});
-
-				// Preload the emoticon
-				if (options.emoticonsEnabled) {
-					preLoadCache.push(createElement('img', {
-						src: root + (url.url || url)
-					}));
-				}
 			});
 		};
 
@@ -5098,8 +5084,6 @@
 			sourceEditor.readonly = !readOnly;
 
 			updateToolBar(readOnly);
-
-			return base;
 		};
 
 		/**
@@ -5138,8 +5122,6 @@
 			if (icons.rtl) {
 				icons.rtl(rtl);
 			}
-
-			return base;
 		};
 
 		/**
@@ -5193,8 +5175,6 @@
 			}
 
 			base.dimensions(width$1, null, saveWidth);
-
-			return base;
 		};
 
 		/**
@@ -5262,8 +5242,6 @@
 
 				height(editorContainer, height$1);
 			}
-
-			return base;
 		};
 
 		/**
@@ -5307,8 +5285,6 @@
 			}
 
 			base.dimensions(null, height$1, saveHeight);
-
-			return base;
 		};
 
 		/**
@@ -5354,8 +5330,6 @@
 			}
 
 			autoExpand();
-
-			return base;
 		};
 
 		autoExpand = function () {
@@ -5820,8 +5794,6 @@
 			} else {
 				base.wysiwygEditorInsertText(text, endText);
 			}
-
-			return base;
 		};
 
 		/**
@@ -5904,8 +5876,7 @@
 			if (position) {
 				sourceEditor.selectionStart = position.start;
 				sourceEditor.selectionEnd = position.end;
-
-				return base;
+				return;
 			}
 
 			return {
@@ -5960,8 +5931,6 @@
 			} else {
 				base.setSourceEditorValue(val);
 			}
-
-			return base;
 		};
 
 		/**
@@ -6016,7 +5985,7 @@
 		) {
 			if (base.inSourceMode()) {
 				base.sourceEditorInsertText(start, end);
-				return base;
+				return;
 			}
 
 			// Add the selection between start and end
@@ -6043,8 +6012,6 @@
 			}
 
 			base.wysiwygEditorInsertHtml(start);
-
-			return base;
 		};
 
 		/**
@@ -6151,7 +6118,7 @@
 			}
 
 			wysiwygBody.innerHTML = value;
-			replaceEmoticons();
+			//replaceEmoticons();
 
 			appendNewLine();
 			triggerValueChanged();
@@ -6192,7 +6159,7 @@
 		 */
 		replaceEmoticons = function () {
 			if (options.emoticonsEnabled) {
-				replace(wysiwygBody, allEmoticons, options.emoticonsCompat);
+				doReplaceEmoticons(wysiwygBody, allEmoticons, options.emoticonsCompat);
 			}
 		};
 
@@ -6235,8 +6202,6 @@
 			if ((inSourceMode && !enable) || (!inSourceMode && enable)) {
 				base.toggleSourceMode();
 			}
-
-			return base;
 		};
 
 		/**
@@ -6738,8 +6703,6 @@
 					}
 				}
 			}
-
-			return base;
 		};
 
 		/**
@@ -6775,8 +6738,6 @@
 					}
 				}
 			}
-
-			return base;
 		};
 
 		/**
@@ -6810,8 +6771,6 @@
 			} else {
 				sourceEditor.blur();
 			}
-
-			return base;
 		};
 
 		/**
@@ -6875,8 +6834,6 @@
 			}
 
 			updateActiveButtons();
-
-			return base;
 		};
 
 		/**
@@ -7098,8 +7055,6 @@
 
 				triggerValueChanged();
 			}
-
-			return base;
 		};
 
 		/**
@@ -7140,8 +7095,6 @@
 			} else {
 				inlineCss.innerHTML = css;
 			}
-
-			return base;
 		};
 
 		/**
@@ -7305,7 +7258,7 @@
 				shortcutHandlers[shortcut] = cmd;
 			}
 
-			return base;
+			return true;
 		};
 
 		/**
@@ -7315,8 +7268,6 @@
 		 */
 		base.removeShortcut = function (shortcut) {
 			delete shortcutHandlers[shortcut.toLowerCase()];
-
-			return base;
 		};
 
 		/**
@@ -7392,7 +7343,7 @@
 			block = block || currentStyledBlockNode();
 
 			if (!block || is(block, 'body')) {
-				return base;
+				return;
 			}
 
 			rangeHelper.saveRange();
@@ -7406,7 +7357,6 @@
 			}
 
 			rangeHelper.restoreRange();
-			return base;
 		};
 
 		/**
@@ -7541,6 +7491,8 @@
 		// run the initializer
 		init();
 
+		base.allEmoticons = allEmoticons;
+
 		return base;
 	}