comparison 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
comparison
equal deleted inserted replaced
32:98f11d0cbbd8 33:c23475f3f466
208 } 208 }
209 }, 209 },
210 quote: { 210 quote: {
211 txtExec: ['[quote]', '[/quote]'] 211 txtExec: ['[quote]', '[/quote]']
212 }, 212 },
213 emoticon: {
214 txtExec: function (editor, caller) {
215 editor.commands.emoticon.base(editor, caller, function(target) {
216 return '[emoticon]' + target.getAttribute(EMOTICON_DATA_ATTR) + '[/emoticon]';
217 } );
218 },
219 },
213 youtube: { 220 youtube: {
214 txtExec: function (editor, caller) { 221 txtExec: function (editor, caller) {
215 getEditorCommand('youtube')._dropDown( 222 getEditorCommand('youtube')._dropDown(
216 editor, 223 editor,
217 caller, 224 caller,
504 } 511 }
505 }, 512 },
506 format: function (element, content) { 513 format: function (element, content) {
507 return attr(element, EMOTICON_DATA_ATTR) + content; 514 return attr(element, EMOTICON_DATA_ATTR) + content;
508 }, 515 },
509 html: '{0}' 516 html: function (token, attrs, content, editor) {
517 return editor.allEmoticons[content] || token.val + content + token.closing.val;
518 }
510 }, 519 },
511 // END_COMMAND 520 // END_COMMAND
512 521
513 // START_COMMAND: Horizontal Rule 522 // START_COMMAND: Horizontal Rule
514 hr: { 523 hr: {
540 return element.style ? element.style[name] : null; 549 return element.style ? element.style[name] : null;
541 }; 550 };
542 551
543 // check if this is an emoticon image 552 // check if this is an emoticon image
544 if (attr(element, EMOTICON_DATA_ATTR)) { 553 if (attr(element, EMOTICON_DATA_ATTR)) {
545 return content; 554 return '[emoticon]' + content + '[/emoticon]';
546 } 555 }
547 556
548 width = attr(element, 'width') || style('width'); 557 width = attr(element, 'width') || style('width');
549 height = attr(element, 'height') || style('height'); 558 height = attr(element, 'height') || style('height');
550 559
1023 * @param {Object} options 1032 * @param {Object} options
1024 * @class BBCodeParser 1033 * @class BBCodeParser
1025 * @name BBCodeParser 1034 * @name BBCodeParser
1026 * @since v1.4.0 1035 * @since v1.4.0
1027 */ 1036 */
1028 function newBBCodeParser(options) { 1037 function newBBCodeParser(editor) {
1029 var base = {}; 1038 var base = {};
1030 1039
1031 base.opts = extend({}, BBCodeParser.defaults, options); 1040 base.opts = extend({}, BBCodeParser.defaults, editor.opts.parserOptions);
1032 1041
1033 /** 1042 /**
1034 * Takes a BBCode string and splits it into open, 1043 * Takes a BBCode string and splits it into open,
1035 * content and close tags. 1044 * content and close tags.
1036 * 1045 *
1827 ); 1836 );
1828 } else { 1837 } else {
1829 html = bbcode.html( 1838 html = bbcode.html(
1830 token, 1839 token,
1831 token.attrs, 1840 token.attrs,
1832 content 1841 content,
1842 editor
1833 ); 1843 );
1834 } 1844 }
1835 } else { 1845 } else {
1836 html = token.val + content + 1846 html = token.val + content +
1837 (token.closing ? token.closing.val : ''); 1847 (token.closing ? token.closing.val : '');
2501 /** 2511 /**
2502 * Initializer 2512 * Initializer
2503 * @private 2513 * @private
2504 */ 2514 */
2505 base.init = function (editor) { 2515 base.init = function (editor) {
2516 base.editor = editor;
2506 base.opts = editor.opts; 2517 base.opts = editor.opts;
2507 base.elementToBbcode = elementToBbcode; 2518 base.elementToBbcode = elementToBbcode;
2508 2519
2509 // build the BBCode cache 2520 // build the BBCode cache
2510 buildBbcodeCache(); 2521 buildBbcodeCache();
2511 2522
2512 editor.commands = extendDeep( 2523 editor.commands = extendDeep(
2513 {}, defaultCommandsOverrides, editor.commands 2524 {}, editor.commands, defaultCommandsOverrides
2514 ); 2525 );
2515 2526
2516 // Add BBCode helper methods 2527 // Add BBCode helper methods
2517 editor.toBBCode = base.toSource; 2528 editor.toBBCode = base.toSource;
2518 editor.fromBBCode = base.toHtml; 2529 editor.fromBBCode = base.toHtml;
2525 * @param {string} source 2536 * @param {string} source
2526 * @param {boolean} [legacyAsFragment] Used by fromBBCode() method 2537 * @param {boolean} [legacyAsFragment] Used by fromBBCode() method
2527 */ 2538 */
2528 function toHtml(asFragment) { 2539 function toHtml(asFragment) {
2529 return function(source, legacyAsFragment) { 2540 return function(source, legacyAsFragment) {
2530 var parser = newBBCodeParser(base.opts.parserOptions); 2541 var parser = newBBCodeParser(base.editor);
2531 var html = parser.toHTML( 2542 var html = parser.toHTML(
2532 base.opts.bbcodeTrim ? source.trim() : source 2543 base.opts.bbcodeTrim ? source.trim() : source
2533 ); 2544 );
2534 2545
2535 return (asFragment || legacyAsFragment) ? 2546 return (asFragment || legacyAsFragment) ?
2552 context = context || document; 2563 context = context || document;
2553 2564
2554 var bbcode, elements; 2565 var bbcode, elements;
2555 var containerParent = context.createElement('div'); 2566 var containerParent = context.createElement('div');
2556 var container = context.createElement('div'); 2567 var container = context.createElement('div');
2557 var parser = newBBCodeParser(base.opts.parserOptions); 2568 var parser = newBBCodeParser(base.editor);
2558 2569
2559 container.innerHTML = html; 2570 container.innerHTML = html;
2560 css(containerParent, 'visibility', 'hidden'); 2571 css(containerParent, 'visibility', 'hidden');
2561 containerParent.appendChild(container); 2572 containerParent.appendChild(container);
2562 context.body.appendChild(containerParent); 2573 context.body.appendChild(containerParent);