Added support for custom bbcode buttons
authorAlexander Ebert <ebert@woltlab.com>
Wed, 8 Jun 2016 11:40:33 +0000 (13:40 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 8 Jun 2016 11:41:48 +0000 (13:41 +0200)
com.woltlab.wcf/templates/wysiwyg.tpl
com.woltlab.wcf/templates/wysiwygToolbar.tpl
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabButton.js
wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php

index 9b58e60cfc278cd50e7b524abedbef5d14078692..1d058ab78287e7f4c6aff44f75802429676b3c71 100644 (file)
@@ -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
                                }
                        };
                        
index f3f2e522300676a83792ad7b60a4d043299a9c4b..0a934062dc6854f6e65081e528396a348edbef2b 100644 (file)
@@ -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}
index 8708e139dfdff01b9d46bf6899256b32e915b36f..8a449f21c87a8f1cf1846f25e5d936b52710a859 100644 (file)
@@ -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();
                }
        };
 };
index ace986f1d751542a6fee04d5208c44fb4a479031..e02522d6c84cfdd45c1b3ae26e6aeabcb3023caa 100644 (file)
@@ -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;
                        }