2 * Provides custom buttons for CKEditor.
4 * In short we're applying a style element on the current selection which will be replaced
5 * with the plain BBCode tag (e.g. [tt]) afterwards. Using insertText() or insertHtml() does
6 * not work here as it discards the inline styles set for the selection.
8 * @author Alexander Ebert
9 * @copyright 2001-2014 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * Transforms the BBCode span-element into a plain BBCode.
16 * @param CKEDITOR editor
18 function transformBBCode(editor
) {
19 editor
.fire('lockSnapshot');
22 $(editor
.container
.$).find('span.wcfBBCode').removeClass('wcfBBCode').html(function() {
23 var $bbcode
= $(this).data('bbcode');
24 $markerID
= WCF
.getRandomID();
25 return '[' + $bbcode
+ ']' + $(this).html() + '<span id="' + $markerID
+ '" />[/' + $bbcode
+ ']';
28 if ($markerID
!== null && typeof window
.getSelection
!= "undefined") {
29 var $marker
= $('#' + $markerID
).get(0);
30 var $range
= document
.createRange();
31 $range
.setStartAfter($marker
);
32 $range
.collapse(true);
33 var $selection
= window
.getSelection();
34 $selection
.removeAllRanges();
35 $selection
.addRange($range
);
40 editor
.fire('unlockSnapshot');
43 // listens for 'afterCommandExec' to transform BBCodes into plain text
44 CKEDITOR
.on('instanceReady', function(event
) {
45 event
.editor
.on('afterCommandExec', function(ev
) {
46 if (ev
.data
.name
.indexOf('__wcf_') == 0) {
47 transformBBCode(ev
.editor
);
53 * Enables this plugin.
55 CKEDITOR
.plugins
.add('wbutton', {
57 * list of required plugins
60 requires
: [ 'button' ],
63 * Initializes the 'wbutton' plugin.
65 * @param CKEDITOR editor
67 init: function(editor
) {
68 if (!__CKEDITOR_BUTTONS
.length
) {
72 for (var $i
= 0, $length
= __CKEDITOR_BUTTONS
.length
; $i
< $length
; $i
++) {
73 this._wcfAddButton(editor
, __CKEDITOR_BUTTONS
[$i
]);
78 * Adds command and button for given BBCode.
80 * @param CKEDITOR editor
81 * @param object button
83 _wcfAddButton: function(editor
, button
) {
84 var $style
= new CKEDITOR
.style({
88 'data-bbcode': button
.name
91 editor
.addCommand('__wcf_' + button
.name
, new CKEDITOR
.styleCommand($style
));
92 editor
.ui
.addButton('__wcf_' + button
.name
, {
93 command
: '__wcf_' + button
.name
,