From: Alexander Ebert Date: Wed, 8 Jun 2016 11:40:33 +0000 (+0200) Subject: Added support for custom bbcode buttons X-Git-Tag: 3.0.0_Beta_1~1486 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=04ae73f3db478df2652536703c769cb644c4b3e6;p=GitHub%2FWoltLab%2FWCF.git Added support for custom bbcode buttons --- diff --git a/com.woltlab.wcf/templates/wysiwyg.tpl b/com.woltlab.wcf/templates/wysiwyg.tpl index 9b58e60cfc..1d058ab782 100644 --- a/com.woltlab.wcf/templates/wysiwyg.tpl +++ b/com.woltlab.wcf/templates/wysiwyg.tpl @@ -49,7 +49,7 @@ 'wcf.editor.image.source.error.invalid': '{lang}wcf.editor.image.source.error.invalid{/lang}' }); - var buttons = [], buttonOptions = []; + var buttons = [], buttonOptions = [], customButtons = []; {include file='wysiwygToolbar'} // TODO: Should the media stuff be here? @@ -85,7 +85,8 @@ toolbarFixed: false, woltlab: { autosave: autosave, - buttons: buttonOptions + buttons: buttonOptions, + customButtons: customButtons } }; diff --git a/com.woltlab.wcf/templates/wysiwygToolbar.tpl b/com.woltlab.wcf/templates/wysiwygToolbar.tpl index f3f2e52230..0a934062dc 100644 --- a/com.woltlab.wcf/templates/wysiwygToolbar.tpl +++ b/com.woltlab.wcf/templates/wysiwygToolbar.tpl @@ -16,8 +16,6 @@ buttonOptions = { woltlabQuote: { icon: 'fa-comment', title: '{lang}wcf.editor.button.quote{/lang}' }, woltlabSize: { icon: 'fa-text-height', title: '{lang}wcf.editor.button.size{/lang}' } }; - -buttons = []; buttons.push('html'); @@ -58,3 +56,9 @@ buttons.push('wcfSeparator'); buttons.push('woltlabMedia'); buttons.push('woltlabQuote'); + +{foreach from=$__wcf->getBBCodeHandler()->getButtonBBCodes(true) item=__bbcode} + buttonOptions['{$__bbcode->bbcodeTag}'] = { icon: '{$__bbcode->wysiwygIcon}', title: '{lang}{$__bbcode->buttonLabel}{/lang}' }; + buttons.push('{$__bbcode->bbcodeTag}'); + customButtons.push('{$__bbcode->bbcodeTag}'); +{/foreach} diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js index 8708e139df..8a449f21c8 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js @@ -3,9 +3,20 @@ $.Redactor.prototype.WoltLabButton = function() { return { init: function() { + // add custom buttons + var button, buttonName, i, length; + //noinspection JSUnresolvedVariable + for (i = 0, length = this.opts.woltlab.customButtons.length; i < length; i++) { + //noinspection JSUnresolvedVariable + buttonName = this.opts.woltlab.customButtons[i]; + + button = this.button.add(buttonName, ''); + this.button.addCallback(button, this.WoltLabButton._handleCustomButton); + } + // set button icons and labels - var button, buttonData, buttonName; - for (var i = 0, length = this.opts.buttons.length; i < length; i++) { + var buttonData; + for (i = 0, length = this.opts.buttons.length; i < length; i++) { buttonName = this.opts.buttons[i]; if (buttonName === 'wcfSeparator') { @@ -75,6 +86,21 @@ $.Redactor.prototype.WoltLabButton = function() { }); WCF.DOMNodeInsertedHandler.execute(); + }, + + _handleCustomButton: function (bbcode) { + var data = { cancel: false }; + WCF.System.Event.fireEvent('com.woltlab.wcf.redactor2', 'bbcode_' + bbcode + '_' + this.$element[0].id, data); + + if (data.cancel === true) { + return; + } + + this.buffer.set(); + + var html = '[' + bbcode + ']' + this.selection.html() + (this.selection.is() ? '' : this.marker.html()) + '[/' + bbcode + ']'; + this.insert.html(html); + this.selection.restore(); } }; }; diff --git a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php index ace986f1d7..e02522d6c8 100644 --- a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php @@ -78,11 +78,17 @@ class BBCodeHandler extends SingletonFactory { /** * Returns a list of BBCodes displayed as buttons. * + * @param boolean $excludeCoreBBCodes do not return bbcodes that are available by default * @return BBCode[] */ - public function getButtonBBCodes() { + public function getButtonBBCodes($excludeCoreBBCodes = false) { $buttons = []; + $coreBBCodes = ['align', 'b', 'color', 'i', 'img', 'list', 's', 'size', 'sub', 'sup', 'quote', 'table', 'u', 'url']; foreach ($this->buttonBBCodes as $bbcode) { + if ($excludeCoreBBCodes && in_array($bbcode->bbcodeTag, $coreBBCodes)) { + continue; + } + if ($this->isAvailableBBCode($bbcode->bbcodeTag)) { $buttons[] = $bbcode; }