$(function ($) { 'use strict'; var mybbCmd = { align: ['left', 'center', 'right', 'justify'], fsStr: ['xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'], fSize: [9, 12, 15, 17, 23, 31], video: { 'Dailymotion': { 'match': /(dailymotion\.com\/video\/|dai\.ly\/)([^\/]+)/, 'url': '//www.dailymotion.com/embed/video/', 'html': '' }, 'Facebook': { 'match': /facebook\.com\/(?:photo.php\?v=|video\/video.php\?v=|video\/embed\?video_id=|v\/?)(\d+)/, 'url': 'https://www.facebook.com/video/embed?video_id=', 'html': '' }, 'Liveleak': { 'match': /liveleak\.com\/(?:view\?[a-z]=)([^\/]+)/, 'url': 'http://www.liveleak.com/ll_embed?i=', 'html': '' }, 'MetaCafe': { 'match': /metacafe\.com\/watch\/([^\/]+)/, 'url': 'http://www.metacafe.com/embed/', 'html': '' }, 'Mixer': { 'match': /mixer\.com\/([^\/]+)/, 'url': '//mixer.com/embed/player/', 'html': '' }, 'Vemio': { 'match': /vimeo.com\/(\d+)($|\/)/, 'url': '//player.vimeo.com/video/', 'html': '' }, 'Youtube': { 'match': /(?:v=|v\/|embed\/|youtu\.be\/)(.{11})/, 'url': '//www.youtube.com/embed/', 'html': '' }, 'Twitch': { 'match': /twitch\.tv\/(?:[\w+_-]+)\/v\/(\d+)/, 'url': '//player.twitch.tv/?video=v', 'html': '' } } }; // Add custom MyBB CSS $('').appendTo('body'); // Update editor to use align= as alignment $.sceditor.formats.bbcode .set('align', { html: function (element, attrs, content) { return '
' + content + '
'; }, isInline: false }); $.each(mybbCmd.align, function (i, val) { $.sceditor.formats.bbcode.set(val, { format: '[align=' + val + ']{0}[/align]' }); $.sceditor.command .set(val, { txtExec: ['[align=' + val + ']', '[/align]'] }); }); // Update font to support MyBB's BBCode dialect $.sceditor.formats.bbcode .set('list', { html: function (element, attrs, content) { var type = (attrs.defaultattr === '1' ? 'ol' : 'ul'); if (attrs.defaultattr === 'a') type = 'ol type="a"'; return '<' + type + '>' + content + ''; }, breakAfter: false }) .set('ul', { format: '[list]{0}[/list]' }) .set('ol', { format: function ($elm, content) { var type = ($($elm).attr('type') === 'a' ? 'a' : '1'); return '[list=' + type + ']' + content + '[/list]'; } }) .set('li', { format: '[*]{0}', excludeClosing: true }) .set('*', { excludeClosing: true, isInline: true }); $.sceditor.command .set('bulletlist', { txtExec: ['[list]\n[*]', '\n[/list]'] }) .set('orderedlist', { txtExec: ['[list=1]\n[*]', '\n[/list]'] }); // Update size tag to use xx-small-xx-large instead of 1-7 $.sceditor.formats.bbcode.set('size', { format: function ($elm, content) { var fontSize, size = $($elm).attr('size'); if (!size) { fontSize = $($elm).css('fontSize'); // Most browsers return px value but IE returns 1-7 if (fontSize.indexOf('px') > -1) { // convert size to an int fontSize = parseInt(fontSize); size = 1; $.each(mybbCmd.fSize, function (i, val) { if (fontSize > val) size = i + 2; }); } else { size = (~~fontSize) + 1; } size = (size >= 7) ? mybbCmd.fsStr[6] : ((size <= 1) ? mybbCmd.fsStr[0] : mybbCmd.fsStr[size - 1]); } else { size = mybbCmd.fsStr[size - 1]; } return '[size=' + size + ']' + content + '[/size]'; }, html: function (token, attrs, content) { var size = $.inArray(attrs.defaultattr, mybbCmd.fsStr) + 1; if (!isNaN(attrs.defaultattr)) { size = attrs.defaultattr; if (size > 7) size = 7; if (size < 1) size = 1; } if (size < 0) { size = 0; } return '' + content + ''; } }); $.sceditor.command.set('size', { _dropDown: function (editor, caller, callback) { var content = $('
'), clickFunc = function (e) { callback($(this).data('size')); editor.closeDropDown(true); e.preventDefault(); }; for (var i = 1; i <= 7; i++) content.append($('' + i + '').on('click', clickFunc)); editor.createDropDown(caller, 'fontsize-picker', content.get(0)); }, exec: function (caller) { var editor = this; $.sceditor.command.get('size')._dropDown( editor, caller, function (fontSize) { editor.execCommand('fontsize', fontSize); } ); }, txtExec: function (caller) { var editor = this; $.sceditor.command.get('size')._dropDown( editor, caller, function (size) { size = (~~size); size = (size > 7) ? 7 : ((size < 1) ? 1 : size); editor.insertText('[size=' + mybbCmd.fsStr[size - 1] + ']', '[/size]'); } ); } }); // Update quote to support pid and dateline $.sceditor.formats.bbcode.set('quote', { format: function (element, content) { var author = '', $elm = $(element), $cite = $elm.children('cite').first(); $cite.html($cite.text()); if ($cite.length === 1 || $elm.data('author')) { author = $cite.text() || $elm.data('author'); $elm.data('author', author); $cite.remove(); content = this.elementToBbcode(element); author = '=' + author.replace(/(^\s+|\s+$)/g, ''); $elm.prepend($cite); } if ($elm.data('pid')) author += " pid='" + $elm.data('pid') + "'"; if ($elm.data('dateline')) author += " dateline='" + $elm.data('dateline') + "'"; return '[quote' + author + ']' + content + '[/quote]'; }, html: function (token, attrs, content) { var data = ''; if (attrs.pid) data += ' data-pid="' + attrs.pid + '"'; if (attrs.dateline) data += ' data-dateline="' + attrs.dateline + '"'; if (typeof attrs.defaultattr !== "undefined") content = '' + attrs.defaultattr.replace(/ /g, ' ') + '' + content; return '' + content + ''; }, quoteType: function (val, name) { return "'" + val.replace("'", "\\'") + "'"; }, breakStart: true, breakEnd: true }); // Update font tag to allow limiting to only first in stack $.sceditor.formats.bbcode.set('font', { format: function (element, content) { var font; if (element.nodeName.toLowerCase() !== 'font' || !(font = $(element).attr('face'))) font = $(element).css('font-family'); if (typeof font == 'string' && font != '' && font != 'defaultattr') { return '[font=' + this.stripQuotes(font) + ']' + content + '[/font]'; } else { return content; } }, html: function (token, attrs, content) { if (typeof attrs.defaultattr == 'string' && attrs.defaultattr != '' && attrs.defaultattr != '{defaultattr}') { return '' + content + ''; } else { return content; } } }); // Add MyBB PHP command $.sceditor.formats.bbcode.set('php', { allowsEmpty: true, isInline: false, allowedChildren: ['#', '#newline'], format: '[php]{0}[/php]', html: '{0}' }); $.sceditor.command.set("php", { _dropDown: function (editor, caller) { var $content; $content = $( '
' + '
' + ' ' + '