diff src/formats/bbcode.js @ 33:c23475f3f466

improve emoticons
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 14 Aug 2022 20:36:17 -0600
parents db061869f28f
children 20d1f23225fe
line wrap: on
line diff
--- a/src/formats/bbcode.js	Sat Aug 13 22:37:46 2022 -0600
+++ b/src/formats/bbcode.js	Sun Aug 14 20:36:17 2022 -0600
@@ -210,6 +210,13 @@
 		quote: {
 			txtExec: ['[quote]', '[/quote]']
 		},
+		emoticon: {
+			txtExec: function (editor, caller) {
+				editor.commands.emoticon.base(editor, caller, function(target) {
+					return '[emoticon]' + target.getAttribute(EMOTICON_DATA_ATTR) + '[/emoticon]';
+				} );
+			},
+		},
 		youtube: {
 			txtExec: function (editor, caller) {
 				getEditorCommand('youtube')._dropDown(
@@ -506,7 +513,9 @@
 			format: function (element, content) {
 				return attr(element, EMOTICON_DATA_ATTR) + content;
 			},
-			html: '{0}'
+			html: function (token, attrs, content, editor) {
+				return editor.allEmoticons[content] || token.val + content + token.closing.val;
+			}
 		},
 		// END_COMMAND
 
@@ -542,7 +551,7 @@
 
 				// check if this is an emoticon image
 				if (attr(element, EMOTICON_DATA_ATTR)) {
-					return content;
+					return '[emoticon]' + content + '[/emoticon]';
 				}
 
 				width = attr(element, 'width') || style('width');
@@ -1025,10 +1034,10 @@
 	 * @name BBCodeParser
 	 * @since v1.4.0
 	 */
-	function newBBCodeParser(options) {
+	function newBBCodeParser(editor) {
 		var base = {};
 
-		base.opts = extend({}, BBCodeParser.defaults, options);
+		base.opts = extend({}, BBCodeParser.defaults, editor.opts.parserOptions);
 
 		/**
 		 * Takes a BBCode string and splits it into open,
@@ -1829,7 +1838,8 @@
 							html = bbcode.html(
 								token,
 								token.attrs,
-								content
+								content,
+								editor
 							);
 						}
 					} else {
@@ -2503,6 +2513,7 @@
 		 * @private
 		 */
 		base.init = function (editor) {
+			base.editor = editor;
 			base.opts = editor.opts;
 			base.elementToBbcode = elementToBbcode;
 
@@ -2510,7 +2521,7 @@
 			buildBbcodeCache();
 
 			editor.commands = extendDeep(
-				{}, defaultCommandsOverrides, editor.commands
+				{}, editor.commands, defaultCommandsOverrides
 			);
 
 			// Add BBCode helper methods
@@ -2527,7 +2538,7 @@
 		 */
 		function toHtml(asFragment) {
 			return function(source, legacyAsFragment) {
-				var	parser = newBBCodeParser(base.opts.parserOptions);
+				var	parser = newBBCodeParser(base.editor);
 				var html = parser.toHTML(
 					base.opts.bbcodeTrim ? source.trim() : source
 				);
@@ -2554,7 +2565,7 @@
 				var	bbcode, elements;
 				var containerParent = context.createElement('div');
 				var container = context.createElement('div');
-				var parser = newBBCodeParser(base.opts.parserOptions);
+				var parser = newBBCodeParser(base.editor);
 
 				container.innerHTML = html;
 				css(containerParent, 'visibility', 'hidden');